class ThresholdScale(Pmw
.MegaWidget
):
""" Megawidget containing a scale and an indicator.
def __init__(self
, parent
= None, **kw
):
# Define the megawidget options.
('colors', ('green', 'red'), None),
('orient', 'vertical', Pmw
.INITOPT
),
('labelmargin', 0, Pmw
.INITOPT
),
('labelpos', None, Pmw
.INITOPT
),
('threshold', (50,), None),
('value', None, Pmw
.INITOPT
),
self
.defineoptions(kw
, optiondefs
)
# Initialise base class (after defining options).
Pmw
.MegaWidget
.__init
__(self
, parent
)
interior
= self
.interior()
# Create the indicator component.
self
.indicator
= self
.createcomponent('indicator',
# Create the value component.
self
.value
= self
.createcomponent('value',
# Create the scale component.
if self
['orient'] == 'vertical':
# The default scale range seems to be
# the wrong way around - reverse it.
self
.scale
= self
.createcomponent('scale',
command
= self
._doCommand
,
# Use grid to position all components
if self
['orient'] == 'vertical':
self
.indicator
.grid(row
= 1, column
= 1)
self
.value
.grid(row
= 2, column
= 1)
self
.scale
.grid(row
= 3, column
= 1)
self
.createlabel(interior
, childRows
=3)
self
.indicator
.grid(row
= 1, column
= 1)
self
.value
.grid(row
= 1, column
= 2)
self
.scale
.grid(row
= 1, column
= 3)
self
.createlabel(interior
, childCols
=3)
# Check keywords and initialise options.
def _doCommand(self
, valueStr
):
valueInt
= self
.scale
.get()
thresholds
= self
['threshold']
for index
in range(len(colors
) - 1):
if valueInt
<= thresholds
[index
]:
self
.indicator
.configure(background
= color
)
self
.value
.configure(text
= valueStr
)
Pmw
.forwardmethods(ThresholdScale
, Tkinter
.Scale
, 'scale')
# Initialise Tkinter and Pmw.
root
.title('Pmw ThresholdScale demonstration')
# Create and pack two ThresholdScale megawidgets.
mega1
= ThresholdScale(scale_showvalue
= 1)
mega1
.pack(side
= 'left', padx
= 10, pady
= 10)
colors
= ('green', 'yellow', 'red'),
mega2
.pack(side
= 'left', padx
= 10, pady
= 10)
# Create and pack two horizontal ThresholdScale megawidgets.
label_text
= 'Horizontal')
mega3
.pack(side
= 'top', padx
= 10, pady
= 10)
mega4
= ThresholdScale(orient
= 'horizontal')
mega4
.pack(side
= 'top', padx
= 10, pady
= 10)