class RadioSelect(Pmw
.MegaWidget
):
# A collection of several buttons. In single mode, only one
# button may be selected. In multiple mode, any number of buttons
def __init__(self
, parent
= None, **kw
):
# Define the megawidget options.
('buttontype', 'button', INITOPT
),
('labelmargin', 0, INITOPT
),
('labelpos', None, INITOPT
),
('orient', 'horizontal', INITOPT
),
('selectmode', 'single', INITOPT
),
self
.defineoptions(kw
, optiondefs
, dynamicGroups
= ('Button',))
# Initialise the base class (after defining the options).
Pmw
.MegaWidget
.__init
__(self
, parent
)
interior
= self
.interior()
if self
['labelpos'] is None:
self
._radioSelectFrame
= self
._hull
self
._radioSelectFrame
= self
.createcomponent('frame',
Tkinter
.Frame
, (interior
,))
self
._radioSelectFrame
.grid(column
=2, row
=2, sticky
='nsew')
interior
.grid_columnconfigure(2, weight
=1)
interior
.grid_rowconfigure(2, weight
=1)
self
.createlabel(interior
)
# Initialise instance variables.
if self
['selectmode'] == 'single':
elif self
['selectmode'] == 'multiple':
raise ValueError, 'bad selectmode option "' + \
self
['selectmode'] + '": should be single or multiple'
if self
['buttontype'] == 'button':
self
.buttonClass
= Tkinter
.Button
elif self
['buttontype'] == 'radiobutton':
self
.var
= Tkinter
.StringVar()
self
.buttonClass
= Tkinter
.Radiobutton
elif self
['buttontype'] == 'checkbutton':
self
.buttonClass
= Tkinter
.Checkbutton
raise ValueError, 'bad buttontype option "' + \
'": should be button, radiobutton or checkbutton'
if self
['orient'] not in ('horizontal', 'vertical'):
raise ValueError, 'bad orient option ' + repr(self
['orient']) + \
': must be either \'horizontal\' or \'vertical\''
# Check keywords and initialise options.
def getcurselection(self
):
return tuple(self
.selection
)
return self
.getcurselection()
def setvalue(self
, textOrList
):
self
.__setSingleValue
(textOrList
)
oldselection
= self
.selection
self
.selection
= textOrList
for button
in self
._buttonList
:
if button
in oldselection
:
if button
not in self
.selection
:
# button is currently selected but should not be
widget
= self
.component(button
)
if self
['buttontype'] == 'checkbutton':
widget
.configure(relief
='raised')
if button
in self
.selection
:
# button is not currently selected but should be
widget
= self
.component(button
)
if self
['buttontype'] == 'checkbutton':
widget
.configure(relief
='sunken')
return len(self
._buttonList
)
# Return the integer index of the button with the given index.
listLength
= len(self
._buttonList
)
if type(index
) == types
.IntType
:
raise ValueError, 'index "%s" is out of range' % index
raise ValueError, 'RadioSelect has no buttons'
for count
in range(listLength
):
name
= self
._buttonList
[count
]
validValues
= 'a name, a number or Pmw.END'
'bad index "%s": must be %s' % (index
, validValues
)
def button(self
, buttonIndex
):
name
= self
._buttonList
[self
.index(buttonIndex
)]
return self
.component(name
)
def add(self
, componentName
, **kw
):
if componentName
in self
._buttonList
:
raise ValueError, 'button "%s" already exists' % componentName
lambda self
=self
, name
=componentName
: self
.invoke(name
)
if not kw
.has_key('text'):
kw
['text'] = componentName
if self
['buttontype'] == 'radiobutton':
if not kw
.has_key('anchor'):
if not kw
.has_key('variable'):
kw
['variable'] = self
.var
if not kw
.has_key('value'):
elif self
['buttontype'] == 'checkbutton':
if not kw
.has_key('anchor'):
button
= apply(self
.createcomponent
, (componentName
,
self
.buttonClass
, (self
._radioSelectFrame
,)), kw
)
if self
['orient'] == 'horizontal':
self
._radioSelectFrame
.grid_rowconfigure(0, weight
=1)
col
= len(self
._buttonList
)
button
.grid(column
=col
, row
=0, padx
= self
['padx'],
pady
= self
['pady'], sticky
='nsew')
self
._radioSelectFrame
.grid_columnconfigure(col
, weight
=1)
self
._radioSelectFrame
.grid_columnconfigure(0, weight
=1)
row
= len(self
._buttonList
)
button
.grid(column
=0, row
=row
, padx
= self
['padx'],
pady
= self
['pady'], sticky
='ew')
self
._radioSelectFrame
.grid_rowconfigure(row
, weight
=1)
self
._buttonList
.append(componentName
)
for name
in self
._buttonList
:
self
.destroycomponent(name
)
def __setSingleValue(self
, value
):
if self
['buttontype'] == 'radiobutton':
widget
= self
.component(value
)
for button
in self
._buttonList
:
widget
= self
.component(button
)
widget
.configure(relief
='sunken')
widget
.configure(relief
='raised')
index
= self
.index(index
)
name
= self
._buttonList
[index
]
self
.__setSingleValue
(name
)
command
= self
['command']
widget
= self
.component(name
)
if name
in self
.selection
:
if self
['buttontype'] == 'checkbutton':
widget
.configure(relief
='raised')
self
.selection
.remove(name
)
if self
['buttontype'] == 'checkbutton':
widget
.configure(relief
='sunken')
self
.selection
.append(name
)
command
= self
['command']
return command(name
, state
)