Skip to content

Commit dc492e6

Browse files
committed
Un PMW-ified
1 parent 63c9e98 commit dc492e6

1 file changed

Lines changed: 84 additions & 71 deletions

File tree

Tools/pynche/ChipViewer.py

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,92 @@
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-
1+
from types import StringType
172
from Tkinter import *
18-
import Pmw
3+
import ColorDB
194

20-
class ChipWidget(Pmw.MegaWidget):
5+
6+
class ChipWidget:
217
_WIDTH = 150
228
_HEIGHT = 80
239

24-
def __init__(self, parent=None, **kw):
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)
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-
61-
# called whenever `color' option is set
62-
def __set_color(self):
63-
color = self['color']
64-
self.__chip['background'] = color
65-
self.__name['text'] = color
10+
def __init__(self,
11+
parent = None,
12+
width = _WIDTH,
13+
height = _HEIGHT,
14+
text = 'Color',
15+
initialcolor = 'blue',
16+
presscmd = None,
17+
releasecmd = None):
18+
# create the text label
19+
self.__label = Label(parent, text=text)
20+
self.__label.grid(row=0, column=0)
21+
# create the color chip, implemented as a frame
22+
self.__chip = Frame(parent, relief=RAISED, borderwidth=2,
23+
width=width,
24+
height=height,
25+
background=initialcolor)
26+
self.__chip.grid(row=1, column=0)
27+
# create the color name, ctor argument must be a string
28+
self.__name = Label(parent, text=initialcolor)
29+
self.__name.grid(row=2, column=0)
30+
#
31+
# set bindings
32+
if presscmd:
33+
self.__chip.bind('<ButtonPress-1>', presscmd)
34+
if releasecmd:
35+
self.__chip.bind('<ButtonRelease-1>', releasecmd)
36+
37+
def set_color(self, color):
38+
self.__chip.config(background=color)
39+
self.__name.config(text=color)
40+
41+
def get_color(self):
42+
return self.__chip['background']
43+
44+
def press(self):
45+
self.__chip.configure(relief=SUNKEN)
46+
47+
def release(self):
48+
self.__chip.configure(relief=RAISED)
6649

6750

6851

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)
75-
widget = ChipWidget(root, color='red',
76-
chip_width=200,
77-
label_text='Selected Color')
78-
widget.pack()
79-
root.mainloop()
52+
class ChipViewer:
53+
def __init__(self, switchboard, parent=None):
54+
self.__sb = switchboard
55+
self.__frame = Frame(parent)
56+
self.__frame.pack()
57+
# create the chip that will display the currently selected color
58+
# exactly
59+
self.__sframe = Frame(self.__frame)
60+
self.__sframe.grid(row=0, column=0)
61+
self.__selected = ChipWidget(self.__sframe, text='Selected')
62+
# create the chip that will display the nearest real X11 color
63+
# database color name
64+
self.__nframe = Frame(self.__frame)
65+
self.__nframe.grid(row=0, column=1)
66+
self.__nearest = ChipWidget(self.__nframe, text='Nearest',
67+
presscmd = self.__buttonpress,
68+
releasecmd = self.__buttonrelease)
69+
70+
def update_yourself(self, red, green, blue):
71+
# TBD: should exactname default to X11 color name if their is an exact
72+
# match for the rgb triplet? Part of me says it's nice to see both
73+
# names for the color, the other part says that it's better to
74+
# feedback the exact match.
75+
rgbtuple = (red, green, blue)
76+
try:
77+
allcolors = self.__sb.colordb().find_byrgb(rgbtuple)
78+
exactname = allcolors[0]
79+
except ColorDB.BadColor:
80+
exactname = ColorDB.triplet_to_rrggbb(rgbtuple)
81+
nearest = self.__sb.colordb().nearest(red, green, blue)
82+
self.__selected.set_color(exactname)
83+
self.__nearest.set_color(nearest)
84+
85+
def __buttonpress(self, event=None):
86+
self.__nearest.press()
87+
88+
def __buttonrelease(self, event=None):
89+
self.__nearest.release()
90+
colorname = self.__nearest.get_color()
91+
red, green, blue = self.__sb.colordb().find_byname(colorname)
92+
self.__sb.update_views(red, green, blue)

0 commit comments

Comments
 (0)