|
| 1 | +from Tkinter import * |
| 2 | +import Pmw |
| 3 | + |
| 4 | +class ChipWidget(Pmw.MegaWidget): |
| 5 | + _WIDTH = 80 |
| 6 | + _HEIGHT = 100 |
| 7 | + |
| 8 | + def __init__(self, parent=None, **kw): |
| 9 | + optionsdefs = (('chipcolor', 'blue', self.__set_color), |
| 10 | + ('width', self._WIDTH, self.__set_dims), |
| 11 | + ('height', self._HEIGHT, self.__set_dims), |
| 12 | + ('text', 'Color', self.__set_label), |
| 13 | + ) |
| 14 | + self.defineoptions(kw, optionsdefs) |
| 15 | + |
| 16 | + # initialize base class -- after defining options |
| 17 | + Pmw.MegaWidget.__init__(self, parent) |
| 18 | + interiorarg = (self.interior(),) |
| 19 | + |
| 20 | + # create the label |
| 21 | + self.__label = self.createcomponent( |
| 22 | + # component name, aliases, group |
| 23 | + 'label', (), None, |
| 24 | + # widget class, widget args |
| 25 | + Label, interiorarg) |
| 26 | + self.__label.grid(row=0, column=0) |
| 27 | + |
| 28 | + # create the color chip |
| 29 | + self.__chip = self.createcomponent( |
| 30 | + 'chip', (), None, |
| 31 | + Frame, interiorarg, |
| 32 | + relief=RAISED, borderwidth=2) |
| 33 | + self.__chip.grid(row=1, column=0) |
| 34 | + |
| 35 | + # create the color name |
| 36 | + self.__name = self.createcomponent( |
| 37 | + 'name', (), None, |
| 38 | + Label, interiorarg,) |
| 39 | + self.__name.grid(row=2, column=0) |
| 40 | + |
| 41 | + # Check keywords and initialize options |
| 42 | + self.initialiseoptions(ChipWidget) |
| 43 | + |
| 44 | + # called whenever `chipcolor' option is set |
| 45 | + def __set_color(self): |
| 46 | + color = self['chipcolor'] |
| 47 | + self.__chip['background'] = color |
| 48 | + self.__name['text'] = color |
| 49 | + |
| 50 | + def __set_dims(self): |
| 51 | + width = self['width'] |
| 52 | + height = self['height'] |
| 53 | + self.__chip.configure(width=width, height=height) |
| 54 | + |
| 55 | + def __set_label(self): |
| 56 | + self.__label['text'] = self['text'] |
| 57 | + |
| 58 | +Pmw.forwardmethods(ChipWidget, Frame, '__chip') |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + root = Pmw.initialise(fontScheme='pmw1') |
| 64 | + root.title('ChipWidget demonstration') |
| 65 | + |
| 66 | + exitbtn = Button(root, text='Exit', command=root.destroy) |
| 67 | + exitbtn.pack(side=BOTTOM) |
| 68 | + widget = ChipWidget(root, chipcolor='red', width=200, |
| 69 | + text='Selected Color') |
| 70 | + widget.pack() |
| 71 | + root.mainloop() |
0 commit comments