Barry Warsaw | b6db1b9 | 1998-01-30 21:11:52 +0000 | [diff] [blame] | 1 | """Color chip megawidget. |
| 2 | This widget is used for displaying a color. It consists of three components: |
| 3 | |
| 4 | label -- a Tkinter.Label, this is the chip's label which is displayed |
| 5 | about the color chip |
| 6 | chip -- A Tkinter.Frame, the frame displaying the color |
| 7 | name -- a Tkinter.Label, the name of the color |
| 8 | |
| 9 | In addition, the megawidget understands the following options: |
| 10 | |
| 11 | color -- the color displayed in the chip and name widgets |
| 12 | |
| 13 | When run as a script, this program displays a sample chip. |
| 14 | """ |
| 15 | |
| 16 | |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 17 | from Tkinter import * |
| 18 | import Pmw |
| 19 | |
| 20 | class ChipWidget(Pmw.MegaWidget): |
Barry Warsaw | 37400e8 | 1998-02-11 18:54:23 +0000 | [diff] [blame] | 21 | _WIDTH = 150 |
| 22 | _HEIGHT = 80 |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 23 | |
| 24 | def __init__(self, parent=None, **kw): |
Barry Warsaw | 31ac518 | 1998-03-10 00:16:09 +0000 | [diff] [blame] | 25 | options = (('chip_borderwidth', 2, None), |
| 26 | ('chip_width', self._WIDTH, None), |
| 27 | ('chip_height', self._HEIGHT, None), |
| 28 | ('label_text', 'Color', None), |
| 29 | ('color', 'blue', self.__set_color), |
| 30 | ) |
| 31 | self.defineoptions(kw, options) |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 32 | |
| 33 | # initialize base class -- after defining options |
| 34 | Pmw.MegaWidget.__init__(self, parent) |
| 35 | interiorarg = (self.interior(),) |
| 36 | |
| 37 | # create the label |
| 38 | self.__label = self.createcomponent( |
| 39 | # component name, aliases, group |
| 40 | 'label', (), None, |
| 41 | # widget class, widget args |
| 42 | Label, interiorarg) |
| 43 | self.__label.grid(row=0, column=0) |
| 44 | |
| 45 | # create the color chip |
| 46 | self.__chip = self.createcomponent( |
| 47 | 'chip', (), None, |
| 48 | Frame, interiorarg, |
| 49 | relief=RAISED, borderwidth=2) |
| 50 | self.__chip.grid(row=1, column=0) |
| 51 | |
| 52 | # create the color name |
| 53 | self.__name = self.createcomponent( |
| 54 | 'name', (), None, |
| 55 | Label, interiorarg,) |
| 56 | self.__name.grid(row=2, column=0) |
| 57 | |
| 58 | # Check keywords and initialize options |
| 59 | self.initialiseoptions(ChipWidget) |
| 60 | |
Barry Warsaw | b6db1b9 | 1998-01-30 21:11:52 +0000 | [diff] [blame] | 61 | # called whenever `color' option is set |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 62 | def __set_color(self): |
Barry Warsaw | b6db1b9 | 1998-01-30 21:11:52 +0000 | [diff] [blame] | 63 | color = self['color'] |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 64 | self.__chip['background'] = color |
| 65 | self.__name['text'] = color |
| 66 | |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | if __name__ == '__main__': |
| 70 | root = Pmw.initialise(fontScheme='pmw1') |
| 71 | root.title('ChipWidget demonstration') |
| 72 | |
| 73 | exitbtn = Button(root, text='Exit', command=root.destroy) |
| 74 | exitbtn.pack(side=BOTTOM) |
Barry Warsaw | b6db1b9 | 1998-01-30 21:11:52 +0000 | [diff] [blame] | 75 | widget = ChipWidget(root, color='red', |
| 76 | chip_width=200, |
| 77 | label_text='Selected Color') |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 78 | widget.pack() |
| 79 | root.mainloop() |