Use py_resource module
[python/dscho.git] / Lib / tkinter / Tkinter.py
blobd033823f88aa7e0021a7801053bb8c3e6423b458
1 # Tkinter.py -- Tk/Tcl widget wrappers
3 __version__ = "$Revision$"
5 try:
6 # See if modern _tkinter is present
7 import _tkinter
8 tkinter = _tkinter # b/w compat
9 except ImportError:
10 # No modern _tkinter -- try oldfashioned tkinter
11 import tkinter
12 if hasattr(tkinter, "__path__"):
13 import sys, os
14 # Append standard platform specific directory
15 p = tkinter.__path__
16 for dir in sys.path:
17 if (dir not in p and
18 os.path.basename(dir) == sys.platform):
19 p.append(dir)
20 del sys, os, p, dir
21 from tkinter import tkinter
22 TclError = tkinter.TclError
23 from types import *
24 from Tkconstants import *
25 import string; _string = string; del string
27 TkVersion = _string.atof(tkinter.TK_VERSION)
28 TclVersion = _string.atof(tkinter.TCL_VERSION)
30 ######################################################################
31 # Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
32 # they are defined here (and not in Tkconstants):
33 ######################################################################
34 if TkVersion >= 4.1:
35 READABLE = 2
36 WRITABLE = 4
37 EXCEPTION = 8
38 else:
39 READABLE = 1
40 WRITABLE = 2
41 EXCEPTION = 4
44 def _flatten(tuple):
45 res = ()
46 for item in tuple:
47 if type(item) in (TupleType, ListType):
48 res = res + _flatten(item)
49 elif item is not None:
50 res = res + (item,)
51 return res
53 def _cnfmerge(cnfs):
54 if type(cnfs) is DictionaryType:
55 return cnfs
56 elif type(cnfs) in (NoneType, StringType):
58 return cnfs
59 else:
60 cnf = {}
61 for c in _flatten(cnfs):
62 for k, v in c.items():
63 cnf[k] = v
64 return cnf
66 class Event:
67 pass
69 _default_root = None
71 def _tkerror(err):
72 pass
74 def _exit(code='0'):
75 raise SystemExit, code
77 _varnum = 0
78 class Variable:
79 def __init__(self, master=None):
80 global _default_root
81 global _varnum
82 if master:
83 self._tk = master.tk
84 else:
85 self._tk = _default_root.tk
86 self._name = 'PY_VAR' + `_varnum`
87 _varnum = _varnum + 1
88 def __del__(self):
89 self._tk.globalunsetvar(self._name)
90 def __str__(self):
91 return self._name
92 def set(self, value):
93 return self._tk.globalsetvar(self._name, value)
95 class StringVar(Variable):
96 def __init__(self, master=None):
97 Variable.__init__(self, master)
98 def get(self):
99 return self._tk.globalgetvar(self._name)
101 class IntVar(Variable):
102 def __init__(self, master=None):
103 Variable.__init__(self, master)
104 def get(self):
105 return self._tk.getint(self._tk.globalgetvar(self._name))
107 class DoubleVar(Variable):
108 def __init__(self, master=None):
109 Variable.__init__(self, master)
110 def get(self):
111 return self._tk.getdouble(self._tk.globalgetvar(self._name))
113 class BooleanVar(Variable):
114 def __init__(self, master=None):
115 Variable.__init__(self, master)
116 def get(self):
117 return self._tk.getboolean(self._tk.globalgetvar(self._name))
119 def mainloop(n=0):
120 _default_root.tk.mainloop(n)
122 def getint(s):
123 return _default_root.tk.getint(s)
125 def getdouble(s):
126 return _default_root.tk.getdouble(s)
128 def getboolean(s):
129 return _default_root.tk.getboolean(s)
131 class Misc:
132 def tk_strictMotif(self, boolean=None):
133 return self.tk.getboolean(self.tk.call(
134 'set', 'tk_strictMotif', boolean))
135 def tk_menuBar(self, *args):
136 apply(self.tk.call, ('tk_menuBar', self._w) + args)
137 def wait_variable(self, name='PY_VAR'):
138 self.tk.call('tkwait', 'variable', name)
139 waitvar = wait_variable # XXX b/w compat
140 def wait_window(self, window=None):
141 if window == None:
142 window = self
143 self.tk.call('tkwait', 'window', window._w)
144 def wait_visibility(self, window=None):
145 if window == None:
146 window = self
147 self.tk.call('tkwait', 'visibility', window._w)
148 def setvar(self, name='PY_VAR', value='1'):
149 self.tk.setvar(name, value)
150 def getvar(self, name='PY_VAR'):
151 return self.tk.getvar(name)
152 def getint(self, s):
153 return self.tk.getint(s)
154 def getdouble(self, s):
155 return self.tk.getdouble(s)
156 def getboolean(self, s):
157 return self.tk.getboolean(s)
158 def focus_set(self):
159 self.tk.call('focus', self._w)
160 focus = focus_set # XXX b/w compat?
161 def focus_default_set(self):
162 self.tk.call('focus', 'default', self._w)
163 def focus_default_none(self):
164 self.tk.call('focus', 'default', 'none')
165 focus_default = focus_default_set
166 def focus_none(self):
167 self.tk.call('focus', 'none')
168 def focus_get(self):
169 name = self.tk.call('focus')
170 if name == 'none': return None
171 return self._nametowidget(name)
172 def tk_focusNext(self):
173 name = self.tk.call('tk_focusNext', self._w)
174 if not name: return None
175 return self._nametowidget(name)
176 def tk_focusPrev(self):
177 name = self.tk.call('tk_focusPrev', self._w)
178 if not name: return None
179 return self._nametowidget(name)
180 def after(self, ms, func=None, *args):
181 if not func:
182 # I'd rather use time.sleep(ms*0.001)
183 self.tk.call('after', ms)
184 else:
185 # XXX Disgusting hack to clean up after calling func
186 tmp = []
187 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
188 try:
189 apply(func, args)
190 finally:
191 tk.deletecommand(tmp[0])
192 name = self._register(callit)
193 tmp.append(name)
194 return self.tk.call('after', ms, name)
195 def after_idle(self, func, *args):
196 return apply(self.after, ('idle', func) + args)
197 def after_cancel(self, id):
198 self.tk.call('after', 'cancel', id)
199 def bell(self, displayof=None):
200 if displayof:
201 self.tk.call('bell', '-displayof', displayof)
202 else:
203 self.tk.call('bell', '-displayof', self._w)
204 # XXX grab current w/o window argument
205 def grab_current(self):
206 name = self.tk.call('grab', 'current', self._w)
207 if not name: return None
208 return self._nametowidget(name)
209 def grab_release(self):
210 self.tk.call('grab', 'release', self._w)
211 def grab_set(self):
212 self.tk.call('grab', 'set', self._w)
213 def grab_set_global(self):
214 self.tk.call('grab', 'set', '-global', self._w)
215 def grab_status(self):
216 status = self.tk.call('grab', 'status', self._w)
217 if status == 'none': status = None
218 return status
219 def lower(self, belowThis=None):
220 self.tk.call('lower', self._w, belowThis)
221 def option_add(self, pattern, value, priority = None):
222 self.tk.call('option', 'add', pattern, value, priority)
223 def option_clear(self):
224 self.tk.call('option', 'clear')
225 def option_get(self, name, className):
226 return self.tk.call('option', 'get', self._w, name, className)
227 def option_readfile(self, fileName, priority = None):
228 self.tk.call('option', 'readfile', fileName, priority)
229 def selection_clear(self):
230 self.tk.call('selection', 'clear', self._w)
231 def selection_get(self, type=None):
232 return self.tk.call('selection', 'get', type)
233 def selection_handle(self, func, type=None, format=None):
234 name = self._register(func)
235 self.tk.call('selection', 'handle', self._w,
236 name, type, format)
237 def selection_own(self, func=None):
238 name = self._register(func)
239 self.tk.call('selection', 'own', self._w, name)
240 def selection_own_get(self):
241 return self._nametowidget(self.tk.call('selection', 'own'))
242 def send(self, interp, cmd, *args):
243 return apply(self.tk.call, ('send', interp, cmd) + args)
244 def lower(self, belowThis=None):
245 self.tk.call('lift', self._w, belowThis)
246 def tkraise(self, aboveThis=None):
247 self.tk.call('raise', self._w, aboveThis)
248 lift = tkraise
249 def colormodel(self, value=None):
250 return self.tk.call('tk', 'colormodel', self._w, value)
251 def winfo_atom(self, name):
252 return self.tk.getint(self.tk.call('winfo', 'atom', name))
253 def winfo_atomname(self, id):
254 return self.tk.call('winfo', 'atomname', id)
255 def winfo_cells(self):
256 return self.tk.getint(
257 self.tk.call('winfo', 'cells', self._w))
258 def winfo_children(self):
259 return map(self._nametowidget,
260 self.tk.splitlist(self.tk.call(
261 'winfo', 'children', self._w)))
262 def winfo_class(self):
263 return self.tk.call('winfo', 'class', self._w)
264 def winfo_containing(self, rootX, rootY):
265 return self.tk.call('winfo', 'containing', rootX, rootY)
266 def winfo_depth(self):
267 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
268 def winfo_exists(self):
269 return self.tk.getint(
270 self.tk.call('winfo', 'exists', self._w))
271 def winfo_fpixels(self, number):
272 return self.tk.getdouble(self.tk.call(
273 'winfo', 'fpixels', self._w, number))
274 def winfo_geometry(self):
275 return self.tk.call('winfo', 'geometry', self._w)
276 def winfo_height(self):
277 return self.tk.getint(
278 self.tk.call('winfo', 'height', self._w))
279 def winfo_id(self):
280 return self.tk.getint(
281 self.tk.call('winfo', 'id', self._w))
282 def winfo_interps(self):
283 return self.tk.splitlist(
284 self.tk.call('winfo', 'interps'))
285 def winfo_ismapped(self):
286 return self.tk.getint(
287 self.tk.call('winfo', 'ismapped', self._w))
288 def winfo_name(self):
289 return self.tk.call('winfo', 'name', self._w)
290 def winfo_parent(self):
291 return self.tk.call('winfo', 'parent', self._w)
292 def winfo_pathname(self, id):
293 return self.tk.call('winfo', 'pathname', id)
294 def winfo_pixels(self, number):
295 return self.tk.getint(
296 self.tk.call('winfo', 'pixels', self._w, number))
297 def winfo_reqheight(self):
298 return self.tk.getint(
299 self.tk.call('winfo', 'reqheight', self._w))
300 def winfo_reqwidth(self):
301 return self.tk.getint(
302 self.tk.call('winfo', 'reqwidth', self._w))
303 def winfo_rgb(self, color):
304 return self._getints(
305 self.tk.call('winfo', 'rgb', self._w, color))
306 def winfo_rootx(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'rootx', self._w))
309 def winfo_rooty(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'rooty', self._w))
312 def winfo_screen(self):
313 return self.tk.call('winfo', 'screen', self._w)
314 def winfo_screencells(self):
315 return self.tk.getint(
316 self.tk.call('winfo', 'screencells', self._w))
317 def winfo_screendepth(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'screendepth', self._w))
320 def winfo_screenheight(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'screenheight', self._w))
323 def winfo_screenmmheight(self):
324 return self.tk.getint(
325 self.tk.call('winfo', 'screenmmheight', self._w))
326 def winfo_screenmmwidth(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'screenmmwidth', self._w))
329 def winfo_screenvisual(self):
330 return self.tk.call('winfo', 'screenvisual', self._w)
331 def winfo_screenwidth(self):
332 return self.tk.getint(
333 self.tk.call('winfo', 'screenwidth', self._w))
334 def winfo_toplevel(self):
335 return self._nametowidget(self.tk.call(
336 'winfo', 'toplevel', self._w))
337 def winfo_visual(self):
338 return self.tk.call('winfo', 'visual', self._w)
339 def winfo_vrootheight(self):
340 return self.tk.getint(
341 self.tk.call('winfo', 'vrootheight', self._w))
342 def winfo_vrootwidth(self):
343 return self.tk.getint(
344 self.tk.call('winfo', 'vrootwidth', self._w))
345 def winfo_vrootx(self):
346 return self.tk.getint(
347 self.tk.call('winfo', 'vrootx', self._w))
348 def winfo_vrooty(self):
349 return self.tk.getint(
350 self.tk.call('winfo', 'vrooty', self._w))
351 def winfo_width(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'width', self._w))
354 def winfo_x(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'x', self._w))
357 def winfo_y(self):
358 return self.tk.getint(
359 self.tk.call('winfo', 'y', self._w))
360 def update(self):
361 self.tk.call('update')
362 def update_idletasks(self):
363 self.tk.call('update', 'idletasks')
364 def bindtags(self, tagList=None):
365 if tagList is None:
366 return self.tk.splitlist(
367 self.tk.call('bindtags', self._w))
368 else:
369 self.tk.call('bindtags', self._w, tagList)
370 def _bind(self, what, sequence, func, add):
371 if func:
372 cmd = ("%sset _tkinter_break [%s %s]\n"
373 'if {"$_tkinter_break" == "break"} break\n') \
374 % (add and '+' or '',
375 self._register(func, self._substitute),
376 _string.join(self._subst_format))
377 apply(self.tk.call, what + (sequence, cmd))
378 elif func == '':
379 apply(self.tk.call, what + (sequence, func))
380 else:
381 return apply(self.tk.call, what + (sequence,))
382 def bind(self, sequence=None, func=None, add=None):
383 return self._bind(('bind', self._w), sequence, func, add)
384 def unbind(self, sequence):
385 self.tk.call('bind', self._w, sequence, '')
386 def bind_all(self, sequence=None, func=None, add=None):
387 return self._bind(('bind', 'all'), sequence, func, add)
388 def unbind_all(self, sequence):
389 self.tk.call('bind', 'all' , sequence, '')
390 def bind_class(self, className, sequence=None, func=None, add=None):
391 self._bind(('bind', className), sequence, func, add)
392 def unbind_class(self, className, sequence):
393 self.tk.call('bind', className , sequence, '')
394 def mainloop(self, n=0):
395 self.tk.mainloop(n)
396 def quit(self):
397 self.tk.quit()
398 def _getints(self, string):
399 if not string: return None
400 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
401 def _getdoubles(self, string):
402 if not string: return None
403 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
404 def _getboolean(self, string):
405 if string:
406 return self.tk.getboolean(string)
407 def _options(self, cnf, kw = None):
408 if kw:
409 cnf = _cnfmerge((cnf, kw))
410 else:
411 cnf = _cnfmerge(cnf)
412 res = ()
413 for k, v in cnf.items():
414 if k[-1] == '_': k = k[:-1]
415 if callable(v):
416 v = self._register(v)
417 res = res + ('-'+k, v)
418 return res
419 def _nametowidget(self, name):
420 w = self
421 if name[0] == '.':
422 w = w._root()
423 name = name[1:]
424 find = _string.find
425 while name:
426 i = find(name, '.')
427 if i >= 0:
428 name, tail = name[:i], name[i+1:]
429 else:
430 tail = ''
431 w = w.children[name]
432 name = tail
433 return w
434 def _register(self, func, subst=None):
435 f = CallWrapper(func, subst, self).__call__
436 name = `id(f)`
437 try:
438 func = func.im_func
439 except AttributeError:
440 pass
441 try:
442 name = name + func.__name__
443 except AttributeError:
444 pass
445 self.tk.createcommand(name, f)
446 return name
447 register = _register
448 def _root(self):
449 w = self
450 while w.master: w = w.master
451 return w
452 _subst_format = ('%#', '%b', '%f', '%h', '%k',
453 '%s', '%t', '%w', '%x', '%y',
454 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
455 def _substitute(self, *args):
456 tk = self.tk
457 if len(args) != len(self._subst_format): return args
458 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
459 # Missing: (a, c, d, m, o, v, B, R)
460 e = Event()
461 e.serial = tk.getint(nsign)
462 e.num = tk.getint(b)
463 try: e.focus = tk.getboolean(f)
464 except TclError: pass
465 e.height = tk.getint(h)
466 e.keycode = tk.getint(k)
467 # For Visibility events, event state is a string and
468 # not an integer:
469 try:
470 e.state = tk.getint(s)
471 except TclError:
472 e.state = s
473 e.time = tk.getint(t)
474 e.width = tk.getint(w)
475 e.x = tk.getint(x)
476 e.y = tk.getint(y)
477 e.char = A
478 try: e.send_event = tk.getboolean(E)
479 except TclError: pass
480 e.keysym = K
481 e.keysym_num = tk.getint(N)
482 e.type = T
483 e.widget = self._nametowidget(W)
484 e.x_root = tk.getint(X)
485 e.y_root = tk.getint(Y)
486 return (e,)
487 def _report_exception(self):
488 import sys
489 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
490 root = self._root()
491 root.report_callback_exception(exc, val, tb)
493 class CallWrapper:
494 def __init__(self, func, subst, widget):
495 self.func = func
496 self.subst = subst
497 self.widget = widget
498 def __call__(self, *args):
499 try:
500 if self.subst:
501 args = apply(self.subst, args)
502 return apply(self.func, args)
503 except SystemExit, msg:
504 raise SystemExit, msg
505 except:
506 self.widget._report_exception()
508 class Wm:
509 def aspect(self,
510 minNumer=None, minDenom=None,
511 maxNumer=None, maxDenom=None):
512 return self._getints(
513 self.tk.call('wm', 'aspect', self._w,
514 minNumer, minDenom,
515 maxNumer, maxDenom))
516 def client(self, name=None):
517 return self.tk.call('wm', 'client', self._w, name)
518 def command(self, value=None):
519 return self.tk.call('wm', 'command', self._w, value)
520 def deiconify(self):
521 return self.tk.call('wm', 'deiconify', self._w)
522 def focusmodel(self, model=None):
523 return self.tk.call('wm', 'focusmodel', self._w, model)
524 def frame(self):
525 return self.tk.call('wm', 'frame', self._w)
526 def geometry(self, newGeometry=None):
527 return self.tk.call('wm', 'geometry', self._w, newGeometry)
528 def grid(self,
529 baseWidht=None, baseHeight=None,
530 widthInc=None, heightInc=None):
531 return self._getints(self.tk.call(
532 'wm', 'grid', self._w,
533 baseWidht, baseHeight, widthInc, heightInc))
534 def group(self, pathName=None):
535 return self.tk.call('wm', 'group', self._w, pathName)
536 def iconbitmap(self, bitmap=None):
537 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
538 def iconify(self):
539 return self.tk.call('wm', 'iconify', self._w)
540 def iconmask(self, bitmap=None):
541 return self.tk.call('wm', 'iconmask', self._w, bitmap)
542 def iconname(self, newName=None):
543 return self.tk.call('wm', 'iconname', self._w, newName)
544 def iconposition(self, x=None, y=None):
545 return self._getints(self.tk.call(
546 'wm', 'iconposition', self._w, x, y))
547 def iconwindow(self, pathName=None):
548 return self.tk.call('wm', 'iconwindow', self._w, pathName)
549 def maxsize(self, width=None, height=None):
550 return self._getints(self.tk.call(
551 'wm', 'maxsize', self._w, width, height))
552 def minsize(self, width=None, height=None):
553 return self._getints(self.tk.call(
554 'wm', 'minsize', self._w, width, height))
555 def overrideredirect(self, boolean=None):
556 return self._getboolean(self.tk.call(
557 'wm', 'overrideredirect', self._w, boolean))
558 def positionfrom(self, who=None):
559 return self.tk.call('wm', 'positionfrom', self._w, who)
560 def protocol(self, name=None, func=None):
561 if callable(func):
562 command = self._register(func)
563 else:
564 command = func
565 return self.tk.call(
566 'wm', 'protocol', self._w, name, command)
567 def resizable(self, width=None, height=None):
568 return self.tk.call('wm', 'resizable', self._w, width, height)
569 def sizefrom(self, who=None):
570 return self.tk.call('wm', 'sizefrom', self._w, who)
571 def state(self):
572 return self.tk.call('wm', 'state', self._w)
573 def title(self, string=None):
574 return self.tk.call('wm', 'title', self._w, string)
575 def transient(self, master=None):
576 return self.tk.call('wm', 'transient', self._w, master)
577 def withdraw(self):
578 return self.tk.call('wm', 'withdraw', self._w)
580 class Tk(Misc, Wm):
581 _w = '.'
582 def __init__(self, screenName=None, baseName=None, className='Tk'):
583 global _default_root
584 self.master = None
585 self.children = {}
586 if baseName is None:
587 import sys, os
588 baseName = os.path.basename(sys.argv[0])
589 if baseName[-3:] == '.py': baseName = baseName[:-3]
590 self.tk = tkinter.create(screenName, baseName, className)
591 try:
592 # Disable event scanning except for Command-Period
593 import MacOS
594 MacOS.EnableAppswitch(0)
595 except ImportError:
596 pass
597 else:
598 # Work around nasty MacTk bug
599 self.update()
600 # Version sanity checks
601 tk_version = self.tk.getvar('tk_version')
602 if tk_version != tkinter.TK_VERSION:
603 raise RuntimeError, \
604 "tk.h version (%s) doesn't match libtk.a version (%s)" \
605 % (tkinter.TK_VERSION, tk_version)
606 tcl_version = self.tk.getvar('tcl_version')
607 if tcl_version != tkinter.TCL_VERSION:
608 raise RuntimeError, \
609 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
610 % (tkinter.TCL_VERSION, tcl_version)
611 if TkVersion < 4.0:
612 raise RuntimeError, \
613 "Tk 4.0 or higher is required; found Tk %s" \
614 % str(TkVersion)
615 self.tk.createcommand('tkerror', _tkerror)
616 self.tk.createcommand('exit', _exit)
617 self.readprofile(baseName, className)
618 if not _default_root:
619 _default_root = self
620 def destroy(self):
621 for c in self.children.values(): c.destroy()
622 self.tk.call('destroy', self._w)
623 def __str__(self):
624 return self._w
625 def readprofile(self, baseName, className):
626 import os
627 if os.environ.has_key('HOME'): home = os.environ['HOME']
628 else: home = os.curdir
629 class_tcl = os.path.join(home, '.%s.tcl' % className)
630 class_py = os.path.join(home, '.%s.py' % className)
631 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
632 base_py = os.path.join(home, '.%s.py' % baseName)
633 dir = {'self': self}
634 exec 'from Tkinter import *' in dir
635 if os.path.isfile(class_tcl):
636 print 'source', `class_tcl`
637 self.tk.call('source', class_tcl)
638 if os.path.isfile(class_py):
639 print 'execfile', `class_py`
640 execfile(class_py, dir)
641 if os.path.isfile(base_tcl):
642 print 'source', `base_tcl`
643 self.tk.call('source', base_tcl)
644 if os.path.isfile(base_py):
645 print 'execfile', `base_py`
646 execfile(base_py, dir)
647 def report_callback_exception(self, exc, val, tb):
648 import traceback
649 print "Exception in Tkinter callback"
650 traceback.print_exception(exc, val, tb)
652 class Pack:
653 def config(self, cnf={}, **kw):
654 apply(self.tk.call,
655 ('pack', 'configure', self._w)
656 + self._options(cnf, kw))
657 configure = config
658 pack = config
659 def __setitem__(self, key, value):
660 Pack.config({key: value})
661 def forget(self):
662 self.tk.call('pack', 'forget', self._w)
663 pack_forget = forget
664 def info(self):
665 words = self.tk.splitlist(
666 self.tk.call('pack', 'info', self._w))
667 dict = {}
668 for i in range(0, len(words), 2):
669 key = words[i][1:]
670 value = words[i+1]
671 if value[:1] == '.':
672 value = self._nametowidget(value)
673 dict[key] = value
674 return dict
675 pack_info = info
676 _noarg_ = ['_noarg_']
677 def propagate(self, flag=_noarg_):
678 if flag is Pack._noarg_:
679 return self._getboolean(self.tk.call(
680 'pack', 'propagate', self._w))
681 else:
682 self.tk.call('pack', 'propagate', self._w, flag)
683 pack_propagate = propagate
684 def slaves(self):
685 return map(self._nametowidget,
686 self.tk.splitlist(
687 self.tk.call('pack', 'slaves', self._w)))
688 pack_slaves = slaves
690 class Place:
691 def config(self, cnf={}, **kw):
692 for k in ['in_']:
693 if kw.has_key(k):
694 kw[k[:-1]] = kw[k]
695 del kw[k]
696 apply(self.tk.call,
697 ('place', 'configure', self._w)
698 + self._options(cnf, kw))
699 configure = config
700 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 place_forget = forget
706 def info(self):
707 words = self.tk.splitlist(
708 self.tk.call('place', 'info', self._w))
709 dict = {}
710 for i in range(0, len(words), 2):
711 key = words[i][1:]
712 value = words[i+1]
713 if value[:1] == '.':
714 value = self._nametowidget(value)
715 dict[key] = value
716 return dict
717 place_info = info
718 def slaves(self):
719 return map(self._nametowidget,
720 self.tk.splitlist(
721 self.tk.call(
722 'place', 'slaves', self._w)))
723 place_slaves = slaves
725 class Grid:
726 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
727 def config(self, cnf={}, **kw):
728 apply(self.tk.call,
729 ('grid', 'configure', self._w)
730 + self._options(cnf, kw))
731 grid = config
732 def __setitem__(self, key, value):
733 Grid.config({key: value})
734 def bbox(self, column, row):
735 return self._getints(
736 self.tk.call(
737 'grid', 'bbox', self._w, column, row)) or None
738 grid_bbox = bbox
739 def columnconfigure(self, index, cnf={}, **kw):
740 if type(cnf) is not DictionaryType and not kw:
741 options = self._options({cnf: None})
742 else:
743 options = self._options(cnf, kw)
744 res = apply(self.tk.call,
745 ('grid', 'columnconfigure', self._w, index)
746 + options)
747 if options == ('-minsize', None):
748 return self.tk.getint(res) or None
749 elif options == ('-weight', None):
750 return self.tk.getdouble(res) or None
751 def forget(self):
752 self.tk.call('grid', 'forget', self._w)
753 grid_forget = forget
754 def info(self):
755 words = self.tk.splitlist(
756 self.tk.call('grid', 'info', self._w))
757 dict = {}
758 for i in range(0, len(words), 2):
759 key = words[i][1:]
760 value = words[i+1]
761 if value[:1] == '.':
762 value = self._nametowidget(value)
763 dict[key] = value
764 return dict
765 grid_info = info
766 def location(self, x, y):
767 return self._getints(
768 self.tk.call(
769 'grid', 'location', self._w, x, y)) or None
770 _noarg_ = ['_noarg_']
771 def propagate(self, flag=_noarg_):
772 if flag is Grid._noarg_:
773 return self._getboolean(self.tk.call(
774 'grid', 'propagate', self._w))
775 else:
776 self.tk.call('grid', 'propagate', self._w, flag)
777 grid_propagate = propagate
778 def rowconfigure(self, index, cnf={}, **kw):
779 if type(cnf) is not DictionaryType and not kw:
780 options = self._options({cnf: None})
781 else:
782 options = self._options(cnf, kw)
783 res = apply(self.tk.call,
784 ('grid', 'rowconfigure', self._w, index)
785 + options)
786 if options == ('-minsize', None):
787 return self.tk.getint(res) or None
788 elif options == ('-weight', None):
789 return self.tk.getdouble(res) or None
790 def size(self):
791 return self._getints(
792 self.tk.call('grid', 'size', self._w)) or None
793 def slaves(self, *args):
794 return map(self._nametowidget,
795 self.tk.splitlist(
796 apply(self.tk.call,
797 ('grid', 'slaves', self._w) + args)))
798 grid_slaves = slaves
800 class Widget(Misc, Pack, Place, Grid):
801 def _setup(self, master, cnf):
802 global _default_root
803 if not master:
804 if not _default_root:
805 _default_root = Tk()
806 master = _default_root
807 if not _default_root:
808 _default_root = master
809 self.master = master
810 self.tk = master.tk
811 if cnf.has_key('name'):
812 name = cnf['name']
813 del cnf['name']
814 else:
815 name = `id(self)`
816 self._name = name
817 if master._w=='.':
818 self._w = '.' + name
819 else:
820 self._w = master._w + '.' + name
821 self.children = {}
822 if self.master.children.has_key(self._name):
823 self.master.children[self._name].destroy()
824 self.master.children[self._name] = self
825 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
826 if kw:
827 cnf = _cnfmerge((cnf, kw))
828 self.widgetName = widgetName
829 Widget._setup(self, master, cnf)
830 apply(self.tk.call,
831 (widgetName, self._w) + extra + self._options(cnf))
832 def config(self, cnf=None, **kw):
833 # XXX ought to generalize this so tag_config etc. can use it
834 if kw:
835 cnf = _cnfmerge((cnf, kw))
836 elif cnf:
837 cnf = _cnfmerge(cnf)
838 if cnf is None:
839 cnf = {}
840 for x in self.tk.split(
841 self.tk.call(self._w, 'configure')):
842 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
843 return cnf
844 if type(cnf) is StringType:
845 x = self.tk.split(self.tk.call(
846 self._w, 'configure', '-'+cnf))
847 return (x[0][1:],) + x[1:]
848 for k in cnf.keys():
849 if type(k) is ClassType:
850 k.config(self, cnf[k])
851 del cnf[k]
852 apply(self.tk.call, (self._w, 'configure')
853 + self._options(cnf))
854 configure = config
855 def cget(self, key):
856 return self.tk.call(self._w, 'cget', '-' + key)
857 __getitem__ = cget
858 def __setitem__(self, key, value):
859 Widget.config(self, {key: value})
860 def keys(self):
861 return map(lambda x: x[0][1:],
862 self.tk.split(self.tk.call(self._w, 'configure')))
863 def __str__(self):
864 return self._w
865 def destroy(self):
866 for c in self.children.values(): c.destroy()
867 if self.master.children.has_key(self._name):
868 del self.master.children[self._name]
869 self.tk.call('destroy', self._w)
870 def _do(self, name, args=()):
871 return apply(self.tk.call, (self._w, name) + args)
873 class Toplevel(Widget, Wm):
874 def __init__(self, master=None, cnf={}, **kw):
875 if kw:
876 cnf = _cnfmerge((cnf, kw))
877 extra = ()
878 for wmkey in ['screen', 'class_', 'class', 'visual',
879 'colormap']:
880 if cnf.has_key(wmkey):
881 val = cnf[wmkey]
882 # TBD: a hack needed because some keys
883 # are not valid as keyword arguments
884 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
885 else: opt = '-'+wmkey
886 extra = extra + (opt, val)
887 del cnf[wmkey]
888 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
889 root = self._root()
890 self.iconname(root.iconname())
891 self.title(root.title())
893 class Button(Widget):
894 def __init__(self, master=None, cnf={}, **kw):
895 Widget.__init__(self, master, 'button', cnf, kw)
896 def tkButtonEnter(self, *dummy):
897 self.tk.call('tkButtonEnter', self._w)
898 def tkButtonLeave(self, *dummy):
899 self.tk.call('tkButtonLeave', self._w)
900 def tkButtonDown(self, *dummy):
901 self.tk.call('tkButtonDown', self._w)
902 def tkButtonUp(self, *dummy):
903 self.tk.call('tkButtonUp', self._w)
904 def tkButtonInvoke(self, *dummy):
905 self.tk.call('tkButtonInvoke', self._w)
906 def flash(self):
907 self.tk.call(self._w, 'flash')
908 def invoke(self):
909 self.tk.call(self._w, 'invoke')
911 # Indices:
912 # XXX I don't like these -- take them away
913 def AtEnd():
914 return 'end'
915 def AtInsert(*args):
916 s = 'insert'
917 for a in args:
918 if a: s = s + (' ' + a)
919 return s
920 def AtSelFirst():
921 return 'sel.first'
922 def AtSelLast():
923 return 'sel.last'
924 def At(x, y=None):
925 if y is None:
926 return '@' + `x`
927 else:
928 return '@' + `x` + ',' + `y`
930 class Canvas(Widget):
931 def __init__(self, master=None, cnf={}, **kw):
932 Widget.__init__(self, master, 'canvas', cnf, kw)
933 def addtag(self, *args):
934 self._do('addtag', args)
935 def addtag_above(self, tagOrId):
936 self.addtag('above', tagOrId)
937 def addtag_all(self):
938 self.addtag('all')
939 def addtag_below(self, tagOrId):
940 self.addtag('below', tagOrId)
941 def addtag_closest(self, x, y, halo=None, start=None):
942 self.addtag('closest', x, y, halo, start)
943 def addtag_enclosed(self, x1, y1, x2, y2):
944 self.addtag('enclosed', x1, y1, x2, y2)
945 def addtag_overlapping(self, x1, y1, x2, y2):
946 self.addtag('overlapping', x1, y1, x2, y2)
947 def addtag_withtag(self, tagOrId):
948 self.addtag('withtag', tagOrId)
949 def bbox(self, *args):
950 return self._getints(self._do('bbox', args)) or None
951 def tag_unbind(self, tagOrId, sequence):
952 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
953 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
954 return self._bind((self._w, 'tag', 'bind', tagOrId),
955 sequence, func, add)
956 def canvasx(self, screenx, gridspacing=None):
957 return self.tk.getdouble(self.tk.call(
958 self._w, 'canvasx', screenx, gridspacing))
959 def canvasy(self, screeny, gridspacing=None):
960 return self.tk.getdouble(self.tk.call(
961 self._w, 'canvasy', screeny, gridspacing))
962 def coords(self, *args):
963 return self._do('coords', args)
964 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
965 args = _flatten(args)
966 cnf = args[-1]
967 if type(cnf) in (DictionaryType, TupleType):
968 args = args[:-1]
969 else:
970 cnf = {}
971 return self.tk.getint(apply(
972 self.tk.call,
973 (self._w, 'create', itemType)
974 + args + self._options(cnf, kw)))
975 def create_arc(self, *args, **kw):
976 return self._create('arc', args, kw)
977 def create_bitmap(self, *args, **kw):
978 return self._create('bitmap', args, kw)
979 def create_image(self, *args, **kw):
980 return self._create('image', args, kw)
981 def create_line(self, *args, **kw):
982 return self._create('line', args, kw)
983 def create_oval(self, *args, **kw):
984 return self._create('oval', args, kw)
985 def create_polygon(self, *args, **kw):
986 return self._create('polygon', args, kw)
987 def create_rectangle(self, *args, **kw):
988 return self._create('rectangle', args, kw)
989 def create_text(self, *args, **kw):
990 return self._create('text', args, kw)
991 def create_window(self, *args, **kw):
992 return self._create('window', args, kw)
993 def dchars(self, *args):
994 self._do('dchars', args)
995 def delete(self, *args):
996 self._do('delete', args)
997 def dtag(self, *args):
998 self._do('dtag', args)
999 def find(self, *args):
1000 return self._getints(self._do('find', args)) or ()
1001 def find_above(self, tagOrId):
1002 return self.find('above', tagOrId)
1003 def find_all(self):
1004 return self.find('all')
1005 def find_below(self, tagOrId):
1006 return self.find('below', tagOrId)
1007 def find_closest(self, x, y, halo=None, start=None):
1008 return self.find('closest', x, y, halo, start)
1009 def find_enclosed(self, x1, y1, x2, y2):
1010 return self.find('enclosed', x1, y1, x2, y2)
1011 def find_overlapping(self, x1, y1, x2, y2):
1012 return self.find('overlapping', x1, y1, x2, y2)
1013 def find_withtag(self, tagOrId):
1014 return self.find('withtag', tagOrId)
1015 def focus(self, *args):
1016 return self._do('focus', args)
1017 def gettags(self, *args):
1018 return self.tk.splitlist(self._do('gettags', args))
1019 def icursor(self, *args):
1020 self._do('icursor', args)
1021 def index(self, *args):
1022 return self.tk.getint(self._do('index', args))
1023 def insert(self, *args):
1024 self._do('insert', args)
1025 def itemcget(self, tagOrId, option):
1026 return self._do('itemcget', (tagOrId, '-'+option))
1027 def itemconfig(self, tagOrId, cnf=None, **kw):
1028 if cnf is None and not kw:
1029 cnf = {}
1030 for x in self.tk.split(
1031 self._do('itemconfigure', (tagOrId))):
1032 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1033 return cnf
1034 if type(cnf) == StringType and not kw:
1035 x = self.tk.split(self._do('itemconfigure',
1036 (tagOrId, '-'+cnf,)))
1037 return (x[0][1:],) + x[1:]
1038 self._do('itemconfigure', (tagOrId,)
1039 + self._options(cnf, kw))
1040 itemconfigure = itemconfig
1041 def lower(self, *args):
1042 self._do('lower', args)
1043 def move(self, *args):
1044 self._do('move', args)
1045 def postscript(self, cnf={}, **kw):
1046 return self._do('postscript', self._options(cnf, kw))
1047 def tkraise(self, *args):
1048 self._do('raise', args)
1049 lift = tkraise
1050 def scale(self, *args):
1051 self._do('scale', args)
1052 def scan_mark(self, x, y):
1053 self.tk.call(self._w, 'scan', 'mark', x, y)
1054 def scan_dragto(self, x, y):
1055 self.tk.call(self._w, 'scan', 'dragto', x, y)
1056 def select_adjust(self, tagOrId, index):
1057 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
1058 def select_clear(self):
1059 self.tk.call(self._w, 'select', 'clear')
1060 def select_from(self, tagOrId, index):
1061 self.tk.call(self._w, 'select', 'set', tagOrId, index)
1062 def select_item(self):
1063 self.tk.call(self._w, 'select', 'item')
1064 def select_to(self, tagOrId, index):
1065 self.tk.call(self._w, 'select', 'to', tagOrId, index)
1066 def type(self, tagOrId):
1067 return self.tk.call(self._w, 'type', tagOrId) or None
1068 def xview(self, *args):
1069 if not args:
1070 return self._getdoubles(self.tk.call(self._w, 'xview'))
1071 apply(self.tk.call, (self._w, 'xview')+args)
1072 def yview(self, *args):
1073 if not args:
1074 return self._getdoubles(self.tk.call(self._w, 'yview'))
1075 apply(self.tk.call, (self._w, 'yview')+args)
1077 class Checkbutton(Widget):
1078 def __init__(self, master=None, cnf={}, **kw):
1079 Widget.__init__(self, master, 'checkbutton', cnf, kw)
1080 def deselect(self):
1081 self.tk.call(self._w, 'deselect')
1082 def flash(self):
1083 self.tk.call(self._w, 'flash')
1084 def invoke(self):
1085 self.tk.call(self._w, 'invoke')
1086 def select(self):
1087 self.tk.call(self._w, 'select')
1088 def toggle(self):
1089 self.tk.call(self._w, 'toggle')
1091 class Entry(Widget):
1092 def __init__(self, master=None, cnf={}, **kw):
1093 Widget.__init__(self, master, 'entry', cnf, kw)
1094 def delete(self, first, last=None):
1095 self.tk.call(self._w, 'delete', first, last)
1096 def get(self):
1097 return self.tk.call(self._w, 'get')
1098 def icursor(self, index):
1099 self.tk.call(self._w, 'icursor', index)
1100 def index(self, index):
1101 return self.tk.getint(self.tk.call(
1102 self._w, 'index', index))
1103 def insert(self, index, string):
1104 self.tk.call(self._w, 'insert', index, string)
1105 def scan_mark(self, x):
1106 self.tk.call(self._w, 'scan', 'mark', x)
1107 def scan_dragto(self, x):
1108 self.tk.call(self._w, 'scan', 'dragto', x)
1109 def selection_adjust(self, index):
1110 self.tk.call(self._w, 'selection', 'adjust', index)
1111 select_adjust = selection_adjust
1112 def selection_clear(self):
1113 self.tk.call(self._w, 'selection', 'clear')
1114 select_clear = selection_clear
1115 def selection_from(self, index):
1116 self.tk.call(self._w, 'selection', 'set', index)
1117 select_from = selection_from
1118 def selection_present(self):
1119 return self.tk.getboolean(
1120 self.tk.call(self._w, 'selection', 'present'))
1121 select_present = selection_present
1122 def selection_range(self, start, end):
1123 self.tk.call(self._w, 'selection', 'range', start, end)
1124 select_range = selection_range
1125 def selection_to(self, index):
1126 self.tk.call(self._w, 'selection', 'to', index)
1127 select_to = selection_to
1128 def xview(self, index):
1129 self.tk.call(self._w, 'xview', index)
1130 def xview_moveto(self, fraction):
1131 self.tk.call(self._w, 'xview', 'moveto', fraction)
1132 def xview_scroll(self, number, what):
1133 self.tk.call(self._w, 'xview', 'scroll', number, what)
1135 class Frame(Widget):
1136 def __init__(self, master=None, cnf={}, **kw):
1137 cnf = _cnfmerge((cnf, kw))
1138 extra = ()
1139 if cnf.has_key('class'):
1140 extra = ('-class', cnf['class'])
1141 del cnf['class']
1142 Widget.__init__(self, master, 'frame', cnf, {}, extra)
1144 class Label(Widget):
1145 def __init__(self, master=None, cnf={}, **kw):
1146 Widget.__init__(self, master, 'label', cnf, kw)
1148 class Listbox(Widget):
1149 def __init__(self, master=None, cnf={}, **kw):
1150 Widget.__init__(self, master, 'listbox', cnf, kw)
1151 def activate(self, index):
1152 self.tk.call(self._w, 'activate', index)
1153 def bbox(self, *args):
1154 return self._getints(self._do('bbox', args)) or None
1155 def curselection(self):
1156 # XXX Ought to apply self._getints()...
1157 return self.tk.splitlist(self.tk.call(
1158 self._w, 'curselection'))
1159 def delete(self, first, last=None):
1160 self.tk.call(self._w, 'delete', first, last)
1161 def get(self, first, last=None):
1162 if last:
1163 return self.tk.splitlist(self.tk.call(
1164 self._w, 'get', first, last))
1165 else:
1166 return self.tk.call(self._w, 'get', first)
1167 def insert(self, index, *elements):
1168 apply(self.tk.call,
1169 (self._w, 'insert', index) + elements)
1170 def nearest(self, y):
1171 return self.tk.getint(self.tk.call(
1172 self._w, 'nearest', y))
1173 def scan_mark(self, x, y):
1174 self.tk.call(self._w, 'scan', 'mark', x, y)
1175 def scan_dragto(self, x, y):
1176 self.tk.call(self._w, 'scan', 'dragto', x, y)
1177 def see(self, index):
1178 self.tk.call(self._w, 'see', index)
1179 def index(self, index):
1180 i = self.tk.call(self._w, 'index', index)
1181 if i == 'none': return None
1182 return self.tk.getint(i)
1183 def select_adjust(self, index):
1184 self.tk.call(self._w, 'select', 'adjust', index)
1185 def select_anchor(self, index):
1186 self.tk.call(self._w, 'selection', 'anchor', index)
1187 def select_clear(self, first, last=None):
1188 self.tk.call(self._w,
1189 'selection', 'clear', first, last)
1190 def select_includes(self, index):
1191 return self.tk.getboolean(self.tk.call(
1192 self._w, 'selection', 'includes', index))
1193 def select_set(self, first, last=None):
1194 self.tk.call(self._w, 'selection', 'set', first, last)
1195 def size(self):
1196 return self.tk.getint(self.tk.call(self._w, 'size'))
1197 def xview(self, *what):
1198 if not what:
1199 return self._getdoubles(self.tk.call(self._w, 'xview'))
1200 apply(self.tk.call, (self._w, 'xview')+what)
1201 def yview(self, *what):
1202 if not what:
1203 return self._getdoubles(self.tk.call(self._w, 'yview'))
1204 apply(self.tk.call, (self._w, 'yview')+what)
1206 class Menu(Widget):
1207 def __init__(self, master=None, cnf={}, **kw):
1208 Widget.__init__(self, master, 'menu', cnf, kw)
1209 def tk_bindForTraversal(self):
1210 self.tk.call('tk_bindForTraversal', self._w)
1211 def tk_mbPost(self):
1212 self.tk.call('tk_mbPost', self._w)
1213 def tk_mbUnpost(self):
1214 self.tk.call('tk_mbUnpost')
1215 def tk_traverseToMenu(self, char):
1216 self.tk.call('tk_traverseToMenu', self._w, char)
1217 def tk_traverseWithinMenu(self, char):
1218 self.tk.call('tk_traverseWithinMenu', self._w, char)
1219 def tk_getMenuButtons(self):
1220 return self.tk.call('tk_getMenuButtons', self._w)
1221 def tk_nextMenu(self, count):
1222 self.tk.call('tk_nextMenu', count)
1223 def tk_nextMenuEntry(self, count):
1224 self.tk.call('tk_nextMenuEntry', count)
1225 def tk_invokeMenu(self):
1226 self.tk.call('tk_invokeMenu', self._w)
1227 def tk_firstMenu(self):
1228 self.tk.call('tk_firstMenu', self._w)
1229 def tk_mbButtonDown(self):
1230 self.tk.call('tk_mbButtonDown', self._w)
1231 def tk_popup(self, x, y, entry=""):
1232 self.tk.call('tk_popup', self._w, x, y, entry)
1233 def activate(self, index):
1234 self.tk.call(self._w, 'activate', index)
1235 def add(self, itemType, cnf={}, **kw):
1236 apply(self.tk.call, (self._w, 'add', itemType)
1237 + self._options(cnf, kw))
1238 def add_cascade(self, cnf={}, **kw):
1239 self.add('cascade', cnf or kw)
1240 def add_checkbutton(self, cnf={}, **kw):
1241 self.add('checkbutton', cnf or kw)
1242 def add_command(self, cnf={}, **kw):
1243 self.add('command', cnf or kw)
1244 def add_radiobutton(self, cnf={}, **kw):
1245 self.add('radiobutton', cnf or kw)
1246 def add_separator(self, cnf={}, **kw):
1247 self.add('separator', cnf or kw)
1248 def delete(self, index1, index2=None):
1249 self.tk.call(self._w, 'delete', index1, index2)
1250 def entryconfig(self, index, cnf=None, **kw):
1251 if cnf is None and not kw:
1252 cnf = {}
1253 for x in self.tk.split(apply(self.tk.call,
1254 (self._w, 'entryconfigure', index))):
1255 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1256 return cnf
1257 if type(cnf) == StringType and not kw:
1258 x = self.tk.split(apply(self.tk.call,
1259 (self._w, 'entryconfigure', index, '-'+cnf)))
1260 return (x[0][1:],) + x[1:]
1261 apply(self.tk.call, (self._w, 'entryconfigure', index)
1262 + self._options(cnf, kw))
1263 entryconfigure = entryconfig
1264 def index(self, index):
1265 i = self.tk.call(self._w, 'index', index)
1266 if i == 'none': return None
1267 return self.tk.getint(i)
1268 def invoke(self, index):
1269 return self.tk.call(self._w, 'invoke', index)
1270 def post(self, x, y):
1271 self.tk.call(self._w, 'post', x, y)
1272 def unpost(self):
1273 self.tk.call(self._w, 'unpost')
1274 def yposition(self, index):
1275 return self.tk.getint(self.tk.call(
1276 self._w, 'yposition', index))
1278 class Menubutton(Widget):
1279 def __init__(self, master=None, cnf={}, **kw):
1280 Widget.__init__(self, master, 'menubutton', cnf, kw)
1282 class Message(Widget):
1283 def __init__(self, master=None, cnf={}, **kw):
1284 Widget.__init__(self, master, 'message', cnf, kw)
1286 class Radiobutton(Widget):
1287 def __init__(self, master=None, cnf={}, **kw):
1288 Widget.__init__(self, master, 'radiobutton', cnf, kw)
1289 def deselect(self):
1290 self.tk.call(self._w, 'deselect')
1291 def flash(self):
1292 self.tk.call(self._w, 'flash')
1293 def invoke(self):
1294 self.tk.call(self._w, 'invoke')
1295 def select(self):
1296 self.tk.call(self._w, 'select')
1298 class Scale(Widget):
1299 def __init__(self, master=None, cnf={}, **kw):
1300 Widget.__init__(self, master, 'scale', cnf, kw)
1301 def get(self):
1302 return self.tk.getint(self.tk.call(self._w, 'get'))
1303 def set(self, value):
1304 self.tk.call(self._w, 'set', value)
1306 class Scrollbar(Widget):
1307 def __init__(self, master=None, cnf={}, **kw):
1308 Widget.__init__(self, master, 'scrollbar', cnf, kw)
1309 def activate(self, index):
1310 self.tk.call(self._w, 'activate', index)
1311 def delta(self, deltax, deltay):
1312 return self.getdouble(self.tk.call(
1313 self._w, 'delta', deltax, deltay))
1314 def fraction(self, x, y):
1315 return self.getdouble(self.tk.call(
1316 self._w, 'fraction', x, y))
1317 def identify(self, x, y):
1318 return self.tk.call(self._w, 'identify', x, y)
1319 def get(self):
1320 return self._getdoubles(self.tk.call(self._w, 'get'))
1321 def set(self, *args):
1322 apply(self.tk.call, (self._w, 'set')+args)
1324 class Text(Widget):
1325 def __init__(self, master=None, cnf={}, **kw):
1326 Widget.__init__(self, master, 'text', cnf, kw)
1327 self.bind('<Delete>', self.bspace)
1328 def bbox(self, *args):
1329 return self._getints(self._do('bbox', args)) or None
1330 def bspace(self, *args):
1331 self.delete('insert')
1332 def tk_textSelectTo(self, index):
1333 self.tk.call('tk_textSelectTo', self._w, index)
1334 def tk_textBackspace(self):
1335 self.tk.call('tk_textBackspace', self._w)
1336 def tk_textIndexCloser(self, a, b, c):
1337 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1338 def tk_textResetAnchor(self, index):
1339 self.tk.call('tk_textResetAnchor', self._w, index)
1340 def compare(self, index1, op, index2):
1341 return self.tk.getboolean(self.tk.call(
1342 self._w, 'compare', index1, op, index2))
1343 def debug(self, boolean=None):
1344 return self.tk.getboolean(self.tk.call(
1345 self._w, 'debug', boolean))
1346 def delete(self, index1, index2=None):
1347 self.tk.call(self._w, 'delete', index1, index2)
1348 def dlineinfo(self, index):
1349 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
1350 def get(self, index1, index2=None):
1351 return self.tk.call(self._w, 'get', index1, index2)
1352 def index(self, index):
1353 return self.tk.call(self._w, 'index', index)
1354 def insert(self, index, chars, *args):
1355 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
1356 def mark_gravity(self, markName, direction=None):
1357 return apply(self.tk.call,
1358 (self._w, 'mark', 'gravity', markName, direction))
1359 def mark_names(self):
1360 return self.tk.splitlist(self.tk.call(
1361 self._w, 'mark', 'names'))
1362 def mark_set(self, markName, index):
1363 self.tk.call(self._w, 'mark', 'set', markName, index)
1364 def mark_unset(self, *markNames):
1365 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1366 def scan_mark(self, x, y):
1367 self.tk.call(self._w, 'scan', 'mark', x, y)
1368 def scan_dragto(self, x, y):
1369 self.tk.call(self._w, 'scan', 'dragto', x, y)
1370 def search(self, pattern, index, stopindex=None,
1371 forwards=None, backwards=None, exact=None,
1372 regexp=None, nocase=None, count=None):
1373 args = [self._w, 'search']
1374 if forwards: args.append('-forwards')
1375 if backwards: args.append('-backwards')
1376 if exact: args.append('-exact')
1377 if regexp: args.append('-regexp')
1378 if nocase: args.append('-nocase')
1379 if count: args.append('-count'); args.append(count)
1380 if pattern[0] == '-': args.append('--')
1381 args.append(pattern)
1382 args.append(index)
1383 if stopindex: args.append(stopindex)
1384 return apply(self.tk.call, tuple(args))
1385 def see(self, index):
1386 self.tk.call(self._w, 'see', index)
1387 def tag_add(self, tagName, index1, index2=None):
1388 self.tk.call(
1389 self._w, 'tag', 'add', tagName, index1, index2)
1390 def tag_unbind(self, tagName, sequence):
1391 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
1392 def tag_bind(self, tagName, sequence, func, add=None):
1393 return self._bind((self._w, 'tag', 'bind', tagName),
1394 sequence, func, add)
1395 def tag_cget(self, tagName, option):
1396 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
1397 def tag_config(self, tagName, cnf={}, **kw):
1398 if type(cnf) == StringType:
1399 x = self.tk.split(self.tk.call(
1400 self._w, 'tag', 'configure', tagName, '-'+cnf))
1401 return (x[0][1:],) + x[1:]
1402 apply(self.tk.call,
1403 (self._w, 'tag', 'configure', tagName)
1404 + self._options(cnf, kw))
1405 tag_configure = tag_config
1406 def tag_delete(self, *tagNames):
1407 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
1408 def tag_lower(self, tagName, belowThis=None):
1409 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
1410 def tag_names(self, index=None):
1411 return self.tk.splitlist(
1412 self.tk.call(self._w, 'tag', 'names', index))
1413 def tag_nextrange(self, tagName, index1, index2=None):
1414 return self.tk.splitlist(self.tk.call(
1415 self._w, 'tag', 'nextrange', tagName, index1, index2))
1416 def tag_raise(self, tagName, aboveThis=None):
1417 self.tk.call(
1418 self._w, 'tag', 'raise', tagName, aboveThis)
1419 def tag_ranges(self, tagName):
1420 return self.tk.splitlist(self.tk.call(
1421 self._w, 'tag', 'ranges', tagName))
1422 def tag_remove(self, tagName, index1, index2=None):
1423 self.tk.call(
1424 self._w, 'tag', 'remove', tagName, index1, index2)
1425 def window_cget(self, index, option):
1426 return self.tk.call(self._w, 'window', 'cget', index, option)
1427 def window_config(self, index, cnf={}, **kw):
1428 if type(cnf) == StringType:
1429 x = self.tk.split(self.tk.call(
1430 self._w, 'window', 'configure',
1431 index, '-'+cnf))
1432 return (x[0][1:],) + x[1:]
1433 apply(self.tk.call,
1434 (self._w, 'window', 'configure', index)
1435 + self._options(cnf, kw))
1436 window_configure = window_config
1437 def window_create(self, index, cnf={}, **kw):
1438 apply(self.tk.call,
1439 (self._w, 'window', 'create', index)
1440 + self._options(cnf, kw))
1441 def window_names(self):
1442 return self.tk.splitlist(
1443 self.tk.call(self._w, 'window', 'names'))
1444 def xview(self, *what):
1445 if not what:
1446 return self._getdoubles(self.tk.call(self._w, 'xview'))
1447 apply(self.tk.call, (self._w, 'xview')+what)
1448 def yview(self, *what):
1449 if not what:
1450 return self._getdoubles(self.tk.call(self._w, 'yview'))
1451 apply(self.tk.call, (self._w, 'yview')+what)
1452 def yview_pickplace(self, *what):
1453 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
1455 class OptionMenu(Widget):
1456 def __init__(self, master, variable, value, *values):
1457 self.widgetName = 'tk_optionMenu'
1458 Widget._setup(self, master, {})
1459 self.menuname = apply(
1460 self.tk.call,
1461 (self.widgetName, self._w, variable, value) + values)
1463 class Image:
1464 def __init__(self, imgtype, name=None, cnf={}, **kw):
1465 self.name = None
1466 master = _default_root
1467 if not master: raise RuntimeError, 'Too early to create image'
1468 self.tk = master.tk
1469 if not name: name = `id(self)`
1470 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1471 elif kw: cnf = kw
1472 options = ()
1473 for k, v in cnf.items():
1474 if callable(v):
1475 v = self._register(v)
1476 options = options + ('-'+k, v)
1477 apply(self.tk.call,
1478 ('image', 'create', imgtype, name,) + options)
1479 self.name = name
1480 def __str__(self): return self.name
1481 def __del__(self):
1482 if self.name:
1483 self.tk.call('image', 'delete', self.name)
1484 def __setitem__(self, key, value):
1485 self.tk.call(self.name, 'configure', '-'+key, value)
1486 def __getitem__(self, key):
1487 return self.tk.call(self.name, 'configure', '-'+key)
1488 def height(self):
1489 return self.tk.getint(
1490 self.tk.call('image', 'height', self.name))
1491 def type(self):
1492 return self.tk.call('image', 'type', self.name)
1493 def width(self):
1494 return self.tk.getint(
1495 self.tk.call('image', 'width', self.name))
1497 class PhotoImage(Image):
1498 def __init__(self, name=None, cnf={}, **kw):
1499 apply(Image.__init__, (self, 'photo', name, cnf), kw)
1500 def blank(self):
1501 self.tk.call(self.name, 'blank')
1502 def cget(self, option):
1503 return self.tk.call(self.name, 'cget', '-' + option)
1504 # XXX config
1505 def __getitem__(self, key):
1506 return self.tk.call(self.name, 'cget', '-' + key)
1507 def copy(self):
1508 destImage = PhotoImage()
1509 self.tk.call(destImage, 'copy', self.name)
1510 return destImage
1511 def zoom(self,x,y=''):
1512 destImage = PhotoImage()
1513 if y=='': y=x
1514 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1515 return destImage
1516 def subsample(self,x,y=''):
1517 destImage = PhotoImage()
1518 if y=='': y=x
1519 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1520 return destImage
1521 def get(self, x, y):
1522 return self.tk.call(self.name, 'get', x, y)
1523 def put(self, data, to=None):
1524 args = (self.name, 'put', data)
1525 if to:
1526 args = args + to
1527 apply(self.tk.call, args)
1528 # XXX read
1529 def write(self, filename, format=None, from_coords=None):
1530 args = (self.name, 'write', filename)
1531 if format:
1532 args = args + ('-format', format)
1533 if from_coords:
1534 args = args + ('-from',) + tuple(from_coords)
1535 apply(self.tk.call, args)
1537 class BitmapImage(Image):
1538 def __init__(self, name=None, cnf={}, **kw):
1539 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1541 def image_names(): return _default_root.tk.call('image', 'names')
1542 def image_types(): return _default_root.tk.call('image', 'types')
1544 ######################################################################
1545 # Extensions:
1547 class Studbutton(Button):
1548 def __init__(self, master=None, cnf={}, **kw):
1549 Widget.__init__(self, master, 'studbutton', cnf, kw)
1550 self.bind('<Any-Enter>', self.tkButtonEnter)
1551 self.bind('<Any-Leave>', self.tkButtonLeave)
1552 self.bind('<1>', self.tkButtonDown)
1553 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1555 class Tributton(Button):
1556 def __init__(self, master=None, cnf={}, **kw):
1557 Widget.__init__(self, master, 'tributton', cnf, kw)
1558 self.bind('<Any-Enter>', self.tkButtonEnter)
1559 self.bind('<Any-Leave>', self.tkButtonLeave)
1560 self.bind('<1>', self.tkButtonDown)
1561 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1562 self['fg'] = self['bg']
1563 self['activebackground'] = self['bg']
1566 # Emacs cruft
1567 # Local Variables:
1568 # py-indent-offset: 8
1569 # End: