ChipViewer.py 2.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
"""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.
"""


Barry Warsaw's avatar
Barry Warsaw committed
17 18 19 20
from Tkinter import *
import Pmw

class ChipWidget(Pmw.MegaWidget):
Barry Warsaw's avatar
Barry Warsaw committed
21 22
    _WIDTH = 150
    _HEIGHT = 80
Barry Warsaw's avatar
Barry Warsaw committed
23 24

    def __init__(self, parent=None, **kw):
Barry Warsaw's avatar
Barry Warsaw committed
25 26 27 28 29 30 31
	options = (('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, options)
Barry Warsaw's avatar
Barry Warsaw committed
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

	# 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)

61
    # called whenever `color' option is set
Barry Warsaw's avatar
Barry Warsaw committed
62
    def __set_color(self):
63
	color = self['color']
Barry Warsaw's avatar
Barry Warsaw committed
64 65 66 67 68 69 70 71 72 73 74
	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)
75 76 77
    widget = ChipWidget(root, color='red',
			chip_width=200,
			label_text='Selected Color')
Barry Warsaw's avatar
Barry Warsaw committed
78 79
    widget.pack()
    root.mainloop()