blob: 3b52ae11827096c6cf17de568fe6651593e84ace [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 Rossum71b1a901995-09-18 21:54:35 +0000428 f = self._wrap(func, subst)
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 Rossum71b1a901995-09-18 21:54:35 +0000477 def _wrap(self, func, subst=None):
478 return CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000479
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000480class CallWrapper:
481 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000482 self.func = func
483 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000484 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000485 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000486 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000487 if self.subst:
488 args = apply(self.subst, args)
489 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000490 except SystemExit, msg:
491 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000492 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000493 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000494
495class Wm:
496 def aspect(self,
497 minNumer=None, minDenom=None,
498 maxNumer=None, maxDenom=None):
499 return self._getints(
500 self.tk.call('wm', 'aspect', self._w,
501 minNumer, minDenom,
502 maxNumer, maxDenom))
503 def client(self, name=None):
504 return self.tk.call('wm', 'client', self._w, name)
505 def command(self, value=None):
506 return self.tk.call('wm', 'command', self._w, value)
507 def deiconify(self):
508 return self.tk.call('wm', 'deiconify', self._w)
509 def focusmodel(self, model=None):
510 return self.tk.call('wm', 'focusmodel', self._w, model)
511 def frame(self):
512 return self.tk.call('wm', 'frame', self._w)
513 def geometry(self, newGeometry=None):
514 return self.tk.call('wm', 'geometry', self._w, newGeometry)
515 def grid(self,
516 baseWidht=None, baseHeight=None,
517 widthInc=None, heightInc=None):
518 return self._getints(self.tk.call(
519 'wm', 'grid', self._w,
520 baseWidht, baseHeight, widthInc, heightInc))
521 def group(self, pathName=None):
522 return self.tk.call('wm', 'group', self._w, pathName)
523 def iconbitmap(self, bitmap=None):
524 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
525 def iconify(self):
526 return self.tk.call('wm', 'iconify', self._w)
527 def iconmask(self, bitmap=None):
528 return self.tk.call('wm', 'iconmask', self._w, bitmap)
529 def iconname(self, newName=None):
530 return self.tk.call('wm', 'iconname', self._w, newName)
531 def iconposition(self, x=None, y=None):
532 return self._getints(self.tk.call(
533 'wm', 'iconposition', self._w, x, y))
534 def iconwindow(self, pathName=None):
535 return self.tk.call('wm', 'iconwindow', self._w, pathName)
536 def maxsize(self, width=None, height=None):
537 return self._getints(self.tk.call(
538 'wm', 'maxsize', self._w, width, height))
539 def minsize(self, width=None, height=None):
540 return self._getints(self.tk.call(
541 'wm', 'minsize', self._w, width, height))
542 def overrideredirect(self, boolean=None):
543 return self._getboolean(self.tk.call(
544 'wm', 'overrideredirect', self._w, boolean))
545 def positionfrom(self, who=None):
546 return self.tk.call('wm', 'positionfrom', self._w, who)
547 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000548 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000549 command = self._register(func)
550 else:
551 command = func
552 return self.tk.call(
553 'wm', 'protocol', self._w, name, command)
554 def sizefrom(self, who=None):
555 return self.tk.call('wm', 'sizefrom', self._w, who)
556 def state(self):
557 return self.tk.call('wm', 'state', self._w)
558 def title(self, string=None):
559 return self.tk.call('wm', 'title', self._w, string)
560 def transient(self, master=None):
561 return self.tk.call('wm', 'transient', self._w, master)
562 def withdraw(self):
563 return self.tk.call('wm', 'withdraw', self._w)
564
565class Tk(Misc, Wm):
566 _w = '.'
567 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000568 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000569 self.master = None
570 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000571 if baseName is None:
572 import sys, os
573 baseName = os.path.basename(sys.argv[0])
574 if baseName[-3:] == '.py': baseName = baseName[:-3]
575 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000576 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000577 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000578 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000579 if not _default_root:
580 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000581 def destroy(self):
582 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000583 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000584 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000585 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000586 def readprofile(self, baseName, className):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000587 ##print __import__
588 import os, pdb
Guido van Rossum27b77a41994-07-12 15:52:32 +0000589 if os.environ.has_key('HOME'): home = os.environ['HOME']
590 else: home = os.curdir
591 class_tcl = os.path.join(home, '.%s.tcl' % className)
592 class_py = os.path.join(home, '.%s.py' % className)
593 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
594 base_py = os.path.join(home, '.%s.py' % baseName)
595 dir = {'self': self}
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000596 ##pdb.run('from Tkinter import *', dir)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000597 exec 'from Tkinter import *' in dir
598 if os.path.isfile(class_tcl):
599 print 'source', `class_tcl`
600 self.tk.call('source', class_tcl)
601 if os.path.isfile(class_py):
602 print 'execfile', `class_py`
603 execfile(class_py, dir)
604 if os.path.isfile(base_tcl):
605 print 'source', `base_tcl`
606 self.tk.call('source', base_tcl)
607 if os.path.isfile(base_py):
608 print 'execfile', `base_py`
609 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000610 def report_callback_exception(self, exc, val, tb):
611 import traceback
612 print "Exception in Tkinter callback"
613 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000614
615class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000616 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000617 apply(self.tk.call,
618 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000619 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000620 pack = config
621 def __setitem__(self, key, value):
622 Pack.config({key: value})
623 def forget(self):
624 self.tk.call('pack', 'forget', self._w)
625 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000626 words = self.tk.splitlist(
627 self.tk.call('pack', 'newinfo', self._w))
628 dict = {}
629 for i in range(0, len(words), 2):
630 key = words[i][1:]
631 value = words[i+1]
632 if value[0] == '.':
633 value = self._nametowidget(value)
634 dict[key] = value
635 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000636 info = newinfo
Guido van Rossum5505d561994-12-30 17:16:35 +0000637 _noarg_ = ['_noarg_']
638 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000639 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000640 return self._getboolean(self.tk.call(
641 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000642 else:
643 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum18468821994-06-20 07:49:28 +0000644 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000645 return map(self._nametowidget,
646 self.tk.splitlist(
647 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000648
649class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000650 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000651 apply(self.tk.call,
652 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000653 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000654 place = config
655 def __setitem__(self, key, value):
656 Place.config({key: value})
657 def forget(self):
658 self.tk.call('place', 'forget', self._w)
659 def info(self):
660 return self.tk.call('place', 'info', self._w)
661 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000662 return map(self._nametowidget,
663 self.tk.splitlist(
664 self.tk.call(
665 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000666
Guido van Rossum18468821994-06-20 07:49:28 +0000667class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000668 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000669 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000670 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000671 if not _default_root:
672 _default_root = Tk()
673 master = _default_root
674 if not _default_root:
675 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000676 self.master = master
677 self.tk = master.tk
678 if cnf.has_key('name'):
679 name = cnf['name']
680 del cnf['name']
681 else:
682 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000683 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000684 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000685 self._w = '.' + name
686 else:
687 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000688 self.children = {}
689 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000690 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000691 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000692 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
693 if kw:
694 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000695 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000696 Widget._setup(self, master, cnf)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +0000697 apply(self.tk.call, (widgetName, self._w)+extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000698 if cnf:
699 Widget.config(self, cnf)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000700 def config(self, cnf=None, **kw):
701 # XXX ought to generalize this so tag_config etc. can use it
702 if kw:
703 cnf = _cnfmerge((cnf, kw))
704 else:
705 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000706 if cnf is None:
707 cnf = {}
708 for x in self.tk.split(
709 self.tk.call(self._w, 'configure')):
710 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
711 return cnf
712 if type(cnf) == StringType:
713 x = self.tk.split(self.tk.call(
714 self._w, 'configure', '-'+cnf))
715 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000716 for k in cnf.keys():
717 if type(k) == ClassType:
718 k.config(self, cnf[k])
719 del cnf[k]
720 apply(self.tk.call, (self._w, 'configure')
721 + self._options(cnf))
722 def __getitem__(self, key):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000723 if TkVersion >= 4.0:
724 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000725 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000726 self._w, 'configure', '-' + key))
727 return v[4]
728 def __setitem__(self, key, value):
729 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000730 def keys(self):
731 return map(lambda x: x[0][1:],
732 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000733 def __str__(self):
734 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000735 def destroy(self):
736 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000737 if self.master.children.has_key(self._name):
738 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000739 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000740 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000741 return apply(self.tk.call, (self._w, name) + args)
742 def unbind_class(self, seq):
743 Misc.unbind_class(self, self.widgetName, seq)
Guido van Rossum18468821994-06-20 07:49:28 +0000744
745class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000746 def __init__(self, master=None, cnf={}, **kw):
747 if kw:
748 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000749 extra = ()
750 if cnf.has_key('screen'):
751 extra = ('-screen', cnf['screen'])
752 del cnf['screen']
753 if cnf.has_key('class'):
754 extra = extra + ('-class', cnf['class'])
755 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000756 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000757 root = self._root()
758 self.iconname(root.iconname())
759 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000760
761class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000762 def __init__(self, master=None, cnf={}, **kw):
763 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000764 def tkButtonEnter(self, *dummy):
765 self.tk.call('tkButtonEnter', self._w)
766 def tkButtonLeave(self, *dummy):
767 self.tk.call('tkButtonLeave', self._w)
768 def tkButtonDown(self, *dummy):
769 self.tk.call('tkButtonDown', self._w)
770 def tkButtonUp(self, *dummy):
771 self.tk.call('tkButtonUp', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000772 def flash(self):
773 self.tk.call(self._w, 'flash')
774 def invoke(self):
775 self.tk.call(self._w, 'invoke')
776
777# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000778# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000779def AtEnd():
780 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000781def AtInsert(*args):
782 s = 'insert'
783 for a in args:
784 if a: s = s + (' ' + a)
785 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000786def AtSelFirst():
787 return 'sel.first'
788def AtSelLast():
789 return 'sel.last'
790def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000791 if y is None:
792 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000793 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000794 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000795
796class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000797 def __init__(self, master=None, cnf={}, **kw):
798 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000799 def addtag(self, *args):
800 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000801 def addtag_above(self, tagOrId):
802 self.addtag('above', tagOrId)
803 def addtag_all(self):
804 self.addtag('all')
805 def addtag_below(self, tagOrId):
806 self.addtag('below', tagOrId)
807 def addtag_closest(self, x, y, halo=None, start=None):
808 self.addtag('closest', x, y, halo, start)
809 def addtag_enclosed(self, x1, y1, x2, y2):
810 self.addtag('enclosed', x1, y1, x2, y2)
811 def addtag_overlapping(self, x1, y1, x2, y2):
812 self.addtag('overlapping', x1, y1, x2, y2)
813 def addtag_withtag(self, tagOrId):
814 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000815 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000816 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000817 def tag_unbind(self, tagOrId, sequence):
818 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
819 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000820 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000821 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000822 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000823 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000824 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000825 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000826 self._w, 'canvasx', screenx, gridspacing))
827 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000828 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000829 self._w, 'canvasy', screeny, gridspacing))
830 def coords(self, *args):
831 return self._do('coords', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000832 def _create(self, itemType, args, kw): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000833 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000834 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000835 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000836 args = args[:-1]
837 else:
838 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000839 return self.tk.getint(apply(
840 self.tk.call,
841 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000842 + args + self._options(cnf, kw)))
843 def create_arc(self, *args, **kw):
844 return self._create('arc', args, kw)
845 def create_bitmap(self, *args, **kw):
846 return self._create('bitmap', args, kw)
847 def create_image(self, *args, **kw):
848 return self._create('image', args, kw)
849 def create_line(self, *args, **kw):
850 return self._create('line', args, kw)
851 def create_oval(self, *args, **kw):
852 return self._create('oval', args, kw)
853 def create_polygon(self, *args, **kw):
854 return self._create('polygon', args, kw)
855 def create_rectangle(self, *args, **kw):
856 return self._create('rectangle', args, kw)
857 def create_text(self, *args, **kw):
858 return self._create('text', args, kw)
859 def create_window(self, *args, **kw):
860 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000861 def dchars(self, *args):
862 self._do('dchars', args)
863 def delete(self, *args):
864 self._do('delete', args)
865 def dtag(self, *args):
866 self._do('dtag', args)
867 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000868 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000869 def find_above(self, tagOrId):
870 return self.find('above', tagOrId)
871 def find_all(self):
872 return self.find('all')
873 def find_below(self, tagOrId):
874 return self.find('below', tagOrId)
875 def find_closest(self, x, y, halo=None, start=None):
876 return self.find('closest', x, y, halo, start)
877 def find_enclosed(self, x1, y1, x2, y2):
878 return self.find('enclosed', x1, y1, x2, y2)
879 def find_overlapping(self, x1, y1, x2, y2):
880 return self.find('overlapping', x1, y1, x2, y2)
881 def find_withtag(self, tagOrId):
882 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000883 def focus(self, *args):
884 return self._do('focus', args)
885 def gettags(self, *args):
886 return self.tk.splitlist(self._do('gettags', args))
887 def icursor(self, *args):
888 self._do('icursor', args)
889 def index(self, *args):
890 return self.tk.getint(self._do('index', args))
891 def insert(self, *args):
892 self._do('insert', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000893 def itemconfig(self, tagOrId, cnf=None, **kw):
894 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000895 cnf = {}
896 for x in self.tk.split(
897 self._do('itemconfigure', (tagOrId))):
898 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
899 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000900 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000901 x = self.tk.split(self._do('itemconfigure',
902 (tagOrId, '-'+cnf,)))
903 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000904 self._do('itemconfigure', (tagOrId,)
905 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000906 def lower(self, *args):
907 self._do('lower', args)
908 def move(self, *args):
909 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000910 def postscript(self, cnf={}, **kw):
911 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000912 def tkraise(self, *args):
913 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000914 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000915 def scale(self, *args):
916 self._do('scale', args)
917 def scan_mark(self, x, y):
918 self.tk.call(self._w, 'scan', 'mark', x, y)
919 def scan_dragto(self, x, y):
920 self.tk.call(self._w, 'scan', 'dragto', x, y)
921 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000922 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000923 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000924 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000925 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000926 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000927 def select_item(self):
928 self.tk.call(self._w, 'select', 'item')
929 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000930 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000931 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000932 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000933 def xview(self, *args):
934 apply(self.tk.call, (self._w, 'xview')+args)
935 def yview(self, *args):
936 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +0000937
938class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000939 def __init__(self, master=None, cnf={}, **kw):
940 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000941 def deselect(self):
942 self.tk.call(self._w, 'deselect')
943 def flash(self):
944 self.tk.call(self._w, 'flash')
945 def invoke(self):
946 self.tk.call(self._w, 'invoke')
947 def select(self):
948 self.tk.call(self._w, 'select')
949 def toggle(self):
950 self.tk.call(self._w, 'toggle')
951
952class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000953 def __init__(self, master=None, cnf={}, **kw):
954 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000955 def tk_entryBackspace(self):
956 self.tk.call('tk_entryBackspace', self._w)
957 def tk_entryBackword(self):
958 self.tk.call('tk_entryBackword', self._w)
959 def tk_entrySeeCaret(self):
960 self.tk.call('tk_entrySeeCaret', self._w)
961 def delete(self, first, last=None):
962 self.tk.call(self._w, 'delete', first, last)
963 def get(self):
964 return self.tk.call(self._w, 'get')
965 def icursor(self, index):
966 self.tk.call(self._w, 'icursor', index)
967 def index(self, index):
968 return self.tk.getint(self.tk.call(
969 self._w, 'index', index))
970 def insert(self, index, string):
971 self.tk.call(self._w, 'insert', index, string)
972 def scan_mark(self, x):
973 self.tk.call(self._w, 'scan', 'mark', x)
974 def scan_dragto(self, x):
975 self.tk.call(self._w, 'scan', 'dragto', x)
976 def select_adjust(self, index):
977 self.tk.call(self._w, 'select', 'adjust', index)
978 def select_clear(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000979 self.tk.call(self._w, 'select', 'clear', 'end')
Guido van Rossum18468821994-06-20 07:49:28 +0000980 def select_from(self, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000981 self.tk.call(self._w, 'select', 'set', index)
Guido van Rossum1d59df21995-08-11 14:21:06 +0000982 def select_present(self):
983 return self.tk.getboolean(
984 self.tk.call(self._w, 'select', 'present'))
985 def select_range(self, start, end):
986 self.tk.call(self._w, 'select', 'range', start, end)
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def select_to(self, index):
988 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000989 def view(self, index):
990 self.tk.call(self._w, 'view', index)
Guido van Rossum18468821994-06-20 07:49:28 +0000991
992class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000993 def __init__(self, master=None, cnf={}, **kw):
994 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000995 extra = ()
996 if cnf.has_key('class'):
997 extra = ('-class', cnf['class'])
998 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000999 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001000 def tk_menuBar(self, *args):
1001 apply(self.tk.call, ('tk_menuBar', self._w) + args)
1002
1003class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001004 def __init__(self, master=None, cnf={}, **kw):
1005 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001006
Guido van Rossum18468821994-06-20 07:49:28 +00001007class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001008 def __init__(self, master=None, cnf={}, **kw):
1009 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001010 def tk_listboxSingleSelect(self):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001011 if TkVersion >= 4.0:
1012 self['selectmode'] = 'single'
1013 else:
1014 self.tk.call('tk_listboxSingleSelect', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001015 def curselection(self):
1016 return self.tk.splitlist(self.tk.call(
1017 self._w, 'curselection'))
1018 def delete(self, first, last=None):
1019 self.tk.call(self._w, 'delete', first, last)
1020 def get(self, index):
1021 return self.tk.call(self._w, 'get', index)
1022 def insert(self, index, *elements):
1023 apply(self.tk.call,
1024 (self._w, 'insert', index) + elements)
1025 def nearest(self, y):
1026 return self.tk.getint(self.tk.call(
1027 self._w, 'nearest', y))
1028 def scan_mark(self, x, y):
1029 self.tk.call(self._w, 'scan', 'mark', x, y)
1030 def scan_dragto(self, x, y):
1031 self.tk.call(self._w, 'scan', 'dragto', x, y)
1032 def select_adjust(self, index):
1033 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001034 if TkVersion >= 4.0:
1035 def select_anchor(self, index):
1036 self.tk.call(self._w, 'selection', 'anchor', index)
1037 def select_clear(self, first, last=None):
1038 self.tk.call(self._w,
1039 'selection', 'clear', first, last)
1040 def select_includes(self, index):
1041 return self.tk.getboolean(self.tk.call(
1042 self._w, 'selection', 'includes', index))
1043 def select_set(self, first, last=None):
1044 self.tk.call(self._w, 'selection', 'set', first, last)
1045 else:
1046 def select_clear(self):
1047 self.tk.call(self._w, 'select', 'clear')
1048 def select_from(self, index):
1049 self.tk.call(self._w, 'select', 'from', index)
1050 def select_to(self, index):
1051 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001052 def size(self):
1053 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001054 def xview(self, *what):
1055 apply(self.tk.call, (self._w, 'xview')+what)
1056 def yview(self, *what):
1057 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001058
1059class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001060 def __init__(self, master=None, cnf={}, **kw):
1061 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001062 def tk_bindForTraversal(self):
1063 self.tk.call('tk_bindForTraversal', self._w)
1064 def tk_mbPost(self):
1065 self.tk.call('tk_mbPost', self._w)
1066 def tk_mbUnpost(self):
1067 self.tk.call('tk_mbUnpost')
1068 def tk_traverseToMenu(self, char):
1069 self.tk.call('tk_traverseToMenu', self._w, char)
1070 def tk_traverseWithinMenu(self, char):
1071 self.tk.call('tk_traverseWithinMenu', self._w, char)
1072 def tk_getMenuButtons(self):
1073 return self.tk.call('tk_getMenuButtons', self._w)
1074 def tk_nextMenu(self, count):
1075 self.tk.call('tk_nextMenu', count)
1076 def tk_nextMenuEntry(self, count):
1077 self.tk.call('tk_nextMenuEntry', count)
1078 def tk_invokeMenu(self):
1079 self.tk.call('tk_invokeMenu', self._w)
1080 def tk_firstMenu(self):
1081 self.tk.call('tk_firstMenu', self._w)
1082 def tk_mbButtonDown(self):
1083 self.tk.call('tk_mbButtonDown', self._w)
1084 def activate(self, index):
1085 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001086 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001087 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001088 + self._options(cnf, kw))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001089 def add_cascade(self, cnf={}):
1090 self.add('cascade', cnf)
1091 def add_checkbutton(self, cnf={}):
1092 self.add('checkbutton', cnf)
1093 def add_command(self, cnf={}):
1094 self.add('command', cnf)
1095 def add_radiobutton(self, cnf={}):
1096 self.add('radiobutton', cnf)
1097 def add_separator(self, cnf={}):
1098 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001099 def delete(self, index1, index2=None):
1100 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001101 def entryconfig(self, index, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001102 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001103 + self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001104 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001105 i = self.tk.call(self._w, 'index', index)
1106 if i == 'none': return None
1107 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001108 def invoke(self, index):
1109 return self.tk.call(self._w, 'invoke', index)
1110 def post(self, x, y):
1111 self.tk.call(self._w, 'post', x, y)
1112 def unpost(self):
1113 self.tk.call(self._w, 'unpost')
1114 def yposition(self, index):
1115 return self.tk.getint(self.tk.call(
1116 self._w, 'yposition', index))
1117
1118class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001119 def __init__(self, master=None, cnf={}, **kw):
1120 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001121
1122class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001123 def __init__(self, master=None, cnf={}, **kw):
1124 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001125
1126class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001127 def __init__(self, master=None, cnf={}, **kw):
1128 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001129 def deselect(self):
1130 self.tk.call(self._w, 'deselect')
1131 def flash(self):
1132 self.tk.call(self._w, 'flash')
1133 def invoke(self):
1134 self.tk.call(self._w, 'invoke')
1135 def select(self):
1136 self.tk.call(self._w, 'select')
1137
1138class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001139 def __init__(self, master=None, cnf={}, **kw):
1140 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001141 def get(self):
1142 return self.tk.getint(self.tk.call(self._w, 'get'))
1143 def set(self, value):
1144 self.tk.call(self._w, 'set', value)
1145
1146class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001147 def __init__(self, master=None, cnf={}, **kw):
1148 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001149 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001150 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001151 def set(self, *args):
1152 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001153
1154class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001155 def __init__(self, master=None, cnf={}, **kw):
1156 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001157 self.bind('<Delete>', self.bspace)
1158 def bspace(self, *args):
1159 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001160 def tk_textSelectTo(self, index):
1161 self.tk.call('tk_textSelectTo', self._w, index)
1162 def tk_textBackspace(self):
1163 self.tk.call('tk_textBackspace', self._w)
1164 def tk_textIndexCloser(self, a, b, c):
1165 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1166 def tk_textResetAnchor(self, index):
1167 self.tk.call('tk_textResetAnchor', self._w, index)
1168 def compare(self, index1, op, index2):
1169 return self.tk.getboolean(self.tk.call(
1170 self._w, 'compare', index1, op, index2))
1171 def debug(self, boolean=None):
1172 return self.tk.getboolean(self.tk.call(
1173 self._w, 'debug', boolean))
1174 def delete(self, index1, index2=None):
1175 self.tk.call(self._w, 'delete', index1, index2)
1176 def get(self, index1, index2=None):
1177 return self.tk.call(self._w, 'get', index1, index2)
1178 def index(self, index):
1179 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001180 def insert(self, index, chars, *args):
1181 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001182 def mark_names(self):
1183 return self.tk.splitlist(self.tk.call(
1184 self._w, 'mark', 'names'))
1185 def mark_set(self, markName, index):
1186 self.tk.call(self._w, 'mark', 'set', markName, index)
1187 def mark_unset(self, markNames):
1188 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1189 def scan_mark(self, y):
1190 self.tk.call(self._w, 'scan', 'mark', y)
1191 def scan_dragto(self, y):
1192 self.tk.call(self._w, 'scan', 'dragto', y)
1193 def tag_add(self, tagName, index1, index2=None):
1194 self.tk.call(
1195 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001196 def tag_unbind(self, tagName, sequence):
1197 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001198 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001199 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001200 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001201 self.tk.call(self._w, 'tag', 'bind',
1202 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001203 (add + name,) + self._subst_format)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001204 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001205 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001206 (self._w, 'tag', 'configure', tagName)
1207 + self._options(cnf, kw))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001208 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001209 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001210 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001211 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001212 def tag_names(self, index=None):
1213 return self.tk.splitlist(
1214 self.tk.call(self._w, 'tag', 'names', index))
1215 def tag_nextrange(self, tagName, index1, index2=None):
1216 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001217 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001218 def tag_raise(self, tagName, aboveThis=None):
1219 self.tk.call(
1220 self._w, 'tag', 'raise', tagName, aboveThis)
1221 def tag_ranges(self, tagName):
1222 return self.tk.splitlist(self.tk.call(
1223 self._w, 'tag', 'ranges', tagName))
1224 def tag_remove(self, tagName, index1, index2=None):
1225 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001226 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001227 def window_cget(self, index, option):
1228 return self.tk.call(self._w, 'window', 'cget', index, option)
1229 def window_config(self, index, cnf={}, **kw):
1230 apply(self.tk.call,
1231 (self._w, 'window', 'configure', index)
1232 + self._options(cnf, kw))
1233 def window_create(self, index, cnf={}, **kw):
1234 apply(self.tk.call,
1235 (self._w, 'window', 'create', index)
1236 + self._options(cnf, kw))
1237 def window_names(self):
1238 return self.tk.splitlist(
1239 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001240 def yview(self, *what):
1241 apply(self.tk.call, (self._w, 'yview')+what)
1242 def yview_pickplace(self, *what):
1243 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001244
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001245class OptionMenu(Widget):
1246 def __init__(self, master, variable, value, *values):
1247 self.widgetName = 'tk_optionMenu'
1248 Widget._setup(self, master, {})
1249 self.menuname = apply(
1250 self.tk.call,
1251 (self.widgetName, self._w, variable, value) + values)
1252
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001253class Image:
1254 def __init__(self, imgtype, name=None, cnf={}, **kw):
1255 self.name = None
1256 master = _default_root
1257 if not master: raise RuntimeError, 'Too early to create image'
1258 self.tk = master.tk
1259 if not name: name = `id(self)`
1260 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1261 elif kw: cnf = kw
1262 options = ()
1263 for k, v in cnf.items():
1264 if type(v) in CallableTypes:
1265 v = self._register(v)
1266 options = options + ('-'+k, v)
1267 apply(self.tk.call,
1268 ('image', 'create', imgtype, name,) + options)
1269 self.name = name
1270 def __str__(self): return self.name
1271 def __del__(self):
1272 if self.name:
1273 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001274 def __setitem__(self, key, value):
1275 self.tk.call(self.name, 'configure', '-'+key, value)
1276 def __getitem__(self, key):
1277 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001278 def height(self):
1279 return self.tk.getint(
1280 self.tk.call('image', 'height', self.name))
1281 def type(self):
1282 return self.tk.call('image', 'type', self.name)
1283 def width(self):
1284 return self.tk.getint(
1285 self.tk.call('image', 'width', self.name))
1286
1287class PhotoImage(Image):
1288 def __init__(self, name=None, cnf={}, **kw):
1289 apply(Image.__init__, (self, 'photo', name, cnf), kw)
1290
1291class BitmapImage(Image):
1292 def __init__(self, name=None, cnf={}, **kw):
1293 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1294
1295def image_names(): return _default_root.tk.call('image', 'names')
1296def image_types(): return _default_root.tk.call('image', 'types')
1297
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001298######################################################################
1299# Extensions:
1300
1301class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001302 def __init__(self, master=None, cnf={}, **kw):
1303 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001304 self.bind('<Any-Enter>', self.tkButtonEnter)
1305 self.bind('<Any-Leave>', self.tkButtonLeave)
1306 self.bind('<1>', self.tkButtonDown)
1307 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001308
1309class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001310 def __init__(self, master=None, cnf={}, **kw):
1311 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001312 self.bind('<Any-Enter>', self.tkButtonEnter)
1313 self.bind('<Any-Leave>', self.tkButtonLeave)
1314 self.bind('<1>', self.tkButtonDown)
1315 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1316 self['fg'] = self['bg']
1317 self['activebackground'] = self['bg']