| 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 | # Define the megawidget options. |
| 10 | optiondefs = ( |
| 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), |
| 17 | ) |
| 18 | self.defineoptions(kw, optiondefs) |
| 19 | |
| 20 | # Initialise base class (after defining options). |
| 21 | Pmw.MegaWidget.__init__(self, parent) |
| 22 | |
| 23 | # Create the components. |
| 24 | interior = self.interior() |
| 25 | |
| 26 | # Create the indicator component. |
| 27 | self.indicator = self.createcomponent('indicator', |
| 28 | (), None, |
| 29 | Tkinter.Frame, interior, |
| 30 | width = 16, |
| 31 | height = 16, |
| 32 | borderwidth = 2, |
| 33 | relief = 'raised') |
| 34 | |
| 35 | # Create the value component. |
| 36 | self.value = self.createcomponent('value', |
| 37 | (), None, |
| 38 | Tkinter.Label, interior, |
| 39 | width = 3) |
| 40 | |
| 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. |
| 45 | from_ = 100 |
| 46 | to = 0 |
| 47 | else: |
| 48 | from_ = 0 |
| 49 | to = 100 |
| 50 | |
| 51 | self.scale = self.createcomponent('scale', |
| 52 | (), None, |
| 53 | Tkinter.Scale, interior, |
| 54 | orient = self['orient'], |
| 55 | command = self._doCommand, |
| 56 | tickinterval = 20, |
| 57 | length = 200, |
| 58 | from_ = from_, |
| 59 | to = to, |
| 60 | showvalue = 0) |
| 61 | |
| 62 | value = self['value'] |
| 63 | if value is not None: |
| 64 | self.scale.set(value) |
| 65 | |
| 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) |
| 71 | # Create the label. |
| 72 | self.createlabel(interior, childRows=3) |
| 73 | else: |
| 74 | self.indicator.grid(row = 1, column = 1) |
| 75 | self.value.grid(row = 1, column = 2) |
| 76 | self.scale.grid(row = 1, column = 3) |
| 77 | # Create the label. |
| 78 | self.createlabel(interior, childCols=3) |
| 79 | |
| 80 | # Check keywords and initialise options. |
| 81 | self.initialiseoptions() |
| 82 | |
| 83 | def _doCommand(self, valueStr): |
| 84 | valueInt = self.scale.get() |
| 85 | colors = self['colors'] |
| 86 | thresholds = self['threshold'] |
| 87 | color = colors[-1] |
| 88 | for index in range(len(colors) - 1): |
| 89 | if valueInt <= thresholds[index]: |
| 90 | color = colors[index] |
| 91 | break |
| 92 | self.indicator.configure(background = color) |
| 93 | self.value.configure(text = valueStr) |
| 94 | |
| 95 | Pmw.forwardmethods(ThresholdScale, Tkinter.Scale, 'scale') |
| 96 | |
| 97 | # Initialise Tkinter and Pmw. |
| 98 | root = Pmw.initialise() |
| 99 | root.title('Pmw ThresholdScale demonstration') |
| 100 | |
| 101 | # Create and pack two ThresholdScale megawidgets. |
| 102 | mega1 = ThresholdScale(scale_showvalue = 1) |
| 103 | mega1.pack(side = 'left', padx = 10, pady = 10) |
| 104 | |
| 105 | mega2 = ThresholdScale( |
| 106 | colors = ('green', 'yellow', 'red'), |
| 107 | threshold = (50, 75), |
| 108 | value = 80, |
| 109 | indicator_width = 32, |
| 110 | scale_width = 25) |
| 111 | mega2.pack(side = 'left', padx = 10, pady = 10) |
| 112 | |
| 113 | # Create and pack two horizontal ThresholdScale megawidgets. |
| 114 | mega3 = ThresholdScale( |
| 115 | orient = 'horizontal', |
| 116 | labelpos = 'n', |
| 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) |
| 121 | |
| 122 | # Let's go. |
| 123 | root.mainloop() |