blob: 066dfe39452f82a0bc4d5c2e0e79781f2b542715 [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum18468821994-06-20 07:49:28 +00003import tkinter
4from tkinter import TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00005from types import *
Guido van Rossum18468821994-06-20 07:49:28 +00006
Guido van Rossum761c5ab1995-07-14 15:29:10 +00007CallableTypes = (FunctionType, MethodType,
8 BuiltinFunctionType, BuiltinMethodType)
9
10TkVersion = eval(tkinter.TK_VERSION)
11TclVersion = eval(tkinter.TCL_VERSION)
12if TkVersion < 4.0:
13 raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
Guido van Rossum18468821994-06-20 07:49:28 +000014
Guido van Rossum2dcf5291994-07-06 09:23:20 +000015def _flatten(tuple):
16 res = ()
17 for item in tuple:
18 if type(item) in (TupleType, ListType):
19 res = res + _flatten(item)
20 else:
21 res = res + (item,)
22 return res
23
24def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000025 if type(cnfs) is DictionaryType:
26 _fixgeometry(cnfs)
27 return cnfs
28 elif type(cnfs) in (NoneType, StringType):
29
Guido van Rossum2dcf5291994-07-06 09:23:20 +000030 return cnfs
31 else:
32 cnf = {}
33 for c in _flatten(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000034 _fixgeometry(c)
Guido van Rossum2dcf5291994-07-06 09:23:20 +000035 for k, v in c.items():
36 cnf[k] = v
37 return cnf
38
Guido van Rossum761c5ab1995-07-14 15:29:10 +000039if TkVersion >= 4.0:
40 _fixg_warning = "Warning: patched up pre-Tk-4.0 geometry option\n"
41 def _fixgeometry(c):
42 if c.has_key('geometry'):
43 wh = _parsegeometry(c['geometry'])
44 if wh:
45 # Print warning message -- once
46 global _fixg_warning
47 if _fixg_warning:
48 import sys
49 sys.stderr.write(_fixg_warning)
50 _fixg_warning = None
51 w, h = wh
52 c['width'] = w
53 c['height'] = h
54 del c['geometry']
55 def _parsegeometry(s):
56 from string import splitfields
57 fields = splitfields(s, 'x')
58 if len(fields) == 2:
59 return tuple(fields)
60 # else: return None
61else:
62 def _fixgeometry(c): pass
63
Guido van Rossum2dcf5291994-07-06 09:23:20 +000064class Event:
65 pass
66
Guido van Rossumaec5dc91994-06-27 07:55:12 +000067_default_root = None
68
Guido van Rossum45853db1994-06-20 12:19:19 +000069def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000070 pass
71
Guido van Rossum97aeca11994-07-07 13:12:12 +000072def _exit(code='0'):
73 import sys
74 sys.exit(getint(code))
75
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076_varnum = 0
77class Variable:
78 def __init__(self, master=None):
79 global _default_root
80 global _varnum
81 if master:
82 self._tk = master.tk
83 else:
84 self._tk = _default_root.tk
85 self._name = 'PY_VAR' + `_varnum`
86 _varnum = _varnum + 1
87 def __del__(self):
88 self._tk.unsetvar(self._name)
89 def __str__(self):
90 return self._name
91 def __call__(self, value=None):
92 if value == None:
93 return self.get()
94 else:
95 self.set(value)
96 def set(self, value):
97 return self._tk.setvar(self._name, value)
98
99class StringVar(Variable):
100 def __init__(self, master=None):
101 Variable.__init__(self, master)
102 def get(self):
103 return self._tk.getvar(self._name)
104
105class IntVar(Variable):
106 def __init__(self, master=None):
107 Variable.__init__(self, master)
108 def get(self):
109 return self._tk.getint(self._tk.getvar(self._name))
110
111class DoubleVar(Variable):
112 def __init__(self, master=None):
113 Variable.__init__(self, master)
114 def get(self):
115 return self._tk.getdouble(self._tk.getvar(self._name))
116
117class BooleanVar(Variable):
118 def __init__(self, master=None):
119 Variable.__init__(self, master)
120 def get(self):
121 return self._tk.getboolean(self._tk.getvar(self._name))
122
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000123def mainloop():
124 _default_root.tk.mainloop()
125
126def getint(s):
127 return _default_root.tk.getint(s)
128
129def getdouble(s):
130 return _default_root.tk.getdouble(s)
131
132def getboolean(s):
133 return _default_root.tk.getboolean(s)
134
Guido van Rossum18468821994-06-20 07:49:28 +0000135class Misc:
136 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000137 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000138 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000139 def tk_menuBar(self, *args):
140 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000141 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000142 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000143 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000144 def wait_window(self, window=None):
145 if window == None:
146 window = self
147 self.tk.call('tkwait', 'window', window._w)
148 def wait_visibility(self, window=None):
149 if window == None:
150 window = self
151 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000152 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000153 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000154 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000155 return self.tk.getvar(name)
156 def getint(self, s):
157 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000158 def getdouble(self, s):
159 return self.tk.getdouble(s)
160 def getboolean(self, s):
161 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000162 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000163 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000164 focus = focus_set # XXX b/w compat?
165 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000166 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000167 def focus_default_none(self):
168 self.tk.call('focus', 'default', 'none')
169 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000170 def focus_none(self):
171 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000172 def focus_get(self):
173 name = self.tk.call('focus')
174 if name == 'none': return None
175 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000176 def after(self, ms, func=None, *args):
177 if not func:
178 self.tk.call('after', ms)
179 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000180 # XXX Disgusting hack to clean up after calling func
181 tmp = []
182 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
183 try:
184 apply(func, args)
185 finally:
186 tk.deletecommand(tmp[0])
187 name = self._register(callit)
188 tmp.append(name)
189 self.tk.call('after', ms, name)
Guido van Rossum45853db1994-06-20 12:19:19 +0000190 # XXX grab current w/o window argument
191 def grab_current(self):
192 name = self.tk.call('grab', 'current', self._w)
193 if not name: return None
194 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000195 def grab_release(self):
196 self.tk.call('grab', 'release', self._w)
197 def grab_set(self):
198 self.tk.call('grab', 'set', self._w)
199 def grab_set_global(self):
200 self.tk.call('grab', 'set', '-global', self._w)
201 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000202 status = self.tk.call('grab', 'status', self._w)
203 if status == 'none': status = None
204 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000205 def lower(self, belowThis=None):
206 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000207 def option_add(self, pattern, value, priority = None):
208 self.tk.call('option', 'add', pattern, priority)
209 def option_clear(self):
210 self.tk.call('option', 'clear')
211 def option_get(self, name, className):
212 return self.tk.call('option', 'get', self._w, name, className)
213 def option_readfile(self, fileName, priority = None):
214 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000215 def selection_clear(self):
216 self.tk.call('selection', 'clear', self._w)
217 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000218 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000219 def selection_handle(self, func, type=None, format=None):
220 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000221 self.tk.call('selection', 'handle', self._w,
222 name, type, format)
223 def selection_own(self, func=None):
224 name = self._register(func)
225 self.tk.call('selection', 'own', self._w, name)
226 def selection_own_get(self):
227 return self._nametowidget(self.tk.call('selection', 'own'))
228 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000229 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000230 def lower(self, belowThis=None):
231 self.tk.call('lift', self._w, belowThis)
232 def tkraise(self, aboveThis=None):
233 self.tk.call('raise', self._w, aboveThis)
234 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000235 def colormodel(self, value=None):
236 return self.tk.call('tk', 'colormodel', self._w, value)
237 def winfo_atom(self, name):
238 return self.tk.getint(self.tk.call('winfo', 'atom', name))
239 def winfo_atomname(self, id):
240 return self.tk.call('winfo', 'atomname', id)
241 def winfo_cells(self):
242 return self.tk.getint(
243 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000244 def winfo_children(self):
245 return map(self._nametowidget,
246 self.tk.splitlist(self.tk.call(
247 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000248 def winfo_class(self):
249 return self.tk.call('winfo', 'class', self._w)
250 def winfo_containing(self, rootX, rootY):
251 return self.tk.call('winfo', 'containing', rootx, rootY)
252 def winfo_depth(self):
253 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
254 def winfo_exists(self):
255 return self.tk.getint(
256 self.tk.call('winfo', 'exists', self._w))
257 def winfo_fpixels(self, number):
258 return self.tk.getdouble(self.tk.call(
259 'winfo', 'fpixels', self._w, number))
260 def winfo_geometry(self):
261 return self.tk.call('winfo', 'geometry', self._w)
262 def winfo_height(self):
263 return self.tk.getint(
264 self.tk.call('winfo', 'height', self._w))
265 def winfo_id(self):
266 return self.tk.getint(
267 self.tk.call('winfo', 'id', self._w))
268 def winfo_interps(self):
269 return self.tk.splitlist(
270 self.tk.call('winfo', 'interps'))
271 def winfo_ismapped(self):
272 return self.tk.getint(
273 self.tk.call('winfo', 'ismapped', self._w))
274 def winfo_name(self):
275 return self.tk.call('winfo', 'name', self._w)
276 def winfo_parent(self):
277 return self.tk.call('winfo', 'parent', self._w)
278 def winfo_pathname(self, id):
279 return self.tk.call('winfo', 'pathname', id)
280 def winfo_pixels(self, number):
281 return self.tk.getint(
282 self.tk.call('winfo', 'pixels', self._w, number))
283 def winfo_reqheight(self):
284 return self.tk.getint(
285 self.tk.call('winfo', 'reqheight', self._w))
286 def winfo_reqwidth(self):
287 return self.tk.getint(
288 self.tk.call('winfo', 'reqwidth', self._w))
289 def winfo_rgb(self, color):
290 return self._getints(
291 self.tk.call('winfo', 'rgb', self._w, color))
292 def winfo_rootx(self):
293 return self.tk.getint(
294 self.tk.call('winfo', 'rootx', self._w))
295 def winfo_rooty(self):
296 return self.tk.getint(
297 self.tk.call('winfo', 'rooty', self._w))
298 def winfo_screen(self):
299 return self.tk.call('winfo', 'screen', self._w)
300 def winfo_screencells(self):
301 return self.tk.getint(
302 self.tk.call('winfo', 'screencells', self._w))
303 def winfo_screendepth(self):
304 return self.tk.getint(
305 self.tk.call('winfo', 'screendepth', self._w))
306 def winfo_screenheight(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'screenheight', self._w))
309 def winfo_screenmmheight(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'screenmmheight', self._w))
312 def winfo_screenmmwidth(self):
313 return self.tk.getint(
314 self.tk.call('winfo', 'screenmmwidth', self._w))
315 def winfo_screenvisual(self):
316 return self.tk.call('winfo', 'screenvisual', self._w)
317 def winfo_screenwidth(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'screenwidth', self._w))
320 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000321 return self._nametowidget(self.tk.call(
322 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000323 def winfo_visual(self):
324 return self.tk.call('winfo', 'visual', self._w)
325 def winfo_vrootheight(self):
326 return self.tk.getint(
327 self.tk.call('winfo', 'vrootheight', self._w))
328 def winfo_vrootwidth(self):
329 return self.tk.getint(
330 self.tk.call('winfo', 'vrootwidth', self._w))
331 def winfo_vrootx(self):
332 return self.tk.getint(
333 self.tk.call('winfo', 'vrootx', self._w))
334 def winfo_vrooty(self):
335 return self.tk.getint(
336 self.tk.call('winfo', 'vrooty', self._w))
337 def winfo_width(self):
338 return self.tk.getint(
339 self.tk.call('winfo', 'width', self._w))
340 def winfo_x(self):
341 return self.tk.getint(
342 self.tk.call('winfo', 'x', self._w))
343 def winfo_y(self):
344 return self.tk.getint(
345 self.tk.call('winfo', 'y', self._w))
346 def update(self):
347 self.tk.call('update')
348 def update_idletasks(self):
349 self.tk.call('update', 'idletasks')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000350 def bind(self, sequence, func=None, add=''):
351 if add: add = '+'
352 if func:
353 name = self._register(func, self._substitute)
354 self.tk.call('bind', self._w, sequence,
355 (add + name,) + self._subst_format)
356 else:
357 return self.tk.call('bind', self._w, sequence)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000358 def unbind(self, sequence):
359 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000360 def bind_all(self, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000361 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000362 if func:
363 name = self._register(func, self._substitute)
364 self.tk.call('bind', 'all' , sequence,
365 (add + name,) + self._subst_format)
366 else:
367 return self.tk.call('bind', 'all', sequence)
368 def unbind_all(self, sequence):
369 self.tk.call('bind', 'all' , sequence, '')
370 def bind_class(self, className, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000371 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000372 if func:
373 name = self._register(func, self._substitute)
374 self.tk.call('bind', className , sequence,
375 (add + name,) + self._subst_format)
376 else:
377 return self.tk.call('bind', className, sequence)
378 def unbind_class(self, className, sequence):
379 self.tk.call('bind', className , sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +0000380 def mainloop(self):
381 self.tk.mainloop()
382 def quit(self):
383 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000384 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000385 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000386 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
387 def _getdoubles(self, string):
388 if not string: return None
389 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000390 def _getboolean(self, string):
391 if string:
392 return self.tk.getboolean(string)
Guido van Rossum18468821994-06-20 07:49:28 +0000393 def _options(self, cnf):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000394 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000395 res = ()
396 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000397 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000398 v = self._register(v)
399 res = res + ('-'+k, v)
400 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000401 def _nametowidget(self, name):
402 w = self
403 if name[0] == '.':
404 w = w._root()
405 name = name[1:]
406 from string import find
407 while name:
408 i = find(name, '.')
409 if i >= 0:
410 name, tail = name[:i], name[i+1:]
411 else:
412 tail = ''
413 w = w.children[name]
414 name = tail
415 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000416 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000417 f = _CallSafely(func, subst).__call__
418 name = `id(f)`
419 if hasattr(func, 'im_func'):
420 func = func.im_func
421 if hasattr(func, 'func_name') and \
422 type(func.func_name) == type(''):
423 name = name + func.func_name
424 self.tk.createcommand(name, f)
425 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000426 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000427 def _root(self):
428 w = self
429 while w.master: w = w.master
430 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000431 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000432 '%s', '%t', '%w', '%x', '%y',
433 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
434 def _substitute(self, *args):
435 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000436 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000437 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
438 # Missing: (a, c, d, m, o, v, B, R)
439 e = Event()
440 e.serial = tk.getint(nsign)
441 e.num = tk.getint(b)
442 try: e.focus = tk.getboolean(f)
443 except TclError: pass
444 e.height = tk.getint(h)
445 e.keycode = tk.getint(k)
446 e.state = tk.getint(s)
447 e.time = tk.getint(t)
448 e.width = tk.getint(w)
449 e.x = tk.getint(x)
450 e.y = tk.getint(y)
451 e.char = A
452 try: e.send_event = tk.getboolean(E)
453 except TclError: pass
454 e.keysym = K
455 e.keysym_num = tk.getint(N)
456 e.type = T
457 e.widget = self._nametowidget(W)
458 e.x_root = tk.getint(X)
459 e.y_root = tk.getint(Y)
460 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000461
462class _CallSafely:
463 def __init__(self, func, subst=None):
464 self.func = func
465 self.subst = subst
466 def __call__(self, *args):
467 if self.subst:
468 args = self.apply_func(self.subst, args)
469 args = self.apply_func(self.func, args)
470 def apply_func(self, func, args):
471 import sys
472 try:
473 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000474 except SystemExit, msg:
475 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000476 except:
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000477 import traceback
478 print "Exception in Tkinter callback"
479 traceback.print_exc()
Guido van Rossum18468821994-06-20 07:49:28 +0000480
481class Wm:
482 def aspect(self,
483 minNumer=None, minDenom=None,
484 maxNumer=None, maxDenom=None):
485 return self._getints(
486 self.tk.call('wm', 'aspect', self._w,
487 minNumer, minDenom,
488 maxNumer, maxDenom))
489 def client(self, name=None):
490 return self.tk.call('wm', 'client', self._w, name)
491 def command(self, value=None):
492 return self.tk.call('wm', 'command', self._w, value)
493 def deiconify(self):
494 return self.tk.call('wm', 'deiconify', self._w)
495 def focusmodel(self, model=None):
496 return self.tk.call('wm', 'focusmodel', self._w, model)
497 def frame(self):
498 return self.tk.call('wm', 'frame', self._w)
499 def geometry(self, newGeometry=None):
500 return self.tk.call('wm', 'geometry', self._w, newGeometry)
501 def grid(self,
502 baseWidht=None, baseHeight=None,
503 widthInc=None, heightInc=None):
504 return self._getints(self.tk.call(
505 'wm', 'grid', self._w,
506 baseWidht, baseHeight, widthInc, heightInc))
507 def group(self, pathName=None):
508 return self.tk.call('wm', 'group', self._w, pathName)
509 def iconbitmap(self, bitmap=None):
510 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
511 def iconify(self):
512 return self.tk.call('wm', 'iconify', self._w)
513 def iconmask(self, bitmap=None):
514 return self.tk.call('wm', 'iconmask', self._w, bitmap)
515 def iconname(self, newName=None):
516 return self.tk.call('wm', 'iconname', self._w, newName)
517 def iconposition(self, x=None, y=None):
518 return self._getints(self.tk.call(
519 'wm', 'iconposition', self._w, x, y))
520 def iconwindow(self, pathName=None):
521 return self.tk.call('wm', 'iconwindow', self._w, pathName)
522 def maxsize(self, width=None, height=None):
523 return self._getints(self.tk.call(
524 'wm', 'maxsize', self._w, width, height))
525 def minsize(self, width=None, height=None):
526 return self._getints(self.tk.call(
527 'wm', 'minsize', self._w, width, height))
528 def overrideredirect(self, boolean=None):
529 return self._getboolean(self.tk.call(
530 'wm', 'overrideredirect', self._w, boolean))
531 def positionfrom(self, who=None):
532 return self.tk.call('wm', 'positionfrom', self._w, who)
533 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000534 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000535 command = self._register(func)
536 else:
537 command = func
538 return self.tk.call(
539 'wm', 'protocol', self._w, name, command)
540 def sizefrom(self, who=None):
541 return self.tk.call('wm', 'sizefrom', self._w, who)
542 def state(self):
543 return self.tk.call('wm', 'state', self._w)
544 def title(self, string=None):
545 return self.tk.call('wm', 'title', self._w, string)
546 def transient(self, master=None):
547 return self.tk.call('wm', 'transient', self._w, master)
548 def withdraw(self):
549 return self.tk.call('wm', 'withdraw', self._w)
550
551class Tk(Misc, Wm):
552 _w = '.'
553 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000554 self.master = None
555 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000556 if baseName is None:
557 import sys, os
558 baseName = os.path.basename(sys.argv[0])
559 if baseName[-3:] == '.py': baseName = baseName[:-3]
560 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000561 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000562 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000563 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000564 def destroy(self):
565 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000566 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000567 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000568 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000569 def readprofile(self, baseName, className):
570 import os
571 if os.environ.has_key('HOME'): home = os.environ['HOME']
572 else: home = os.curdir
573 class_tcl = os.path.join(home, '.%s.tcl' % className)
574 class_py = os.path.join(home, '.%s.py' % className)
575 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
576 base_py = os.path.join(home, '.%s.py' % baseName)
577 dir = {'self': self}
578 exec 'from Tkinter import *' in dir
579 if os.path.isfile(class_tcl):
580 print 'source', `class_tcl`
581 self.tk.call('source', class_tcl)
582 if os.path.isfile(class_py):
583 print 'execfile', `class_py`
584 execfile(class_py, dir)
585 if os.path.isfile(base_tcl):
586 print 'source', `base_tcl`
587 self.tk.call('source', base_tcl)
588 if os.path.isfile(base_py):
589 print 'execfile', `base_py`
590 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000591
592class Pack:
593 def config(self, cnf={}):
594 apply(self.tk.call,
595 ('pack', 'configure', self._w)
596 + self._options(cnf))
597 pack = config
598 def __setitem__(self, key, value):
599 Pack.config({key: value})
600 def forget(self):
601 self.tk.call('pack', 'forget', self._w)
602 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000603 words = self.tk.splitlist(
604 self.tk.call('pack', 'newinfo', self._w))
605 dict = {}
606 for i in range(0, len(words), 2):
607 key = words[i][1:]
608 value = words[i+1]
609 if value[0] == '.':
610 value = self._nametowidget(value)
611 dict[key] = value
612 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000613 info = newinfo
Guido van Rossum5505d561994-12-30 17:16:35 +0000614 _noarg_ = ['_noarg_']
615 def propagate(self, flag=_noarg_):
616 if boolean is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000617 return self._getboolean(self.tk.call(
618 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000619 else:
620 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum18468821994-06-20 07:49:28 +0000621 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000622 return map(self._nametowidget,
623 self.tk.splitlist(
624 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000625
626class Place:
627 def config(self, cnf={}):
628 apply(self.tk.call,
629 ('place', 'configure', self._w)
630 + self._options(cnf))
631 place = config
632 def __setitem__(self, key, value):
633 Place.config({key: value})
634 def forget(self):
635 self.tk.call('place', 'forget', self._w)
636 def info(self):
637 return self.tk.call('place', 'info', self._w)
638 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000639 return map(self._nametowidget,
640 self.tk.splitlist(
641 self.tk.call(
642 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000643
Guido van Rossum18468821994-06-20 07:49:28 +0000644class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000645 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000646 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000647 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000648 if not _default_root:
649 _default_root = Tk()
650 master = _default_root
651 if not _default_root:
652 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000653 self.master = master
654 self.tk = master.tk
655 if cnf.has_key('name'):
656 name = cnf['name']
657 del cnf['name']
658 else:
659 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000660 self._name = name
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000661 if self.widgetName=='photo':
662 self._w=name
663 elif master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000664 self._w = '.' + name
665 else:
666 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000667 self.children = {}
668 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000669 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000670 self.master.children[self._name] = self
671 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000672 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000673 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000674 Widget._setup(self, master, cnf)
675 extra1=()
676 if widgetName=='photo':
677 extra1=('image', 'create')
678 apply(self.tk.call, extra1+(widgetName, self._w)+extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000679 if cnf:
680 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000681 def config(self, cnf=None):
Guido van Rossum2a390311994-07-06 10:20:11 +0000682 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000683 if cnf is None:
684 cnf = {}
685 for x in self.tk.split(
686 self.tk.call(self._w, 'configure')):
687 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
688 return cnf
689 if type(cnf) == StringType:
690 x = self.tk.split(self.tk.call(
691 self._w, 'configure', '-'+cnf))
692 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000693 for k in cnf.keys():
694 if type(k) == ClassType:
695 k.config(self, cnf[k])
696 del cnf[k]
697 apply(self.tk.call, (self._w, 'configure')
698 + self._options(cnf))
699 def __getitem__(self, key):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000700 if TkVersion >= 4.0:
701 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000702 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000703 self._w, 'configure', '-' + key))
704 return v[4]
705 def __setitem__(self, key, value):
706 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000707 def keys(self):
708 return map(lambda x: x[0][1:],
709 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000710 def __str__(self):
711 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000712 def destroy(self):
713 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000714 if self.master.children.has_key(self._name):
715 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000716 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000717 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000718 return apply(self.tk.call, (self._w, name) + args)
719 def unbind_class(self, seq):
720 Misc.unbind_class(self, self.widgetName, seq)
Guido van Rossum18468821994-06-20 07:49:28 +0000721
722class Toplevel(Widget, Wm):
723 def __init__(self, master=None, cnf={}):
724 extra = ()
725 if cnf.has_key('screen'):
726 extra = ('-screen', cnf['screen'])
727 del cnf['screen']
728 if cnf.has_key('class'):
729 extra = extra + ('-class', cnf['class'])
730 del cnf['class']
731 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000732 root = self._root()
733 self.iconname(root.iconname())
734 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000735
736class Button(Widget):
737 def __init__(self, master=None, cnf={}):
738 Widget.__init__(self, master, 'button', cnf)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000739 def tkButtonEnter(self, *dummy):
740 self.tk.call('tkButtonEnter', self._w)
741 def tkButtonLeave(self, *dummy):
742 self.tk.call('tkButtonLeave', self._w)
743 def tkButtonDown(self, *dummy):
744 self.tk.call('tkButtonDown', self._w)
745 def tkButtonUp(self, *dummy):
746 self.tk.call('tkButtonUp', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000747 def flash(self):
748 self.tk.call(self._w, 'flash')
749 def invoke(self):
750 self.tk.call(self._w, 'invoke')
751
752# Indices:
753def AtEnd():
754 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000755def AtInsert(*args):
756 s = 'insert'
757 for a in args:
758 if a: s = s + (' ' + a)
759 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000760def AtSelFirst():
761 return 'sel.first'
762def AtSelLast():
763 return 'sel.last'
764def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000765 if y is None:
766 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000767 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000768 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000769
770class Canvas(Widget):
771 def __init__(self, master=None, cnf={}):
772 Widget.__init__(self, master, 'canvas', cnf)
773 def addtag(self, *args):
774 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000775 def addtag_above(self, tagOrId):
776 self.addtag('above', tagOrId)
777 def addtag_all(self):
778 self.addtag('all')
779 def addtag_below(self, tagOrId):
780 self.addtag('below', tagOrId)
781 def addtag_closest(self, x, y, halo=None, start=None):
782 self.addtag('closest', x, y, halo, start)
783 def addtag_enclosed(self, x1, y1, x2, y2):
784 self.addtag('enclosed', x1, y1, x2, y2)
785 def addtag_overlapping(self, x1, y1, x2, y2):
786 self.addtag('overlapping', x1, y1, x2, y2)
787 def addtag_withtag(self, tagOrId):
788 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000789 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000790 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000791 def tag_unbind(self, tagOrId, sequence):
792 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
793 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000794 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000795 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000796 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000797 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000798 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000799 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000800 self._w, 'canvasx', screenx, gridspacing))
801 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000802 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000803 self._w, 'canvasy', screeny, gridspacing))
804 def coords(self, *args):
805 return self._do('coords', args)
806 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000807 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000808 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000809 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000810 args = args[:-1]
811 else:
812 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000813 return self.tk.getint(apply(
814 self.tk.call,
815 (self._w, 'create', itemType)
816 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000817 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000818 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000819 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000820 return Canvas._create(self, 'bitmap', args)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000821 def create_image(self, *args):
822 return Canvas._create(self, 'image', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000823 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000824 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000825 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000826 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000827 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000828 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000829 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000830 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000831 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000832 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000833 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000834 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000835 def dchars(self, *args):
836 self._do('dchars', args)
837 def delete(self, *args):
838 self._do('delete', args)
839 def dtag(self, *args):
840 self._do('dtag', args)
841 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000842 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000843 def find_above(self, tagOrId):
844 return self.find('above', tagOrId)
845 def find_all(self):
846 return self.find('all')
847 def find_below(self, tagOrId):
848 return self.find('below', tagOrId)
849 def find_closest(self, x, y, halo=None, start=None):
850 return self.find('closest', x, y, halo, start)
851 def find_enclosed(self, x1, y1, x2, y2):
852 return self.find('enclosed', x1, y1, x2, y2)
853 def find_overlapping(self, x1, y1, x2, y2):
854 return self.find('overlapping', x1, y1, x2, y2)
855 def find_withtag(self, tagOrId):
856 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000857 def focus(self, *args):
858 return self._do('focus', args)
859 def gettags(self, *args):
860 return self.tk.splitlist(self._do('gettags', args))
861 def icursor(self, *args):
862 self._do('icursor', args)
863 def index(self, *args):
864 return self.tk.getint(self._do('index', args))
865 def insert(self, *args):
866 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000867 def itemconfig(self, tagOrId, cnf=None):
868 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000869 cnf = {}
870 for x in self.tk.split(
871 self._do('itemconfigure', (tagOrId))):
872 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
873 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000874 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000875 x = self.tk.split(self._do('itemconfigure',
876 (tagOrId, '-'+cnf,)))
877 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000878 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
879 def lower(self, *args):
880 self._do('lower', args)
881 def move(self, *args):
882 self._do('move', args)
883 def postscript(self, cnf={}):
884 return self._do('postscript', self._options(cnf))
885 def tkraise(self, *args):
886 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000887 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000888 def scale(self, *args):
889 self._do('scale', args)
890 def scan_mark(self, x, y):
891 self.tk.call(self._w, 'scan', 'mark', x, y)
892 def scan_dragto(self, x, y):
893 self.tk.call(self._w, 'scan', 'dragto', x, y)
894 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000895 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000896 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000897 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000898 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000899 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000900 def select_item(self):
901 self.tk.call(self._w, 'select', 'item')
902 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000903 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000904 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000905 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000906 def xview(self, *args):
907 apply(self.tk.call, (self._w, 'xview')+args)
908 def yview(self, *args):
909 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +0000910
911class Checkbutton(Widget):
912 def __init__(self, master=None, cnf={}):
913 Widget.__init__(self, master, 'checkbutton', cnf)
914 def deselect(self):
915 self.tk.call(self._w, 'deselect')
916 def flash(self):
917 self.tk.call(self._w, 'flash')
918 def invoke(self):
919 self.tk.call(self._w, 'invoke')
920 def select(self):
921 self.tk.call(self._w, 'select')
922 def toggle(self):
923 self.tk.call(self._w, 'toggle')
924
925class Entry(Widget):
926 def __init__(self, master=None, cnf={}):
927 Widget.__init__(self, master, 'entry', cnf)
928 def tk_entryBackspace(self):
929 self.tk.call('tk_entryBackspace', self._w)
930 def tk_entryBackword(self):
931 self.tk.call('tk_entryBackword', self._w)
932 def tk_entrySeeCaret(self):
933 self.tk.call('tk_entrySeeCaret', self._w)
934 def delete(self, first, last=None):
935 self.tk.call(self._w, 'delete', first, last)
936 def get(self):
937 return self.tk.call(self._w, 'get')
938 def icursor(self, index):
939 self.tk.call(self._w, 'icursor', index)
940 def index(self, index):
941 return self.tk.getint(self.tk.call(
942 self._w, 'index', index))
943 def insert(self, index, string):
944 self.tk.call(self._w, 'insert', index, string)
945 def scan_mark(self, x):
946 self.tk.call(self._w, 'scan', 'mark', x)
947 def scan_dragto(self, x):
948 self.tk.call(self._w, 'scan', 'dragto', x)
949 def select_adjust(self, index):
950 self.tk.call(self._w, 'select', 'adjust', index)
951 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000952 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000953 def select_from(self, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000954 self.tk.call(self._w, 'select', 'set', index)
Guido van Rossum18468821994-06-20 07:49:28 +0000955 def select_to(self, index):
956 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000957 def view(self, index):
958 self.tk.call(self._w, 'view', index)
Guido van Rossum18468821994-06-20 07:49:28 +0000959
960class Frame(Widget):
961 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000962 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000963 extra = ()
964 if cnf.has_key('class'):
965 extra = ('-class', cnf['class'])
966 del cnf['class']
967 Widget.__init__(self, master, 'frame', cnf, extra)
968 def tk_menuBar(self, *args):
969 apply(self.tk.call, ('tk_menuBar', self._w) + args)
970
971class Label(Widget):
972 def __init__(self, master=None, cnf={}):
973 Widget.__init__(self, master, 'label', cnf)
974
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000975class Photo(Widget):
976 def __init__(self, master=None, cnf={}):
977 Widget.__init__(self, master, 'photo', cnf)
978
Guido van Rossum18468821994-06-20 07:49:28 +0000979class Listbox(Widget):
980 def __init__(self, master=None, cnf={}):
981 Widget.__init__(self, master, 'listbox', cnf)
982 def tk_listboxSingleSelect(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000983 if TkVersion >= 4.0:
984 self['selectmode'] = 'single'
985 else:
986 self.tk.call('tk_listboxSingleSelect', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def curselection(self):
988 return self.tk.splitlist(self.tk.call(
989 self._w, 'curselection'))
990 def delete(self, first, last=None):
991 self.tk.call(self._w, 'delete', first, last)
992 def get(self, index):
993 return self.tk.call(self._w, 'get', index)
994 def insert(self, index, *elements):
995 apply(self.tk.call,
996 (self._w, 'insert', index) + elements)
997 def nearest(self, y):
998 return self.tk.getint(self.tk.call(
999 self._w, 'nearest', y))
1000 def scan_mark(self, x, y):
1001 self.tk.call(self._w, 'scan', 'mark', x, y)
1002 def scan_dragto(self, x, y):
1003 self.tk.call(self._w, 'scan', 'dragto', x, y)
1004 def select_adjust(self, index):
1005 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001006 if TkVersion >= 4.0:
1007 def select_anchor(self, index):
1008 self.tk.call(self._w, 'selection', 'anchor', index)
1009 def select_clear(self, first, last=None):
1010 self.tk.call(self._w,
1011 'selection', 'clear', first, last)
1012 def select_includes(self, index):
1013 return self.tk.getboolean(self.tk.call(
1014 self._w, 'selection', 'includes', index))
1015 def select_set(self, first, last=None):
1016 self.tk.call(self._w, 'selection', 'set', first, last)
1017 else:
1018 def select_clear(self):
1019 self.tk.call(self._w, 'select', 'clear')
1020 def select_from(self, index):
1021 self.tk.call(self._w, 'select', 'from', index)
1022 def select_to(self, index):
1023 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001024 def size(self):
1025 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001026 def xview(self, *what):
1027 apply(self.tk.call, (self._w, 'xview')+what)
1028 def yview(self, *what):
1029 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001030
1031class Menu(Widget):
1032 def __init__(self, master=None, cnf={}):
1033 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001034 def tk_bindForTraversal(self):
1035 self.tk.call('tk_bindForTraversal', self._w)
1036 def tk_mbPost(self):
1037 self.tk.call('tk_mbPost', self._w)
1038 def tk_mbUnpost(self):
1039 self.tk.call('tk_mbUnpost')
1040 def tk_traverseToMenu(self, char):
1041 self.tk.call('tk_traverseToMenu', self._w, char)
1042 def tk_traverseWithinMenu(self, char):
1043 self.tk.call('tk_traverseWithinMenu', self._w, char)
1044 def tk_getMenuButtons(self):
1045 return self.tk.call('tk_getMenuButtons', self._w)
1046 def tk_nextMenu(self, count):
1047 self.tk.call('tk_nextMenu', count)
1048 def tk_nextMenuEntry(self, count):
1049 self.tk.call('tk_nextMenuEntry', count)
1050 def tk_invokeMenu(self):
1051 self.tk.call('tk_invokeMenu', self._w)
1052 def tk_firstMenu(self):
1053 self.tk.call('tk_firstMenu', self._w)
1054 def tk_mbButtonDown(self):
1055 self.tk.call('tk_mbButtonDown', self._w)
1056 def activate(self, index):
1057 self.tk.call(self._w, 'activate', index)
1058 def add(self, itemType, cnf={}):
1059 apply(self.tk.call, (self._w, 'add', itemType)
1060 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001061 def add_cascade(self, cnf={}):
1062 self.add('cascade', cnf)
1063 def add_checkbutton(self, cnf={}):
1064 self.add('checkbutton', cnf)
1065 def add_command(self, cnf={}):
1066 self.add('command', cnf)
1067 def add_radiobutton(self, cnf={}):
1068 self.add('radiobutton', cnf)
1069 def add_separator(self, cnf={}):
1070 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001071 def delete(self, index1, index2=None):
1072 self.tk.call(self._w, 'delete', index1, index2)
1073 def entryconfig(self, index, cnf={}):
1074 apply(self.tk.call, (self._w, 'entryconfigure', index)
1075 + self._options(cnf))
1076 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001077 i = self.tk.call(self._w, 'index', index)
1078 if i == 'none': return None
1079 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001080 def invoke(self, index):
1081 return self.tk.call(self._w, 'invoke', index)
1082 def post(self, x, y):
1083 self.tk.call(self._w, 'post', x, y)
1084 def unpost(self):
1085 self.tk.call(self._w, 'unpost')
1086 def yposition(self, index):
1087 return self.tk.getint(self.tk.call(
1088 self._w, 'yposition', index))
1089
1090class Menubutton(Widget):
1091 def __init__(self, master=None, cnf={}):
1092 Widget.__init__(self, master, 'menubutton', cnf)
1093
1094class Message(Widget):
1095 def __init__(self, master=None, cnf={}):
1096 Widget.__init__(self, master, 'message', cnf)
1097
1098class Radiobutton(Widget):
1099 def __init__(self, master=None, cnf={}):
1100 Widget.__init__(self, master, 'radiobutton', cnf)
1101 def deselect(self):
1102 self.tk.call(self._w, 'deselect')
1103 def flash(self):
1104 self.tk.call(self._w, 'flash')
1105 def invoke(self):
1106 self.tk.call(self._w, 'invoke')
1107 def select(self):
1108 self.tk.call(self._w, 'select')
1109
1110class Scale(Widget):
1111 def __init__(self, master=None, cnf={}):
1112 Widget.__init__(self, master, 'scale', cnf)
1113 def get(self):
1114 return self.tk.getint(self.tk.call(self._w, 'get'))
1115 def set(self, value):
1116 self.tk.call(self._w, 'set', value)
1117
1118class Scrollbar(Widget):
1119 def __init__(self, master=None, cnf={}):
1120 Widget.__init__(self, master, 'scrollbar', cnf)
1121 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001122 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001123 def set(self, *args):
1124 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001125
1126class Text(Widget):
1127 def __init__(self, master=None, cnf={}):
1128 Widget.__init__(self, master, 'text', cnf)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001129 self.bind('<Delete>', self.bspace)
1130 def bspace(self, *args):
1131 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001132 def tk_textSelectTo(self, index):
1133 self.tk.call('tk_textSelectTo', self._w, index)
1134 def tk_textBackspace(self):
1135 self.tk.call('tk_textBackspace', self._w)
1136 def tk_textIndexCloser(self, a, b, c):
1137 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1138 def tk_textResetAnchor(self, index):
1139 self.tk.call('tk_textResetAnchor', self._w, index)
1140 def compare(self, index1, op, index2):
1141 return self.tk.getboolean(self.tk.call(
1142 self._w, 'compare', index1, op, index2))
1143 def debug(self, boolean=None):
1144 return self.tk.getboolean(self.tk.call(
1145 self._w, 'debug', boolean))
1146 def delete(self, index1, index2=None):
1147 self.tk.call(self._w, 'delete', index1, index2)
1148 def get(self, index1, index2=None):
1149 return self.tk.call(self._w, 'get', index1, index2)
1150 def index(self, index):
1151 return self.tk.call(self._w, 'index', index)
1152 def insert(self, index, chars):
1153 self.tk.call(self._w, 'insert', index, chars)
1154 def mark_names(self):
1155 return self.tk.splitlist(self.tk.call(
1156 self._w, 'mark', 'names'))
1157 def mark_set(self, markName, index):
1158 self.tk.call(self._w, 'mark', 'set', markName, index)
1159 def mark_unset(self, markNames):
1160 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1161 def scan_mark(self, y):
1162 self.tk.call(self._w, 'scan', 'mark', y)
1163 def scan_dragto(self, y):
1164 self.tk.call(self._w, 'scan', 'dragto', y)
1165 def tag_add(self, tagName, index1, index2=None):
1166 self.tk.call(
1167 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001168 def tag_unbind(self, tagName, sequence):
1169 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001170 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001171 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001172 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001173 self.tk.call(self._w, 'tag', 'bind',
1174 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001175 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001176 def tag_config(self, tagName, cnf={}):
1177 apply(self.tk.call,
1178 (self._w, 'tag', 'configure', tagName)
1179 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001180 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001181 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001182 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001183 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001184 def tag_names(self, index=None):
1185 return self.tk.splitlist(
1186 self.tk.call(self._w, 'tag', 'names', index))
1187 def tag_nextrange(self, tagName, index1, index2=None):
1188 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001189 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001190 def tag_raise(self, tagName, aboveThis=None):
1191 self.tk.call(
1192 self._w, 'tag', 'raise', tagName, aboveThis)
1193 def tag_ranges(self, tagName):
1194 return self.tk.splitlist(self.tk.call(
1195 self._w, 'tag', 'ranges', tagName))
1196 def tag_remove(self, tagName, index1, index2=None):
1197 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001198 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001199 def yview(self, *what):
1200 apply(self.tk.call, (self._w, 'yview')+what)
1201 def yview_pickplace(self, *what):
1202 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001203
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001204######################################################################
1205# Extensions:
1206
1207class Studbutton(Button):
1208 def __init__(self, master=None, cnf={}):
1209 Widget.__init__(self, master, 'studbutton', cnf)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001210 self.bind('<Any-Enter>', self.tkButtonEnter)
1211 self.bind('<Any-Leave>', self.tkButtonLeave)
1212 self.bind('<1>', self.tkButtonDown)
1213 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001214
1215class Tributton(Button):
1216 def __init__(self, master=None, cnf={}):
1217 Widget.__init__(self, master, 'tributton', cnf)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001218 self.bind('<Any-Enter>', self.tkButtonEnter)
1219 self.bind('<Any-Leave>', self.tkButtonLeave)
1220 self.bind('<1>', self.tkButtonDown)
1221 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1222 self['fg'] = self['bg']
1223 self['activebackground'] = self['bg']