summaryrefslogtreecommitdiffstats
path: root/Tools/pynche/ChipViewer.py
blob: f1429fd6106d28a08aac96eecec2469f9a8086ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Color chip megawidget.
This widget is used for displaying a color.  It consists of three components:

    label -- a Tkinter.Label, this is the chip's label which is displayed
             about the color chip 
    chip  -- A Tkinter.Frame, the frame displaying the color
    name  -- a Tkinter.Label, the name of the color

In addition, the megawidget understands the following options:

    color -- the color displayed in the chip and name widgets

When run as a script, this program displays a sample chip.
"""


from Tkinter import *
import Pmw

class ChipWidget(Pmw.MegaWidget):
    _WIDTH = 150
    _HEIGHT = 80

    def __init__(self, parent=None, **kw):
	optionsdefs = (('chip_borderwidth', 2,            None),
		       ('chip_width',       self._WIDTH,  None),
		       ('chip_height',      self._HEIGHT, None),
		       ('label_text',       'Color',      None),
		       ('color',            'blue',       self.__set_color),
		       )
	self.defineoptions(kw, optionsdefs)

	# initialize base class -- after defining options
	Pmw.MegaWidget.__init__(self, parent)
	interiorarg = (self.interior(),)

	# create the label
	self.__label = self.createcomponent(
	    # component name, aliases, group
	    'label', (), None,
	    # widget class, widget args
	    Label, interiorarg)
	self.__label.grid(row=0, column=0)

	# create the color chip
	self.__chip = self.createcomponent(
	    'chip', (), None,
	    Frame, interiorarg,
	    relief=RAISED, borderwidth=2)
	self.__chip.grid(row=1, column=0)

	# create the color name
	self.__name = self.createcomponent(
	    'name', (), None,
	    Label, interiorarg,)
	self.__name.grid(row=2, column=0)

	# Check keywords and initialize options
	self.initialiseoptions(ChipWidget)

    # called whenever `color' option is set
    def __set_color(self):
	color = self['color']
	self.__chip['background'] = color
	self.__name['text'] = color



if __name__ == '__main__':
    root = Pmw.initialise(fontScheme='pmw1')
    root.title('ChipWidget demonstration')

    exitbtn = Button(root, text='Exit', command=root.destroy)
    exitbtn.pack(side=BOTTOM)
    widget = ChipWidget(root, color='red',
			chip_width=200,
			label_text='Selected Color')
    widget.pack()
    root.mainloop()