1998-02-10 00:13:06 +00:00
|
|
|
from Tkinter import *
|
|
|
|
import Pmw
|
1998-02-11 17:11:34 +00:00
|
|
|
import string
|
1998-02-10 00:13:06 +00:00
|
|
|
|
|
|
|
class TypeinWidget(Pmw.MegaWidget):
|
|
|
|
def __init__(self, parent=None, **kw):
|
1998-02-11 17:11:34 +00:00
|
|
|
options = (('color', (128, 128, 128), self.__set_color),
|
|
|
|
('delegate', None, None),
|
1998-02-10 00:13:06 +00:00
|
|
|
)
|
1998-03-10 00:17:30 +00:00
|
|
|
self.__update = 1
|
1998-02-10 00:13:06 +00:00
|
|
|
self.defineoptions(kw, options)
|
|
|
|
|
1998-02-12 19:52:46 +00:00
|
|
|
Pmw.MegaWidget.__init__(self, parent)
|
1998-02-10 00:13:06 +00:00
|
|
|
interiorarg = (self.interior(),)
|
|
|
|
|
|
|
|
# create the x, y, and z label
|
|
|
|
self.__x = self.createcomponent(
|
|
|
|
'x', (), None,
|
|
|
|
Pmw.EntryField, interiorarg,
|
|
|
|
label_text='Red',
|
1998-02-11 17:11:34 +00:00
|
|
|
label_width=5,
|
|
|
|
label_anchor=E,
|
|
|
|
labelpos=W,
|
1998-02-10 00:13:06 +00:00
|
|
|
maxwidth=4,
|
1998-02-11 17:11:34 +00:00
|
|
|
entry_width=4,
|
1998-02-10 00:13:06 +00:00
|
|
|
validate=self.__validate,
|
1998-03-10 00:17:30 +00:00
|
|
|
modifiedcommand=self.__modified,
|
|
|
|
command=self.__force_modify)
|
1998-02-10 00:13:06 +00:00
|
|
|
self.__x.grid(row=0, column=0)
|
|
|
|
|
|
|
|
self.__y = self.createcomponent(
|
|
|
|
'y', (), None,
|
|
|
|
Pmw.EntryField, interiorarg,
|
|
|
|
label_text='Green',
|
1998-02-11 17:11:34 +00:00
|
|
|
label_width=5,
|
|
|
|
label_anchor=E,
|
|
|
|
labelpos=W,
|
1998-02-10 00:13:06 +00:00
|
|
|
maxwidth=4,
|
1998-02-11 17:11:34 +00:00
|
|
|
entry_width=4,
|
1998-02-10 00:13:06 +00:00
|
|
|
validate=self.__validate,
|
1998-03-10 00:17:30 +00:00
|
|
|
modifiedcommand=self.__modified,
|
|
|
|
command=self.__force_modify)
|
1998-02-10 00:13:06 +00:00
|
|
|
self.__y.grid(row=1, column=0)
|
|
|
|
|
|
|
|
self.__z = self.createcomponent(
|
|
|
|
'z', (), None,
|
|
|
|
Pmw.EntryField, interiorarg,
|
|
|
|
label_text='Blue',
|
1998-02-11 17:11:34 +00:00
|
|
|
label_width=5,
|
|
|
|
label_anchor=E,
|
|
|
|
labelpos=W,
|
1998-02-10 00:13:06 +00:00
|
|
|
maxwidth=4,
|
1998-02-11 17:11:34 +00:00
|
|
|
entry_width=4,
|
1998-02-10 00:13:06 +00:00
|
|
|
validate=self.__validate,
|
1998-03-10 00:17:30 +00:00
|
|
|
modifiedcommand=self.__modified,
|
|
|
|
command=self.__force_modify)
|
1998-02-10 00:13:06 +00:00
|
|
|
self.__z.grid(row=2, column=0)
|
|
|
|
|
|
|
|
# Check keywords and initialize options
|
|
|
|
self.initialiseoptions(TypeinWidget)
|
|
|
|
|
1998-02-11 17:11:34 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
1998-03-10 00:17:30 +00:00
|
|
|
def set_update_on_typing(self, flag):
|
|
|
|
self.__update = flag
|
|
|
|
|
1998-02-11 17:11:34 +00:00
|
|
|
#
|
|
|
|
# PRIVATE INTERFACE
|
|
|
|
#
|
|
|
|
|
1998-02-10 00:13:06 +00:00
|
|
|
# called to validate the entry text
|
|
|
|
def __str_to_int(self, text):
|
|
|
|
try:
|
1998-02-11 17:11:34 +00:00
|
|
|
if text[:2] == '0x':
|
|
|
|
return string.atoi(text[2:], 16)
|
|
|
|
else:
|
|
|
|
return string.atoi(text)
|
1998-02-10 00:13:06 +00:00
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __validate(self, text):
|
|
|
|
val = self.__str_to_int(text)
|
1998-02-13 21:29:13 +00:00
|
|
|
if (val is not None) and (val >= 0) and (val < 256):
|
1998-02-10 00:13:06 +00:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
# called whenever a text entry is modified
|
1998-03-10 00:17:30 +00:00
|
|
|
def __modified(self, force=None):
|
1998-02-10 00:13:06 +00:00
|
|
|
# these are guaranteed to be valid, right?
|
1998-03-16 23:09:04 +00:00
|
|
|
vals = []
|
|
|
|
for field in (self.__x, self.__y, self.__z):
|
|
|
|
vals.append(field.get())
|
1998-02-18 00:06:20 +00:00
|
|
|
rgbs = tuple(map(self.__str_to_int, vals))
|
1998-02-11 17:11:34 +00:00
|
|
|
valids = map(self.__validate, vals)
|
|
|
|
delegate = self['delegate']
|
1998-03-10 00:17:30 +00:00
|
|
|
if ((force or self.__update) and
|
|
|
|
(None not in rgbs) and
|
|
|
|
(-1 not in valids) and
|
|
|
|
delegate):
|
|
|
|
#
|
1998-02-11 17:11:34 +00:00
|
|
|
delegate.set_color(self, rgbs)
|
1998-02-10 00:13:06 +00:00
|
|
|
|
1998-03-10 00:17:30 +00:00
|
|
|
def __force_modify(self):
|
|
|
|
self.__modified(force=1)
|
|
|
|
|
1998-02-10 00:13:06 +00:00
|
|
|
# called whenever the color option is changed
|
|
|
|
def __set_color(self):
|
1998-02-11 17:11:34 +00:00
|
|
|
rgbtuple = self['color']
|
|
|
|
self.set_color(self, rgbtuple)
|