blob: a2a90347c44230c498d50945adf5ff13097f7cc6 [file] [log] [blame]
Barry Warsawb6db1b91998-01-30 21:11:52 +00001"""Color chip megawidget.
2This 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
9In addition, the megawidget understands the following options:
10
11 color -- the color displayed in the chip and name widgets
12
13When run as a script, this program displays a sample chip.
14"""
15
16
Barry Warsaw7080a7f1998-01-29 23:48:55 +000017from Tkinter import *
18import Pmw
19
20class ChipWidget(Pmw.MegaWidget):
Barry Warsaw37400e81998-02-11 18:54:23 +000021 _WIDTH = 150
22 _HEIGHT = 80
Barry Warsaw7080a7f1998-01-29 23:48:55 +000023
24 def __init__(self, parent=None, **kw):
Barry Warsaw31ac5181998-03-10 00:16:09 +000025 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 Warsaw7080a7f1998-01-29 23:48:55 +000032
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 Warsawb6db1b91998-01-30 21:11:52 +000061 # called whenever `color' option is set
Barry Warsaw7080a7f1998-01-29 23:48:55 +000062 def __set_color(self):
Barry Warsawb6db1b91998-01-30 21:11:52 +000063 color = self['color']
Barry Warsaw7080a7f1998-01-29 23:48:55 +000064 self.__chip['background'] = color
65 self.__name['text'] = color
66
Barry Warsaw7080a7f1998-01-29 23:48:55 +000067
68
69if __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 Warsawb6db1b91998-01-30 21:11:52 +000075 widget = ChipWidget(root, color='red',
76 chip_width=200,
77 label_text='Selected Color')
Barry Warsaw7080a7f1998-01-29 23:48:55 +000078 widget.pack()
79 root.mainloop()