| 1 | import Tkinter |
| 2 | import Pmw |
| 3 | |
| 4 | class ThresholdScale(Pmw.MegaWidget): |
| 5 | """ Megawidget containing a scale and an indicator. |
| 6 | """ |
| 7 | |
| 8 | def __init__(self, parent = None, **kw): |
| 9 | |
| 10 | # Define the megawidget options. |
| 11 | optiondefs = ( |
| 12 | ('colors', ('green', 'red'), None), |
| 13 | ('threshold', 50, None), |
| 14 | ('value', None, Pmw.INITOPT), |
| 15 | ) |
| 16 | self.defineoptions(kw, optiondefs) |
| 17 | |
| 18 | # Initialise base class (after defining options). |
| 19 | Pmw.MegaWidget.__init__(self, parent) |
| 20 | |
| 21 | # Create the components. |
| 22 | interior = self.interior() |
| 23 | |
| 24 | # Create the indicator component. |
| 25 | self.indicator = self.createcomponent('indicator', |
| 26 | (), None, |
| 27 | Tkinter.Frame, interior, |
| 28 | width = 16, |
| 29 | height = 16, |
| 30 | borderwidth = 2, |
| 31 | relief = 'raised') |
| 32 | self.indicator.grid() |
| 33 | |
| 34 | # Create the scale component. |
| 35 | self.scale = self.createcomponent('scale', |
| 36 | (), None, |
| 37 | Tkinter.Scale, interior, |
| 38 | command = self._doCommand, |
| 39 | tickinterval = 20, |
| 40 | length = 200, |
| 41 | from_ = 100, |
| 42 | to = 0, |
| 43 | showvalue = 0) |
| 44 | self.scale.grid() |
| 45 | |
| 46 | value = self['value'] |
| 47 | if value is not None: |
| 48 | self.scale.set(value) |
| 49 | |
| 50 | # Check keywords and initialise options. |
| 51 | self.initialiseoptions() |
| 52 | |
| 53 | def _doCommand(self, valueStr): |
| 54 | if self.scale.get() > self['threshold']: |
| 55 | color = self['colors'][1] |
| 56 | else: |
| 57 | color = self['colors'][0] |
| 58 | self.indicator.configure(background = color) |
| 59 | |
| 60 | Pmw.forwardmethods(ThresholdScale, Tkinter.Scale, 'scale') |
| 61 | |
| 62 | # Initialise Tkinter and Pmw. |
| 63 | root = Pmw.initialise() |
| 64 | root.title('Pmw ThresholdScale demonstration') |
| 65 | |
| 66 | # Create and pack two ThresholdScale megawidgets. |
| 67 | mega1 = ThresholdScale() |
| 68 | mega1.pack(side = 'left', padx = 10, pady = 10) |
| 69 | |
| 70 | mega2 = ThresholdScale( |
| 71 | colors = ('green', 'yellow'), |
| 72 | threshold = 75, |
| 73 | value = 80, |
| 74 | indicator_width = 32, |
| 75 | scale_width = 25) |
| 76 | mega2.pack(side = 'left', padx = 10, pady = 10) |
| 77 | |
| 78 | # Let's go. |
| 79 | root.mainloop() |