mered Greg's suggestions, added docstring

This commit is contained in:
Barry Warsaw 1998-01-30 21:11:52 +00:00
parent 7080a7f69e
commit b6db1b94c5

View file

@ -1,3 +1,19 @@
"""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 * from Tkinter import *
import Pmw import Pmw
@ -6,10 +22,11 @@ class ChipWidget(Pmw.MegaWidget):
_HEIGHT = 100 _HEIGHT = 100
def __init__(self, parent=None, **kw): def __init__(self, parent=None, **kw):
optionsdefs = (('chipcolor', 'blue', self.__set_color), optionsdefs = (('chip_borderwidth', 2, None),
('width', self._WIDTH, self.__set_dims), ('chip_width', self._WIDTH, None),
('height', self._HEIGHT, self.__set_dims), ('chip_height', self._HEIGHT, None),
('text', 'Color', self.__set_label), ('label_text', 'Color', None),
('color', 'blue', self.__set_color),
) )
self.defineoptions(kw, optionsdefs) self.defineoptions(kw, optionsdefs)
@ -41,22 +58,12 @@ class ChipWidget(Pmw.MegaWidget):
# Check keywords and initialize options # Check keywords and initialize options
self.initialiseoptions(ChipWidget) self.initialiseoptions(ChipWidget)
# called whenever `chipcolor' option is set # called whenever `color' option is set
def __set_color(self): def __set_color(self):
color = self['chipcolor'] color = self['color']
self.__chip['background'] = color self.__chip['background'] = color
self.__name['text'] = color self.__name['text'] = color
def __set_dims(self):
width = self['width']
height = self['height']
self.__chip.configure(width=width, height=height)
def __set_label(self):
self.__label['text'] = self['text']
Pmw.forwardmethods(ChipWidget, Frame, '__chip')
if __name__ == '__main__': if __name__ == '__main__':
@ -65,7 +72,8 @@ if __name__ == '__main__':
exitbtn = Button(root, text='Exit', command=root.destroy) exitbtn = Button(root, text='Exit', command=root.destroy)
exitbtn.pack(side=BOTTOM) exitbtn.pack(side=BOTTOM)
widget = ChipWidget(root, chipcolor='red', width=200, widget = ChipWidget(root, color='red',
text='Selected Color') chip_width=200,
label_text='Selected Color')
widget.pack() widget.pack()
root.mainloop() root.mainloop()