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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
# The options of a widget are described by the following attributes
# of the Pack and Widget dialogs:
#
# Dialog.current: {name: value}
# -- changes during Widget's lifetime
#
# Dialog.options: {name: (default, klass)}
# -- depends on widget class only
#
# Dialog.classes: {klass: (v0, v1, v2, ...) | 'boolean' | 'other'}
# -- totally static, though different between PackDialog and WidgetDialog
# (but even that could be unified)
from Tkinter import *
class Option:
varclass = StringVar # May be overridden
def __init__(self, dialog, option):
self.dialog = dialog
self.option = option
self.master = dialog.top
self.default, self.klass = dialog.options[option]
self.var = self.varclass(self.master)
self.frame = Frame(self.master,
{Pack: {'expand': 0, 'fill': 'x'}})
self.label = Label(self.frame,
{'text': option + ':',
Pack: {'side': 'left'},
})
self.update()
self.addoption()
def refresh(self):
self.dialog.refresh()
self.update()
def update(self):
try:
self.current = self.dialog.current[self.option]
except KeyError:
self.current = self.default
self.var.set(self.current)
def set(self, e=None): # Should be overridden
pass
class BooleanOption(Option):
varclass = BooleanVar
def addoption(self):
self.button = Checkbutton(self.frame,
{'text': 'on/off',
'onvalue': '1',
'offvalue': '0',
'variable': self.var,
'relief': 'raised',
'borderwidth': 2,
'command': self.set,
Pack: {'side': 'right'},
})
class EnumOption(Option):
def addoption(self):
self.button = Menubutton(self.frame,
{'textvariable': self.var,
'relief': 'raised',
'borderwidth': 2,
Pack: {'side': 'right'},
})
self.menu = Menu(self.button)
self.button['menu'] = self.menu
for v in self.dialog.classes[self.klass]:
self.menu.add_radiobutton(
{'label': v,
'variable': self.var,
'value': v,
'command': self.set,
})
class StringOption(Option):
def addoption(self):
self.entry = Entry(self.frame,
{'textvariable': self.var,
'width': 10,
'relief': 'sunken',
'borderwidth': 2,
Pack: {'side': 'right',
'fill': 'x', 'expand': 1},
})
self.entry.bind('<Return>', self.set)
class ReadonlyOption(Option):
def addoption(self):
self.label = Label(self.frame,
{'textvariable': self.var,
Pack: {'side': 'right'}})
class Dialog:
def __init__(self, master):
self.master = master
self.refresh()
self.top = Toplevel(self.master)
self.top.title(self.__class__.__name__)
self.top.minsize(1, 1)
self.addchoices()
def addchoices(self):
self.choices = {}
list = []
for k, dc in self.options.items():
list.append(k, dc)
list.sort()
for k, (d, c) in list:
try:
cl = self.classes[c]
except KeyError:
cl = 'unknown'
if type(cl) == TupleType:
cl = self.enumoption
elif cl == 'boolean':
cl = self.booleanoption
elif cl == 'readonly':
cl = self.readonlyoption
else:
cl = self.stringoption
self.choices[k] = cl(self, k)
booleanoption = BooleanOption
stringoption = StringOption
enumoption = EnumOption
readonlyoption = ReadonlyOption
class PackDialog(Dialog):
def __init__(self, widget):
self.widget = widget
Dialog.__init__(self, widget)
def refresh(self):
self.current = self.widget.newinfo()
self.current['.class'] = self.widget.winfo_class()
class packoption: # Mix-in class
def set(self, e=None):
self.current = self.var.get()
try:
Pack.config(self.dialog.widget,
{self.option: self.current})
except TclError:
self.refresh()
class booleanoption(packoption, BooleanOption): pass
class enumoption(packoption, EnumOption): pass
class stringoption(packoption, StringOption): pass
class readonlyoption(packoption, ReadonlyOption): pass
options = {
'.class': (None, 'Class'),
'after': (None, 'Widet'),
'anchor': ('center', 'Anchor'),
'before': (None, 'Widget'),
'expand': ('no', 'Boolean'),
'fill': ('none', 'Fill'),
'in': (None, 'Widget'),
'ipadx': (0, 'Pad'),
'ipady': (0, 'Pad'),
'padx': (0, 'Pad'),
'pady': (0, 'Pad'),
'side': ('top', 'Side'),
}
classes = {
'Anchor': ('n','ne', 'e','se', 's','sw', 'w','nw', 'center'),
'Boolean': 'boolean',
'Class': 'readonly',
'Expand': 'boolean',
'Fill': ('none', 'x', 'y', 'both'),
'Pad': 'pixel',
'Side': ('top', 'right', 'bottom', 'left'),
'Widget': 'readonly',
}
class RemotePackDialog(PackDialog):
def __init__(self, master, app, widget):
self.master = master
self.app = app
self.widget = widget
self.refresh()
self.top = Toplevel(self.master)
self.top.title('Remote %s Pack: %s' % (self.app, self.widget))
self.top.minsize(1, 1) # XXX
self.addchoices()
def refresh(self):
try:
words = self.master.tk.splitlist(
self.master.send(self.app,
'pack',
'newinfo',
self.widget))
except TclError, msg:
print 'send pack newinfo', self.widget, ':', msg
return
dict = {}
for i in range(0, len(words), 2):
key = words[i][1:]
value = words[i+1]
dict[key] = value
dict['.class'] = self.master.send(self.app,
'winfo',
'class',
self.widget)
self.current = dict
class remotepackoption: # Mix-in class
def set(self, e=None):
self.current = self.var.get()
try:
self.dialog.master.send(
self.dialog.app,
'pack', 'config', self.dialog.widget,
'-'+self.option, self.current)
except TclError, msg:
print 'send pack config ... :', msg
self.refresh()
class booleanoption(remotepackoption, BooleanOption): pass
class enumoption(remotepackoption, EnumOption): pass
class stringoption(remotepackoption, StringOption): pass
class readonlyoption(remotepackoption, ReadonlyOption): pass
class WidgetDialog(Dialog):
def __init__(self, widget):
self.widget = widget
if self.addclasses.has_key(self.widget.widgetName):
classes = {}
for c in (self.classes,
self.addclasses[self.widget.widgetName]):
for k in c.keys():
classes[k] = c[k]
self.classes = classes
Dialog.__init__(self, widget)
def refresh(self):
self.configuration = self.widget.config()
self.current = {}
self.options = {}
options['.class'] = (None, 'Class')
self.current['.class'] = self.widget.winfo_class()
for k, v in self.configuration.items():
if len(v) > 4:
self.current[k] = v[4]
self.options[k] = v[3], v[2] # default, klass
class widgetoption: # Mix-in class
def set(self, e=None):
self.current = self.var.get()
try:
self.dialog.widget[self.option] = self.current
except TclError:
self.refresh()
class booleanoption(widgetoption, BooleanOption): pass
class enumoption(widgetoption, EnumOption): pass
class stringoption(widgetoption, StringOption): pass
class readonlyoption(widgetoption, ReadonlyOption): pass
# Universal classes
classes = {
'Anchor': ('n','ne', 'e','se', 's','sw', 'w','nw', 'center'),
'Aspect': 'integer',
'Background': 'color',
'Bitmap': 'bitmap',
'BorderWidth': 'pixel',
'Class': 'readonly',
'CloseEnough': 'double',
'Command': 'command',
'Confine': 'boolean',
'Cursor': 'cursor',
'CursorWidth': 'pixel',
'DisabledForeground': 'color',
'ExportSelection': 'boolean',
'Font': 'font',
'Foreground': 'color',
'From': 'integer',
'Geometry': 'geometry',
'Height': 'pixel',
'InsertWidth': 'time',
'Justify': ('left', 'center', 'right'),
'Label': 'string',
'Length': 'pixel',
'MenuName': 'widget',
'OffTime': 'time',
'OnTime': 'time',
'Orient': ('horizontal', 'vertical'),
'Pad': 'pixel',
'Relief': ('raised', 'sunken', 'flat', 'ridge', 'groove'),
'RepeatDelay': 'time',
'RepeatInterval': 'time',
'ScrollCommand': 'command',
'ScrollIncrement': 'pixel',
'ScrollRegion': 'rectangle',
'ShowValue': 'boolean',
'SetGrid': 'boolean',
'Sliderforeground': 'color',
'SliderLength': 'pixel',
'Text': 'string',
'TickInterval': 'integer',
'To': 'integer',
'Underline': 'index',
'Variable': 'variable',
'Value': 'string',
'Width': 'pixel',
'Wrap': ('none', 'char', 'word'),
}
# Classes that (may) differ per widget type
_tristate = {'State': ('normal', 'active', 'disabled')}
_bistate = {'State': ('normal', 'disabled')}
addclasses = {
'button': _tristate,
'radiobutton': _tristate,
'checkbutton': _tristate,
'entry': _bistate,
'text': _bistate,
'menubutton': _tristate,
'slider': _bistate,
}
def test():
import sys
root = Tk()
root.minsize(1, 1)
if sys.argv[2:]:
pd = RemotePackDialog(root, sys.argv[1], sys.argv[2])
else:
frame = Frame(root, {Pack: {'expand': 1, 'fill': 'both'}})
button = Button(frame, {'text': 'button',
Pack: {'expand': 1}})
canvas = Canvas(frame, {Pack: {}})
bpd = PackDialog(button)
bwd = WidgetDialog(button)
cpd = PackDialog(canvas)
cwd = WidgetDialog(canvas)
root.mainloop()
test()
|