mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Un PMW-ified
This commit is contained in:
parent
63c9e9851e
commit
dc492e6738
1 changed files with 79 additions and 66 deletions
|
@ -1,79 +1,92 @@
|
||||||
"""Color chip megawidget.
|
from types import StringType
|
||||||
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 ColorDB
|
||||||
|
|
||||||
class ChipWidget(Pmw.MegaWidget):
|
|
||||||
|
class ChipWidget:
|
||||||
_WIDTH = 150
|
_WIDTH = 150
|
||||||
_HEIGHT = 80
|
_HEIGHT = 80
|
||||||
|
|
||||||
def __init__(self, parent=None, **kw):
|
def __init__(self,
|
||||||
options = (('chip_borderwidth', 2, None),
|
parent = None,
|
||||||
('chip_width', self._WIDTH, None),
|
width = _WIDTH,
|
||||||
('chip_height', self._HEIGHT, None),
|
height = _HEIGHT,
|
||||||
('label_text', 'Color', None),
|
text = 'Color',
|
||||||
('color', 'blue', self.__set_color),
|
initialcolor = 'blue',
|
||||||
)
|
presscmd = None,
|
||||||
self.defineoptions(kw, options)
|
releasecmd = None):
|
||||||
|
# create the text label
|
||||||
|
self.__label = Label(parent, text=text)
|
||||||
|
self.__label.grid(row=0, column=0)
|
||||||
|
# create the color chip, implemented as a frame
|
||||||
|
self.__chip = Frame(parent, relief=RAISED, borderwidth=2,
|
||||||
|
width=width,
|
||||||
|
height=height,
|
||||||
|
background=initialcolor)
|
||||||
|
self.__chip.grid(row=1, column=0)
|
||||||
|
# create the color name, ctor argument must be a string
|
||||||
|
self.__name = Label(parent, text=initialcolor)
|
||||||
|
self.__name.grid(row=2, column=0)
|
||||||
|
#
|
||||||
|
# set bindings
|
||||||
|
if presscmd:
|
||||||
|
self.__chip.bind('<ButtonPress-1>', presscmd)
|
||||||
|
if releasecmd:
|
||||||
|
self.__chip.bind('<ButtonRelease-1>', releasecmd)
|
||||||
|
|
||||||
# initialize base class -- after defining options
|
def set_color(self, color):
|
||||||
Pmw.MegaWidget.__init__(self, parent)
|
self.__chip.config(background=color)
|
||||||
interiorarg = (self.interior(),)
|
self.__name.config(text=color)
|
||||||
|
|
||||||
# create the label
|
def get_color(self):
|
||||||
self.__label = self.createcomponent(
|
return self.__chip['background']
|
||||||
# component name, aliases, group
|
|
||||||
'label', (), None,
|
|
||||||
# widget class, widget args
|
|
||||||
Label, interiorarg)
|
|
||||||
self.__label.grid(row=0, column=0)
|
|
||||||
|
|
||||||
# create the color chip
|
def press(self):
|
||||||
self.__chip = self.createcomponent(
|
self.__chip.configure(relief=SUNKEN)
|
||||||
'chip', (), None,
|
|
||||||
Frame, interiorarg,
|
|
||||||
relief=RAISED, borderwidth=2)
|
|
||||||
self.__chip.grid(row=1, column=0)
|
|
||||||
|
|
||||||
# create the color name
|
def release(self):
|
||||||
self.__name = self.createcomponent(
|
self.__chip.configure(relief=RAISED)
|
||||||
'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__':
|
class ChipViewer:
|
||||||
root = Pmw.initialise(fontScheme='pmw1')
|
def __init__(self, switchboard, parent=None):
|
||||||
root.title('ChipWidget demonstration')
|
self.__sb = switchboard
|
||||||
|
self.__frame = Frame(parent)
|
||||||
|
self.__frame.pack()
|
||||||
|
# create the chip that will display the currently selected color
|
||||||
|
# exactly
|
||||||
|
self.__sframe = Frame(self.__frame)
|
||||||
|
self.__sframe.grid(row=0, column=0)
|
||||||
|
self.__selected = ChipWidget(self.__sframe, text='Selected')
|
||||||
|
# create the chip that will display the nearest real X11 color
|
||||||
|
# database color name
|
||||||
|
self.__nframe = Frame(self.__frame)
|
||||||
|
self.__nframe.grid(row=0, column=1)
|
||||||
|
self.__nearest = ChipWidget(self.__nframe, text='Nearest',
|
||||||
|
presscmd = self.__buttonpress,
|
||||||
|
releasecmd = self.__buttonrelease)
|
||||||
|
|
||||||
exitbtn = Button(root, text='Exit', command=root.destroy)
|
def update_yourself(self, red, green, blue):
|
||||||
exitbtn.pack(side=BOTTOM)
|
# TBD: should exactname default to X11 color name if their is an exact
|
||||||
widget = ChipWidget(root, color='red',
|
# match for the rgb triplet? Part of me says it's nice to see both
|
||||||
chip_width=200,
|
# names for the color, the other part says that it's better to
|
||||||
label_text='Selected Color')
|
# feedback the exact match.
|
||||||
widget.pack()
|
rgbtuple = (red, green, blue)
|
||||||
root.mainloop()
|
try:
|
||||||
|
allcolors = self.__sb.colordb().find_byrgb(rgbtuple)
|
||||||
|
exactname = allcolors[0]
|
||||||
|
except ColorDB.BadColor:
|
||||||
|
exactname = ColorDB.triplet_to_rrggbb(rgbtuple)
|
||||||
|
nearest = self.__sb.colordb().nearest(red, green, blue)
|
||||||
|
self.__selected.set_color(exactname)
|
||||||
|
self.__nearest.set_color(nearest)
|
||||||
|
|
||||||
|
def __buttonpress(self, event=None):
|
||||||
|
self.__nearest.press()
|
||||||
|
|
||||||
|
def __buttonrelease(self, event=None):
|
||||||
|
self.__nearest.release()
|
||||||
|
colorname = self.__nearest.get_color()
|
||||||
|
red, green, blue = self.__sb.colordb().find_byname(colorname)
|
||||||
|
self.__sb.update_views(red, green, blue)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue