Mercurial > cpython-lru_cache-weakref
annotate Tools/pynche/ChipViewer.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 1d84bf5f9478 |
| children | 37d399aa8c16 |
| rev | line source |
|---|---|
| 9223 | 1 """Chip viewer and widget. |
| 2 | |
| 3 In the lower left corner of the main Pynche window, you will see two | |
| 4 ChipWidgets, one for the selected color and one for the nearest color. The | |
| 5 selected color is the actual RGB value expressed as an X11 #COLOR name. The | |
| 6 nearest color is the named color from the X11 database that is closest to the | |
| 7 selected color in 3D space. There may be other colors equally close, but the | |
| 8 nearest one is the first one found. | |
| 9 | |
| 10 Clicking on the nearest color chip selects that named color. | |
| 11 | |
| 12 The ChipViewer class includes the entire lower left quandrant; i.e. both the | |
| 13 selected and nearest ChipWidgets. | |
| 14 """ | |
| 15 | |
| 9168 | 16 from Tkinter import * |
| 17 import ColorDB | |
|
7179
7d61fa9b57a8
mered Greg's suggestions, added docstring
Barry Warsaw <barry@python.org>
parents:
7178
diff
changeset
|
18 |
| 9168 | 19 |
| 20 class ChipWidget: | |
| 7238 | 21 _WIDTH = 150 |
| 22 _HEIGHT = 80 | |
| 7178 | 23 |
| 9168 | 24 def __init__(self, |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
9414
diff
changeset
|
25 master = None, |
| 9168 | 26 width = _WIDTH, |
| 27 height = _HEIGHT, | |
| 28 text = 'Color', | |
| 29 initialcolor = 'blue', | |
| 30 presscmd = None, | |
| 31 releasecmd = None): | |
| 32 # create the text label | |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
9414
diff
changeset
|
33 self.__label = Label(master, text=text) |
| 9168 | 34 self.__label.grid(row=0, column=0) |
| 35 # create the color chip, implemented as a frame | |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
9414
diff
changeset
|
36 self.__chip = Frame(master, relief=RAISED, borderwidth=2, |
| 9168 | 37 width=width, |
| 38 height=height, | |
| 39 background=initialcolor) | |
| 40 self.__chip.grid(row=1, column=0) | |
|
19446
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
41 # create the color name |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
42 self.__namevar = StringVar() |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
43 self.__namevar.set(initialcolor) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
44 self.__name = Entry(master, textvariable=self.__namevar, |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
45 relief=FLAT, justify=CENTER, state=DISABLED, |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
46 font=self.__label['font']) |
| 9168 | 47 self.__name.grid(row=2, column=0) |
|
19446
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
48 # create the message area |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
49 self.__msgvar = StringVar() |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
50 self.__name = Entry(master, textvariable=self.__msgvar, |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
51 relief=FLAT, justify=CENTER, state=DISABLED, |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
52 font=self.__label['font']) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
53 self.__name.grid(row=3, column=0) |
| 9168 | 54 # set bindings |
| 55 if presscmd: | |
| 56 self.__chip.bind('<ButtonPress-1>', presscmd) | |
| 57 if releasecmd: | |
| 58 self.__chip.bind('<ButtonRelease-1>', releasecmd) | |
| 7178 | 59 |
|
19446
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
60 def set_color(self, color): |
| 9168 | 61 self.__chip.config(background=color) |
| 7178 | 62 |
| 9168 | 63 def get_color(self): |
| 64 return self.__chip['background'] | |
| 7178 | 65 |
|
19446
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
66 def set_name(self, colorname): |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
67 self.__namevar.set(colorname) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
68 |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
69 def set_message(self, message): |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
70 self.__msgvar.set(message) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
71 |
| 9168 | 72 def press(self): |
| 73 self.__chip.configure(relief=SUNKEN) | |
| 7178 | 74 |
| 9168 | 75 def release(self): |
| 76 self.__chip.configure(relief=RAISED) | |
| 7178 | 77 |
| 78 | |
| 79 | |
| 9168 | 80 class ChipViewer: |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
9414
diff
changeset
|
81 def __init__(self, switchboard, master=None): |
| 9168 | 82 self.__sb = switchboard |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
9414
diff
changeset
|
83 self.__frame = Frame(master, relief=RAISED, borderwidth=1) |
|
9496
5d81e42c6ba2
(ChipViewer): Make frame sticky NSEW to fix geometry problem.
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
84 self.__frame.grid(row=3, column=0, ipadx=5, sticky='NSEW') |
| 9168 | 85 # create the chip that will display the currently selected color |
| 86 # exactly | |
| 87 self.__sframe = Frame(self.__frame) | |
| 88 self.__sframe.grid(row=0, column=0) | |
| 89 self.__selected = ChipWidget(self.__sframe, text='Selected') | |
| 90 # create the chip that will display the nearest real X11 color | |
| 91 # database color name | |
| 92 self.__nframe = Frame(self.__frame) | |
| 93 self.__nframe.grid(row=0, column=1) | |
| 94 self.__nearest = ChipWidget(self.__nframe, text='Nearest', | |
| 95 presscmd = self.__buttonpress, | |
| 96 releasecmd = self.__buttonrelease) | |
| 7178 | 97 |
| 9168 | 98 def update_yourself(self, red, green, blue): |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
99 # Selected always shows the #rrggbb name of the color, nearest always |
| 18455 | 100 # shows the name of the nearest color in the database. BAW: should |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
101 # an exact match be indicated in some way? |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
102 # |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
19446
diff
changeset
|
103 # Always use the #rrggbb style to actually set the color, since we may |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
104 # not be using X color names (e.g. "web-safe" names) |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
105 colordb = self.__sb.colordb() |
| 9168 | 106 rgbtuple = (red, green, blue) |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
107 rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple) |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
108 # find the nearest |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
109 nearest = colordb.nearest(red, green, blue) |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
110 nearest_tuple = colordb.find_byname(nearest) |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
111 nearest_rrggbb = ColorDB.triplet_to_rrggbb(nearest_tuple) |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
112 self.__selected.set_color(rrggbb) |
|
19446
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
113 self.__nearest.set_color(nearest_rrggbb) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
114 # set the name and messages areas |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
115 self.__selected.set_name(rrggbb) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
116 if rrggbb == nearest_rrggbb: |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
117 self.__selected.set_message(nearest) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
118 else: |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
119 self.__selected.set_message('') |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
120 self.__nearest.set_name(nearest_rrggbb) |
|
f5d87c41f65b
Implement some suggestions by Laura Creighton.
Barry Warsaw <barry@python.org>
parents:
18455
diff
changeset
|
121 self.__nearest.set_message(nearest) |
| 9168 | 122 |
| 123 def __buttonpress(self, event=None): | |
| 124 self.__nearest.press() | |
| 125 | |
| 126 def __buttonrelease(self, event=None): | |
| 127 self.__nearest.release() | |
|
10744
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
128 rrggbb = self.__nearest.get_color() |
|
4c9de6fd267b
Lots of changes to support loading alternative color name database.
Barry Warsaw <barry@python.org>
parents:
9496
diff
changeset
|
129 red, green, blue = ColorDB.rrggbb_to_triplet(rrggbb) |
| 9168 | 130 self.__sb.update_views(red, green, blue) |
