blob: 914c6034a55645b7c69476ad189daa3031df129b [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 Rossum35f67fb1995-08-04 03:50:29 +000015# Symbolic constants
16
17# Booleans
18NO=FALSE=OFF=0
19YES=TRUE=ON=1
20
21# -anchor
22N='n'
23S='s'
24W='w'
25E='e'
26NW='nw'
27SW='sw'
28NE='ne'
29SE='se'
30CENTER='center'
31
32# -fill
33NONE='none'
34X='x'
35Y='y'
36BOTH='both'
37
38# -side
39LEFT='left'
40TOP='top'
41RIGHT='right'
42BOTTOM='bottom'
43
44# -relief
45RAISED='raised'
46SUNKEN='sunken'
47FLAT='flat'
48RIDGE='ridge'
49GROOVE='groove'
50
51# -orient
52HORIZONTAL='horizontal'
53VERTICAL='vertical'
54
Guido van Rossuma22a70a1995-08-04 03:51:48 +000055# -tabs
56NUMERIC='numeric'
57
58# -wrap
59CHAR='char'
60WORD='word'
61
62# -align
63BASELINE='baseline'
64
65# Special tags, marks and insert positions
66SEL='sel'
67SEL_FIRST='sel.first'
68SEL_LAST='sel.last'
69END='end'
70INSERT='insert'
71CURRENT='current'
Guido van Rossumbf4d8f91995-09-01 20:35:37 +000072ANCHOR='anchor'
Guido van Rossuma22a70a1995-08-04 03:51:48 +000073
Guido van Rossum2dcf5291994-07-06 09:23:20 +000074def _flatten(tuple):
75 res = ()
76 for item in tuple:
77 if type(item) in (TupleType, ListType):
78 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000079 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000080 res = res + (item,)
81 return res
82
83def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000084 if type(cnfs) is DictionaryType:
85 _fixgeometry(cnfs)
86 return cnfs
87 elif type(cnfs) in (NoneType, StringType):
88
Guido van Rossum2dcf5291994-07-06 09:23:20 +000089 return cnfs
90 else:
91 cnf = {}
92 for c in _flatten(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000093 _fixgeometry(c)
Guido van Rossum2dcf5291994-07-06 09:23:20 +000094 for k, v in c.items():
95 cnf[k] = v
96 return cnf
97
Guido van Rossum761c5ab1995-07-14 15:29:10 +000098if TkVersion >= 4.0:
99 _fixg_warning = "Warning: patched up pre-Tk-4.0 geometry option\n"
100 def _fixgeometry(c):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000101 if c and c.has_key('geometry'):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000102 wh = _parsegeometry(c['geometry'])
103 if wh:
104 # Print warning message -- once
105 global _fixg_warning
106 if _fixg_warning:
107 import sys
108 sys.stderr.write(_fixg_warning)
109 _fixg_warning = None
110 w, h = wh
111 c['width'] = w
112 c['height'] = h
113 del c['geometry']
114 def _parsegeometry(s):
115 from string import splitfields
116 fields = splitfields(s, 'x')
117 if len(fields) == 2:
118 return tuple(fields)
119 # else: return None
120else:
121 def _fixgeometry(c): pass
122
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000123class Event:
124 pass
125
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000126_default_root = None
127
Guido van Rossum45853db1994-06-20 12:19:19 +0000128def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +0000129 pass
130
Guido van Rossum97aeca11994-07-07 13:12:12 +0000131def _exit(code='0'):
132 import sys
133 sys.exit(getint(code))
134
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000135_varnum = 0
136class Variable:
137 def __init__(self, master=None):
138 global _default_root
139 global _varnum
140 if master:
141 self._tk = master.tk
142 else:
143 self._tk = _default_root.tk
144 self._name = 'PY_VAR' + `_varnum`
145 _varnum = _varnum + 1
146 def __del__(self):
147 self._tk.unsetvar(self._name)
148 def __str__(self):
149 return self._name
150 def __call__(self, value=None):
151 if value == None:
152 return self.get()
153 else:
154 self.set(value)
155 def set(self, value):
156 return self._tk.setvar(self._name, value)
157
158class StringVar(Variable):
159 def __init__(self, master=None):
160 Variable.__init__(self, master)
161 def get(self):
162 return self._tk.getvar(self._name)
163
164class IntVar(Variable):
165 def __init__(self, master=None):
166 Variable.__init__(self, master)
167 def get(self):
168 return self._tk.getint(self._tk.getvar(self._name))
169
170class DoubleVar(Variable):
171 def __init__(self, master=None):
172 Variable.__init__(self, master)
173 def get(self):
174 return self._tk.getdouble(self._tk.getvar(self._name))
175
176class BooleanVar(Variable):
177 def __init__(self, master=None):
178 Variable.__init__(self, master)
179 def get(self):
180 return self._tk.getboolean(self._tk.getvar(self._name))
181
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000182def mainloop(n=0):
183 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000184
185def getint(s):
186 return _default_root.tk.getint(s)
187
188def getdouble(s):
189 return _default_root.tk.getdouble(s)
190
191def getboolean(s):
192 return _default_root.tk.getboolean(s)
193
Guido van Rossum18468821994-06-20 07:49:28 +0000194class Misc:
195 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000196 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000197 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000198 def tk_menuBar(self, *args):
199 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000200 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000201 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000202 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000203 def wait_window(self, window=None):
204 if window == None:
205 window = self
206 self.tk.call('tkwait', 'window', window._w)
207 def wait_visibility(self, window=None):
208 if window == None:
209 window = self
210 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000211 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000212 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000213 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000214 return self.tk.getvar(name)
215 def getint(self, s):
216 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000217 def getdouble(self, s):
218 return self.tk.getdouble(s)
219 def getboolean(self, s):
220 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000221 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000222 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000223 focus = focus_set # XXX b/w compat?
224 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000225 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000226 def focus_default_none(self):
227 self.tk.call('focus', 'default', 'none')
228 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000229 def focus_none(self):
230 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000231 def focus_get(self):
232 name = self.tk.call('focus')
233 if name == 'none': return None
234 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000235 def after(self, ms, func=None, *args):
236 if not func:
237 self.tk.call('after', ms)
238 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000239 # XXX Disgusting hack to clean up after calling func
240 tmp = []
241 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
242 try:
243 apply(func, args)
244 finally:
245 tk.deletecommand(tmp[0])
246 name = self._register(callit)
247 tmp.append(name)
248 self.tk.call('after', ms, name)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000249 def bell(self, displayof=None):
250 if displayof:
251 self.tk.call('bell', '-displayof', displayof)
252 else:
253 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000254 # XXX grab current w/o window argument
255 def grab_current(self):
256 name = self.tk.call('grab', 'current', self._w)
257 if not name: return None
258 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000259 def grab_release(self):
260 self.tk.call('grab', 'release', self._w)
261 def grab_set(self):
262 self.tk.call('grab', 'set', self._w)
263 def grab_set_global(self):
264 self.tk.call('grab', 'set', '-global', self._w)
265 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000266 status = self.tk.call('grab', 'status', self._w)
267 if status == 'none': status = None
268 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000269 def lower(self, belowThis=None):
270 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000271 def option_add(self, pattern, value, priority = None):
272 self.tk.call('option', 'add', pattern, priority)
273 def option_clear(self):
274 self.tk.call('option', 'clear')
275 def option_get(self, name, className):
276 return self.tk.call('option', 'get', self._w, name, className)
277 def option_readfile(self, fileName, priority = None):
278 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000279 def selection_clear(self):
280 self.tk.call('selection', 'clear', self._w)
281 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000282 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000283 def selection_handle(self, func, type=None, format=None):
284 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000285 self.tk.call('selection', 'handle', self._w,
286 name, type, format)
287 def selection_own(self, func=None):
288 name = self._register(func)
289 self.tk.call('selection', 'own', self._w, name)
290 def selection_own_get(self):
291 return self._nametowidget(self.tk.call('selection', 'own'))
292 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000293 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000294 def lower(self, belowThis=None):
295 self.tk.call('lift', self._w, belowThis)
296 def tkraise(self, aboveThis=None):
297 self.tk.call('raise', self._w, aboveThis)
298 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000299 def colormodel(self, value=None):
300 return self.tk.call('tk', 'colormodel', self._w, value)
301 def winfo_atom(self, name):
302 return self.tk.getint(self.tk.call('winfo', 'atom', name))
303 def winfo_atomname(self, id):
304 return self.tk.call('winfo', 'atomname', id)
305 def winfo_cells(self):
306 return self.tk.getint(
307 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000308 def winfo_children(self):
309 return map(self._nametowidget,
310 self.tk.splitlist(self.tk.call(
311 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000312 def winfo_class(self):
313 return self.tk.call('winfo', 'class', self._w)
314 def winfo_containing(self, rootX, rootY):
315 return self.tk.call('winfo', 'containing', rootx, rootY)
316 def winfo_depth(self):
317 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
318 def winfo_exists(self):
319 return self.tk.getint(
320 self.tk.call('winfo', 'exists', self._w))
321 def winfo_fpixels(self, number):
322 return self.tk.getdouble(self.tk.call(
323 'winfo', 'fpixels', self._w, number))
324 def winfo_geometry(self):
325 return self.tk.call('winfo', 'geometry', self._w)
326 def winfo_height(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'height', self._w))
329 def winfo_id(self):
330 return self.tk.getint(
331 self.tk.call('winfo', 'id', self._w))
332 def winfo_interps(self):
333 return self.tk.splitlist(
334 self.tk.call('winfo', 'interps'))
335 def winfo_ismapped(self):
336 return self.tk.getint(
337 self.tk.call('winfo', 'ismapped', self._w))
338 def winfo_name(self):
339 return self.tk.call('winfo', 'name', self._w)
340 def winfo_parent(self):
341 return self.tk.call('winfo', 'parent', self._w)
342 def winfo_pathname(self, id):
343 return self.tk.call('winfo', 'pathname', id)
344 def winfo_pixels(self, number):
345 return self.tk.getint(
346 self.tk.call('winfo', 'pixels', self._w, number))
347 def winfo_reqheight(self):
348 return self.tk.getint(
349 self.tk.call('winfo', 'reqheight', self._w))
350 def winfo_reqwidth(self):
351 return self.tk.getint(
352 self.tk.call('winfo', 'reqwidth', self._w))
353 def winfo_rgb(self, color):
354 return self._getints(
355 self.tk.call('winfo', 'rgb', self._w, color))
356 def winfo_rootx(self):
357 return self.tk.getint(
358 self.tk.call('winfo', 'rootx', self._w))
359 def winfo_rooty(self):
360 return self.tk.getint(
361 self.tk.call('winfo', 'rooty', self._w))
362 def winfo_screen(self):
363 return self.tk.call('winfo', 'screen', self._w)
364 def winfo_screencells(self):
365 return self.tk.getint(
366 self.tk.call('winfo', 'screencells', self._w))
367 def winfo_screendepth(self):
368 return self.tk.getint(
369 self.tk.call('winfo', 'screendepth', self._w))
370 def winfo_screenheight(self):
371 return self.tk.getint(
372 self.tk.call('winfo', 'screenheight', self._w))
373 def winfo_screenmmheight(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'screenmmheight', self._w))
376 def winfo_screenmmwidth(self):
377 return self.tk.getint(
378 self.tk.call('winfo', 'screenmmwidth', self._w))
379 def winfo_screenvisual(self):
380 return self.tk.call('winfo', 'screenvisual', self._w)
381 def winfo_screenwidth(self):
382 return self.tk.getint(
383 self.tk.call('winfo', 'screenwidth', self._w))
384 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000385 return self._nametowidget(self.tk.call(
386 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000387 def winfo_visual(self):
388 return self.tk.call('winfo', 'visual', self._w)
389 def winfo_vrootheight(self):
390 return self.tk.getint(
391 self.tk.call('winfo', 'vrootheight', self._w))
392 def winfo_vrootwidth(self):
393 return self.tk.getint(
394 self.tk.call('winfo', 'vrootwidth', self._w))
395 def winfo_vrootx(self):
396 return self.tk.getint(
397 self.tk.call('winfo', 'vrootx', self._w))
398 def winfo_vrooty(self):
399 return self.tk.getint(
400 self.tk.call('winfo', 'vrooty', self._w))
401 def winfo_width(self):
402 return self.tk.getint(
403 self.tk.call('winfo', 'width', self._w))
404 def winfo_x(self):
405 return self.tk.getint(
406 self.tk.call('winfo', 'x', self._w))
407 def winfo_y(self):
408 return self.tk.getint(
409 self.tk.call('winfo', 'y', self._w))
410 def update(self):
411 self.tk.call('update')
412 def update_idletasks(self):
413 self.tk.call('update', 'idletasks')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000414 def bind(self, sequence, func=None, add=''):
415 if add: add = '+'
416 if func:
417 name = self._register(func, self._substitute)
418 self.tk.call('bind', self._w, sequence,
419 (add + name,) + self._subst_format)
420 else:
421 return self.tk.call('bind', self._w, sequence)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000422 def unbind(self, sequence):
423 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000424 def bind_all(self, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000425 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000426 if func:
427 name = self._register(func, self._substitute)
428 self.tk.call('bind', 'all' , sequence,
429 (add + name,) + self._subst_format)
430 else:
431 return self.tk.call('bind', 'all', sequence)
432 def unbind_all(self, sequence):
433 self.tk.call('bind', 'all' , sequence, '')
434 def bind_class(self, className, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000435 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000436 if func:
437 name = self._register(func, self._substitute)
438 self.tk.call('bind', className , sequence,
439 (add + name,) + self._subst_format)
440 else:
441 return self.tk.call('bind', className, sequence)
442 def unbind_class(self, className, sequence):
443 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000444 def mainloop(self, n=0):
445 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000446 def quit(self):
447 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000448 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000449 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000450 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
451 def _getdoubles(self, string):
452 if not string: return None
453 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000454 def _getboolean(self, string):
455 if string:
456 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000457 def _options(self, cnf, kw = None):
458 if kw:
459 cnf = _cnfmerge((cnf, kw))
460 else:
461 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000462 res = ()
463 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000464 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000465 v = self._register(v)
466 res = res + ('-'+k, v)
467 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000468 def _nametowidget(self, name):
469 w = self
470 if name[0] == '.':
471 w = w._root()
472 name = name[1:]
473 from string import find
474 while name:
475 i = find(name, '.')
476 if i >= 0:
477 name, tail = name[:i], name[i+1:]
478 else:
479 tail = ''
480 w = w.children[name]
481 name = tail
482 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000483 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000484 f = _CallSafely(func, subst).__call__
485 name = `id(f)`
486 if hasattr(func, 'im_func'):
487 func = func.im_func
488 if hasattr(func, 'func_name') and \
489 type(func.func_name) == type(''):
490 name = name + func.func_name
491 self.tk.createcommand(name, f)
492 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000493 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000494 def _root(self):
495 w = self
496 while w.master: w = w.master
497 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000498 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000499 '%s', '%t', '%w', '%x', '%y',
500 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
501 def _substitute(self, *args):
502 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000503 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000504 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
505 # Missing: (a, c, d, m, o, v, B, R)
506 e = Event()
507 e.serial = tk.getint(nsign)
508 e.num = tk.getint(b)
509 try: e.focus = tk.getboolean(f)
510 except TclError: pass
511 e.height = tk.getint(h)
512 e.keycode = tk.getint(k)
513 e.state = tk.getint(s)
514 e.time = tk.getint(t)
515 e.width = tk.getint(w)
516 e.x = tk.getint(x)
517 e.y = tk.getint(y)
518 e.char = A
519 try: e.send_event = tk.getboolean(E)
520 except TclError: pass
521 e.keysym = K
522 e.keysym_num = tk.getint(N)
523 e.type = T
524 e.widget = self._nametowidget(W)
525 e.x_root = tk.getint(X)
526 e.y_root = tk.getint(Y)
527 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000528
529class _CallSafely:
530 def __init__(self, func, subst=None):
531 self.func = func
532 self.subst = subst
533 def __call__(self, *args):
534 if self.subst:
535 args = self.apply_func(self.subst, args)
536 args = self.apply_func(self.func, args)
537 def apply_func(self, func, args):
538 import sys
539 try:
540 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000541 except SystemExit, msg:
542 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000543 except:
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000544 import traceback
545 print "Exception in Tkinter callback"
546 traceback.print_exc()
Guido van Rossum18468821994-06-20 07:49:28 +0000547
548class Wm:
549 def aspect(self,
550 minNumer=None, minDenom=None,
551 maxNumer=None, maxDenom=None):
552 return self._getints(
553 self.tk.call('wm', 'aspect', self._w,
554 minNumer, minDenom,
555 maxNumer, maxDenom))
556 def client(self, name=None):
557 return self.tk.call('wm', 'client', self._w, name)
558 def command(self, value=None):
559 return self.tk.call('wm', 'command', self._w, value)
560 def deiconify(self):
561 return self.tk.call('wm', 'deiconify', self._w)
562 def focusmodel(self, model=None):
563 return self.tk.call('wm', 'focusmodel', self._w, model)
564 def frame(self):
565 return self.tk.call('wm', 'frame', self._w)
566 def geometry(self, newGeometry=None):
567 return self.tk.call('wm', 'geometry', self._w, newGeometry)
568 def grid(self,
569 baseWidht=None, baseHeight=None,
570 widthInc=None, heightInc=None):
571 return self._getints(self.tk.call(
572 'wm', 'grid', self._w,
573 baseWidht, baseHeight, widthInc, heightInc))
574 def group(self, pathName=None):
575 return self.tk.call('wm', 'group', self._w, pathName)
576 def iconbitmap(self, bitmap=None):
577 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
578 def iconify(self):
579 return self.tk.call('wm', 'iconify', self._w)
580 def iconmask(self, bitmap=None):
581 return self.tk.call('wm', 'iconmask', self._w, bitmap)
582 def iconname(self, newName=None):
583 return self.tk.call('wm', 'iconname', self._w, newName)
584 def iconposition(self, x=None, y=None):
585 return self._getints(self.tk.call(
586 'wm', 'iconposition', self._w, x, y))
587 def iconwindow(self, pathName=None):
588 return self.tk.call('wm', 'iconwindow', self._w, pathName)
589 def maxsize(self, width=None, height=None):
590 return self._getints(self.tk.call(
591 'wm', 'maxsize', self._w, width, height))
592 def minsize(self, width=None, height=None):
593 return self._getints(self.tk.call(
594 'wm', 'minsize', self._w, width, height))
595 def overrideredirect(self, boolean=None):
596 return self._getboolean(self.tk.call(
597 'wm', 'overrideredirect', self._w, boolean))
598 def positionfrom(self, who=None):
599 return self.tk.call('wm', 'positionfrom', self._w, who)
600 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000601 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000602 command = self._register(func)
603 else:
604 command = func
605 return self.tk.call(
606 'wm', 'protocol', self._w, name, command)
607 def sizefrom(self, who=None):
608 return self.tk.call('wm', 'sizefrom', self._w, who)
609 def state(self):
610 return self.tk.call('wm', 'state', self._w)
611 def title(self, string=None):
612 return self.tk.call('wm', 'title', self._w, string)
613 def transient(self, master=None):
614 return self.tk.call('wm', 'transient', self._w, master)
615 def withdraw(self):
616 return self.tk.call('wm', 'withdraw', self._w)
617
618class Tk(Misc, Wm):
619 _w = '.'
620 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000621 self.master = None
622 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000623 if baseName is None:
624 import sys, os
625 baseName = os.path.basename(sys.argv[0])
626 if baseName[-3:] == '.py': baseName = baseName[:-3]
627 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000628 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000629 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000630 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000631 def destroy(self):
632 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000633 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000634 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000635 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000636 def readprofile(self, baseName, className):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000637 ##print __import__
638 import os, pdb
Guido van Rossum27b77a41994-07-12 15:52:32 +0000639 if os.environ.has_key('HOME'): home = os.environ['HOME']
640 else: home = os.curdir
641 class_tcl = os.path.join(home, '.%s.tcl' % className)
642 class_py = os.path.join(home, '.%s.py' % className)
643 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
644 base_py = os.path.join(home, '.%s.py' % baseName)
645 dir = {'self': self}
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000646 ##pdb.run('from Tkinter import *', dir)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000647 exec 'from Tkinter import *' in dir
648 if os.path.isfile(class_tcl):
649 print 'source', `class_tcl`
650 self.tk.call('source', class_tcl)
651 if os.path.isfile(class_py):
652 print 'execfile', `class_py`
653 execfile(class_py, dir)
654 if os.path.isfile(base_tcl):
655 print 'source', `base_tcl`
656 self.tk.call('source', base_tcl)
657 if os.path.isfile(base_py):
658 print 'execfile', `base_py`
659 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000660
661class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000662 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000663 apply(self.tk.call,
664 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000665 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000666 pack = config
667 def __setitem__(self, key, value):
668 Pack.config({key: value})
669 def forget(self):
670 self.tk.call('pack', 'forget', self._w)
671 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000672 words = self.tk.splitlist(
673 self.tk.call('pack', 'newinfo', self._w))
674 dict = {}
675 for i in range(0, len(words), 2):
676 key = words[i][1:]
677 value = words[i+1]
678 if value[0] == '.':
679 value = self._nametowidget(value)
680 dict[key] = value
681 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000682 info = newinfo
Guido van Rossum5505d561994-12-30 17:16:35 +0000683 _noarg_ = ['_noarg_']
684 def propagate(self, flag=_noarg_):
685 if boolean is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000686 return self._getboolean(self.tk.call(
687 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000688 else:
689 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum18468821994-06-20 07:49:28 +0000690 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000691 return map(self._nametowidget,
692 self.tk.splitlist(
693 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000694
695class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000696 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000697 apply(self.tk.call,
698 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000699 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000700 place = config
701 def __setitem__(self, key, value):
702 Place.config({key: value})
703 def forget(self):
704 self.tk.call('place', 'forget', self._w)
705 def info(self):
706 return self.tk.call('place', 'info', self._w)
707 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000708 return map(self._nametowidget,
709 self.tk.splitlist(
710 self.tk.call(
711 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000712
Guido van Rossum18468821994-06-20 07:49:28 +0000713class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000714 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000715 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000716 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000717 if not _default_root:
718 _default_root = Tk()
719 master = _default_root
720 if not _default_root:
721 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000722 self.master = master
723 self.tk = master.tk
724 if cnf.has_key('name'):
725 name = cnf['name']
726 del cnf['name']
727 else:
728 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000729 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000730 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000731 self._w = '.' + name
732 else:
733 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000734 self.children = {}
735 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000736 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000737 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000738 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
739 if kw:
740 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000741 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000742 Widget._setup(self, master, cnf)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +0000743 apply(self.tk.call, (widgetName, self._w)+extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000744 if cnf:
745 Widget.config(self, cnf)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000746 def config(self, cnf=None, **kw):
747 # XXX ought to generalize this so tag_config etc. can use it
748 if kw:
749 cnf = _cnfmerge((cnf, kw))
750 else:
751 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000752 if cnf is None:
753 cnf = {}
754 for x in self.tk.split(
755 self.tk.call(self._w, 'configure')):
756 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
757 return cnf
758 if type(cnf) == StringType:
759 x = self.tk.split(self.tk.call(
760 self._w, 'configure', '-'+cnf))
761 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000762 for k in cnf.keys():
763 if type(k) == ClassType:
764 k.config(self, cnf[k])
765 del cnf[k]
766 apply(self.tk.call, (self._w, 'configure')
767 + self._options(cnf))
768 def __getitem__(self, key):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000769 if TkVersion >= 4.0:
770 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000771 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000772 self._w, 'configure', '-' + key))
773 return v[4]
774 def __setitem__(self, key, value):
775 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000776 def keys(self):
777 return map(lambda x: x[0][1:],
778 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000779 def __str__(self):
780 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000781 def destroy(self):
782 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000783 if self.master.children.has_key(self._name):
784 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000785 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000786 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000787 return apply(self.tk.call, (self._w, name) + args)
788 def unbind_class(self, seq):
789 Misc.unbind_class(self, self.widgetName, seq)
Guido van Rossum18468821994-06-20 07:49:28 +0000790
791class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000792 def __init__(self, master=None, cnf={}, **kw):
793 if kw:
794 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000795 extra = ()
796 if cnf.has_key('screen'):
797 extra = ('-screen', cnf['screen'])
798 del cnf['screen']
799 if cnf.has_key('class'):
800 extra = extra + ('-class', cnf['class'])
801 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000802 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000803 root = self._root()
804 self.iconname(root.iconname())
805 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000806
807class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000808 def __init__(self, master=None, cnf={}, **kw):
809 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000810 def tkButtonEnter(self, *dummy):
811 self.tk.call('tkButtonEnter', self._w)
812 def tkButtonLeave(self, *dummy):
813 self.tk.call('tkButtonLeave', self._w)
814 def tkButtonDown(self, *dummy):
815 self.tk.call('tkButtonDown', self._w)
816 def tkButtonUp(self, *dummy):
817 self.tk.call('tkButtonUp', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000818 def flash(self):
819 self.tk.call(self._w, 'flash')
820 def invoke(self):
821 self.tk.call(self._w, 'invoke')
822
823# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000824# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000825def AtEnd():
826 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000827def AtInsert(*args):
828 s = 'insert'
829 for a in args:
830 if a: s = s + (' ' + a)
831 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000832def AtSelFirst():
833 return 'sel.first'
834def AtSelLast():
835 return 'sel.last'
836def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000837 if y is None:
838 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000839 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000840 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000841
842class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000843 def __init__(self, master=None, cnf={}, **kw):
844 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000845 def addtag(self, *args):
846 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000847 def addtag_above(self, tagOrId):
848 self.addtag('above', tagOrId)
849 def addtag_all(self):
850 self.addtag('all')
851 def addtag_below(self, tagOrId):
852 self.addtag('below', tagOrId)
853 def addtag_closest(self, x, y, halo=None, start=None):
854 self.addtag('closest', x, y, halo, start)
855 def addtag_enclosed(self, x1, y1, x2, y2):
856 self.addtag('enclosed', x1, y1, x2, y2)
857 def addtag_overlapping(self, x1, y1, x2, y2):
858 self.addtag('overlapping', x1, y1, x2, y2)
859 def addtag_withtag(self, tagOrId):
860 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000861 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000862 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000863 def tag_unbind(self, tagOrId, sequence):
864 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
865 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000866 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000867 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000868 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000869 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000870 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000871 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000872 self._w, 'canvasx', screenx, gridspacing))
873 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000874 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000875 self._w, 'canvasy', screeny, gridspacing))
876 def coords(self, *args):
877 return self._do('coords', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000878 def _create(self, itemType, args, kw): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000879 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000880 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000881 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000882 args = args[:-1]
883 else:
884 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000885 return self.tk.getint(apply(
886 self.tk.call,
887 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000888 + args + self._options(cnf, kw)))
889 def create_arc(self, *args, **kw):
890 return self._create('arc', args, kw)
891 def create_bitmap(self, *args, **kw):
892 return self._create('bitmap', args, kw)
893 def create_image(self, *args, **kw):
894 return self._create('image', args, kw)
895 def create_line(self, *args, **kw):
896 return self._create('line', args, kw)
897 def create_oval(self, *args, **kw):
898 return self._create('oval', args, kw)
899 def create_polygon(self, *args, **kw):
900 return self._create('polygon', args, kw)
901 def create_rectangle(self, *args, **kw):
902 return self._create('rectangle', args, kw)
903 def create_text(self, *args, **kw):
904 return self._create('text', args, kw)
905 def create_window(self, *args, **kw):
906 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000907 def dchars(self, *args):
908 self._do('dchars', args)
909 def delete(self, *args):
910 self._do('delete', args)
911 def dtag(self, *args):
912 self._do('dtag', args)
913 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000914 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000915 def find_above(self, tagOrId):
916 return self.find('above', tagOrId)
917 def find_all(self):
918 return self.find('all')
919 def find_below(self, tagOrId):
920 return self.find('below', tagOrId)
921 def find_closest(self, x, y, halo=None, start=None):
922 return self.find('closest', x, y, halo, start)
923 def find_enclosed(self, x1, y1, x2, y2):
924 return self.find('enclosed', x1, y1, x2, y2)
925 def find_overlapping(self, x1, y1, x2, y2):
926 return self.find('overlapping', x1, y1, x2, y2)
927 def find_withtag(self, tagOrId):
928 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000929 def focus(self, *args):
930 return self._do('focus', args)
931 def gettags(self, *args):
932 return self.tk.splitlist(self._do('gettags', args))
933 def icursor(self, *args):
934 self._do('icursor', args)
935 def index(self, *args):
936 return self.tk.getint(self._do('index', args))
937 def insert(self, *args):
938 self._do('insert', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000939 def itemconfig(self, tagOrId, cnf=None, **kw):
940 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000941 cnf = {}
942 for x in self.tk.split(
943 self._do('itemconfigure', (tagOrId))):
944 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
945 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000946 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000947 x = self.tk.split(self._do('itemconfigure',
948 (tagOrId, '-'+cnf,)))
949 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000950 self._do('itemconfigure', (tagOrId,)
951 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000952 def lower(self, *args):
953 self._do('lower', args)
954 def move(self, *args):
955 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000956 def postscript(self, cnf={}, **kw):
957 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000958 def tkraise(self, *args):
959 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000960 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000961 def scale(self, *args):
962 self._do('scale', args)
963 def scan_mark(self, x, y):
964 self.tk.call(self._w, 'scan', 'mark', x, y)
965 def scan_dragto(self, x, y):
966 self.tk.call(self._w, 'scan', 'dragto', x, y)
967 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000968 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000969 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000970 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000971 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000972 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000973 def select_item(self):
974 self.tk.call(self._w, 'select', 'item')
975 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000976 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000977 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000978 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000979 def xview(self, *args):
980 apply(self.tk.call, (self._w, 'xview')+args)
981 def yview(self, *args):
982 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +0000983
984class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000985 def __init__(self, master=None, cnf={}, **kw):
986 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def deselect(self):
988 self.tk.call(self._w, 'deselect')
989 def flash(self):
990 self.tk.call(self._w, 'flash')
991 def invoke(self):
992 self.tk.call(self._w, 'invoke')
993 def select(self):
994 self.tk.call(self._w, 'select')
995 def toggle(self):
996 self.tk.call(self._w, 'toggle')
997
998class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000999 def __init__(self, master=None, cnf={}, **kw):
1000 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001001 def tk_entryBackspace(self):
1002 self.tk.call('tk_entryBackspace', self._w)
1003 def tk_entryBackword(self):
1004 self.tk.call('tk_entryBackword', self._w)
1005 def tk_entrySeeCaret(self):
1006 self.tk.call('tk_entrySeeCaret', self._w)
1007 def delete(self, first, last=None):
1008 self.tk.call(self._w, 'delete', first, last)
1009 def get(self):
1010 return self.tk.call(self._w, 'get')
1011 def icursor(self, index):
1012 self.tk.call(self._w, 'icursor', index)
1013 def index(self, index):
1014 return self.tk.getint(self.tk.call(
1015 self._w, 'index', index))
1016 def insert(self, index, string):
1017 self.tk.call(self._w, 'insert', index, string)
1018 def scan_mark(self, x):
1019 self.tk.call(self._w, 'scan', 'mark', x)
1020 def scan_dragto(self, x):
1021 self.tk.call(self._w, 'scan', 'dragto', x)
1022 def select_adjust(self, index):
1023 self.tk.call(self._w, 'select', 'adjust', index)
1024 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001025 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +00001026 def select_from(self, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001027 self.tk.call(self._w, 'select', 'set', index)
Guido van Rossum1d59df21995-08-11 14:21:06 +00001028 def select_present(self):
1029 return self.tk.getboolean(
1030 self.tk.call(self._w, 'select', 'present'))
1031 def select_range(self, start, end):
1032 self.tk.call(self._w, 'select', 'range', start, end)
Guido van Rossum18468821994-06-20 07:49:28 +00001033 def select_to(self, index):
1034 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001035 def view(self, index):
1036 self.tk.call(self._w, 'view', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001037
1038class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001039 def __init__(self, master=None, cnf={}, **kw):
1040 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001041 extra = ()
1042 if cnf.has_key('class'):
1043 extra = ('-class', cnf['class'])
1044 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001045 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001046 def tk_menuBar(self, *args):
1047 apply(self.tk.call, ('tk_menuBar', self._w) + args)
1048
1049class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001050 def __init__(self, master=None, cnf={}, **kw):
1051 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001052
Guido van Rossum18468821994-06-20 07:49:28 +00001053class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001054 def __init__(self, master=None, cnf={}, **kw):
1055 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001056 def tk_listboxSingleSelect(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001057 if TkVersion >= 4.0:
1058 self['selectmode'] = 'single'
1059 else:
1060 self.tk.call('tk_listboxSingleSelect', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001061 def curselection(self):
1062 return self.tk.splitlist(self.tk.call(
1063 self._w, 'curselection'))
1064 def delete(self, first, last=None):
1065 self.tk.call(self._w, 'delete', first, last)
1066 def get(self, index):
1067 return self.tk.call(self._w, 'get', index)
1068 def insert(self, index, *elements):
1069 apply(self.tk.call,
1070 (self._w, 'insert', index) + elements)
1071 def nearest(self, y):
1072 return self.tk.getint(self.tk.call(
1073 self._w, 'nearest', y))
1074 def scan_mark(self, x, y):
1075 self.tk.call(self._w, 'scan', 'mark', x, y)
1076 def scan_dragto(self, x, y):
1077 self.tk.call(self._w, 'scan', 'dragto', x, y)
1078 def select_adjust(self, index):
1079 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001080 if TkVersion >= 4.0:
1081 def select_anchor(self, index):
1082 self.tk.call(self._w, 'selection', 'anchor', index)
1083 def select_clear(self, first, last=None):
1084 self.tk.call(self._w,
1085 'selection', 'clear', first, last)
1086 def select_includes(self, index):
1087 return self.tk.getboolean(self.tk.call(
1088 self._w, 'selection', 'includes', index))
1089 def select_set(self, first, last=None):
1090 self.tk.call(self._w, 'selection', 'set', first, last)
1091 else:
1092 def select_clear(self):
1093 self.tk.call(self._w, 'select', 'clear')
1094 def select_from(self, index):
1095 self.tk.call(self._w, 'select', 'from', index)
1096 def select_to(self, index):
1097 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001098 def size(self):
1099 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001100 def xview(self, *what):
1101 apply(self.tk.call, (self._w, 'xview')+what)
1102 def yview(self, *what):
1103 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001104
1105class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001106 def __init__(self, master=None, cnf={}, **kw):
1107 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001108 def tk_bindForTraversal(self):
1109 self.tk.call('tk_bindForTraversal', self._w)
1110 def tk_mbPost(self):
1111 self.tk.call('tk_mbPost', self._w)
1112 def tk_mbUnpost(self):
1113 self.tk.call('tk_mbUnpost')
1114 def tk_traverseToMenu(self, char):
1115 self.tk.call('tk_traverseToMenu', self._w, char)
1116 def tk_traverseWithinMenu(self, char):
1117 self.tk.call('tk_traverseWithinMenu', self._w, char)
1118 def tk_getMenuButtons(self):
1119 return self.tk.call('tk_getMenuButtons', self._w)
1120 def tk_nextMenu(self, count):
1121 self.tk.call('tk_nextMenu', count)
1122 def tk_nextMenuEntry(self, count):
1123 self.tk.call('tk_nextMenuEntry', count)
1124 def tk_invokeMenu(self):
1125 self.tk.call('tk_invokeMenu', self._w)
1126 def tk_firstMenu(self):
1127 self.tk.call('tk_firstMenu', self._w)
1128 def tk_mbButtonDown(self):
1129 self.tk.call('tk_mbButtonDown', self._w)
1130 def activate(self, index):
1131 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001132 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001133 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001134 + self._options(cnf, kw))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001135 def add_cascade(self, cnf={}):
1136 self.add('cascade', cnf)
1137 def add_checkbutton(self, cnf={}):
1138 self.add('checkbutton', cnf)
1139 def add_command(self, cnf={}):
1140 self.add('command', cnf)
1141 def add_radiobutton(self, cnf={}):
1142 self.add('radiobutton', cnf)
1143 def add_separator(self, cnf={}):
1144 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001145 def delete(self, index1, index2=None):
1146 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001147 def entryconfig(self, index, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001148 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001149 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001151 i = self.tk.call(self._w, 'index', index)
1152 if i == 'none': return None
1153 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001154 def invoke(self, index):
1155 return self.tk.call(self._w, 'invoke', index)
1156 def post(self, x, y):
1157 self.tk.call(self._w, 'post', x, y)
1158 def unpost(self):
1159 self.tk.call(self._w, 'unpost')
1160 def yposition(self, index):
1161 return self.tk.getint(self.tk.call(
1162 self._w, 'yposition', index))
1163
1164class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001165 def __init__(self, master=None, cnf={}, **kw):
1166 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001167
1168class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001169 def __init__(self, master=None, cnf={}, **kw):
1170 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001171
1172class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001173 def __init__(self, master=None, cnf={}, **kw):
1174 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001175 def deselect(self):
1176 self.tk.call(self._w, 'deselect')
1177 def flash(self):
1178 self.tk.call(self._w, 'flash')
1179 def invoke(self):
1180 self.tk.call(self._w, 'invoke')
1181 def select(self):
1182 self.tk.call(self._w, 'select')
1183
1184class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001185 def __init__(self, master=None, cnf={}, **kw):
1186 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001187 def get(self):
1188 return self.tk.getint(self.tk.call(self._w, 'get'))
1189 def set(self, value):
1190 self.tk.call(self._w, 'set', value)
1191
1192class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001193 def __init__(self, master=None, cnf={}, **kw):
1194 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001195 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001196 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001197 def set(self, *args):
1198 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001199
1200class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001201 def __init__(self, master=None, cnf={}, **kw):
1202 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001203 self.bind('<Delete>', self.bspace)
1204 def bspace(self, *args):
1205 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001206 def tk_textSelectTo(self, index):
1207 self.tk.call('tk_textSelectTo', self._w, index)
1208 def tk_textBackspace(self):
1209 self.tk.call('tk_textBackspace', self._w)
1210 def tk_textIndexCloser(self, a, b, c):
1211 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1212 def tk_textResetAnchor(self, index):
1213 self.tk.call('tk_textResetAnchor', self._w, index)
1214 def compare(self, index1, op, index2):
1215 return self.tk.getboolean(self.tk.call(
1216 self._w, 'compare', index1, op, index2))
1217 def debug(self, boolean=None):
1218 return self.tk.getboolean(self.tk.call(
1219 self._w, 'debug', boolean))
1220 def delete(self, index1, index2=None):
1221 self.tk.call(self._w, 'delete', index1, index2)
1222 def get(self, index1, index2=None):
1223 return self.tk.call(self._w, 'get', index1, index2)
1224 def index(self, index):
1225 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001226 def insert(self, index, chars, *args):
1227 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001228 def mark_names(self):
1229 return self.tk.splitlist(self.tk.call(
1230 self._w, 'mark', 'names'))
1231 def mark_set(self, markName, index):
1232 self.tk.call(self._w, 'mark', 'set', markName, index)
1233 def mark_unset(self, markNames):
1234 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1235 def scan_mark(self, y):
1236 self.tk.call(self._w, 'scan', 'mark', y)
1237 def scan_dragto(self, y):
1238 self.tk.call(self._w, 'scan', 'dragto', y)
1239 def tag_add(self, tagName, index1, index2=None):
1240 self.tk.call(
1241 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001242 def tag_unbind(self, tagName, sequence):
1243 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001244 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001245 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001246 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001247 self.tk.call(self._w, 'tag', 'bind',
1248 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001249 (add + name,) + self._subst_format)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001250 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001251 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001252 (self._w, 'tag', 'configure', tagName)
1253 + self._options(cnf, kw))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001254 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001255 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001256 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001257 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001258 def tag_names(self, index=None):
1259 return self.tk.splitlist(
1260 self.tk.call(self._w, 'tag', 'names', index))
1261 def tag_nextrange(self, tagName, index1, index2=None):
1262 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001263 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001264 def tag_raise(self, tagName, aboveThis=None):
1265 self.tk.call(
1266 self._w, 'tag', 'raise', tagName, aboveThis)
1267 def tag_ranges(self, tagName):
1268 return self.tk.splitlist(self.tk.call(
1269 self._w, 'tag', 'ranges', tagName))
1270 def tag_remove(self, tagName, index1, index2=None):
1271 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001272 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001273 def window_cget(self, index, option):
1274 return self.tk.call(self._w, 'window', 'cget', index, option)
1275 def window_config(self, index, cnf={}, **kw):
1276 apply(self.tk.call,
1277 (self._w, 'window', 'configure', index)
1278 + self._options(cnf, kw))
1279 def window_create(self, index, cnf={}, **kw):
1280 apply(self.tk.call,
1281 (self._w, 'window', 'create', index)
1282 + self._options(cnf, kw))
1283 def window_names(self):
1284 return self.tk.splitlist(
1285 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001286 def yview(self, *what):
1287 apply(self.tk.call, (self._w, 'yview')+what)
1288 def yview_pickplace(self, *what):
1289 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001290
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001291class OptionMenu(Widget):
1292 def __init__(self, master, variable, value, *values):
1293 self.widgetName = 'tk_optionMenu'
1294 Widget._setup(self, master, {})
1295 self.menuname = apply(
1296 self.tk.call,
1297 (self.widgetName, self._w, variable, value) + values)
1298
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001299class Image:
1300 def __init__(self, imgtype, name=None, cnf={}, **kw):
1301 self.name = None
1302 master = _default_root
1303 if not master: raise RuntimeError, 'Too early to create image'
1304 self.tk = master.tk
1305 if not name: name = `id(self)`
1306 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1307 elif kw: cnf = kw
1308 options = ()
1309 for k, v in cnf.items():
1310 if type(v) in CallableTypes:
1311 v = self._register(v)
1312 options = options + ('-'+k, v)
1313 apply(self.tk.call,
1314 ('image', 'create', imgtype, name,) + options)
1315 self.name = name
1316 def __str__(self): return self.name
1317 def __del__(self):
1318 if self.name:
1319 self.tk.call('image', 'delete', self.name)
1320 def height(self):
1321 return self.tk.getint(
1322 self.tk.call('image', 'height', self.name))
1323 def type(self):
1324 return self.tk.call('image', 'type', self.name)
1325 def width(self):
1326 return self.tk.getint(
1327 self.tk.call('image', 'width', self.name))
1328
1329class PhotoImage(Image):
1330 def __init__(self, name=None, cnf={}, **kw):
1331 apply(Image.__init__, (self, 'photo', name, cnf), kw)
1332
1333class BitmapImage(Image):
1334 def __init__(self, name=None, cnf={}, **kw):
1335 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1336
1337def image_names(): return _default_root.tk.call('image', 'names')
1338def image_types(): return _default_root.tk.call('image', 'types')
1339
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001340######################################################################
1341# Extensions:
1342
1343class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001344 def __init__(self, master=None, cnf={}, **kw):
1345 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001346 self.bind('<Any-Enter>', self.tkButtonEnter)
1347 self.bind('<Any-Leave>', self.tkButtonLeave)
1348 self.bind('<1>', self.tkButtonDown)
1349 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001350
1351class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001352 def __init__(self, master=None, cnf={}, **kw):
1353 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001354 self.bind('<Any-Enter>', self.tkButtonEnter)
1355 self.bind('<Any-Leave>', self.tkButtonLeave)
1356 self.bind('<1>', self.tkButtonDown)
1357 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1358 self['fg'] = self['bg']
1359 self['activebackground'] = self['bg']