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