blob: abe8f823590ccee9bb189942703561c813f92356 [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 Rossuma5773dd1995-09-07 19:22:00 +00006from Tkconstants import *
Guido van Rossum18468821994-06-20 07:49:28 +00007
Guido van Rossum761c5ab1995-07-14 15:29:10 +00008CallableTypes = (FunctionType, MethodType,
9 BuiltinFunctionType, BuiltinMethodType)
10
11TkVersion = eval(tkinter.TK_VERSION)
12TclVersion = eval(tkinter.TCL_VERSION)
13if TkVersion < 4.0:
14 raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher"
Guido van Rossum18468821994-06-20 07:49:28 +000015
Guido van Rossuma22a70a1995-08-04 03:51:48 +000016
Guido van Rossum2dcf5291994-07-06 09:23:20 +000017def _flatten(tuple):
18 res = ()
19 for item in tuple:
20 if type(item) in (TupleType, ListType):
21 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000022 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000023 res = res + (item,)
24 return res
25
26def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000027 if type(cnfs) is DictionaryType:
28 _fixgeometry(cnfs)
29 return cnfs
30 elif type(cnfs) in (NoneType, StringType):
31
Guido van Rossum2dcf5291994-07-06 09:23:20 +000032 return cnfs
33 else:
34 cnf = {}
35 for c in _flatten(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000036 _fixgeometry(c)
Guido van Rossum2dcf5291994-07-06 09:23:20 +000037 for k, v in c.items():
38 cnf[k] = v
39 return cnf
40
Guido van Rossum761c5ab1995-07-14 15:29:10 +000041if TkVersion >= 4.0:
42 _fixg_warning = "Warning: patched up pre-Tk-4.0 geometry option\n"
43 def _fixgeometry(c):
Guido van Rossum35f67fb1995-08-04 03:50:29 +000044 if c and c.has_key('geometry'):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000045 wh = _parsegeometry(c['geometry'])
46 if wh:
47 # Print warning message -- once
48 global _fixg_warning
49 if _fixg_warning:
50 import sys
51 sys.stderr.write(_fixg_warning)
52 _fixg_warning = None
53 w, h = wh
54 c['width'] = w
55 c['height'] = h
56 del c['geometry']
57 def _parsegeometry(s):
58 from string import splitfields
59 fields = splitfields(s, 'x')
60 if len(fields) == 2:
61 return tuple(fields)
62 # else: return None
63else:
64 def _fixgeometry(c): pass
65
Guido van Rossum2dcf5291994-07-06 09:23:20 +000066class Event:
67 pass
68
Guido van Rossumaec5dc91994-06-27 07:55:12 +000069_default_root = None
70
Guido van Rossum45853db1994-06-20 12:19:19 +000071def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000072 pass
73
Guido van Rossum97aeca11994-07-07 13:12:12 +000074def _exit(code='0'):
75 import sys
76 sys.exit(getint(code))
77
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078_varnum = 0
79class Variable:
80 def __init__(self, master=None):
81 global _default_root
82 global _varnum
83 if master:
84 self._tk = master.tk
85 else:
86 self._tk = _default_root.tk
87 self._name = 'PY_VAR' + `_varnum`
88 _varnum = _varnum + 1
89 def __del__(self):
90 self._tk.unsetvar(self._name)
91 def __str__(self):
92 return self._name
93 def __call__(self, value=None):
94 if value == None:
95 return self.get()
96 else:
97 self.set(value)
98 def set(self, value):
99 return self._tk.setvar(self._name, value)
100
101class StringVar(Variable):
102 def __init__(self, master=None):
103 Variable.__init__(self, master)
104 def get(self):
105 return self._tk.getvar(self._name)
106
107class IntVar(Variable):
108 def __init__(self, master=None):
109 Variable.__init__(self, master)
110 def get(self):
111 return self._tk.getint(self._tk.getvar(self._name))
112
113class DoubleVar(Variable):
114 def __init__(self, master=None):
115 Variable.__init__(self, master)
116 def get(self):
117 return self._tk.getdouble(self._tk.getvar(self._name))
118
119class BooleanVar(Variable):
120 def __init__(self, master=None):
121 Variable.__init__(self, master)
122 def get(self):
123 return self._tk.getboolean(self._tk.getvar(self._name))
124
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000125def mainloop(n=0):
126 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000127
128def getint(s):
129 return _default_root.tk.getint(s)
130
131def getdouble(s):
132 return _default_root.tk.getdouble(s)
133
134def getboolean(s):
135 return _default_root.tk.getboolean(s)
136
Guido van Rossum18468821994-06-20 07:49:28 +0000137class Misc:
138 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000139 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000140 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000141 def tk_menuBar(self, *args):
142 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000143 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000144 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000145 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000146 def wait_window(self, window=None):
147 if window == None:
148 window = self
149 self.tk.call('tkwait', 'window', window._w)
150 def wait_visibility(self, window=None):
151 if window == None:
152 window = self
153 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000154 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000155 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000156 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000157 return self.tk.getvar(name)
158 def getint(self, s):
159 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000160 def getdouble(self, s):
161 return self.tk.getdouble(s)
162 def getboolean(self, s):
163 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000164 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000165 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000166 focus = focus_set # XXX b/w compat?
167 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000168 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000169 def focus_default_none(self):
170 self.tk.call('focus', 'default', 'none')
171 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000172 def focus_none(self):
173 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000174 def focus_get(self):
175 name = self.tk.call('focus')
176 if name == 'none': return None
177 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000178 def after(self, ms, func=None, *args):
179 if not func:
180 self.tk.call('after', ms)
181 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000182 # XXX Disgusting hack to clean up after calling func
183 tmp = []
184 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
185 try:
186 apply(func, args)
187 finally:
188 tk.deletecommand(tmp[0])
189 name = self._register(callit)
190 tmp.append(name)
191 self.tk.call('after', ms, name)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000192 def bell(self, displayof=None):
193 if displayof:
194 self.tk.call('bell', '-displayof', displayof)
195 else:
196 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000197 # XXX grab current w/o window argument
198 def grab_current(self):
199 name = self.tk.call('grab', 'current', self._w)
200 if not name: return None
201 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000202 def grab_release(self):
203 self.tk.call('grab', 'release', self._w)
204 def grab_set(self):
205 self.tk.call('grab', 'set', self._w)
206 def grab_set_global(self):
207 self.tk.call('grab', 'set', '-global', self._w)
208 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000209 status = self.tk.call('grab', 'status', self._w)
210 if status == 'none': status = None
211 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000212 def lower(self, belowThis=None):
213 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000214 def option_add(self, pattern, value, priority = None):
215 self.tk.call('option', 'add', pattern, priority)
216 def option_clear(self):
217 self.tk.call('option', 'clear')
218 def option_get(self, name, className):
219 return self.tk.call('option', 'get', self._w, name, className)
220 def option_readfile(self, fileName, priority = None):
221 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000222 def selection_clear(self):
223 self.tk.call('selection', 'clear', self._w)
224 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000225 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000226 def selection_handle(self, func, type=None, format=None):
227 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000228 self.tk.call('selection', 'handle', self._w,
229 name, type, format)
230 def selection_own(self, func=None):
231 name = self._register(func)
232 self.tk.call('selection', 'own', self._w, name)
233 def selection_own_get(self):
234 return self._nametowidget(self.tk.call('selection', 'own'))
235 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000236 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000237 def lower(self, belowThis=None):
238 self.tk.call('lift', self._w, belowThis)
239 def tkraise(self, aboveThis=None):
240 self.tk.call('raise', self._w, aboveThis)
241 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000242 def colormodel(self, value=None):
243 return self.tk.call('tk', 'colormodel', self._w, value)
244 def winfo_atom(self, name):
245 return self.tk.getint(self.tk.call('winfo', 'atom', name))
246 def winfo_atomname(self, id):
247 return self.tk.call('winfo', 'atomname', id)
248 def winfo_cells(self):
249 return self.tk.getint(
250 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000251 def winfo_children(self):
252 return map(self._nametowidget,
253 self.tk.splitlist(self.tk.call(
254 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000255 def winfo_class(self):
256 return self.tk.call('winfo', 'class', self._w)
257 def winfo_containing(self, rootX, rootY):
258 return self.tk.call('winfo', 'containing', rootx, rootY)
259 def winfo_depth(self):
260 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
261 def winfo_exists(self):
262 return self.tk.getint(
263 self.tk.call('winfo', 'exists', self._w))
264 def winfo_fpixels(self, number):
265 return self.tk.getdouble(self.tk.call(
266 'winfo', 'fpixels', self._w, number))
267 def winfo_geometry(self):
268 return self.tk.call('winfo', 'geometry', self._w)
269 def winfo_height(self):
270 return self.tk.getint(
271 self.tk.call('winfo', 'height', self._w))
272 def winfo_id(self):
273 return self.tk.getint(
274 self.tk.call('winfo', 'id', self._w))
275 def winfo_interps(self):
276 return self.tk.splitlist(
277 self.tk.call('winfo', 'interps'))
278 def winfo_ismapped(self):
279 return self.tk.getint(
280 self.tk.call('winfo', 'ismapped', self._w))
281 def winfo_name(self):
282 return self.tk.call('winfo', 'name', self._w)
283 def winfo_parent(self):
284 return self.tk.call('winfo', 'parent', self._w)
285 def winfo_pathname(self, id):
286 return self.tk.call('winfo', 'pathname', id)
287 def winfo_pixels(self, number):
288 return self.tk.getint(
289 self.tk.call('winfo', 'pixels', self._w, number))
290 def winfo_reqheight(self):
291 return self.tk.getint(
292 self.tk.call('winfo', 'reqheight', self._w))
293 def winfo_reqwidth(self):
294 return self.tk.getint(
295 self.tk.call('winfo', 'reqwidth', self._w))
296 def winfo_rgb(self, color):
297 return self._getints(
298 self.tk.call('winfo', 'rgb', self._w, color))
299 def winfo_rootx(self):
300 return self.tk.getint(
301 self.tk.call('winfo', 'rootx', self._w))
302 def winfo_rooty(self):
303 return self.tk.getint(
304 self.tk.call('winfo', 'rooty', self._w))
305 def winfo_screen(self):
306 return self.tk.call('winfo', 'screen', self._w)
307 def winfo_screencells(self):
308 return self.tk.getint(
309 self.tk.call('winfo', 'screencells', self._w))
310 def winfo_screendepth(self):
311 return self.tk.getint(
312 self.tk.call('winfo', 'screendepth', self._w))
313 def winfo_screenheight(self):
314 return self.tk.getint(
315 self.tk.call('winfo', 'screenheight', self._w))
316 def winfo_screenmmheight(self):
317 return self.tk.getint(
318 self.tk.call('winfo', 'screenmmheight', self._w))
319 def winfo_screenmmwidth(self):
320 return self.tk.getint(
321 self.tk.call('winfo', 'screenmmwidth', self._w))
322 def winfo_screenvisual(self):
323 return self.tk.call('winfo', 'screenvisual', self._w)
324 def winfo_screenwidth(self):
325 return self.tk.getint(
326 self.tk.call('winfo', 'screenwidth', self._w))
327 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000328 return self._nametowidget(self.tk.call(
329 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000330 def winfo_visual(self):
331 return self.tk.call('winfo', 'visual', self._w)
332 def winfo_vrootheight(self):
333 return self.tk.getint(
334 self.tk.call('winfo', 'vrootheight', self._w))
335 def winfo_vrootwidth(self):
336 return self.tk.getint(
337 self.tk.call('winfo', 'vrootwidth', self._w))
338 def winfo_vrootx(self):
339 return self.tk.getint(
340 self.tk.call('winfo', 'vrootx', self._w))
341 def winfo_vrooty(self):
342 return self.tk.getint(
343 self.tk.call('winfo', 'vrooty', self._w))
344 def winfo_width(self):
345 return self.tk.getint(
346 self.tk.call('winfo', 'width', self._w))
347 def winfo_x(self):
348 return self.tk.getint(
349 self.tk.call('winfo', 'x', self._w))
350 def winfo_y(self):
351 return self.tk.getint(
352 self.tk.call('winfo', 'y', self._w))
353 def update(self):
354 self.tk.call('update')
355 def update_idletasks(self):
356 self.tk.call('update', 'idletasks')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000357 def bind(self, sequence, func=None, add=''):
358 if add: add = '+'
359 if func:
360 name = self._register(func, self._substitute)
361 self.tk.call('bind', self._w, sequence,
362 (add + name,) + self._subst_format)
363 else:
364 return self.tk.call('bind', self._w, sequence)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000365 def unbind(self, sequence):
366 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000367 def bind_all(self, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000368 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000369 if func:
370 name = self._register(func, self._substitute)
371 self.tk.call('bind', 'all' , sequence,
372 (add + name,) + self._subst_format)
373 else:
374 return self.tk.call('bind', 'all', sequence)
375 def unbind_all(self, sequence):
376 self.tk.call('bind', 'all' , sequence, '')
377 def bind_class(self, className, sequence, func=None, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000378 if add: add = '+'
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000379 if func:
380 name = self._register(func, self._substitute)
381 self.tk.call('bind', className , sequence,
382 (add + name,) + self._subst_format)
383 else:
384 return self.tk.call('bind', className, sequence)
385 def unbind_class(self, className, sequence):
386 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000387 def mainloop(self, n=0):
388 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000389 def quit(self):
390 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000391 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000392 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000393 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
394 def _getdoubles(self, string):
395 if not string: return None
396 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000397 def _getboolean(self, string):
398 if string:
399 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000400 def _options(self, cnf, kw = None):
401 if kw:
402 cnf = _cnfmerge((cnf, kw))
403 else:
404 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000405 res = ()
406 for k, v in cnf.items():
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000407 if k[-1] == '_': k = k[:-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000408 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000409 v = self._register(v)
410 res = res + ('-'+k, v)
411 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000412 def _nametowidget(self, name):
413 w = self
414 if name[0] == '.':
415 w = w._root()
416 name = name[1:]
417 from string import find
418 while name:
419 i = find(name, '.')
420 if i >= 0:
421 name, tail = name[:i], name[i+1:]
422 else:
423 tail = ''
424 w = w.children[name]
425 name = tail
426 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000427 def _register(self, func, subst=None):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000428 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000429 name = `id(f)`
430 if hasattr(func, 'im_func'):
431 func = func.im_func
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000432 if hasattr(func, '__name__') and \
433 type(func.__name__) == type(''):
434 name = name + func.__name__
Guido van Rossum18468821994-06-20 07:49:28 +0000435 self.tk.createcommand(name, f)
436 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000437 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000438 def _root(self):
439 w = self
440 while w.master: w = w.master
441 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000442 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000443 '%s', '%t', '%w', '%x', '%y',
444 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
445 def _substitute(self, *args):
446 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000447 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000448 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
449 # Missing: (a, c, d, m, o, v, B, R)
450 e = Event()
451 e.serial = tk.getint(nsign)
452 e.num = tk.getint(b)
453 try: e.focus = tk.getboolean(f)
454 except TclError: pass
455 e.height = tk.getint(h)
456 e.keycode = tk.getint(k)
457 e.state = tk.getint(s)
458 e.time = tk.getint(t)
459 e.width = tk.getint(w)
460 e.x = tk.getint(x)
461 e.y = tk.getint(y)
462 e.char = A
463 try: e.send_event = tk.getboolean(E)
464 except TclError: pass
465 e.keysym = K
466 e.keysym_num = tk.getint(N)
467 e.type = T
468 e.widget = self._nametowidget(W)
469 e.x_root = tk.getint(X)
470 e.y_root = tk.getint(Y)
471 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000472 def _report_exception(self):
473 import sys
474 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
475 root = self._root()
476 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000477
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000478class CallWrapper:
479 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000480 self.func = func
481 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000482 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000483 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000484 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000485 if self.subst:
486 args = apply(self.subst, args)
487 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000488 except SystemExit, msg:
489 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000490 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000491 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000492
493class Wm:
494 def aspect(self,
495 minNumer=None, minDenom=None,
496 maxNumer=None, maxDenom=None):
497 return self._getints(
498 self.tk.call('wm', 'aspect', self._w,
499 minNumer, minDenom,
500 maxNumer, maxDenom))
501 def client(self, name=None):
502 return self.tk.call('wm', 'client', self._w, name)
503 def command(self, value=None):
504 return self.tk.call('wm', 'command', self._w, value)
505 def deiconify(self):
506 return self.tk.call('wm', 'deiconify', self._w)
507 def focusmodel(self, model=None):
508 return self.tk.call('wm', 'focusmodel', self._w, model)
509 def frame(self):
510 return self.tk.call('wm', 'frame', self._w)
511 def geometry(self, newGeometry=None):
512 return self.tk.call('wm', 'geometry', self._w, newGeometry)
513 def grid(self,
514 baseWidht=None, baseHeight=None,
515 widthInc=None, heightInc=None):
516 return self._getints(self.tk.call(
517 'wm', 'grid', self._w,
518 baseWidht, baseHeight, widthInc, heightInc))
519 def group(self, pathName=None):
520 return self.tk.call('wm', 'group', self._w, pathName)
521 def iconbitmap(self, bitmap=None):
522 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
523 def iconify(self):
524 return self.tk.call('wm', 'iconify', self._w)
525 def iconmask(self, bitmap=None):
526 return self.tk.call('wm', 'iconmask', self._w, bitmap)
527 def iconname(self, newName=None):
528 return self.tk.call('wm', 'iconname', self._w, newName)
529 def iconposition(self, x=None, y=None):
530 return self._getints(self.tk.call(
531 'wm', 'iconposition', self._w, x, y))
532 def iconwindow(self, pathName=None):
533 return self.tk.call('wm', 'iconwindow', self._w, pathName)
534 def maxsize(self, width=None, height=None):
535 return self._getints(self.tk.call(
536 'wm', 'maxsize', self._w, width, height))
537 def minsize(self, width=None, height=None):
538 return self._getints(self.tk.call(
539 'wm', 'minsize', self._w, width, height))
540 def overrideredirect(self, boolean=None):
541 return self._getboolean(self.tk.call(
542 'wm', 'overrideredirect', self._w, boolean))
543 def positionfrom(self, who=None):
544 return self.tk.call('wm', 'positionfrom', self._w, who)
545 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000546 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000547 command = self._register(func)
548 else:
549 command = func
550 return self.tk.call(
551 'wm', 'protocol', self._w, name, command)
552 def sizefrom(self, who=None):
553 return self.tk.call('wm', 'sizefrom', self._w, who)
554 def state(self):
555 return self.tk.call('wm', 'state', self._w)
556 def title(self, string=None):
557 return self.tk.call('wm', 'title', self._w, string)
558 def transient(self, master=None):
559 return self.tk.call('wm', 'transient', self._w, master)
560 def withdraw(self):
561 return self.tk.call('wm', 'withdraw', self._w)
562
563class Tk(Misc, Wm):
564 _w = '.'
565 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000566 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000567 self.master = None
568 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000569 if baseName is None:
570 import sys, os
571 baseName = os.path.basename(sys.argv[0])
572 if baseName[-3:] == '.py': baseName = baseName[:-3]
573 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000574 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000575 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000576 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000577 if not _default_root:
578 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000579 def destroy(self):
580 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000581 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000582 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000583 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000584 def readprofile(self, baseName, className):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000585 ##print __import__
586 import os, pdb
Guido van Rossum27b77a41994-07-12 15:52:32 +0000587 if os.environ.has_key('HOME'): home = os.environ['HOME']
588 else: home = os.curdir
589 class_tcl = os.path.join(home, '.%s.tcl' % className)
590 class_py = os.path.join(home, '.%s.py' % className)
591 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
592 base_py = os.path.join(home, '.%s.py' % baseName)
593 dir = {'self': self}
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000594 ##pdb.run('from Tkinter import *', dir)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000595 exec 'from Tkinter import *' in dir
596 if os.path.isfile(class_tcl):
597 print 'source', `class_tcl`
598 self.tk.call('source', class_tcl)
599 if os.path.isfile(class_py):
600 print 'execfile', `class_py`
601 execfile(class_py, dir)
602 if os.path.isfile(base_tcl):
603 print 'source', `base_tcl`
604 self.tk.call('source', base_tcl)
605 if os.path.isfile(base_py):
606 print 'execfile', `base_py`
607 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000608 def report_callback_exception(self, exc, val, tb):
609 import traceback
610 print "Exception in Tkinter callback"
611 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000612
613class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000614 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000615 apply(self.tk.call,
616 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000617 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000618 pack = config
619 def __setitem__(self, key, value):
620 Pack.config({key: value})
621 def forget(self):
622 self.tk.call('pack', 'forget', self._w)
623 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000624 words = self.tk.splitlist(
625 self.tk.call('pack', 'newinfo', self._w))
626 dict = {}
627 for i in range(0, len(words), 2):
628 key = words[i][1:]
629 value = words[i+1]
630 if value[0] == '.':
631 value = self._nametowidget(value)
632 dict[key] = value
633 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000634 info = newinfo
Guido van Rossum5505d561994-12-30 17:16:35 +0000635 _noarg_ = ['_noarg_']
636 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000637 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000638 return self._getboolean(self.tk.call(
639 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000640 else:
641 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum18468821994-06-20 07:49:28 +0000642 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000643 return map(self._nametowidget,
644 self.tk.splitlist(
645 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000646
647class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000648 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000649 apply(self.tk.call,
650 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000651 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000652 place = config
653 def __setitem__(self, key, value):
654 Place.config({key: value})
655 def forget(self):
656 self.tk.call('place', 'forget', self._w)
657 def info(self):
658 return self.tk.call('place', 'info', self._w)
659 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000660 return map(self._nametowidget,
661 self.tk.splitlist(
662 self.tk.call(
663 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000664
Guido van Rossum18468821994-06-20 07:49:28 +0000665class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000666 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000667 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000668 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000669 if not _default_root:
670 _default_root = Tk()
671 master = _default_root
672 if not _default_root:
673 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000674 self.master = master
675 self.tk = master.tk
676 if cnf.has_key('name'):
677 name = cnf['name']
678 del cnf['name']
679 else:
680 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000681 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000682 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000683 self._w = '.' + name
684 else:
685 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000686 self.children = {}
687 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000688 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000689 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000690 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
691 if kw:
692 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000693 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000694 Widget._setup(self, master, cnf)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +0000695 apply(self.tk.call, (widgetName, self._w)+extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000696 if cnf:
697 Widget.config(self, cnf)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000698 def config(self, cnf=None, **kw):
699 # XXX ought to generalize this so tag_config etc. can use it
700 if kw:
701 cnf = _cnfmerge((cnf, kw))
702 else:
703 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000704 if cnf is None:
705 cnf = {}
706 for x in self.tk.split(
707 self.tk.call(self._w, 'configure')):
708 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
709 return cnf
710 if type(cnf) == StringType:
711 x = self.tk.split(self.tk.call(
712 self._w, 'configure', '-'+cnf))
713 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000714 for k in cnf.keys():
715 if type(k) == ClassType:
716 k.config(self, cnf[k])
717 del cnf[k]
718 apply(self.tk.call, (self._w, 'configure')
719 + self._options(cnf))
720 def __getitem__(self, key):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000721 if TkVersion >= 4.0:
722 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000723 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000724 self._w, 'configure', '-' + key))
725 return v[4]
726 def __setitem__(self, key, value):
727 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000728 def keys(self):
729 return map(lambda x: x[0][1:],
730 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000731 def __str__(self):
732 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000733 def destroy(self):
734 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000735 if self.master.children.has_key(self._name):
736 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000737 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000738 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000739 return apply(self.tk.call, (self._w, name) + args)
740 def unbind_class(self, seq):
741 Misc.unbind_class(self, self.widgetName, seq)
Guido van Rossum18468821994-06-20 07:49:28 +0000742
743class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000744 def __init__(self, master=None, cnf={}, **kw):
745 if kw:
746 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000747 extra = ()
748 if cnf.has_key('screen'):
749 extra = ('-screen', cnf['screen'])
750 del cnf['screen']
751 if cnf.has_key('class'):
752 extra = extra + ('-class', cnf['class'])
753 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000754 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000755 root = self._root()
756 self.iconname(root.iconname())
757 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000758
759class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000760 def __init__(self, master=None, cnf={}, **kw):
761 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000762 def tkButtonEnter(self, *dummy):
763 self.tk.call('tkButtonEnter', self._w)
764 def tkButtonLeave(self, *dummy):
765 self.tk.call('tkButtonLeave', self._w)
766 def tkButtonDown(self, *dummy):
767 self.tk.call('tkButtonDown', self._w)
768 def tkButtonUp(self, *dummy):
769 self.tk.call('tkButtonUp', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000770 def flash(self):
771 self.tk.call(self._w, 'flash')
772 def invoke(self):
773 self.tk.call(self._w, 'invoke')
774
775# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000776# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000777def AtEnd():
778 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000779def AtInsert(*args):
780 s = 'insert'
781 for a in args:
782 if a: s = s + (' ' + a)
783 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000784def AtSelFirst():
785 return 'sel.first'
786def AtSelLast():
787 return 'sel.last'
788def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000789 if y is None:
790 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000791 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000792 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000793
794class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000795 def __init__(self, master=None, cnf={}, **kw):
796 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000797 def addtag(self, *args):
798 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000799 def addtag_above(self, tagOrId):
800 self.addtag('above', tagOrId)
801 def addtag_all(self):
802 self.addtag('all')
803 def addtag_below(self, tagOrId):
804 self.addtag('below', tagOrId)
805 def addtag_closest(self, x, y, halo=None, start=None):
806 self.addtag('closest', x, y, halo, start)
807 def addtag_enclosed(self, x1, y1, x2, y2):
808 self.addtag('enclosed', x1, y1, x2, y2)
809 def addtag_overlapping(self, x1, y1, x2, y2):
810 self.addtag('overlapping', x1, y1, x2, y2)
811 def addtag_withtag(self, tagOrId):
812 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000813 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000814 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000815 def tag_unbind(self, tagOrId, sequence):
816 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
817 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000818 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000819 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000820 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000821 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000822 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000823 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000824 self._w, 'canvasx', screenx, gridspacing))
825 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000826 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000827 self._w, 'canvasy', screeny, gridspacing))
828 def coords(self, *args):
829 return self._do('coords', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000830 def _create(self, itemType, args, kw): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000831 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000832 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000833 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000834 args = args[:-1]
835 else:
836 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000837 return self.tk.getint(apply(
838 self.tk.call,
839 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000840 + args + self._options(cnf, kw)))
841 def create_arc(self, *args, **kw):
842 return self._create('arc', args, kw)
843 def create_bitmap(self, *args, **kw):
844 return self._create('bitmap', args, kw)
845 def create_image(self, *args, **kw):
846 return self._create('image', args, kw)
847 def create_line(self, *args, **kw):
848 return self._create('line', args, kw)
849 def create_oval(self, *args, **kw):
850 return self._create('oval', args, kw)
851 def create_polygon(self, *args, **kw):
852 return self._create('polygon', args, kw)
853 def create_rectangle(self, *args, **kw):
854 return self._create('rectangle', args, kw)
855 def create_text(self, *args, **kw):
856 return self._create('text', args, kw)
857 def create_window(self, *args, **kw):
858 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000859 def dchars(self, *args):
860 self._do('dchars', args)
861 def delete(self, *args):
862 self._do('delete', args)
863 def dtag(self, *args):
864 self._do('dtag', args)
865 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000866 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000867 def find_above(self, tagOrId):
868 return self.find('above', tagOrId)
869 def find_all(self):
870 return self.find('all')
871 def find_below(self, tagOrId):
872 return self.find('below', tagOrId)
873 def find_closest(self, x, y, halo=None, start=None):
874 return self.find('closest', x, y, halo, start)
875 def find_enclosed(self, x1, y1, x2, y2):
876 return self.find('enclosed', x1, y1, x2, y2)
877 def find_overlapping(self, x1, y1, x2, y2):
878 return self.find('overlapping', x1, y1, x2, y2)
879 def find_withtag(self, tagOrId):
880 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000881 def focus(self, *args):
882 return self._do('focus', args)
883 def gettags(self, *args):
884 return self.tk.splitlist(self._do('gettags', args))
885 def icursor(self, *args):
886 self._do('icursor', args)
887 def index(self, *args):
888 return self.tk.getint(self._do('index', args))
889 def insert(self, *args):
890 self._do('insert', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000891 def itemconfig(self, tagOrId, cnf=None, **kw):
892 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000893 cnf = {}
894 for x in self.tk.split(
895 self._do('itemconfigure', (tagOrId))):
896 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
897 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000898 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000899 x = self.tk.split(self._do('itemconfigure',
900 (tagOrId, '-'+cnf,)))
901 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000902 self._do('itemconfigure', (tagOrId,)
903 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000904 def lower(self, *args):
905 self._do('lower', args)
906 def move(self, *args):
907 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000908 def postscript(self, cnf={}, **kw):
909 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000910 def tkraise(self, *args):
911 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000912 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000913 def scale(self, *args):
914 self._do('scale', args)
915 def scan_mark(self, x, y):
916 self.tk.call(self._w, 'scan', 'mark', x, y)
917 def scan_dragto(self, x, y):
918 self.tk.call(self._w, 'scan', 'dragto', x, y)
919 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000920 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000921 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000922 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000923 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000924 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000925 def select_item(self):
926 self.tk.call(self._w, 'select', 'item')
927 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000928 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000929 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000930 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000931 def xview(self, *args):
932 apply(self.tk.call, (self._w, 'xview')+args)
933 def yview(self, *args):
934 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +0000935
936class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000937 def __init__(self, master=None, cnf={}, **kw):
938 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000939 def deselect(self):
940 self.tk.call(self._w, 'deselect')
941 def flash(self):
942 self.tk.call(self._w, 'flash')
943 def invoke(self):
944 self.tk.call(self._w, 'invoke')
945 def select(self):
946 self.tk.call(self._w, 'select')
947 def toggle(self):
948 self.tk.call(self._w, 'toggle')
949
950class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000951 def __init__(self, master=None, cnf={}, **kw):
952 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000953 def tk_entryBackspace(self):
954 self.tk.call('tk_entryBackspace', self._w)
955 def tk_entryBackword(self):
956 self.tk.call('tk_entryBackword', self._w)
957 def tk_entrySeeCaret(self):
958 self.tk.call('tk_entrySeeCaret', self._w)
959 def delete(self, first, last=None):
960 self.tk.call(self._w, 'delete', first, last)
961 def get(self):
962 return self.tk.call(self._w, 'get')
963 def icursor(self, index):
964 self.tk.call(self._w, 'icursor', index)
965 def index(self, index):
966 return self.tk.getint(self.tk.call(
967 self._w, 'index', index))
968 def insert(self, index, string):
969 self.tk.call(self._w, 'insert', index, string)
970 def scan_mark(self, x):
971 self.tk.call(self._w, 'scan', 'mark', x)
972 def scan_dragto(self, x):
973 self.tk.call(self._w, 'scan', 'dragto', x)
974 def select_adjust(self, index):
975 self.tk.call(self._w, 'select', 'adjust', index)
976 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000977 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000978 def select_from(self, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000979 self.tk.call(self._w, 'select', 'set', index)
Guido van Rossum1d59df21995-08-11 14:21:06 +0000980 def select_present(self):
981 return self.tk.getboolean(
982 self.tk.call(self._w, 'select', 'present'))
983 def select_range(self, start, end):
984 self.tk.call(self._w, 'select', 'range', start, end)
Guido van Rossum18468821994-06-20 07:49:28 +0000985 def select_to(self, index):
986 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000987 def view(self, index):
988 self.tk.call(self._w, 'view', index)
Guido van Rossum18468821994-06-20 07:49:28 +0000989
990class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000991 def __init__(self, master=None, cnf={}, **kw):
992 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000993 extra = ()
994 if cnf.has_key('class'):
995 extra = ('-class', cnf['class'])
996 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000997 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +0000998 def tk_menuBar(self, *args):
999 apply(self.tk.call, ('tk_menuBar', self._w) + args)
1000
1001class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001002 def __init__(self, master=None, cnf={}, **kw):
1003 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001004
Guido van Rossum18468821994-06-20 07:49:28 +00001005class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001006 def __init__(self, master=None, cnf={}, **kw):
1007 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001008 def tk_listboxSingleSelect(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001009 if TkVersion >= 4.0:
1010 self['selectmode'] = 'single'
1011 else:
1012 self.tk.call('tk_listboxSingleSelect', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001013 def curselection(self):
1014 return self.tk.splitlist(self.tk.call(
1015 self._w, 'curselection'))
1016 def delete(self, first, last=None):
1017 self.tk.call(self._w, 'delete', first, last)
1018 def get(self, index):
1019 return self.tk.call(self._w, 'get', index)
1020 def insert(self, index, *elements):
1021 apply(self.tk.call,
1022 (self._w, 'insert', index) + elements)
1023 def nearest(self, y):
1024 return self.tk.getint(self.tk.call(
1025 self._w, 'nearest', y))
1026 def scan_mark(self, x, y):
1027 self.tk.call(self._w, 'scan', 'mark', x, y)
1028 def scan_dragto(self, x, y):
1029 self.tk.call(self._w, 'scan', 'dragto', x, y)
1030 def select_adjust(self, index):
1031 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001032 if TkVersion >= 4.0:
1033 def select_anchor(self, index):
1034 self.tk.call(self._w, 'selection', 'anchor', index)
1035 def select_clear(self, first, last=None):
1036 self.tk.call(self._w,
1037 'selection', 'clear', first, last)
1038 def select_includes(self, index):
1039 return self.tk.getboolean(self.tk.call(
1040 self._w, 'selection', 'includes', index))
1041 def select_set(self, first, last=None):
1042 self.tk.call(self._w, 'selection', 'set', first, last)
1043 else:
1044 def select_clear(self):
1045 self.tk.call(self._w, 'select', 'clear')
1046 def select_from(self, index):
1047 self.tk.call(self._w, 'select', 'from', index)
1048 def select_to(self, index):
1049 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001050 def size(self):
1051 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001052 def xview(self, *what):
1053 apply(self.tk.call, (self._w, 'xview')+what)
1054 def yview(self, *what):
1055 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001056
1057class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001058 def __init__(self, master=None, cnf={}, **kw):
1059 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001060 def tk_bindForTraversal(self):
1061 self.tk.call('tk_bindForTraversal', self._w)
1062 def tk_mbPost(self):
1063 self.tk.call('tk_mbPost', self._w)
1064 def tk_mbUnpost(self):
1065 self.tk.call('tk_mbUnpost')
1066 def tk_traverseToMenu(self, char):
1067 self.tk.call('tk_traverseToMenu', self._w, char)
1068 def tk_traverseWithinMenu(self, char):
1069 self.tk.call('tk_traverseWithinMenu', self._w, char)
1070 def tk_getMenuButtons(self):
1071 return self.tk.call('tk_getMenuButtons', self._w)
1072 def tk_nextMenu(self, count):
1073 self.tk.call('tk_nextMenu', count)
1074 def tk_nextMenuEntry(self, count):
1075 self.tk.call('tk_nextMenuEntry', count)
1076 def tk_invokeMenu(self):
1077 self.tk.call('tk_invokeMenu', self._w)
1078 def tk_firstMenu(self):
1079 self.tk.call('tk_firstMenu', self._w)
1080 def tk_mbButtonDown(self):
1081 self.tk.call('tk_mbButtonDown', self._w)
1082 def activate(self, index):
1083 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001084 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001085 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001086 + self._options(cnf, kw))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001087 def add_cascade(self, cnf={}):
1088 self.add('cascade', cnf)
1089 def add_checkbutton(self, cnf={}):
1090 self.add('checkbutton', cnf)
1091 def add_command(self, cnf={}):
1092 self.add('command', cnf)
1093 def add_radiobutton(self, cnf={}):
1094 self.add('radiobutton', cnf)
1095 def add_separator(self, cnf={}):
1096 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001097 def delete(self, index1, index2=None):
1098 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001099 def entryconfig(self, index, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001100 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001101 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001102 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001103 i = self.tk.call(self._w, 'index', index)
1104 if i == 'none': return None
1105 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001106 def invoke(self, index):
1107 return self.tk.call(self._w, 'invoke', index)
1108 def post(self, x, y):
1109 self.tk.call(self._w, 'post', x, y)
1110 def unpost(self):
1111 self.tk.call(self._w, 'unpost')
1112 def yposition(self, index):
1113 return self.tk.getint(self.tk.call(
1114 self._w, 'yposition', index))
1115
1116class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001117 def __init__(self, master=None, cnf={}, **kw):
1118 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001119
1120class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001121 def __init__(self, master=None, cnf={}, **kw):
1122 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001123
1124class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001125 def __init__(self, master=None, cnf={}, **kw):
1126 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def deselect(self):
1128 self.tk.call(self._w, 'deselect')
1129 def flash(self):
1130 self.tk.call(self._w, 'flash')
1131 def invoke(self):
1132 self.tk.call(self._w, 'invoke')
1133 def select(self):
1134 self.tk.call(self._w, 'select')
1135
1136class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001137 def __init__(self, master=None, cnf={}, **kw):
1138 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001139 def get(self):
1140 return self.tk.getint(self.tk.call(self._w, 'get'))
1141 def set(self, value):
1142 self.tk.call(self._w, 'set', value)
1143
1144class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001145 def __init__(self, master=None, cnf={}, **kw):
1146 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001147 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001148 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001149 def set(self, *args):
1150 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001151
1152class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001153 def __init__(self, master=None, cnf={}, **kw):
1154 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001155 self.bind('<Delete>', self.bspace)
1156 def bspace(self, *args):
1157 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001158 def tk_textSelectTo(self, index):
1159 self.tk.call('tk_textSelectTo', self._w, index)
1160 def tk_textBackspace(self):
1161 self.tk.call('tk_textBackspace', self._w)
1162 def tk_textIndexCloser(self, a, b, c):
1163 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1164 def tk_textResetAnchor(self, index):
1165 self.tk.call('tk_textResetAnchor', self._w, index)
1166 def compare(self, index1, op, index2):
1167 return self.tk.getboolean(self.tk.call(
1168 self._w, 'compare', index1, op, index2))
1169 def debug(self, boolean=None):
1170 return self.tk.getboolean(self.tk.call(
1171 self._w, 'debug', boolean))
1172 def delete(self, index1, index2=None):
1173 self.tk.call(self._w, 'delete', index1, index2)
1174 def get(self, index1, index2=None):
1175 return self.tk.call(self._w, 'get', index1, index2)
1176 def index(self, index):
1177 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001178 def insert(self, index, chars, *args):
1179 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001180 def mark_names(self):
1181 return self.tk.splitlist(self.tk.call(
1182 self._w, 'mark', 'names'))
1183 def mark_set(self, markName, index):
1184 self.tk.call(self._w, 'mark', 'set', markName, index)
1185 def mark_unset(self, markNames):
1186 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1187 def scan_mark(self, y):
1188 self.tk.call(self._w, 'scan', 'mark', y)
1189 def scan_dragto(self, y):
1190 self.tk.call(self._w, 'scan', 'dragto', y)
1191 def tag_add(self, tagName, index1, index2=None):
1192 self.tk.call(
1193 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001194 def tag_unbind(self, tagName, sequence):
1195 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001196 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001197 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001198 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001199 self.tk.call(self._w, 'tag', 'bind',
1200 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001201 (add + name,) + self._subst_format)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001202 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001203 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001204 (self._w, 'tag', 'configure', tagName)
1205 + self._options(cnf, kw))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001206 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001207 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001208 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001209 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001210 def tag_names(self, index=None):
1211 return self.tk.splitlist(
1212 self.tk.call(self._w, 'tag', 'names', index))
1213 def tag_nextrange(self, tagName, index1, index2=None):
1214 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001215 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001216 def tag_raise(self, tagName, aboveThis=None):
1217 self.tk.call(
1218 self._w, 'tag', 'raise', tagName, aboveThis)
1219 def tag_ranges(self, tagName):
1220 return self.tk.splitlist(self.tk.call(
1221 self._w, 'tag', 'ranges', tagName))
1222 def tag_remove(self, tagName, index1, index2=None):
1223 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001224 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001225 def window_cget(self, index, option):
1226 return self.tk.call(self._w, 'window', 'cget', index, option)
1227 def window_config(self, index, cnf={}, **kw):
1228 apply(self.tk.call,
1229 (self._w, 'window', 'configure', index)
1230 + self._options(cnf, kw))
1231 def window_create(self, index, cnf={}, **kw):
1232 apply(self.tk.call,
1233 (self._w, 'window', 'create', index)
1234 + self._options(cnf, kw))
1235 def window_names(self):
1236 return self.tk.splitlist(
1237 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001238 def yview(self, *what):
1239 apply(self.tk.call, (self._w, 'yview')+what)
1240 def yview_pickplace(self, *what):
1241 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001242
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001243class OptionMenu(Widget):
1244 def __init__(self, master, variable, value, *values):
1245 self.widgetName = 'tk_optionMenu'
1246 Widget._setup(self, master, {})
1247 self.menuname = apply(
1248 self.tk.call,
1249 (self.widgetName, self._w, variable, value) + values)
1250
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001251class Image:
1252 def __init__(self, imgtype, name=None, cnf={}, **kw):
1253 self.name = None
1254 master = _default_root
1255 if not master: raise RuntimeError, 'Too early to create image'
1256 self.tk = master.tk
1257 if not name: name = `id(self)`
1258 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1259 elif kw: cnf = kw
1260 options = ()
1261 for k, v in cnf.items():
1262 if type(v) in CallableTypes:
1263 v = self._register(v)
1264 options = options + ('-'+k, v)
1265 apply(self.tk.call,
1266 ('image', 'create', imgtype, name,) + options)
1267 self.name = name
1268 def __str__(self): return self.name
1269 def __del__(self):
1270 if self.name:
1271 self.tk.call('image', 'delete', self.name)
1272 def height(self):
1273 return self.tk.getint(
1274 self.tk.call('image', 'height', self.name))
1275 def type(self):
1276 return self.tk.call('image', 'type', self.name)
1277 def width(self):
1278 return self.tk.getint(
1279 self.tk.call('image', 'width', self.name))
1280
1281class PhotoImage(Image):
1282 def __init__(self, name=None, cnf={}, **kw):
1283 apply(Image.__init__, (self, 'photo', name, cnf), kw)
1284
1285class BitmapImage(Image):
1286 def __init__(self, name=None, cnf={}, **kw):
1287 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1288
1289def image_names(): return _default_root.tk.call('image', 'names')
1290def image_types(): return _default_root.tk.call('image', 'types')
1291
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001292######################################################################
1293# Extensions:
1294
1295class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001296 def __init__(self, master=None, cnf={}, **kw):
1297 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001298 self.bind('<Any-Enter>', self.tkButtonEnter)
1299 self.bind('<Any-Leave>', self.tkButtonLeave)
1300 self.bind('<1>', self.tkButtonDown)
1301 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001302
1303class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001304 def __init__(self, master=None, cnf={}, **kw):
1305 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001306 self.bind('<Any-Enter>', self.tkButtonEnter)
1307 self.bind('<Any-Leave>', self.tkButtonLeave)
1308 self.bind('<1>', self.tkButtonDown)
1309 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1310 self['fg'] = self['bg']
1311 self['activebackground'] = self['bg']