blob: f97ee278c9ab306edf228a702862142a812e3bbb [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum18468821994-06-20 07:49:28 +00003import tkinter
4from tkinter import TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00005from types import *
Guido van Rossum18468821994-06-20 07:49:28 +00006
Guido van Rossum761c5ab1995-07-14 15:29:10 +00007CallableTypes = (FunctionType, MethodType,
8 BuiltinFunctionType, BuiltinMethodType)
9
10TkVersion = eval(tkinter.TK_VERSION)
11TclVersion = eval(tkinter.TCL_VERSION)
12if TkVersion < 4.0:
13 raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
Guido van Rossum18468821994-06-20 07:49:28 +000014
Guido van Rossum35f67fb1995-08-04 03:50:29 +000015# Symbolic constants
16
17# Booleans
18NO=FALSE=OFF=0
19YES=TRUE=ON=1
20
21# -anchor
22N='n'
23S='s'
24W='w'
25E='e'
26NW='nw'
27SW='sw'
28NE='ne'
29SE='se'
30CENTER='center'
31
32# -fill
33NONE='none'
34X='x'
35Y='y'
36BOTH='both'
37
38# -side
39LEFT='left'
40TOP='top'
41RIGHT='right'
42BOTTOM='bottom'
43
44# -relief
45RAISED='raised'
46SUNKEN='sunken'
47FLAT='flat'
48RIDGE='ridge'
49GROOVE='groove'
50
51# -orient
52HORIZONTAL='horizontal'
53VERTICAL='vertical'
54
Guido van Rossuma22a70a1995-08-04 03:51:48 +000055# -tabs
56NUMERIC='numeric'
57
58# -wrap
59CHAR='char'
60WORD='word'
61
62# -align
63BASELINE='baseline'
64
65# Special tags, marks and insert positions
66SEL='sel'
67SEL_FIRST='sel.first'
68SEL_LAST='sel.last'
69END='end'
70INSERT='insert'
71CURRENT='current'
72
Guido van Rossum2dcf5291994-07-06 09:23:20 +000073def _flatten(tuple):
74 res = ()
75 for item in tuple:
76 if type(item) in (TupleType, ListType):
77 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000078 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000079 res = res + (item,)
80 return res
81
82def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000083 if type(cnfs) is DictionaryType:
84 _fixgeometry(cnfs)
85 return cnfs
86 elif type(cnfs) in (NoneType, StringType):
87
Guido van Rossum2dcf5291994-07-06 09:23:20 +000088 return cnfs
89 else:
90 cnf = {}
91 for c in _flatten(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000092 _fixgeometry(c)
Guido van Rossum2dcf5291994-07-06 09:23:20 +000093 for k, v in c.items():
94 cnf[k] = v
95 return cnf
96
Guido van Rossum761c5ab1995-07-14 15:29:10 +000097if TkVersion >= 4.0:
98 _fixg_warning = "Warning: patched up pre-Tk-4.0 geometry option\n"
99 def _fixgeometry(c):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000100 if c and c.has_key('geometry'):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000101 wh = _parsegeometry(c['geometry'])
102 if wh:
103 # Print warning message -- once
104 global _fixg_warning
105 if _fixg_warning:
106 import sys
107 sys.stderr.write(_fixg_warning)
108 _fixg_warning = None
109 w, h = wh
110 c['width'] = w
111 c['height'] = h
112 del c['geometry']
113 def _parsegeometry(s):
114 from string import splitfields
115 fields = splitfields(s, 'x')
116 if len(fields) == 2:
117 return tuple(fields)
118 # else: return None
119else:
120 def _fixgeometry(c): pass
121
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000122class Event:
123 pass
124
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000125_default_root = None
126
Guido van Rossum45853db1994-06-20 12:19:19 +0000127def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +0000128 pass
129
Guido van Rossum97aeca11994-07-07 13:12:12 +0000130def _exit(code='0'):
131 import sys
132 sys.exit(getint(code))
133
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000134_varnum = 0
135class Variable:
136 def __init__(self, master=None):
137 global _default_root
138 global _varnum
139 if master:
140 self._tk = master.tk
141 else:
142 self._tk = _default_root.tk
143 self._name = 'PY_VAR' + `_varnum`
144 _varnum = _varnum + 1
145 def __del__(self):
146 self._tk.unsetvar(self._name)
147 def __str__(self):
148 return self._name
149 def __call__(self, value=None):
150 if value == None:
151 return self.get()
152 else:
153 self.set(value)
154 def set(self, value):
155 return self._tk.setvar(self._name, value)
156
157class StringVar(Variable):
158 def __init__(self, master=None):
159 Variable.__init__(self, master)
160 def get(self):
161 return self._tk.getvar(self._name)
162
163class IntVar(Variable):
164 def __init__(self, master=None):
165 Variable.__init__(self, master)
166 def get(self):
167 return self._tk.getint(self._tk.getvar(self._name))
168
169class DoubleVar(Variable):
170 def __init__(self, master=None):
171 Variable.__init__(self, master)
172 def get(self):
173 return self._tk.getdouble(self._tk.getvar(self._name))
174
175class BooleanVar(Variable):
176 def __init__(self, master=None):
177 Variable.__init__(self, master)
178 def get(self):
179 return self._tk.getboolean(self._tk.getvar(self._name))
180
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000181def mainloop(n=0):
182 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000183
184def getint(s):
185 return _default_root.tk.getint(s)
186
187def getdouble(s):
188 return _default_root.tk.getdouble(s)
189
190def getboolean(s):
191 return _default_root.tk.getboolean(s)
192
Guido van Rossum18468821994-06-20 07:49:28 +0000193class Misc:
194 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000195 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000196 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000197 def tk_menuBar(self, *args):
198 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000199 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000200 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000201 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000202 def wait_window(self, window=None):
203 if window == None:
204 window = self
205 self.tk.call('tkwait', 'window', window._w)
206 def wait_visibility(self, window=None):
207 if window == None:
208 window = self
209 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000210 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000211 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000212 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000213 return self.tk.getvar(name)
214 def getint(self, s):
215 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000216 def getdouble(self, s):
217 return self.tk.getdouble(s)
218 def getboolean(self, s):
219 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000220 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000221 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000222 focus = focus_set # XXX b/w compat?
223 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000224 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000225 def focus_default_none(self):
226 self.tk.call('focus', 'default', 'none')
227 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000228 def focus_none(self):
229 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000230 def focus_get(self):
231 name = self.tk.call('focus')
232 if name == 'none': return None
233 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000234 def after(self, ms, func=None, *args):
235 if not func:
236 self.tk.call('after', ms)
237 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000238 # XXX Disgusting hack to clean up after calling func
239 tmp = []
240 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
241 try:
242 apply(func, args)
243 finally:
244 tk.deletecommand(tmp[0])
245 name = self._register(callit)
246 tmp.append(name)
247 self.tk.call('after', ms, name)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000248 def bell(self, displayof=None):
249 if displayof:
250 self.tk.call('bell', '-displayof', displayof)
251 else:
252 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000253 # XXX grab current w/o window argument
254 def grab_current(self):
255 name = self.tk.call('grab', 'current', self._w)
256 if not name: return None
257 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000258 def grab_release(self):
259 self.tk.call('grab', 'release', self._w)
260 def grab_set(self):
261 self.tk.call('grab', 'set', self._w)
262 def grab_set_global(self):
263 self.tk.call('grab', 'set', '-global', self._w)
264 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000265 status = self.tk.call('grab', 'status', self._w)
266 if status == 'none': status = None
267 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000268 def lower(self, belowThis=None):
269 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000270 def option_add(self, pattern, value, priority = None):
271 self.tk.call('option', 'add', pattern, priority)
272 def option_clear(self):
273 self.tk.call('option', 'clear')
274 def option_get(self, name, className):
275 return self.tk.call('option', 'get', self._w, name, className)
276 def option_readfile(self, fileName, priority = None):
277 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000278 def selection_clear(self):
279 self.tk.call('selection', 'clear', self._w)
280 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000281 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000282 def selection_handle(self, func, type=None, format=None):
283 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000284 self.tk.call('selection', 'handle', self._w,
285 name, type, format)
286 def selection_own(self, func=None):
287 name = self._register(func)
288 self.tk.call('selection', 'own', self._w, name)
289 def selection_own_get(self):
290 return self._nametowidget(self.tk.call('selection', 'own'))
291 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000292 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000293 def lower(self, belowThis=None):
294 self.tk.call('lift', self._w, belowThis)
295 def tkraise(self, aboveThis=None):
296 self.tk.call('raise', self._w, aboveThis)
297 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000298 def colormodel(self, value=None):
299 return self.tk.call('tk', 'colormodel', self._w, value)
300 def winfo_atom(self, name):
301 return self.tk.getint(self.tk.call('winfo', 'atom', name))
302 def winfo_atomname(self, id):
303 return self.tk.call('winfo', 'atomname', id)
304 def winfo_cells(self):
305 return self.tk.getint(
306 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000307 def winfo_children(self):
308 return map(self._nametowidget,
309 self.tk.splitlist(self.tk.call(
310 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000311 def winfo_class(self):
312 return self.tk.call('winfo', 'class', self._w)
313 def winfo_containing(self, rootX, rootY):
314 return self.tk.call('winfo', 'containing', rootx, rootY)
315 def winfo_depth(self):
316 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
317 def winfo_exists(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'exists', self._w))
320 def winfo_fpixels(self, number):
321 return self.tk.getdouble(self.tk.call(
322 'winfo', 'fpixels', self._w, number))
323 def winfo_geometry(self):
324 return self.tk.call('winfo', 'geometry', self._w)
325 def winfo_height(self):
326 return self.tk.getint(
327 self.tk.call('winfo', 'height', self._w))
328 def winfo_id(self):
329 return self.tk.getint(
330 self.tk.call('winfo', 'id', self._w))
331 def winfo_interps(self):
332 return self.tk.splitlist(
333 self.tk.call('winfo', 'interps'))
334 def winfo_ismapped(self):
335 return self.tk.getint(
336 self.tk.call('winfo', 'ismapped', self._w))
337 def winfo_name(self):
338 return self.tk.call('winfo', 'name', self._w)
339 def winfo_parent(self):
340 return self.tk.call('winfo', 'parent', self._w)
341 def winfo_pathname(self, id):
342 return self.tk.call('winfo', 'pathname', id)
343 def winfo_pixels(self, number):
344 return self.tk.getint(
345 self.tk.call('winfo', 'pixels', self._w, number))
346 def winfo_reqheight(self):
347 return self.tk.getint(
348 self.tk.call('winfo', 'reqheight', self._w))
349 def winfo_reqwidth(self):
350 return self.tk.getint(
351 self.tk.call('winfo', 'reqwidth', self._w))
352 def winfo_rgb(self, color):
353 return self._getints(
354 self.tk.call('winfo', 'rgb', self._w, color))
355 def winfo_rootx(self):
356 return self.tk.getint(
357 self.tk.call('winfo', 'rootx', self._w))
358 def winfo_rooty(self):
359 return self.tk.getint(
360 self.tk.call('winfo', 'rooty', self._w))
361 def winfo_screen(self):
362 return self.tk.call('winfo', 'screen', self._w)
363 def winfo_screencells(self):
364 return self.tk.getint(
365 self.tk.call('winfo', 'screencells', self._w))
366 def winfo_screendepth(self):
367 return self.tk.getint(
368 self.tk.call('winfo', 'screendepth', self._w))
369 def winfo_screenheight(self):
370 return self.tk.getint(
371 self.tk.call('winfo', 'screenheight', self._w))
372 def winfo_screenmmheight(self):
373 return self.tk.getint(
374 self.tk.call('winfo', 'screenmmheight', self._w))
375 def winfo_screenmmwidth(self):
376 return self.tk.getint(
377 self.tk.call('winfo', 'screenmmwidth', self._w))
378 def winfo_screenvisual(self):
379 return self.tk.call('winfo', 'screenvisual', self._w)
380 def winfo_screenwidth(self):
381 return self.tk.getint(
382 self.tk.call('winfo', 'screenwidth', self._w))
383 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000384 return self._nametowidget(self.tk.call(
385 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000386 def winfo_visual(self):
387 return self.tk.call('winfo', 'visual', self._w)
388 def winfo_vrootheight(self):
389 return self.tk.getint(
390 self.tk.call('winfo', 'vrootheight', self._w))
391 def winfo_vrootwidth(self):
392 return self.tk.getint(
393 self.tk.call('winfo', 'vrootwidth', self._w))
394 def winfo_vrootx(self):
395 return self.tk.getint(
396 self.tk.call('winfo', 'vrootx', self._w))
397 def winfo_vrooty(self):
398 return self.tk.getint(
399 self.tk.call('winfo', 'vrooty', self._w))
400 def winfo_width(self):
401 return self.tk.getint(
402 self.tk.call('winfo', 'width', self._w))
403 def winfo_x(self):
404 return self.tk.getint(
405 self.tk.call('winfo', 'x', self._w))
406 def winfo_y(self):
407 return self.tk.getint(
408 self.tk.call('winfo', 'y', self._w))
409 def update(self):
410 self.tk.call('update')
411 def update_idletasks(self):
412 self.tk.call('update', 'idletasks')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000413 def bind(self, sequence, func=None, add=''):
414 if add: add = '+'
415 if func:
416 name = self._register(func, self._substitute)
417 self.tk.call('bind', self._w, sequence,
418 (add + name,) + self._subst_format)
419 else:
420 return self.tk.call('bind', self._w, sequence)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000421 def unbind(self, sequence):
422 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000423 def bind_all(self, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000424 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000425 if func:
426 name = self._register(func, self._substitute)
427 self.tk.call('bind', 'all' , sequence,
428 (add + name,) + self._subst_format)
429 else:
430 return self.tk.call('bind', 'all', sequence)
431 def unbind_all(self, sequence):
432 self.tk.call('bind', 'all' , sequence, '')
433 def bind_class(self, className, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000434 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000435 if func:
436 name = self._register(func, self._substitute)
437 self.tk.call('bind', className , sequence,
438 (add + name,) + self._subst_format)
439 else:
440 return self.tk.call('bind', className, sequence)
441 def unbind_class(self, className, sequence):
442 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000443 def mainloop(self, n=0):
444 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000445 def quit(self):
446 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000447 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000448 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000449 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
450 def _getdoubles(self, string):
451 if not string: return None
452 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000453 def _getboolean(self, string):
454 if string:
455 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000456 def _options(self, cnf, kw = None):
457 if kw:
458 cnf = _cnfmerge((cnf, kw))
459 else:
460 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000461 res = ()
462 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000463 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000464 v = self._register(v)
465 res = res + ('-'+k, v)
466 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000467 def _nametowidget(self, name):
468 w = self
469 if name[0] == '.':
470 w = w._root()
471 name = name[1:]
472 from string import find
473 while name:
474 i = find(name, '.')
475 if i >= 0:
476 name, tail = name[:i], name[i+1:]
477 else:
478 tail = ''
479 w = w.children[name]
480 name = tail
481 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000482 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000483 f = _CallSafely(func, subst).__call__
484 name = `id(f)`
485 if hasattr(func, 'im_func'):
486 func = func.im_func
487 if hasattr(func, 'func_name') and \
488 type(func.func_name) == type(''):
489 name = name + func.func_name
490 self.tk.createcommand(name, f)
491 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000492 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000493 def _root(self):
494 w = self
495 while w.master: w = w.master
496 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000497 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000498 '%s', '%t', '%w', '%x', '%y',
499 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
500 def _substitute(self, *args):
501 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000502 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000503 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
504 # Missing: (a, c, d, m, o, v, B, R)
505 e = Event()
506 e.serial = tk.getint(nsign)
507 e.num = tk.getint(b)
508 try: e.focus = tk.getboolean(f)
509 except TclError: pass
510 e.height = tk.getint(h)
511 e.keycode = tk.getint(k)
512 e.state = tk.getint(s)
513 e.time = tk.getint(t)
514 e.width = tk.getint(w)
515 e.x = tk.getint(x)
516 e.y = tk.getint(y)
517 e.char = A
518 try: e.send_event = tk.getboolean(E)
519 except TclError: pass
520 e.keysym = K
521 e.keysym_num = tk.getint(N)
522 e.type = T
523 e.widget = self._nametowidget(W)
524 e.x_root = tk.getint(X)
525 e.y_root = tk.getint(Y)
526 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000527
528class _CallSafely:
529 def __init__(self, func, subst=None):
530 self.func = func
531 self.subst = subst
532 def __call__(self, *args):
533 if self.subst:
534 args = self.apply_func(self.subst, args)
535 args = self.apply_func(self.func, args)
536 def apply_func(self, func, args):
537 import sys
538 try:
539 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000540 except SystemExit, msg:
541 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000542 except:
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000543 import traceback
544 print "Exception in Tkinter callback"
545 traceback.print_exc()
Guido van Rossum18468821994-06-20 07:49:28 +0000546
547class Wm:
548 def aspect(self,
549 minNumer=None, minDenom=None,
550 maxNumer=None, maxDenom=None):
551 return self._getints(
552 self.tk.call('wm', 'aspect', self._w,
553 minNumer, minDenom,
554 maxNumer, maxDenom))
555 def client(self, name=None):
556 return self.tk.call('wm', 'client', self._w, name)
557 def command(self, value=None):
558 return self.tk.call('wm', 'command', self._w, value)
559 def deiconify(self):
560 return self.tk.call('wm', 'deiconify', self._w)
561 def focusmodel(self, model=None):
562 return self.tk.call('wm', 'focusmodel', self._w, model)
563 def frame(self):
564 return self.tk.call('wm', 'frame', self._w)
565 def geometry(self, newGeometry=None):
566 return self.tk.call('wm', 'geometry', self._w, newGeometry)
567 def grid(self,
568 baseWidht=None, baseHeight=None,
569 widthInc=None, heightInc=None):
570 return self._getints(self.tk.call(
571 'wm', 'grid', self._w,
572 baseWidht, baseHeight, widthInc, heightInc))
573 def group(self, pathName=None):
574 return self.tk.call('wm', 'group', self._w, pathName)
575 def iconbitmap(self, bitmap=None):
576 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
577 def iconify(self):
578 return self.tk.call('wm', 'iconify', self._w)
579 def iconmask(self, bitmap=None):
580 return self.tk.call('wm', 'iconmask', self._w, bitmap)
581 def iconname(self, newName=None):
582 return self.tk.call('wm', 'iconname', self._w, newName)
583 def iconposition(self, x=None, y=None):
584 return self._getints(self.tk.call(
585 'wm', 'iconposition', self._w, x, y))
586 def iconwindow(self, pathName=None):
587 return self.tk.call('wm', 'iconwindow', self._w, pathName)
588 def maxsize(self, width=None, height=None):
589 return self._getints(self.tk.call(
590 'wm', 'maxsize', self._w, width, height))
591 def minsize(self, width=None, height=None):
592 return self._getints(self.tk.call(
593 'wm', 'minsize', self._w, width, height))
594 def overrideredirect(self, boolean=None):
595 return self._getboolean(self.tk.call(
596 'wm', 'overrideredirect', self._w, boolean))
597 def positionfrom(self, who=None):
598 return self.tk.call('wm', 'positionfrom', self._w, who)
599 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000600 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000601 command = self._register(func)
602 else:
603 command = func
604 return self.tk.call(
605 'wm', 'protocol', self._w, name, command)
606 def sizefrom(self, who=None):
607 return self.tk.call('wm', 'sizefrom', self._w, who)
608 def state(self):
609 return self.tk.call('wm', 'state', self._w)
610 def title(self, string=None):
611 return self.tk.call('wm', 'title', self._w, string)
612 def transient(self, master=None):
613 return self.tk.call('wm', 'transient', self._w, master)
614 def withdraw(self):
615 return self.tk.call('wm', 'withdraw', self._w)
616
617class Tk(Misc, Wm):
618 _w = '.'
619 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000620 self.master = None
621 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000622 if baseName is None:
623 import sys, os
624 baseName = os.path.basename(sys.argv[0])
625 if baseName[-3:] == '.py': baseName = baseName[:-3]
626 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000627 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000628 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000629 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000630 def destroy(self):
631 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000632 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000633 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000634 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000635 def readprofile(self, baseName, className):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000636 ##print __import__
637 import os, pdb
Guido van Rossum27b77a41994-07-12 15:52:32 +0000638 if os.environ.has_key('HOME'): home = os.environ['HOME']
639 else: home = os.curdir
640 class_tcl = os.path.join(home, '.%s.tcl' % className)
641 class_py = os.path.join(home, '.%s.py' % className)
642 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
643 base_py = os.path.join(home, '.%s.py' % baseName)
644 dir = {'self': self}
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000645 ##pdb.run('from Tkinter import *', dir)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000646 exec 'from Tkinter import *' in dir
647 if os.path.isfile(class_tcl):
648 print 'source', `class_tcl`
649 self.tk.call('source', class_tcl)
650 if os.path.isfile(class_py):
651 print 'execfile', `class_py`
652 execfile(class_py, dir)
653 if os.path.isfile(base_tcl):
654 print 'source', `base_tcl`
655 self.tk.call('source', base_tcl)
656 if os.path.isfile(base_py):
657 print 'execfile', `base_py`
658 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000659
660class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000661 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000662 apply(self.tk.call,
663 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000664 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000665 pack = config
666 def __setitem__(self, key, value):
667 Pack.config({key: value})
668 def forget(self):
669 self.tk.call('pack', 'forget', self._w)
670 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000671 words = self.tk.splitlist(
672 self.tk.call('pack', 'newinfo', self._w))
673 dict = {}
674 for i in range(0, len(words), 2):
675 key = words[i][1:]
676 value = words[i+1]
677 if value[0] == '.':
678 value = self._nametowidget(value)
679 dict[key] = value
680 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000681 info = newinfo
Guido van Rossum5505d561994-12-30 17:16:35 +0000682 _noarg_ = ['_noarg_']
683 def propagate(self, flag=_noarg_):
684 if boolean is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000685 return self._getboolean(self.tk.call(
686 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000687 else:
688 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum18468821994-06-20 07:49:28 +0000689 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000690 return map(self._nametowidget,
691 self.tk.splitlist(
692 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000693
694class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000695 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000696 apply(self.tk.call,
697 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000698 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000699 place = config
700 def __setitem__(self, key, value):
701 Place.config({key: value})
702 def forget(self):
703 self.tk.call('place', 'forget', self._w)
704 def info(self):
705 return self.tk.call('place', 'info', self._w)
706 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000707 return map(self._nametowidget,
708 self.tk.splitlist(
709 self.tk.call(
710 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000711
Guido van Rossum18468821994-06-20 07:49:28 +0000712class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000713 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000714 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000715 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000716 if not _default_root:
717 _default_root = Tk()
718 master = _default_root
719 if not _default_root:
720 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000721 self.master = master
722 self.tk = master.tk
723 if cnf.has_key('name'):
724 name = cnf['name']
725 del cnf['name']
726 else:
727 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000728 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000729 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000730 self._w = '.' + name
731 else:
732 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000733 self.children = {}
734 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000735 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000736 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000737 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
738 if kw:
739 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000740 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000741 Widget._setup(self, master, cnf)
742 extra1=()
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000743 apply(self.tk.call, extra1+(widgetName, self._w)+extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000744 if cnf:
745 Widget.config(self, cnf)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000746 def config(self, cnf=None, **kw):
747 # XXX ought to generalize this so tag_config etc. can use it
748 if kw:
749 cnf = _cnfmerge((cnf, kw))
750 else:
751 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000752 if cnf is None:
753 cnf = {}
754 for x in self.tk.split(
755 self.tk.call(self._w, 'configure')):
756 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
757 return cnf
758 if type(cnf) == StringType:
759 x = self.tk.split(self.tk.call(
760 self._w, 'configure', '-'+cnf))
761 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000762 for k in cnf.keys():
763 if type(k) == ClassType:
764 k.config(self, cnf[k])
765 del cnf[k]
766 apply(self.tk.call, (self._w, 'configure')
767 + self._options(cnf))
768 def __getitem__(self, key):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000769 if TkVersion >= 4.0:
770 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000771 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000772 self._w, 'configure', '-' + key))
773 return v[4]
774 def __setitem__(self, key, value):
775 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000776 def keys(self):
777 return map(lambda x: x[0][1:],
778 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000779 def __str__(self):
780 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000781 def destroy(self):
782 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000783 if self.master.children.has_key(self._name):
784 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000785 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000786 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000787 return apply(self.tk.call, (self._w, name) + args)
788 def unbind_class(self, seq):
789 Misc.unbind_class(self, self.widgetName, seq)
Guido van Rossum18468821994-06-20 07:49:28 +0000790
791class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000792 def __init__(self, master=None, cnf={}, **kw):
793 if kw:
794 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000795 extra = ()
796 if cnf.has_key('screen'):
797 extra = ('-screen', cnf['screen'])
798 del cnf['screen']
799 if cnf.has_key('class'):
800 extra = extra + ('-class', cnf['class'])
801 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000802 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000803 root = self._root()
804 self.iconname(root.iconname())
805 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000806
807class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000808 def __init__(self, master=None, cnf={}, **kw):
809 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000810 def tkButtonEnter(self, *dummy):
811 self.tk.call('tkButtonEnter', self._w)
812 def tkButtonLeave(self, *dummy):
813 self.tk.call('tkButtonLeave', self._w)
814 def tkButtonDown(self, *dummy):
815 self.tk.call('tkButtonDown', self._w)
816 def tkButtonUp(self, *dummy):
817 self.tk.call('tkButtonUp', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000818 def flash(self):
819 self.tk.call(self._w, 'flash')
820 def invoke(self):
821 self.tk.call(self._w, 'invoke')
822
823# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000824# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000825def AtEnd():
826 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000827def AtInsert(*args):
828 s = 'insert'
829 for a in args:
830 if a: s = s + (' ' + a)
831 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000832def AtSelFirst():
833 return 'sel.first'
834def AtSelLast():
835 return 'sel.last'
836def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000837 if y is None:
838 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000839 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000840 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000841
842class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000843 def __init__(self, master=None, cnf={}, **kw):
844 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000845 def addtag(self, *args):
846 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000847 def addtag_above(self, tagOrId):
848 self.addtag('above', tagOrId)
849 def addtag_all(self):
850 self.addtag('all')
851 def addtag_below(self, tagOrId):
852 self.addtag('below', tagOrId)
853 def addtag_closest(self, x, y, halo=None, start=None):
854 self.addtag('closest', x, y, halo, start)
855 def addtag_enclosed(self, x1, y1, x2, y2):
856 self.addtag('enclosed', x1, y1, x2, y2)
857 def addtag_overlapping(self, x1, y1, x2, y2):
858 self.addtag('overlapping', x1, y1, x2, y2)
859 def addtag_withtag(self, tagOrId):
860 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000861 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000862 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000863 def tag_unbind(self, tagOrId, sequence):
864 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
865 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000866 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000867 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000868 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000869 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000870 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000871 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000872 self._w, 'canvasx', screenx, gridspacing))
873 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000874 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000875 self._w, 'canvasy', screeny, gridspacing))
876 def coords(self, *args):
877 return self._do('coords', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000878 def _create(self, itemType, args, kw): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000879 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000880 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000881 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000882 args = args[:-1]
883 else:
884 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000885 return self.tk.getint(apply(
886 self.tk.call,
887 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000888 + args + self._options(cnf, kw)))
889 def create_arc(self, *args, **kw):
890 return self._create('arc', args, kw)
891 def create_bitmap(self, *args, **kw):
892 return self._create('bitmap', args, kw)
893 def create_image(self, *args, **kw):
894 return self._create('image', args, kw)
895 def create_line(self, *args, **kw):
896 return self._create('line', args, kw)
897 def create_oval(self, *args, **kw):
898 return self._create('oval', args, kw)
899 def create_polygon(self, *args, **kw):
900 return self._create('polygon', args, kw)
901 def create_rectangle(self, *args, **kw):
902 return self._create('rectangle', args, kw)
903 def create_text(self, *args, **kw):
904 return self._create('text', args, kw)
905 def create_window(self, *args, **kw):
906 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000907 def dchars(self, *args):
908 self._do('dchars', args)
909 def delete(self, *args):
910 self._do('delete', args)
911 def dtag(self, *args):
912 self._do('dtag', args)
913 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000914 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000915 def find_above(self, tagOrId):
916 return self.find('above', tagOrId)
917 def find_all(self):
918 return self.find('all')
919 def find_below(self, tagOrId):
920 return self.find('below', tagOrId)
921 def find_closest(self, x, y, halo=None, start=None):
922 return self.find('closest', x, y, halo, start)
923 def find_enclosed(self, x1, y1, x2, y2):
924 return self.find('enclosed', x1, y1, x2, y2)
925 def find_overlapping(self, x1, y1, x2, y2):
926 return self.find('overlapping', x1, y1, x2, y2)
927 def find_withtag(self, tagOrId):
928 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000929 def focus(self, *args):
930 return self._do('focus', args)
931 def gettags(self, *args):
932 return self.tk.splitlist(self._do('gettags', args))
933 def icursor(self, *args):
934 self._do('icursor', args)
935 def index(self, *args):
936 return self.tk.getint(self._do('index', args))
937 def insert(self, *args):
938 self._do('insert', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000939 def itemconfig(self, tagOrId, cnf=None, **kw):
940 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000941 cnf = {}
942 for x in self.tk.split(
943 self._do('itemconfigure', (tagOrId))):
944 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
945 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000946 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000947 x = self.tk.split(self._do('itemconfigure',
948 (tagOrId, '-'+cnf,)))
949 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000950 self._do('itemconfigure', (tagOrId,)
951 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000952 def lower(self, *args):
953 self._do('lower', args)
954 def move(self, *args):
955 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000956 def postscript(self, cnf={}, **kw):
957 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000958 def tkraise(self, *args):
959 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000960 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000961 def scale(self, *args):
962 self._do('scale', args)
963 def scan_mark(self, x, y):
964 self.tk.call(self._w, 'scan', 'mark', x, y)
965 def scan_dragto(self, x, y):
966 self.tk.call(self._w, 'scan', 'dragto', x, y)
967 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000968 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000969 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000970 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000971 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000972 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000973 def select_item(self):
974 self.tk.call(self._w, 'select', 'item')
975 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000976 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000977 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000978 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000979 def xview(self, *args):
980 apply(self.tk.call, (self._w, 'xview')+args)
981 def yview(self, *args):
982 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +0000983
984class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000985 def __init__(self, master=None, cnf={}, **kw):
986 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def deselect(self):
988 self.tk.call(self._w, 'deselect')
989 def flash(self):
990 self.tk.call(self._w, 'flash')
991 def invoke(self):
992 self.tk.call(self._w, 'invoke')
993 def select(self):
994 self.tk.call(self._w, 'select')
995 def toggle(self):
996 self.tk.call(self._w, 'toggle')
997
998class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000999 def __init__(self, master=None, cnf={}, **kw):
1000 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001001 def tk_entryBackspace(self):
1002 self.tk.call('tk_entryBackspace', self._w)
1003 def tk_entryBackword(self):
1004 self.tk.call('tk_entryBackword', self._w)
1005 def tk_entrySeeCaret(self):
1006 self.tk.call('tk_entrySeeCaret', self._w)
1007 def delete(self, first, last=None):
1008 self.tk.call(self._w, 'delete', first, last)
1009 def get(self):
1010 return self.tk.call(self._w, 'get')
1011 def icursor(self, index):
1012 self.tk.call(self._w, 'icursor', index)
1013 def index(self, index):
1014 return self.tk.getint(self.tk.call(
1015 self._w, 'index', index))
1016 def insert(self, index, string):
1017 self.tk.call(self._w, 'insert', index, string)
1018 def scan_mark(self, x):
1019 self.tk.call(self._w, 'scan', 'mark', x)
1020 def scan_dragto(self, x):
1021 self.tk.call(self._w, 'scan', 'dragto', x)
1022 def select_adjust(self, index):
1023 self.tk.call(self._w, 'select', 'adjust', index)
1024 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001025 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +00001026 def select_from(self, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001027 self.tk.call(self._w, 'select', 'set', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001028 def select_to(self, index):
1029 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001030 def view(self, index):
1031 self.tk.call(self._w, 'view', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001032
1033class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001034 def __init__(self, master=None, cnf={}, **kw):
1035 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001036 extra = ()
1037 if cnf.has_key('class'):
1038 extra = ('-class', cnf['class'])
1039 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001040 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001041 def tk_menuBar(self, *args):
1042 apply(self.tk.call, ('tk_menuBar', self._w) + args)
1043
1044class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001045 def __init__(self, master=None, cnf={}, **kw):
1046 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001047
Guido van Rossum18468821994-06-20 07:49:28 +00001048class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001049 def __init__(self, master=None, cnf={}, **kw):
1050 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001051 def tk_listboxSingleSelect(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001052 if TkVersion >= 4.0:
1053 self['selectmode'] = 'single'
1054 else:
1055 self.tk.call('tk_listboxSingleSelect', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001056 def curselection(self):
1057 return self.tk.splitlist(self.tk.call(
1058 self._w, 'curselection'))
1059 def delete(self, first, last=None):
1060 self.tk.call(self._w, 'delete', first, last)
1061 def get(self, index):
1062 return self.tk.call(self._w, 'get', index)
1063 def insert(self, index, *elements):
1064 apply(self.tk.call,
1065 (self._w, 'insert', index) + elements)
1066 def nearest(self, y):
1067 return self.tk.getint(self.tk.call(
1068 self._w, 'nearest', y))
1069 def scan_mark(self, x, y):
1070 self.tk.call(self._w, 'scan', 'mark', x, y)
1071 def scan_dragto(self, x, y):
1072 self.tk.call(self._w, 'scan', 'dragto', x, y)
1073 def select_adjust(self, index):
1074 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001075 if TkVersion >= 4.0:
1076 def select_anchor(self, index):
1077 self.tk.call(self._w, 'selection', 'anchor', index)
1078 def select_clear(self, first, last=None):
1079 self.tk.call(self._w,
1080 'selection', 'clear', first, last)
1081 def select_includes(self, index):
1082 return self.tk.getboolean(self.tk.call(
1083 self._w, 'selection', 'includes', index))
1084 def select_set(self, first, last=None):
1085 self.tk.call(self._w, 'selection', 'set', first, last)
1086 else:
1087 def select_clear(self):
1088 self.tk.call(self._w, 'select', 'clear')
1089 def select_from(self, index):
1090 self.tk.call(self._w, 'select', 'from', index)
1091 def select_to(self, index):
1092 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001093 def size(self):
1094 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001095 def xview(self, *what):
1096 apply(self.tk.call, (self._w, 'xview')+what)
1097 def yview(self, *what):
1098 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001099
1100class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001101 def __init__(self, master=None, cnf={}, **kw):
1102 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001103 def tk_bindForTraversal(self):
1104 self.tk.call('tk_bindForTraversal', self._w)
1105 def tk_mbPost(self):
1106 self.tk.call('tk_mbPost', self._w)
1107 def tk_mbUnpost(self):
1108 self.tk.call('tk_mbUnpost')
1109 def tk_traverseToMenu(self, char):
1110 self.tk.call('tk_traverseToMenu', self._w, char)
1111 def tk_traverseWithinMenu(self, char):
1112 self.tk.call('tk_traverseWithinMenu', self._w, char)
1113 def tk_getMenuButtons(self):
1114 return self.tk.call('tk_getMenuButtons', self._w)
1115 def tk_nextMenu(self, count):
1116 self.tk.call('tk_nextMenu', count)
1117 def tk_nextMenuEntry(self, count):
1118 self.tk.call('tk_nextMenuEntry', count)
1119 def tk_invokeMenu(self):
1120 self.tk.call('tk_invokeMenu', self._w)
1121 def tk_firstMenu(self):
1122 self.tk.call('tk_firstMenu', self._w)
1123 def tk_mbButtonDown(self):
1124 self.tk.call('tk_mbButtonDown', self._w)
1125 def activate(self, index):
1126 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001127 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001128 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001129 + self._options(cnf, kw))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001130 def add_cascade(self, cnf={}):
1131 self.add('cascade', cnf)
1132 def add_checkbutton(self, cnf={}):
1133 self.add('checkbutton', cnf)
1134 def add_command(self, cnf={}):
1135 self.add('command', cnf)
1136 def add_radiobutton(self, cnf={}):
1137 self.add('radiobutton', cnf)
1138 def add_separator(self, cnf={}):
1139 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001140 def delete(self, index1, index2=None):
1141 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001142 def entryconfig(self, index, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001143 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001144 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001145 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001146 i = self.tk.call(self._w, 'index', index)
1147 if i == 'none': return None
1148 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001149 def invoke(self, index):
1150 return self.tk.call(self._w, 'invoke', index)
1151 def post(self, x, y):
1152 self.tk.call(self._w, 'post', x, y)
1153 def unpost(self):
1154 self.tk.call(self._w, 'unpost')
1155 def yposition(self, index):
1156 return self.tk.getint(self.tk.call(
1157 self._w, 'yposition', index))
1158
1159class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001160 def __init__(self, master=None, cnf={}, **kw):
1161 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001162
1163class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001164 def __init__(self, master=None, cnf={}, **kw):
1165 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001166
1167class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001168 def __init__(self, master=None, cnf={}, **kw):
1169 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001170 def deselect(self):
1171 self.tk.call(self._w, 'deselect')
1172 def flash(self):
1173 self.tk.call(self._w, 'flash')
1174 def invoke(self):
1175 self.tk.call(self._w, 'invoke')
1176 def select(self):
1177 self.tk.call(self._w, 'select')
1178
1179class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001180 def __init__(self, master=None, cnf={}, **kw):
1181 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001182 def get(self):
1183 return self.tk.getint(self.tk.call(self._w, 'get'))
1184 def set(self, value):
1185 self.tk.call(self._w, 'set', value)
1186
1187class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001188 def __init__(self, master=None, cnf={}, **kw):
1189 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001190 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001191 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001192 def set(self, *args):
1193 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001194
1195class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001196 def __init__(self, master=None, cnf={}, **kw):
1197 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001198 self.bind('<Delete>', self.bspace)
1199 def bspace(self, *args):
1200 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001201 def tk_textSelectTo(self, index):
1202 self.tk.call('tk_textSelectTo', self._w, index)
1203 def tk_textBackspace(self):
1204 self.tk.call('tk_textBackspace', self._w)
1205 def tk_textIndexCloser(self, a, b, c):
1206 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1207 def tk_textResetAnchor(self, index):
1208 self.tk.call('tk_textResetAnchor', self._w, index)
1209 def compare(self, index1, op, index2):
1210 return self.tk.getboolean(self.tk.call(
1211 self._w, 'compare', index1, op, index2))
1212 def debug(self, boolean=None):
1213 return self.tk.getboolean(self.tk.call(
1214 self._w, 'debug', boolean))
1215 def delete(self, index1, index2=None):
1216 self.tk.call(self._w, 'delete', index1, index2)
1217 def get(self, index1, index2=None):
1218 return self.tk.call(self._w, 'get', index1, index2)
1219 def index(self, index):
1220 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001221 def insert(self, index, chars, *args):
1222 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001223 def mark_names(self):
1224 return self.tk.splitlist(self.tk.call(
1225 self._w, 'mark', 'names'))
1226 def mark_set(self, markName, index):
1227 self.tk.call(self._w, 'mark', 'set', markName, index)
1228 def mark_unset(self, markNames):
1229 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1230 def scan_mark(self, y):
1231 self.tk.call(self._w, 'scan', 'mark', y)
1232 def scan_dragto(self, y):
1233 self.tk.call(self._w, 'scan', 'dragto', y)
1234 def tag_add(self, tagName, index1, index2=None):
1235 self.tk.call(
1236 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001237 def tag_unbind(self, tagName, sequence):
1238 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001239 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001240 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001241 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001242 self.tk.call(self._w, 'tag', 'bind',
1243 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001244 (add + name,) + self._subst_format)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001245 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001246 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001247 (self._w, 'tag', 'configure', tagName)
1248 + self._options(cnf, kw))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001249 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001250 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001251 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001252 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001253 def tag_names(self, index=None):
1254 return self.tk.splitlist(
1255 self.tk.call(self._w, 'tag', 'names', index))
1256 def tag_nextrange(self, tagName, index1, index2=None):
1257 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001258 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001259 def tag_raise(self, tagName, aboveThis=None):
1260 self.tk.call(
1261 self._w, 'tag', 'raise', tagName, aboveThis)
1262 def tag_ranges(self, tagName):
1263 return self.tk.splitlist(self.tk.call(
1264 self._w, 'tag', 'ranges', tagName))
1265 def tag_remove(self, tagName, index1, index2=None):
1266 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001267 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001268 def window_cget(self, index, option):
1269 return self.tk.call(self._w, 'window', 'cget', index, option)
1270 def window_config(self, index, cnf={}, **kw):
1271 apply(self.tk.call,
1272 (self._w, 'window', 'configure', index)
1273 + self._options(cnf, kw))
1274 def window_create(self, index, cnf={}, **kw):
1275 apply(self.tk.call,
1276 (self._w, 'window', 'create', index)
1277 + self._options(cnf, kw))
1278 def window_names(self):
1279 return self.tk.splitlist(
1280 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001281 def yview(self, *what):
1282 apply(self.tk.call, (self._w, 'yview')+what)
1283 def yview_pickplace(self, *what):
1284 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001285
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001286class Image:
1287 def __init__(self, imgtype, name=None, cnf={}, **kw):
1288 self.name = None
1289 master = _default_root
1290 if not master: raise RuntimeError, 'Too early to create image'
1291 self.tk = master.tk
1292 if not name: name = `id(self)`
1293 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1294 elif kw: cnf = kw
1295 options = ()
1296 for k, v in cnf.items():
1297 if type(v) in CallableTypes:
1298 v = self._register(v)
1299 options = options + ('-'+k, v)
1300 apply(self.tk.call,
1301 ('image', 'create', imgtype, name,) + options)
1302 self.name = name
1303 def __str__(self): return self.name
1304 def __del__(self):
1305 if self.name:
1306 self.tk.call('image', 'delete', self.name)
1307 def height(self):
1308 return self.tk.getint(
1309 self.tk.call('image', 'height', self.name))
1310 def type(self):
1311 return self.tk.call('image', 'type', self.name)
1312 def width(self):
1313 return self.tk.getint(
1314 self.tk.call('image', 'width', self.name))
1315
1316class PhotoImage(Image):
1317 def __init__(self, name=None, cnf={}, **kw):
1318 apply(Image.__init__, (self, 'photo', name, cnf), kw)
1319
1320class BitmapImage(Image):
1321 def __init__(self, name=None, cnf={}, **kw):
1322 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1323
1324def image_names(): return _default_root.tk.call('image', 'names')
1325def image_types(): return _default_root.tk.call('image', 'types')
1326
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001327######################################################################
1328# Extensions:
1329
1330class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001331 def __init__(self, master=None, cnf={}, **kw):
1332 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001333 self.bind('<Any-Enter>', self.tkButtonEnter)
1334 self.bind('<Any-Leave>', self.tkButtonLeave)
1335 self.bind('<1>', self.tkButtonDown)
1336 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001337
1338class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001339 def __init__(self, master=None, cnf={}, **kw):
1340 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001341 self.bind('<Any-Enter>', self.tkButtonEnter)
1342 self.bind('<Any-Leave>', self.tkButtonLeave)
1343 self.bind('<1>', self.tkButtonDown)
1344 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1345 self['fg'] = self['bg']
1346 self['activebackground'] = self['bg']