#Layout looks good, as does validation, and delegate chaining

This commit is contained in:
Barry Warsaw 1998-02-11 17:11:34 +00:00
parent 5d8f0edc33
commit 4a445c689c
1 changed files with 44 additions and 29 deletions

View File

@ -1,10 +1,11 @@
from Tkinter import * from Tkinter import *
import Pmw import Pmw
import string
class TypeinWidget(Pmw.MegaWidget): class TypeinWidget(Pmw.MegaWidget):
def __init__(self, parent=None, **kw): def __init__(self, parent=None, **kw):
options = (('delegate', None, None), options = (('color', (128, 128, 128), self.__set_color),
('color', (128, 128, 128), self.__set_color), ('delegate', None, None),
) )
self.defineoptions(kw, options) self.defineoptions(kw, options)
@ -16,8 +17,11 @@ class TypeinWidget(Pmw.MegaWidget):
'x', (), None, 'x', (), None,
Pmw.EntryField, interiorarg, Pmw.EntryField, interiorarg,
label_text='Red', label_text='Red',
labelpos=E, label_width=5,
label_anchor=E,
labelpos=W,
maxwidth=4, maxwidth=4,
entry_width=4,
validate=self.__validate, validate=self.__validate,
modifiedcommand=self.__modified) modifiedcommand=self.__modified)
self.__x.grid(row=0, column=0) self.__x.grid(row=0, column=0)
@ -26,8 +30,11 @@ class TypeinWidget(Pmw.MegaWidget):
'y', (), None, 'y', (), None,
Pmw.EntryField, interiorarg, Pmw.EntryField, interiorarg,
label_text='Green', label_text='Green',
labelpos=E, label_width=5,
label_anchor=E,
labelpos=W,
maxwidth=4, maxwidth=4,
entry_width=4,
validate=self.__validate, validate=self.__validate,
modifiedcommand=self.__modified) modifiedcommand=self.__modified)
self.__y.grid(row=1, column=0) self.__y.grid(row=1, column=0)
@ -36,35 +43,42 @@ class TypeinWidget(Pmw.MegaWidget):
'z', (), None, 'z', (), None,
Pmw.EntryField, interiorarg, Pmw.EntryField, interiorarg,
label_text='Blue', label_text='Blue',
labelpos=E, label_width=5,
label_anchor=E,
labelpos=W,
maxwidth=4, maxwidth=4,
entry_width=4,
validate=self.__validate, validate=self.__validate,
modifiedcommand=self.__modified) modifiedcommand=self.__modified)
self.__z.grid(row=2, column=0) self.__z.grid(row=2, column=0)
# TBD: gross hack, fix later
self.__initializing = 1
# Check keywords and initialize options # Check keywords and initialize options
self.initialiseoptions(TypeinWidget) self.initialiseoptions(TypeinWidget)
# TBD: gross hack, fix later
self.__initializing = 0
# public set color interface #
def set_color(self, red, green, blue): # PUBLIC INTERFACE
self.__x.configure(label_text=`red`) #
self.__y.configure(label_text=`green`)
self.__z.configure(label_text=`blue`) def set_color(self, obj, rgbtuple):
# dispatch to the delegate # break infloop
delegate = self['delegate'] red, green, blue = rgbtuple
if delegate: self.__x.setentry(`red`)
delegate.set_color(red, green, blue) self.__y.setentry(`green`)
self.__z.setentry(`blue`)
if obj == self:
return
#
# PRIVATE INTERFACE
#
# called to validate the entry text # called to validate the entry text
SAFE_EVAL = {'__builtins__': {}}
def __str_to_int(self, text): def __str_to_int(self, text):
try: try:
val = eval(text, self.SAFE_EVAL, {}) if text[:2] == '0x':
return val return string.atoi(text[2:], 16)
else:
return string.atoi(text)
except: except:
return None return None
@ -78,13 +92,14 @@ class TypeinWidget(Pmw.MegaWidget):
# called whenever a text entry is modified # called whenever a text entry is modified
def __modified(self): def __modified(self):
# these are guaranteed to be valid, right? # these are guaranteed to be valid, right?
red = self.__str_to_int(self.__x['value']) vals = map(lambda x: x.get(), (self.__x, self.__y, self.__z))
green = self.__str_to_int(self.__y['value']) rgbs = map(self.__str_to_int, vals)
blue = self.__str_to_int(self.__z['value']) valids = map(self.__validate, vals)
self.set_color(red, green, blue) delegate = self['delegate']
if None not in rgbs and -1 not in valids and delegate:
delegate.set_color(self, rgbs)
# called whenever the color option is changed # called whenever the color option is changed
def __set_color(self): def __set_color(self):
red, green, blue = self['color'] rgbtuple = self['color']
if not self.__initializing: self.set_color(self, rgbtuple)
self.set_color(red, green, blue)