4 class ThresholdScale(Pmw.MegaWidget):
5 """ Megawidget containing a scale and an indicator.
8 def __init__(self, parent = None, **kw):
9 # Define the megawidget options.
11 ('colors', ('green', 'red'), None),
12 ('orient', 'vertical', Pmw.INITOPT),
13 ('labelmargin', 0, Pmw.INITOPT),
14 ('labelpos', None, Pmw.INITOPT),
15 ('threshold', (50,), None),
16 ('value', None, Pmw.INITOPT),
18 self.defineoptions(kw, optiondefs)
20 # Initialise base class (after defining options).
21 Pmw.MegaWidget.__init__(self, parent)
23 # Create the components.
24 interior = self.interior()
26 # Create the indicator component.
27 self.indicator = self.createcomponent('indicator',
29 Tkinter.Frame, interior,
35 # Create the value component.
36 self.value = self.createcomponent('value',
38 Tkinter.Label, interior,
41 # Create the scale component.
42 if self['orient'] == 'vertical':
43 # The default scale range seems to be
44 # the wrong way around - reverse it.
51 self.scale = self.createcomponent('scale',
53 Tkinter.Scale, interior,
54 orient = self['orient'],
55 command = self._doCommand,
66 # Use grid to position all components
67 if self['orient'] == 'vertical':
68 self.indicator.grid(row = 1, column = 1)
69 self.value.grid(row = 2, column = 1)
70 self.scale.grid(row = 3, column = 1)
72 self.createlabel(interior, childRows=3)
74 self.indicator.grid(row = 1, column = 1)
75 self.value.grid(row = 1, column = 2)
76 self.scale.grid(row = 1, column = 3)
78 self.createlabel(interior, childCols=3)
80 # Check keywords and initialise options.
81 self.initialiseoptions()
83 def _doCommand(self, valueStr):
84 valueInt = self.scale.get()
85 colors = self['colors']
86 thresholds = self['threshold']
88 for index in range(len(colors) - 1):
89 if valueInt <= thresholds[index]:
92 self.indicator.configure(background = color)
93 self.value.configure(text = valueStr)
95 Pmw.forwardmethods(ThresholdScale, Tkinter.Scale, 'scale')
97 # Initialise Tkinter and Pmw.
98 root = Pmw.initialise()
99 root.title('Pmw ThresholdScale demonstration')
101 # Create and pack two ThresholdScale megawidgets.
102 mega1 = ThresholdScale(scale_showvalue = 1)
103 mega1.pack(side = 'left', padx = 10, pady = 10)
105 mega2 = ThresholdScale(
106 colors = ('green', 'yellow', 'red'),
107 threshold = (50, 75),
109 indicator_width = 32,
111 mega2.pack(side = 'left', padx = 10, pady = 10)
113 # Create and pack two horizontal ThresholdScale megawidgets.
114 mega3 = ThresholdScale(
115 orient = 'horizontal',
117 label_text = 'Horizontal')
118 mega3.pack(side = 'top', padx = 10, pady = 10)
119 mega4 = ThresholdScale(orient = 'horizontal')
120 mega4.pack(side = 'top', padx = 10, pady = 10)