3 The TypeinViewer is what you see at the lower right of the main Pynche
4 widget. It contains three text entry fields, one each for red, green, blue.
5 Input into these windows is highly constrained; it only allows you to enter
6 values that are legal for a color axis. This usually means 0-255 for decimal
7 input and 0x0 - 0xff for hex input.
9 You can toggle whether you want to view and input the values in either decimal
10 or hex by clicking on Hexadecimal. By clicking on Update while typing, the
11 color selection will be made on every change to the text field. Otherwise,
12 you must hit Return or Tab to select the color.
22 def __init__(self
, switchboard
, master
=None):
24 self
.__sb
= switchboard
25 optiondb
= switchboard
.optiondb()
26 self
.__hexp
= BooleanVar()
27 self
.__hexp
.set(optiondb
.get('HEXTYPE', 0))
28 self
.__uwtyping
= BooleanVar()
29 self
.__uwtyping
.set(optiondb
.get('UPWHILETYPE', 0))
31 self
.__frame
= Frame(master
, relief
=RAISED
, borderwidth
=1)
32 self
.__frame
.grid(row
=3, column
=1, sticky
='NSEW')
34 self
.__xl
= Label(self
.__frame
, text
='Red:')
35 self
.__xl
.grid(row
=0, column
=0, sticky
=E
)
36 subframe
= Frame(self
.__frame
)
37 subframe
.grid(row
=0, column
=1)
38 self
.__xox
= Label(subframe
, text
='0x')
39 self
.__xox
.grid(row
=0, column
=0, sticky
=E
)
40 self
.__xox
['font'] = 'courier'
41 self
.__x
= Entry(subframe
, width
=3)
42 self
.__x
.grid(row
=0, column
=1)
43 self
.__x
.bindtags(self
.__x
.bindtags() + ('Normalize', 'Update'))
44 self
.__x
.bind_class('Normalize', '<Key>', self
.__normalize
)
45 self
.__x
.bind_class('Update' , '<Key>', self
.__maybeupdate
)
47 self
.__yl
= Label(self
.__frame
, text
='Green:')
48 self
.__yl
.grid(row
=1, column
=0, sticky
=E
)
49 subframe
= Frame(self
.__frame
)
50 subframe
.grid(row
=1, column
=1)
51 self
.__yox
= Label(subframe
, text
='0x')
52 self
.__yox
.grid(row
=0, column
=0, sticky
=E
)
53 self
.__yox
['font'] = 'courier'
54 self
.__y
= Entry(subframe
, width
=3)
55 self
.__y
.grid(row
=0, column
=1)
56 self
.__y
.bindtags(self
.__y
.bindtags() + ('Normalize', 'Update'))
58 self
.__zl
= Label(self
.__frame
, text
='Blue:')
59 self
.__zl
.grid(row
=2, column
=0, sticky
=E
)
60 subframe
= Frame(self
.__frame
)
61 subframe
.grid(row
=2, column
=1)
62 self
.__zox
= Label(subframe
, text
='0x')
63 self
.__zox
.grid(row
=0, column
=0, sticky
=E
)
64 self
.__zox
['font'] = 'courier'
65 self
.__z
= Entry(subframe
, width
=3)
66 self
.__z
.grid(row
=0, column
=1)
67 self
.__z
.bindtags(self
.__z
.bindtags() + ('Normalize', 'Update'))
68 # Update while typing?
69 self
.__uwt
= Checkbutton(self
.__frame
,
70 text
='Update while typing',
71 variable
=self
.__uwtyping
)
72 self
.__uwt
.grid(row
=3, column
=0, columnspan
=2, sticky
=W
)
74 self
.__hex
= Checkbutton(self
.__frame
,
77 command
=self
.__togglehex
)
78 self
.__hex
.grid(row
=4, column
=0, columnspan
=2, sticky
=W
)
80 def __togglehex(self
, event
=None):
81 red
, green
, blue
= self
.__sb
.current_rgb()
86 self
.__xox
['text'] = label
87 self
.__yox
['text'] = label
88 self
.__zox
['text'] = label
89 self
.update_yourself(red
, green
, blue
)
91 def __normalize(self
, event
=None):
94 icursor
= ew
.index(INSERT
)
95 if contents
and contents
[0] in 'xX' and self
.__hexp
.get():
96 contents
= '0' + contents
97 # Figure out the contents in the current base.
100 v
= int(contents
, 16)
105 # If value is not legal, or empty, delete the last character inserted
106 # and ring the bell. Don't ring the bell if the field is empty (it'll
110 elif v
< 0 or v
> 255:
113 contents
= contents
[:i
-1] + contents
[i
:]
116 elif self
.__hexp
.get():
117 contents
= hex(v
)[2:]
121 ew
.insert(0, contents
)
124 def __maybeupdate(self
, event
=None):
125 if self
.__uwtyping
.get() or event
.keysym
in ('Return', 'Tab'):
128 def __update(self
, event
=None):
129 redstr
= self
.__x
.get() or '0'
130 greenstr
= self
.__y
.get() or '0'
131 bluestr
= self
.__z
.get() or '0'
132 if self
.__hexp
.get():
136 red
, green
, blue
= [int(x
, base
) for x
in (redstr
, greenstr
, bluestr
)]
137 self
.__sb
.update_views(red
, green
, blue
)
139 def update_yourself(self
, red
, green
, blue
):
140 if self
.__hexp
.get():
141 sred
, sgreen
, sblue
= [hex(x
)[2:] for x
in (red
, green
, blue
)]
143 sred
, sgreen
, sblue
= red
, green
, blue
144 x
, y
, z
= self
.__x
, self
.__y
, self
.__z
145 xicursor
= x
.index(INSERT
)
146 yicursor
= y
.index(INSERT
)
147 zicursor
= z
.index(INSERT
)
161 def save_options(self
, optiondb
):
162 optiondb
['HEXTYPE'] = self
.__hexp
.get()
163 optiondb
['UPWHILETYPE'] = self
.__uwtyping
.get()