1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
from Tkinter import *
import Pmw
import string
class TypeinWidget(Pmw.MegaWidget):
def __init__(self, parent=None, **kw):
options = (('color', (128, 128, 128), self.__set_color),
('delegate', None, None),
)
self.defineoptions(kw, options)
Pmw.MegaWidget.__init__(self, parent)
interiorarg = (self.interior(),)
# create the x, y, and z label
self.__x = self.createcomponent(
'x', (), None,
Pmw.EntryField, interiorarg,
label_text='Red',
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)
self.__y = self.createcomponent(
'y', (), None,
Pmw.EntryField, interiorarg,
label_text='Green',
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)
self.__z = self.createcomponent(
'z', (), None,
Pmw.EntryField, interiorarg,
label_text='Blue',
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)
# Check keywords and initialize options
self.initialiseoptions(TypeinWidget)
#
# 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
def __str_to_int(self, text):
try:
if text[:2] == '0x':
return string.atoi(text[2:], 16)
else:
return string.atoi(text)
except:
return None
def __validate(self, text):
val = self.__str_to_int(text)
if (val is not None) and (val >= 0) and (val < 256):
return 1
else:
return -1
# called whenever a text entry is modified
def __modified(self):
# these are guaranteed to be valid, right?
vals = map(lambda x: x.get(), (self.__x, self.__y, self.__z))
rgbs = tuple(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):
rgbtuple = self['color']
self.set_color(self, rgbtuple)
|