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