(py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
[python/dscho.git] / Lib / lib-tk / Tkinter.py
blob05e94e52959641747e978a0b06f7f8bd5f0c4f5d
1 # Tkinter.py -- Tk/Tcl widget wrappers
3 import tkinter
4 from tkinter import TclError
6 class _Dummy:
7 def meth(self): return
9 def _func():
10 pass
12 FunctionType = type(_func)
13 ClassType = type(_Dummy)
14 MethodType = type(_Dummy.meth)
15 StringType = type('')
16 TupleType = type(())
17 ListType = type([])
18 DictionaryType = type({})
19 NoneType = type(None)
20 CallableTypes = (FunctionType, MethodType)
22 def _flatten(tuple):
23 res = ()
24 for item in tuple:
25 if type(item) in (TupleType, ListType):
26 res = res + _flatten(item)
27 else:
28 res = res + (item,)
29 return res
31 def _cnfmerge(cnfs):
32 if type(cnfs) in (NoneType, DictionaryType, StringType):
33 return cnfs
34 else:
35 cnf = {}
36 for c in _flatten(cnfs):
37 for k, v in c.items():
38 cnf[k] = v
39 return cnf
41 class Event:
42 pass
44 _default_root = None
46 def _tkerror(err):
47 pass
49 def _exit(code='0'):
50 import sys
51 sys.exit(getint(code))
53 _varnum = 0
54 class Variable:
55 def __init__(self, master=None):
56 global _default_root
57 global _varnum
58 if master:
59 self._tk = master.tk
60 else:
61 self._tk = _default_root.tk
62 self._name = 'PY_VAR' + `_varnum`
63 _varnum = _varnum + 1
64 def __del__(self):
65 self._tk.unsetvar(self._name)
66 def __str__(self):
67 return self._name
68 def __call__(self, value=None):
69 if value == None:
70 return self.get()
71 else:
72 self.set(value)
73 def set(self, value):
74 return self._tk.setvar(self._name, value)
76 class StringVar(Variable):
77 def __init__(self, master=None):
78 Variable.__init__(self, master)
79 def get(self):
80 return self._tk.getvar(self._name)
82 class IntVar(Variable):
83 def __init__(self, master=None):
84 Variable.__init__(self, master)
85 def get(self):
86 return self._tk.getint(self._tk.getvar(self._name))
88 class DoubleVar(Variable):
89 def __init__(self, master=None):
90 Variable.__init__(self, master)
91 def get(self):
92 return self._tk.getdouble(self._tk.getvar(self._name))
94 class BooleanVar(Variable):
95 def __init__(self, master=None):
96 Variable.__init__(self, master)
97 def get(self):
98 return self._tk.getboolean(self._tk.getvar(self._name))
100 def mainloop():
101 _default_root.tk.mainloop()
103 def getint(s):
104 return _default_root.tk.getint(s)
106 def getdouble(s):
107 return _default_root.tk.getdouble(s)
109 def getboolean(s):
110 return _default_root.tk.getboolean(s)
112 class Misc:
113 def tk_strictMotif(self, boolean=None):
114 return self.tk.getboolean(self.tk.call(
115 'set', 'tk_strictMotif', boolean))
116 def tk_menuBar(self, *args):
117 apply(self.tk.call, ('tk_menuBar', self._w) + args)
118 def wait_variable(self, name='PY_VAR'):
119 self.tk.call('tkwait', 'variable', name)
120 waitvar = wait_variable # XXX b/w compat
121 def wait_window(self, window=None):
122 if window == None:
123 window = self
124 self.tk.call('tkwait', 'window', window._w)
125 def wait_visibility(self, window=None):
126 if window == None:
127 window = self
128 self.tk.call('tkwait', 'visibility', window._w)
129 def setvar(self, name='PY_VAR', value='1'):
130 self.tk.setvar(name, value)
131 def getvar(self, name='PY_VAR'):
132 return self.tk.getvar(name)
133 def getint(self, s):
134 return self.tk.getint(s)
135 def getdouble(self, s):
136 return self.tk.getdouble(s)
137 def getboolean(self, s):
138 return self.tk.getboolean(s)
139 def focus_set(self):
140 self.tk.call('focus', self._w)
141 focus = focus_set # XXX b/w compat?
142 def focus_default_set(self):
143 self.tk.call('focus', 'default', self._w)
144 def focus_default_none(self):
145 self.tk.call('focus', 'default', 'none')
146 focus_default = focus_default_set
147 def focus_none(self):
148 self.tk.call('focus', 'none')
149 def focus_get(self):
150 name = self.tk.call('focus')
151 if name == 'none': return None
152 return self._nametowidget(name)
153 def after(self, ms, func=None, *args):
154 if not func:
155 self.tk.call('after', ms)
156 else:
157 # XXX Disgusting hack to clean up after calling func
158 tmp = []
159 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
160 try:
161 apply(func, args)
162 finally:
163 tk.deletecommand(tmp[0])
164 name = self._register(callit)
165 tmp.append(name)
166 self.tk.call('after', ms, name)
167 # XXX grab current w/o window argument
168 def grab_current(self):
169 name = self.tk.call('grab', 'current', self._w)
170 if not name: return None
171 return self._nametowidget(name)
172 def grab_release(self):
173 self.tk.call('grab', 'release', self._w)
174 def grab_set(self):
175 self.tk.call('grab', 'set', self._w)
176 def grab_set_global(self):
177 self.tk.call('grab', 'set', '-global', self._w)
178 def grab_status(self):
179 status = self.tk.call('grab', 'status', self._w)
180 if status == 'none': status = None
181 return status
182 def lower(self, belowThis=None):
183 self.tk.call('lower', self._w, belowThis)
184 def option_add(self, pattern, value, priority = None):
185 self.tk.call('option', 'add', pattern, priority)
186 def option_clear(self):
187 self.tk.call('option', 'clear')
188 def option_get(self, name, className):
189 return self.tk.call('option', 'get', self._w, name, className)
190 def option_readfile(self, fileName, priority = None):
191 self.tk.call('option', 'readfile', fileName, priority)
192 def selection_clear(self):
193 self.tk.call('selection', 'clear', self._w)
194 def selection_get(self, type=None):
195 return self.tk.call('selection', 'get', type)
196 def selection_handle(self, func, type=None, format=None):
197 name = self._register(func)
198 self.tk.call('selection', 'handle', self._w,
199 name, type, format)
200 def selection_own(self, func=None):
201 name = self._register(func)
202 self.tk.call('selection', 'own', self._w, name)
203 def selection_own_get(self):
204 return self._nametowidget(self.tk.call('selection', 'own'))
205 def send(self, interp, cmd, *args):
206 return apply(self.tk.call, ('send', interp, cmd) + args)
207 def lower(self, belowThis=None):
208 self.tk.call('lift', self._w, belowThis)
209 def tkraise(self, aboveThis=None):
210 self.tk.call('raise', self._w, aboveThis)
211 lift = tkraise
212 def colormodel(self, value=None):
213 return self.tk.call('tk', 'colormodel', self._w, value)
214 def winfo_atom(self, name):
215 return self.tk.getint(self.tk.call('winfo', 'atom', name))
216 def winfo_atomname(self, id):
217 return self.tk.call('winfo', 'atomname', id)
218 def winfo_cells(self):
219 return self.tk.getint(
220 self.tk.call('winfo', 'cells', self._w))
221 def winfo_children(self):
222 return map(self._nametowidget,
223 self.tk.splitlist(self.tk.call(
224 'winfo', 'children', self._w)))
225 def winfo_class(self):
226 return self.tk.call('winfo', 'class', self._w)
227 def winfo_containing(self, rootX, rootY):
228 return self.tk.call('winfo', 'containing', rootx, rootY)
229 def winfo_depth(self):
230 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
231 def winfo_exists(self):
232 return self.tk.getint(
233 self.tk.call('winfo', 'exists', self._w))
234 def winfo_fpixels(self, number):
235 return self.tk.getdouble(self.tk.call(
236 'winfo', 'fpixels', self._w, number))
237 def winfo_geometry(self):
238 return self.tk.call('winfo', 'geometry', self._w)
239 def winfo_height(self):
240 return self.tk.getint(
241 self.tk.call('winfo', 'height', self._w))
242 def winfo_id(self):
243 return self.tk.getint(
244 self.tk.call('winfo', 'id', self._w))
245 def winfo_interps(self):
246 return self.tk.splitlist(
247 self.tk.call('winfo', 'interps'))
248 def winfo_ismapped(self):
249 return self.tk.getint(
250 self.tk.call('winfo', 'ismapped', self._w))
251 def winfo_name(self):
252 return self.tk.call('winfo', 'name', self._w)
253 def winfo_parent(self):
254 return self.tk.call('winfo', 'parent', self._w)
255 def winfo_pathname(self, id):
256 return self.tk.call('winfo', 'pathname', id)
257 def winfo_pixels(self, number):
258 return self.tk.getint(
259 self.tk.call('winfo', 'pixels', self._w, number))
260 def winfo_reqheight(self):
261 return self.tk.getint(
262 self.tk.call('winfo', 'reqheight', self._w))
263 def winfo_reqwidth(self):
264 return self.tk.getint(
265 self.tk.call('winfo', 'reqwidth', self._w))
266 def winfo_rgb(self, color):
267 return self._getints(
268 self.tk.call('winfo', 'rgb', self._w, color))
269 def winfo_rootx(self):
270 return self.tk.getint(
271 self.tk.call('winfo', 'rootx', self._w))
272 def winfo_rooty(self):
273 return self.tk.getint(
274 self.tk.call('winfo', 'rooty', self._w))
275 def winfo_screen(self):
276 return self.tk.call('winfo', 'screen', self._w)
277 def winfo_screencells(self):
278 return self.tk.getint(
279 self.tk.call('winfo', 'screencells', self._w))
280 def winfo_screendepth(self):
281 return self.tk.getint(
282 self.tk.call('winfo', 'screendepth', self._w))
283 def winfo_screenheight(self):
284 return self.tk.getint(
285 self.tk.call('winfo', 'screenheight', self._w))
286 def winfo_screenmmheight(self):
287 return self.tk.getint(
288 self.tk.call('winfo', 'screenmmheight', self._w))
289 def winfo_screenmmwidth(self):
290 return self.tk.getint(
291 self.tk.call('winfo', 'screenmmwidth', self._w))
292 def winfo_screenvisual(self):
293 return self.tk.call('winfo', 'screenvisual', self._w)
294 def winfo_screenwidth(self):
295 return self.tk.getint(
296 self.tk.call('winfo', 'screenwidth', self._w))
297 def winfo_toplevel(self):
298 return self._nametowidget(self.tk.call(
299 'winfo', 'toplevel', self._w))
300 def winfo_visual(self):
301 return self.tk.call('winfo', 'visual', self._w)
302 def winfo_vrootheight(self):
303 return self.tk.getint(
304 self.tk.call('winfo', 'vrootheight', self._w))
305 def winfo_vrootwidth(self):
306 return self.tk.getint(
307 self.tk.call('winfo', 'vrootwidth', self._w))
308 def winfo_vrootx(self):
309 return self.tk.getint(
310 self.tk.call('winfo', 'vrootx', self._w))
311 def winfo_vrooty(self):
312 return self.tk.getint(
313 self.tk.call('winfo', 'vrooty', self._w))
314 def winfo_width(self):
315 return self.tk.getint(
316 self.tk.call('winfo', 'width', self._w))
317 def winfo_x(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'x', self._w))
320 def winfo_y(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'y', self._w))
323 def update(self):
324 self.tk.call('update')
325 def update_idletasks(self):
326 self.tk.call('update', 'idletasks')
327 def unbind(self, sequence):
328 self.tk.call('bind', self._w, sequence, '')
329 def bind(self, sequence, func, add=''):
330 if add: add = '+'
331 name = self._register(func, self._substitute)
332 self.tk.call('bind', self._w, sequence,
333 (add + name,) + self._subst_format)
334 def bind_all(self, sequence, func, add=''):
335 if add: add = '+'
336 name = self._register(func, self._substitute)
337 self.tk.call('bind', 'all' , sequence,
338 (add + name,) + self._subst_format)
339 def bind_class(self, className, sequence, func, add=''):
340 if add: add = '+'
341 name = self._register(func, self._substitute)
342 self.tk.call('bind', className , sequence,
343 (add + name,) + self._subst_format)
344 def mainloop(self):
345 self.tk.mainloop()
346 def quit(self):
347 self.tk.quit()
348 def _getints(self, string):
349 if not string: return None
350 res = ()
351 for v in self.tk.splitlist(string):
352 res = res + (self.tk.getint(v),)
353 return res
354 def _getboolean(self, string):
355 if string:
356 return self.tk.getboolean(string)
357 def _options(self, cnf):
358 cnf = _cnfmerge(cnf)
359 res = ()
360 for k, v in cnf.items():
361 if type(v) in CallableTypes:
362 v = self._register(v)
363 res = res + ('-'+k, v)
364 return res
365 def _nametowidget(self, name):
366 w = self
367 if name[0] == '.':
368 w = w._root()
369 name = name[1:]
370 from string import find
371 while name:
372 i = find(name, '.')
373 if i >= 0:
374 name, tail = name[:i], name[i+1:]
375 else:
376 tail = ''
377 w = w.children[name]
378 name = tail
379 return w
380 def _register(self, func, subst=None):
381 f = _CallSafely(func, subst).__call__
382 name = `id(f)`
383 if hasattr(func, 'im_func'):
384 func = func.im_func
385 if hasattr(func, 'func_name') and \
386 type(func.func_name) == type(''):
387 name = name + func.func_name
388 self.tk.createcommand(name, f)
389 return name
390 register = _register
391 def _root(self):
392 w = self
393 while w.master: w = w.master
394 return w
395 _subst_format = ('%#', '%b', '%f', '%h', '%k',
396 '%s', '%t', '%w', '%x', '%y',
397 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
398 def _substitute(self, *args):
399 tk = self.tk
400 if len(args) != len(self._subst_format): return args
401 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
402 # Missing: (a, c, d, m, o, v, B, R)
403 e = Event()
404 e.serial = tk.getint(nsign)
405 e.num = tk.getint(b)
406 try: e.focus = tk.getboolean(f)
407 except TclError: pass
408 e.height = tk.getint(h)
409 e.keycode = tk.getint(k)
410 e.state = tk.getint(s)
411 e.time = tk.getint(t)
412 e.width = tk.getint(w)
413 e.x = tk.getint(x)
414 e.y = tk.getint(y)
415 e.char = A
416 try: e.send_event = tk.getboolean(E)
417 except TclError: pass
418 e.keysym = K
419 e.keysym_num = tk.getint(N)
420 e.type = T
421 e.widget = self._nametowidget(W)
422 e.x_root = tk.getint(X)
423 e.y_root = tk.getint(Y)
424 return (e,)
426 class _CallSafely:
427 def __init__(self, func, subst=None):
428 self.func = func
429 self.subst = subst
430 def __call__(self, *args):
431 if self.subst:
432 args = self.apply_func(self.subst, args)
433 args = self.apply_func(self.func, args)
434 def apply_func(self, func, args):
435 import sys
436 try:
437 return apply(func, args)
438 except SystemExit, msg:
439 raise SystemExit, msg
440 except:
441 try:
442 try:
443 t = sys.exc_traceback
444 while t:
445 sys.stderr.write(
446 ' %s, line %s\n' %
447 (t.tb_frame.f_code,
448 t.tb_lineno))
449 t = t.tb_next
450 finally:
451 sys.stderr.write('%s: %s\n' %
452 (sys.exc_type,
453 sys.exc_value))
454 (sys.last_type,
455 sys.last_value,
456 sys.last_traceback) = (sys.exc_type,
457 sys.exc_value,
458 sys.exc_traceback)
459 import pdb
460 pdb.pm()
461 except:
462 print '*** Error in error handling ***'
463 print sys.exc_type, ':', sys.exc_value
465 class Wm:
466 def aspect(self,
467 minNumer=None, minDenom=None,
468 maxNumer=None, maxDenom=None):
469 return self._getints(
470 self.tk.call('wm', 'aspect', self._w,
471 minNumer, minDenom,
472 maxNumer, maxDenom))
473 def client(self, name=None):
474 return self.tk.call('wm', 'client', self._w, name)
475 def command(self, value=None):
476 return self.tk.call('wm', 'command', self._w, value)
477 def deiconify(self):
478 return self.tk.call('wm', 'deiconify', self._w)
479 def focusmodel(self, model=None):
480 return self.tk.call('wm', 'focusmodel', self._w, model)
481 def frame(self):
482 return self.tk.call('wm', 'frame', self._w)
483 def geometry(self, newGeometry=None):
484 return self.tk.call('wm', 'geometry', self._w, newGeometry)
485 def grid(self,
486 baseWidht=None, baseHeight=None,
487 widthInc=None, heightInc=None):
488 return self._getints(self.tk.call(
489 'wm', 'grid', self._w,
490 baseWidht, baseHeight, widthInc, heightInc))
491 def group(self, pathName=None):
492 return self.tk.call('wm', 'group', self._w, pathName)
493 def iconbitmap(self, bitmap=None):
494 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
495 def iconify(self):
496 return self.tk.call('wm', 'iconify', self._w)
497 def iconmask(self, bitmap=None):
498 return self.tk.call('wm', 'iconmask', self._w, bitmap)
499 def iconname(self, newName=None):
500 return self.tk.call('wm', 'iconname', self._w, newName)
501 def iconposition(self, x=None, y=None):
502 return self._getints(self.tk.call(
503 'wm', 'iconposition', self._w, x, y))
504 def iconwindow(self, pathName=None):
505 return self.tk.call('wm', 'iconwindow', self._w, pathName)
506 def maxsize(self, width=None, height=None):
507 return self._getints(self.tk.call(
508 'wm', 'maxsize', self._w, width, height))
509 def minsize(self, width=None, height=None):
510 return self._getints(self.tk.call(
511 'wm', 'minsize', self._w, width, height))
512 def overrideredirect(self, boolean=None):
513 return self._getboolean(self.tk.call(
514 'wm', 'overrideredirect', self._w, boolean))
515 def positionfrom(self, who=None):
516 return self.tk.call('wm', 'positionfrom', self._w, who)
517 def protocol(self, name=None, func=None):
518 if type(func) in CallableTypes:
519 command = self._register(func)
520 else:
521 command = func
522 return self.tk.call(
523 'wm', 'protocol', self._w, name, command)
524 def sizefrom(self, who=None):
525 return self.tk.call('wm', 'sizefrom', self._w, who)
526 def state(self):
527 return self.tk.call('wm', 'state', self._w)
528 def title(self, string=None):
529 return self.tk.call('wm', 'title', self._w, string)
530 def transient(self, master=None):
531 return self.tk.call('wm', 'transient', self._w, master)
532 def withdraw(self):
533 return self.tk.call('wm', 'withdraw', self._w)
535 class Tk(Misc, Wm):
536 _w = '.'
537 def __init__(self, screenName=None, baseName=None, className='Tk'):
538 self.master = None
539 self.children = {}
540 if baseName is None:
541 import sys, os
542 baseName = os.path.basename(sys.argv[0])
543 if baseName[-3:] == '.py': baseName = baseName[:-3]
544 self.tk = tkinter.create(screenName, baseName, className)
545 self.tk.createcommand('tkerror', _tkerror)
546 self.tk.createcommand('exit', _exit)
547 self.readprofile(baseName, className)
548 def destroy(self):
549 for c in self.children.values(): c.destroy()
550 self.tk.call('destroy', self._w)
551 def __str__(self):
552 return self._w
553 def readprofile(self, baseName, className):
554 import os
555 if os.environ.has_key('HOME'): home = os.environ['HOME']
556 else: home = os.curdir
557 class_tcl = os.path.join(home, '.%s.tcl' % className)
558 class_py = os.path.join(home, '.%s.py' % className)
559 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
560 base_py = os.path.join(home, '.%s.py' % baseName)
561 dir = {'self': self}
562 exec 'from Tkinter import *' in dir
563 if os.path.isfile(class_tcl):
564 print 'source', `class_tcl`
565 self.tk.call('source', class_tcl)
566 if os.path.isfile(class_py):
567 print 'execfile', `class_py`
568 execfile(class_py, dir)
569 if os.path.isfile(base_tcl):
570 print 'source', `base_tcl`
571 self.tk.call('source', base_tcl)
572 if os.path.isfile(base_py):
573 print 'execfile', `base_py`
574 execfile(base_py, dir)
576 class Pack:
577 def config(self, cnf={}):
578 apply(self.tk.call,
579 ('pack', 'configure', self._w)
580 + self._options(cnf))
581 pack = config
582 def __setitem__(self, key, value):
583 Pack.config({key: value})
584 def forget(self):
585 self.tk.call('pack', 'forget', self._w)
586 def newinfo(self):
587 words = self.tk.splitlist(
588 self.tk.call('pack', 'newinfo', self._w))
589 dict = {}
590 for i in range(0, len(words), 2):
591 key = words[i][1:]
592 value = words[i+1]
593 if value[0] == '.':
594 value = self._nametowidget(value)
595 dict[key] = value
596 return dict
597 info = newinfo
598 _noarg_ = ['_noarg_']
599 def propagate(self, flag=_noarg_):
600 if boolean is Pack._noarg_:
601 return self._getboolean(self.tk.call(
602 'pack', 'propagate', self._w))
603 else:
604 self.tk.call('pack', 'propagate', self._w, flag)
605 def slaves(self):
606 return map(self._nametowidget,
607 self.tk.splitlist(
608 self.tk.call('pack', 'slaves', self._w)))
610 class Place:
611 def config(self, cnf={}):
612 apply(self.tk.call,
613 ('place', 'configure', self._w)
614 + self._options(cnf))
615 place = config
616 def __setitem__(self, key, value):
617 Place.config({key: value})
618 def forget(self):
619 self.tk.call('place', 'forget', self._w)
620 def info(self):
621 return self.tk.call('place', 'info', self._w)
622 def slaves(self):
623 return map(self._nametowidget,
624 self.tk.splitlist(
625 self.tk.call(
626 'place', 'slaves', self._w)))
628 class Widget(Misc, Pack, Place):
629 def _setup(self, master, cnf):
630 global _default_root
631 if not master:
632 if not _default_root:
633 _default_root = Tk()
634 master = _default_root
635 if not _default_root:
636 _default_root = master
637 self.master = master
638 self.tk = master.tk
639 if cnf.has_key('name'):
640 name = cnf['name']
641 del cnf['name']
642 else:
643 name = `id(self)`
644 self._name = name
645 if master._w=='.':
646 self._w = '.' + name
647 else:
648 self._w = master._w + '.' + name
649 self.children = {}
650 if self.master.children.has_key(self._name):
651 self.master.children[self._name].destroy()
652 self.master.children[self._name] = self
653 def __init__(self, master, widgetName, cnf={}, extra=()):
654 cnf = _cnfmerge(cnf)
655 Widget._setup(self, master, cnf)
656 self.widgetName = widgetName
657 apply(self.tk.call, (widgetName, self._w) + extra)
658 if cnf:
659 Widget.config(self, cnf)
660 def config(self, cnf=None):
661 cnf = _cnfmerge(cnf)
662 if cnf is None:
663 cnf = {}
664 for x in self.tk.split(
665 self.tk.call(self._w, 'configure')):
666 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
667 return cnf
668 if type(cnf) == StringType:
669 x = self.tk.split(self.tk.call(
670 self._w, 'configure', '-'+cnf))
671 return (x[0][1:],) + x[1:]
672 for k in cnf.keys():
673 if type(k) == ClassType:
674 k.config(self, cnf[k])
675 del cnf[k]
676 apply(self.tk.call, (self._w, 'configure')
677 + self._options(cnf))
678 def __getitem__(self, key):
679 v = self.tk.splitlist(self.tk.call(
680 self._w, 'configure', '-' + key))
681 return v[4]
682 def __setitem__(self, key, value):
683 Widget.config(self, {key: value})
684 def keys(self):
685 return map(lambda x: x[0][1:],
686 self.tk.split(self.tk.call(self._w, 'configure')))
687 def __str__(self):
688 return self._w
689 def destroy(self):
690 for c in self.children.values(): c.destroy()
691 if self.master.children.has_key(self._name):
692 del self.master.children[self._name]
693 self.tk.call('destroy', self._w)
694 def _do(self, name, args=()):
695 return apply(self.tk.call, (self._w, name) + args)
697 class Toplevel(Widget, Wm):
698 def __init__(self, master=None, cnf={}):
699 extra = ()
700 if cnf.has_key('screen'):
701 extra = ('-screen', cnf['screen'])
702 del cnf['screen']
703 if cnf.has_key('class'):
704 extra = extra + ('-class', cnf['class'])
705 del cnf['class']
706 Widget.__init__(self, master, 'toplevel', cnf, extra)
707 root = self._root()
708 self.iconname(root.iconname())
709 self.title(root.title())
711 class Button(Widget):
712 def __init__(self, master=None, cnf={}):
713 Widget.__init__(self, master, 'button', cnf)
714 def tk_butEnter(self, *dummy):
715 self.tk.call('tk_butEnter', self._w)
716 def tk_butLeave(self, *dummy):
717 self.tk.call('tk_butLeave', self._w)
718 def tk_butDown(self, *dummy):
719 self.tk.call('tk_butDown', self._w)
720 def tk_butUp(self, *dummy):
721 self.tk.call('tk_butUp', self._w)
722 def flash(self):
723 self.tk.call(self._w, 'flash')
724 def invoke(self):
725 self.tk.call(self._w, 'invoke')
727 # Indices:
728 def AtEnd():
729 return 'end'
730 def AtInsert(*args):
731 s = 'insert'
732 for a in args:
733 if a: s = s + (' ' + a)
734 return s
735 def AtSelFirst():
736 return 'sel.first'
737 def AtSelLast():
738 return 'sel.last'
739 def At(x, y=None):
740 if y is None:
741 return '@' + `x`
742 else:
743 return '@' + `x` + ',' + `y`
745 class Canvas(Widget):
746 def __init__(self, master=None, cnf={}):
747 Widget.__init__(self, master, 'canvas', cnf)
748 def addtag(self, *args):
749 self._do('addtag', args)
750 def addtag_above(self, tagOrId):
751 self.addtag('above', tagOrId)
752 def addtag_all(self):
753 self.addtag('all')
754 def addtag_below(self, tagOrId):
755 self.addtag('below', tagOrId)
756 def addtag_closest(self, x, y, halo=None, start=None):
757 self.addtag('closest', x, y, halo, start)
758 def addtag_enclosed(self, x1, y1, x2, y2):
759 self.addtag('enclosed', x1, y1, x2, y2)
760 def addtag_overlapping(self, x1, y1, x2, y2):
761 self.addtag('overlapping', x1, y1, x2, y2)
762 def addtag_withtag(self, tagOrId):
763 self.addtag('withtag', tagOrId)
764 def bbox(self, *args):
765 return self._getints(self._do('bbox', args)) or None
766 def tag_unbind(self, tagOrId, sequence):
767 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
768 def tag_bind(self, tagOrId, sequence, func, add=''):
769 if add: add='+'
770 name = self._register(func, self._substitute)
771 self.tk.call(self._w, 'bind', tagOrId, sequence,
772 (add + name,) + self._subst_format)
773 def canvasx(self, screenx, gridspacing=None):
774 return self.tk.getint(self.tk.call(
775 self._w, 'canvasx', screenx, gridspacing))
776 def canvasy(self, screeny, gridspacing=None):
777 return self.tk.getint(self.tk.call(
778 self._w, 'canvasy', screeny, gridspacing))
779 def coords(self, *args):
780 return self._do('coords', args)
781 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
782 args = _flatten(args)
783 cnf = args[-1]
784 if type(cnf) in (DictionaryType, TupleType):
785 args = args[:-1]
786 else:
787 cnf = {}
788 return self.tk.getint(apply(
789 self.tk.call,
790 (self._w, 'create', itemType)
791 + args + self._options(cnf)))
792 def create_arc(self, *args):
793 return Canvas._create(self, 'arc', args)
794 def create_bitmap(self, *args):
795 return Canvas._create(self, 'bitmap', args)
796 def create_line(self, *args):
797 return Canvas._create(self, 'line', args)
798 def create_oval(self, *args):
799 return Canvas._create(self, 'oval', args)
800 def create_polygon(self, *args):
801 return Canvas._create(self, 'polygon', args)
802 def create_rectangle(self, *args):
803 return Canvas._create(self, 'rectangle', args)
804 def create_text(self, *args):
805 return Canvas._create(self, 'text', args)
806 def create_window(self, *args):
807 return Canvas._create(self, 'window', args)
808 def dchars(self, *args):
809 self._do('dchars', args)
810 def delete(self, *args):
811 self._do('delete', args)
812 def dtag(self, *args):
813 self._do('dtag', args)
814 def find(self, *args):
815 return self._getints(self._do('find', args))
816 def find_above(self, tagOrId):
817 return self.find('above', tagOrId)
818 def find_all(self):
819 return self.find('all')
820 def find_below(self, tagOrId):
821 return self.find('below', tagOrId)
822 def find_closest(self, x, y, halo=None, start=None):
823 return self.find('closest', x, y, halo, start)
824 def find_enclosed(self, x1, y1, x2, y2):
825 return self.find('enclosed', x1, y1, x2, y2)
826 def find_overlapping(self, x1, y1, x2, y2):
827 return self.find('overlapping', x1, y1, x2, y2)
828 def find_withtag(self, tagOrId):
829 return self.find('withtag', tagOrId)
830 def focus(self, *args):
831 return self._do('focus', args)
832 def gettags(self, *args):
833 return self.tk.splitlist(self._do('gettags', args))
834 def icursor(self, *args):
835 self._do('icursor', args)
836 def index(self, *args):
837 return self.tk.getint(self._do('index', args))
838 def insert(self, *args):
839 self._do('insert', args)
840 def itemconfig(self, tagOrId, cnf=None):
841 if cnf is None:
842 cnf = {}
843 for x in self.tk.split(
844 self._do('itemconfigure', (tagOrId))):
845 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
846 return cnf
847 if type(cnf) == StringType:
848 x = self.tk.split(self._do('itemconfigure',
849 (tagOrId, '-'+cnf,)))
850 return (x[0][1:],) + x[1:]
851 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
852 def lower(self, *args):
853 self._do('lower', args)
854 def move(self, *args):
855 self._do('move', args)
856 def postscript(self, cnf={}):
857 return self._do('postscript', self._options(cnf))
858 def tkraise(self, *args):
859 self._do('raise', args)
860 lift = tkraise
861 def scale(self, *args):
862 self._do('scale', args)
863 def scan_mark(self, x, y):
864 self.tk.call(self._w, 'scan', 'mark', x, y)
865 def scan_dragto(self, x, y):
866 self.tk.call(self._w, 'scan', 'dragto', x, y)
867 def select_adjust(self, tagOrId, index):
868 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
869 def select_clear(self):
870 self.tk.call(self._w, 'select', 'clear')
871 def select_from(self, tagOrId, index):
872 self.tk.call(self._w, 'select', 'from', tagOrId, index)
873 def select_item(self):
874 self.tk.call(self._w, 'select', 'item')
875 def select_to(self, tagOrId, index):
876 self.tk.call(self._w, 'select', 'to', tagOrId, index)
877 def type(self, tagOrId):
878 return self.tk.call(self._w, 'type', tagOrId) or None
879 def xview(self, index):
880 self.tk.call(self._w, 'xview', index)
881 def yview(self, index):
882 self.tk.call(self._w, 'yview', index)
884 class Checkbutton(Widget):
885 def __init__(self, master=None, cnf={}):
886 Widget.__init__(self, master, 'checkbutton', cnf)
887 def deselect(self):
888 self.tk.call(self._w, 'deselect')
889 def flash(self):
890 self.tk.call(self._w, 'flash')
891 def invoke(self):
892 self.tk.call(self._w, 'invoke')
893 def select(self):
894 self.tk.call(self._w, 'select')
895 def toggle(self):
896 self.tk.call(self._w, 'toggle')
898 class Entry(Widget):
899 def __init__(self, master=None, cnf={}):
900 Widget.__init__(self, master, 'entry', cnf)
901 def tk_entryBackspace(self):
902 self.tk.call('tk_entryBackspace', self._w)
903 def tk_entryBackword(self):
904 self.tk.call('tk_entryBackword', self._w)
905 def tk_entrySeeCaret(self):
906 self.tk.call('tk_entrySeeCaret', self._w)
907 def delete(self, first, last=None):
908 self.tk.call(self._w, 'delete', first, last)
909 def get(self):
910 return self.tk.call(self._w, 'get')
911 def icursor(self, index):
912 self.tk.call(self._w, 'icursor', index)
913 def index(self, index):
914 return self.tk.getint(self.tk.call(
915 self._w, 'index', index))
916 def insert(self, index, string):
917 self.tk.call(self._w, 'insert', index, string)
918 def scan_mark(self, x):
919 self.tk.call(self._w, 'scan', 'mark', x)
920 def scan_dragto(self, x):
921 self.tk.call(self._w, 'scan', 'dragto', x)
922 def select_adjust(self, index):
923 self.tk.call(self._w, 'select', 'adjust', index)
924 def select_clear(self):
925 self.tk.call(self._w, 'select', 'clear')
926 def select_from(self, index):
927 self.tk.call(self._w, 'select', 'from', index)
928 def select_to(self, index):
929 self.tk.call(self._w, 'select', 'to', index)
930 def select_view(self, index):
931 self.tk.call(self._w, 'select', 'view', index)
933 class Frame(Widget):
934 def __init__(self, master=None, cnf={}):
935 cnf = _cnfmerge(cnf)
936 extra = ()
937 if cnf.has_key('class'):
938 extra = ('-class', cnf['class'])
939 del cnf['class']
940 Widget.__init__(self, master, 'frame', cnf, extra)
941 def tk_menuBar(self, *args):
942 apply(self.tk.call, ('tk_menuBar', self._w) + args)
944 class Label(Widget):
945 def __init__(self, master=None, cnf={}):
946 Widget.__init__(self, master, 'label', cnf)
948 class Listbox(Widget):
949 def __init__(self, master=None, cnf={}):
950 Widget.__init__(self, master, 'listbox', cnf)
951 def tk_listboxSingleSelect(self):
952 self.tk.call('tk_listboxSingleSelect', self._w)
953 def curselection(self):
954 return self.tk.splitlist(self.tk.call(
955 self._w, 'curselection'))
956 def delete(self, first, last=None):
957 self.tk.call(self._w, 'delete', first, last)
958 def get(self, index):
959 return self.tk.call(self._w, 'get', index)
960 def insert(self, index, *elements):
961 apply(self.tk.call,
962 (self._w, 'insert', index) + elements)
963 def nearest(self, y):
964 return self.tk.getint(self.tk.call(
965 self._w, 'nearest', y))
966 def scan_mark(self, x, y):
967 self.tk.call(self._w, 'scan', 'mark', x, y)
968 def scan_dragto(self, x, y):
969 self.tk.call(self._w, 'scan', 'dragto', x, y)
970 def select_adjust(self, index):
971 self.tk.call(self._w, 'select', 'adjust', index)
972 def select_clear(self):
973 self.tk.call(self._w, 'select', 'clear')
974 def select_from(self, index):
975 self.tk.call(self._w, 'select', 'from', index)
976 def select_to(self, index):
977 self.tk.call(self._w, 'select', 'to', index)
978 def size(self):
979 return self.tk.getint(self.tk.call(self._w, 'size'))
980 def xview(self, index):
981 self.tk.call(self._w, 'xview', index)
982 def yview(self, index):
983 self.tk.call(self._w, 'yview', index)
985 class Menu(Widget):
986 def __init__(self, master=None, cnf={}):
987 Widget.__init__(self, master, 'menu', cnf)
988 def tk_bindForTraversal(self):
989 self.tk.call('tk_bindForTraversal', self._w)
990 def tk_mbPost(self):
991 self.tk.call('tk_mbPost', self._w)
992 def tk_mbUnpost(self):
993 self.tk.call('tk_mbUnpost')
994 def tk_traverseToMenu(self, char):
995 self.tk.call('tk_traverseToMenu', self._w, char)
996 def tk_traverseWithinMenu(self, char):
997 self.tk.call('tk_traverseWithinMenu', self._w, char)
998 def tk_getMenuButtons(self):
999 return self.tk.call('tk_getMenuButtons', self._w)
1000 def tk_nextMenu(self, count):
1001 self.tk.call('tk_nextMenu', count)
1002 def tk_nextMenuEntry(self, count):
1003 self.tk.call('tk_nextMenuEntry', count)
1004 def tk_invokeMenu(self):
1005 self.tk.call('tk_invokeMenu', self._w)
1006 def tk_firstMenu(self):
1007 self.tk.call('tk_firstMenu', self._w)
1008 def tk_mbButtonDown(self):
1009 self.tk.call('tk_mbButtonDown', self._w)
1010 def activate(self, index):
1011 self.tk.call(self._w, 'activate', index)
1012 def add(self, itemType, cnf={}):
1013 apply(self.tk.call, (self._w, 'add', itemType)
1014 + self._options(cnf))
1015 def add_cascade(self, cnf={}):
1016 self.add('cascade', cnf)
1017 def add_checkbutton(self, cnf={}):
1018 self.add('checkbutton', cnf)
1019 def add_command(self, cnf={}):
1020 self.add('command', cnf)
1021 def add_radiobutton(self, cnf={}):
1022 self.add('radiobutton', cnf)
1023 def add_separator(self, cnf={}):
1024 self.add('separator', cnf)
1025 def delete(self, index1, index2=None):
1026 self.tk.call(self._w, 'delete', index1, index2)
1027 def entryconfig(self, index, cnf={}):
1028 apply(self.tk.call, (self._w, 'entryconfigure', index)
1029 + self._options(cnf))
1030 def index(self, index):
1031 i = self.tk.call(self._w, 'index', index)
1032 if i == 'none': return None
1033 return self.tk.getint(i)
1034 def invoke(self, index):
1035 return self.tk.call(self._w, 'invoke', index)
1036 def post(self, x, y):
1037 self.tk.call(self._w, 'post', x, y)
1038 def unpost(self):
1039 self.tk.call(self._w, 'unpost')
1040 def yposition(self, index):
1041 return self.tk.getint(self.tk.call(
1042 self._w, 'yposition', index))
1044 class Menubutton(Widget):
1045 def __init__(self, master=None, cnf={}):
1046 Widget.__init__(self, master, 'menubutton', cnf)
1048 class Message(Widget):
1049 def __init__(self, master=None, cnf={}):
1050 Widget.__init__(self, master, 'message', cnf)
1052 class Radiobutton(Widget):
1053 def __init__(self, master=None, cnf={}):
1054 Widget.__init__(self, master, 'radiobutton', cnf)
1055 def deselect(self):
1056 self.tk.call(self._w, 'deselect')
1057 def flash(self):
1058 self.tk.call(self._w, 'flash')
1059 def invoke(self):
1060 self.tk.call(self._w, 'invoke')
1061 def select(self):
1062 self.tk.call(self._w, 'select')
1064 class Scale(Widget):
1065 def __init__(self, master=None, cnf={}):
1066 Widget.__init__(self, master, 'scale', cnf)
1067 def get(self):
1068 return self.tk.getint(self.tk.call(self._w, 'get'))
1069 def set(self, value):
1070 self.tk.call(self._w, 'set', value)
1072 class Scrollbar(Widget):
1073 def __init__(self, master=None, cnf={}):
1074 Widget.__init__(self, master, 'scrollbar', cnf)
1075 def get(self):
1076 return self._getints(self.tk.call(self._w, 'get'))
1077 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1078 self.tk.call(self._w, 'set',
1079 totalUnits, windowUnits, firstUnit, lastUnit)
1081 class Text(Widget):
1082 def __init__(self, master=None, cnf={}):
1083 Widget.__init__(self, master, 'text', cnf)
1084 def tk_textSelectTo(self, index):
1085 self.tk.call('tk_textSelectTo', self._w, index)
1086 def tk_textBackspace(self):
1087 self.tk.call('tk_textBackspace', self._w)
1088 def tk_textIndexCloser(self, a, b, c):
1089 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1090 def tk_textResetAnchor(self, index):
1091 self.tk.call('tk_textResetAnchor', self._w, index)
1092 def compare(self, index1, op, index2):
1093 return self.tk.getboolean(self.tk.call(
1094 self._w, 'compare', index1, op, index2))
1095 def debug(self, boolean=None):
1096 return self.tk.getboolean(self.tk.call(
1097 self._w, 'debug', boolean))
1098 def delete(self, index1, index2=None):
1099 self.tk.call(self._w, 'delete', index1, index2)
1100 def get(self, index1, index2=None):
1101 return self.tk.call(self._w, 'get', index1, index2)
1102 def index(self, index):
1103 return self.tk.call(self._w, 'index', index)
1104 def insert(self, index, chars):
1105 self.tk.call(self._w, 'insert', index, chars)
1106 def mark_names(self):
1107 return self.tk.splitlist(self.tk.call(
1108 self._w, 'mark', 'names'))
1109 def mark_set(self, markName, index):
1110 self.tk.call(self._w, 'mark', 'set', markName, index)
1111 def mark_unset(self, markNames):
1112 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1113 def scan_mark(self, y):
1114 self.tk.call(self._w, 'scan', 'mark', y)
1115 def scan_dragto(self, y):
1116 self.tk.call(self._w, 'scan', 'dragto', y)
1117 def tag_add(self, tagName, index1, index2=None):
1118 self.tk.call(
1119 self._w, 'tag', 'add', tagName, index1, index2)
1120 def tag_unbind(self, tagName, sequence):
1121 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
1122 def tag_bind(self, tagName, sequence, func, add=''):
1123 if add: add='+'
1124 name = self._register(func, self._substitute)
1125 self.tk.call(self._w, 'tag', 'bind',
1126 tagName, sequence,
1127 (add + name,) + self._subst_format)
1128 def tag_config(self, tagName, cnf={}):
1129 apply(self.tk.call,
1130 (self._w, 'tag', 'configure', tagName)
1131 + self._options(cnf))
1132 def tag_delete(self, *tagNames):
1133 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
1134 def tag_lower(self, tagName, belowThis=None):
1135 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
1136 def tag_names(self, index=None):
1137 return self.tk.splitlist(
1138 self.tk.call(self._w, 'tag', 'names', index))
1139 def tag_nextrange(self, tagName, index1, index2=None):
1140 return self.tk.splitlist(self.tk.call(
1141 self._w, 'tag', 'nextrange', index1, index2))
1142 def tag_raise(self, tagName, aboveThis=None):
1143 self.tk.call(
1144 self._w, 'tag', 'raise', tagName, aboveThis)
1145 def tag_ranges(self, tagName):
1146 return self.tk.splitlist(self.tk.call(
1147 self._w, 'tag', 'ranges', tagName))
1148 def tag_remove(self, tagName, index1, index2=None):
1149 self.tk.call(
1150 self._w, 'tag', 'remove', tagName, index1, index2)
1151 def yview(self, what):
1152 self.tk.call(self._w, 'yview', what)
1153 def yview_pickplace(self, what):
1154 self.tk.call(self._w, 'yview', '-pickplace', what)
1156 ######################################################################
1157 # Extensions:
1159 class Studbutton(Button):
1160 def __init__(self, master=None, cnf={}):
1161 Widget.__init__(self, master, 'studbutton', cnf)
1162 self.bind('<Any-Enter>', self.tk_butEnter)
1163 self.bind('<Any-Leave>', self.tk_butLeave)
1164 self.bind('<1>', self.tk_butDown)
1165 self.bind('<ButtonRelease-1>', self.tk_butUp)
1167 class Tributton(Button):
1168 def __init__(self, master=None, cnf={}):
1169 Widget.__init__(self, master, 'tributton', cnf)
1170 self.bind('<Any-Enter>', self.tk_butEnter)
1171 self.bind('<Any-Leave>', self.tk_butLeave)
1172 self.bind('<1>', self.tk_butDown)
1173 self.bind('<ButtonRelease-1>', self.tk_butUp)