blob: 7be94c75034699d36d753ffaf3c74bee7a57bdc9 [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
2import tkinter
3from tkinter import TclError
4
5class _Dummy:
6 def meth(self): return
7
8def _isfunctype(func):
Guido van Rossum08a40381994-06-21 11:44:21 +00009 return type(func) in CallableTypes
Guido van Rossum18468821994-06-20 07:49:28 +000010
11FunctionType = type(_isfunctype)
12ClassType = type(_Dummy)
13MethodType = type(_Dummy.meth)
Guido van Rossum08a40381994-06-21 11:44:21 +000014StringType = type('')
Guido van Rossum67ef5f31994-06-20 13:39:14 +000015TupleType = type(())
16ListType = type([])
Guido van Rossum08a40381994-06-21 11:44:21 +000017CallableTypes = (FunctionType, MethodType)
Guido van Rossum18468821994-06-20 07:49:28 +000018
Guido van Rossum45853db1994-06-20 12:19:19 +000019def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000020 pass
21
22class Event:
23 pass
24
25class Misc:
26 def tk_strictMotif(self, boolean=None):
27 self.tk.getboolean(self.tk.call(
28 'set', 'tk_strictMotif', boolean))
29 def waitvar(self, name='VAR'):
30 self.tk.call('tkwait', 'variable', name)
31 def setvar(self, name='VAR', value='1'):
32 self.tk.setvar(name, value)
Guido van Rossum5e8d3721994-06-20 08:12:01 +000033 def getvar(self, name='VAR'):
34 return self.tk.getvar(name)
35 def getint(self, s):
36 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +000037 def getdouble(self, s):
38 return self.tk.getdouble(s)
39 def getboolean(self, s):
40 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +000041 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +000042 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +000043 focus = focus_set # XXX b/w compat?
44 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +000045 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +000046 def focus_default_none(self):
47 self.tk.call('focus', 'default', 'none')
48 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +000049 def focus_none(self):
50 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +000051 def focus_get(self):
52 name = self.tk.call('focus')
53 if name == 'none': return None
54 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +000055 def after(self, ms, func=None, *args):
56 if not func:
57 self.tk.call('after', ms)
58 else:
Guido van Rossum08a40381994-06-21 11:44:21 +000059 # XXX Disgusting hack to clean up after calling func
60 tmp = []
61 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
62 try:
63 apply(func, args)
64 finally:
65 tk.deletecommand(tmp[0])
66 name = self._register(callit)
67 tmp.append(name)
68 self.tk.call('after', ms, name)
Guido van Rossum45853db1994-06-20 12:19:19 +000069 # XXX grab current w/o window argument
70 def grab_current(self):
71 name = self.tk.call('grab', 'current', self._w)
72 if not name: return None
73 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +000074 def grab_release(self):
75 self.tk.call('grab', 'release', self._w)
76 def grab_set(self):
77 self.tk.call('grab', 'set', self._w)
78 def grab_set_global(self):
79 self.tk.call('grab', 'set', '-global', self._w)
80 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +000081 status = self.tk.call('grab', 'status', self._w)
82 if status == 'none': status = None
83 return status
Guido van Rossum18468821994-06-20 07:49:28 +000084 def lower(self, belowThis=None):
85 self.tk.call('lower', self._w, belowThis)
86 def selection_clear(self):
87 self.tk.call('selection', 'clear', self._w)
88 def selection_get(self, type=None):
89 self.tk.call('selection', 'get', type)
90 def selection_handle(self, func, type=None, format=None):
91 name = self._register(func)
92 self.tk.call('selection', 'handle',
93 self._w, name, type, format)
94 #XXX def selection_own(self):
95 # self.tk.call('selection', 'own', self._w)
96 def send(self, interp, cmd, *args): #XXX
97 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +000098 def lower(self, belowThis=None):
99 self.tk.call('lift', self._w, belowThis)
100 def tkraise(self, aboveThis=None):
101 self.tk.call('raise', self._w, aboveThis)
102 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000103 def colormodel(self, value=None):
104 return self.tk.call('tk', 'colormodel', self._w, value)
105 def winfo_atom(self, name):
106 return self.tk.getint(self.tk.call('winfo', 'atom', name))
107 def winfo_atomname(self, id):
108 return self.tk.call('winfo', 'atomname', id)
109 def winfo_cells(self):
110 return self.tk.getint(
111 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000112 def winfo_children(self):
113 return map(self._nametowidget,
114 self.tk.splitlist(self.tk.call(
115 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000116 def winfo_class(self):
117 return self.tk.call('winfo', 'class', self._w)
118 def winfo_containing(self, rootX, rootY):
119 return self.tk.call('winfo', 'containing', rootx, rootY)
120 def winfo_depth(self):
121 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
122 def winfo_exists(self):
123 return self.tk.getint(
124 self.tk.call('winfo', 'exists', self._w))
125 def winfo_fpixels(self, number):
126 return self.tk.getdouble(self.tk.call(
127 'winfo', 'fpixels', self._w, number))
128 def winfo_geometry(self):
129 return self.tk.call('winfo', 'geometry', self._w)
130 def winfo_height(self):
131 return self.tk.getint(
132 self.tk.call('winfo', 'height', self._w))
133 def winfo_id(self):
134 return self.tk.getint(
135 self.tk.call('winfo', 'id', self._w))
136 def winfo_interps(self):
137 return self.tk.splitlist(
138 self.tk.call('winfo', 'interps'))
139 def winfo_ismapped(self):
140 return self.tk.getint(
141 self.tk.call('winfo', 'ismapped', self._w))
142 def winfo_name(self):
143 return self.tk.call('winfo', 'name', self._w)
144 def winfo_parent(self):
145 return self.tk.call('winfo', 'parent', self._w)
146 def winfo_pathname(self, id):
147 return self.tk.call('winfo', 'pathname', id)
148 def winfo_pixels(self, number):
149 return self.tk.getint(
150 self.tk.call('winfo', 'pixels', self._w, number))
151 def winfo_reqheight(self):
152 return self.tk.getint(
153 self.tk.call('winfo', 'reqheight', self._w))
154 def winfo_reqwidth(self):
155 return self.tk.getint(
156 self.tk.call('winfo', 'reqwidth', self._w))
157 def winfo_rgb(self, color):
158 return self._getints(
159 self.tk.call('winfo', 'rgb', self._w, color))
160 def winfo_rootx(self):
161 return self.tk.getint(
162 self.tk.call('winfo', 'rootx', self._w))
163 def winfo_rooty(self):
164 return self.tk.getint(
165 self.tk.call('winfo', 'rooty', self._w))
166 def winfo_screen(self):
167 return self.tk.call('winfo', 'screen', self._w)
168 def winfo_screencells(self):
169 return self.tk.getint(
170 self.tk.call('winfo', 'screencells', self._w))
171 def winfo_screendepth(self):
172 return self.tk.getint(
173 self.tk.call('winfo', 'screendepth', self._w))
174 def winfo_screenheight(self):
175 return self.tk.getint(
176 self.tk.call('winfo', 'screenheight', self._w))
177 def winfo_screenmmheight(self):
178 return self.tk.getint(
179 self.tk.call('winfo', 'screenmmheight', self._w))
180 def winfo_screenmmwidth(self):
181 return self.tk.getint(
182 self.tk.call('winfo', 'screenmmwidth', self._w))
183 def winfo_screenvisual(self):
184 return self.tk.call('winfo', 'screenvisual', self._w)
185 def winfo_screenwidth(self):
186 return self.tk.getint(
187 self.tk.call('winfo', 'screenwidth', self._w))
188 def winfo_toplevel(self):
189 return self.tk.call('winfo', 'toplevel', self._w)
190 def winfo_visual(self):
191 return self.tk.call('winfo', 'visual', self._w)
192 def winfo_vrootheight(self):
193 return self.tk.getint(
194 self.tk.call('winfo', 'vrootheight', self._w))
195 def winfo_vrootwidth(self):
196 return self.tk.getint(
197 self.tk.call('winfo', 'vrootwidth', self._w))
198 def winfo_vrootx(self):
199 return self.tk.getint(
200 self.tk.call('winfo', 'vrootx', self._w))
201 def winfo_vrooty(self):
202 return self.tk.getint(
203 self.tk.call('winfo', 'vrooty', self._w))
204 def winfo_width(self):
205 return self.tk.getint(
206 self.tk.call('winfo', 'width', self._w))
207 def winfo_x(self):
208 return self.tk.getint(
209 self.tk.call('winfo', 'x', self._w))
210 def winfo_y(self):
211 return self.tk.getint(
212 self.tk.call('winfo', 'y', self._w))
213 def update(self):
214 self.tk.call('update')
215 def update_idletasks(self):
216 self.tk.call('update', 'idletasks')
217 def bind(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000218 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000219 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000220 self.tk.call('bind', self._w, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000221 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000222 def bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000223 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000224 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000225 self.tk.call('bind', 'all' , sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000226 (add + `name`,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000227 def bind_class(self, className, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000228 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000229 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000230 self.tk.call('bind', className , sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000231 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000232 def mainloop(self):
233 self.tk.mainloop()
234 def quit(self):
235 self.tk.quit()
236 # Utilities
237 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000238 if not string: return None
239 res = ()
240 for v in self.tk.splitlist(string):
241 res = res + (self.tk.getint(v),)
242 return res
Guido van Rossum18468821994-06-20 07:49:28 +0000243 def _getboolean(self, string):
244 if string:
245 return self.tk.getboolean(string)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000246 # else return None
Guido van Rossum18468821994-06-20 07:49:28 +0000247 def _options(self, cnf):
248 res = ()
249 for k, v in cnf.items():
250 if _isfunctype(v):
251 v = self._register(v)
252 res = res + ('-'+k, v)
253 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000254 def _nametowidget(self, name):
255 w = self
256 if name[0] == '.':
257 w = w._root()
258 name = name[1:]
259 from string import find
260 while name:
261 i = find(name, '.')
262 if i >= 0:
263 name, tail = name[:i], name[i+1:]
264 else:
265 tail = ''
266 w = w.children[name]
267 name = tail
268 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000269 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000270 f = _CallSafely(func, subst).__call__
271 name = `id(f)`
272 if hasattr(func, 'im_func'):
273 func = func.im_func
274 if hasattr(func, 'func_name') and \
275 type(func.func_name) == type(''):
276 name = name + func.func_name
277 self.tk.createcommand(name, f)
278 return name
Guido van Rossum45853db1994-06-20 12:19:19 +0000279 def _root(self):
280 w = self
281 while w.master: w = w.master
282 return w
283 _subst_prefix = ('%#', '%b', '%f', '%h', '%k',
284 '%s', '%t', '%w', '%x', '%y',
285 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
286 def _substitute(self, *args):
287 tk = self.tk
288 if len(args) != len(self._subst_prefix): return args
289 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
290 # Missing: (a, c, d, m, o, v, B, R)
291 e = Event()
292 e.serial = tk.getint(nsign)
293 e.num = tk.getint(b)
294 try: e.focus = tk.getboolean(f)
295 except TclError: pass
296 e.height = tk.getint(h)
297 e.keycode = tk.getint(k)
298 e.state = tk.getint(s)
299 e.time = tk.getint(t)
300 e.width = tk.getint(w)
301 e.x = tk.getint(x)
302 e.y = tk.getint(y)
303 e.char = A
304 try: e.send_event = tk.getboolean(E)
305 except TclError: pass
306 e.keysym = K
307 e.keysym_num = tk.getint(N)
308 e.type = T
309 e.widget = self._nametowidget(W)
310 e.x_root = tk.getint(X)
311 e.y_root = tk.getint(Y)
312 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000313
314class _CallSafely:
315 def __init__(self, func, subst=None):
316 self.func = func
317 self.subst = subst
318 def __call__(self, *args):
319 if self.subst:
320 args = self.apply_func(self.subst, args)
321 args = self.apply_func(self.func, args)
322 def apply_func(self, func, args):
323 import sys
324 try:
325 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000326 except SystemExit, msg:
327 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000328 except:
329 try:
330 try:
331 t = sys.exc_traceback
332 while t:
333 sys.stderr.write(
334 ' %s, line %s\n' %
335 (t.tb_frame.f_code,
336 t.tb_lineno))
337 t = t.tb_next
338 finally:
339 sys.stderr.write('%s: %s\n' %
340 (sys.exc_type,
341 sys.exc_value))
Guido van Rossum45853db1994-06-20 12:19:19 +0000342 (sys.last_type,
343 sys.last_value,
344 sys.last_traceback) = (sys.exc_type,
345 sys.exc_value,
346 sys.exc_traceback)
347 import pdb
348 pdb.pm()
Guido van Rossum18468821994-06-20 07:49:28 +0000349 except:
350 print '*** Error in error handling ***'
351 print sys.exc_type, ':', sys.exc_value
352
353class Wm:
354 def aspect(self,
355 minNumer=None, minDenom=None,
356 maxNumer=None, maxDenom=None):
357 return self._getints(
358 self.tk.call('wm', 'aspect', self._w,
359 minNumer, minDenom,
360 maxNumer, maxDenom))
361 def client(self, name=None):
362 return self.tk.call('wm', 'client', self._w, name)
363 def command(self, value=None):
364 return self.tk.call('wm', 'command', self._w, value)
365 def deiconify(self):
366 return self.tk.call('wm', 'deiconify', self._w)
367 def focusmodel(self, model=None):
368 return self.tk.call('wm', 'focusmodel', self._w, model)
369 def frame(self):
370 return self.tk.call('wm', 'frame', self._w)
371 def geometry(self, newGeometry=None):
372 return self.tk.call('wm', 'geometry', self._w, newGeometry)
373 def grid(self,
374 baseWidht=None, baseHeight=None,
375 widthInc=None, heightInc=None):
376 return self._getints(self.tk.call(
377 'wm', 'grid', self._w,
378 baseWidht, baseHeight, widthInc, heightInc))
379 def group(self, pathName=None):
380 return self.tk.call('wm', 'group', self._w, pathName)
381 def iconbitmap(self, bitmap=None):
382 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
383 def iconify(self):
384 return self.tk.call('wm', 'iconify', self._w)
385 def iconmask(self, bitmap=None):
386 return self.tk.call('wm', 'iconmask', self._w, bitmap)
387 def iconname(self, newName=None):
388 return self.tk.call('wm', 'iconname', self._w, newName)
389 def iconposition(self, x=None, y=None):
390 return self._getints(self.tk.call(
391 'wm', 'iconposition', self._w, x, y))
392 def iconwindow(self, pathName=None):
393 return self.tk.call('wm', 'iconwindow', self._w, pathName)
394 def maxsize(self, width=None, height=None):
395 return self._getints(self.tk.call(
396 'wm', 'maxsize', self._w, width, height))
397 def minsize(self, width=None, height=None):
398 return self._getints(self.tk.call(
399 'wm', 'minsize', self._w, width, height))
400 def overrideredirect(self, boolean=None):
401 return self._getboolean(self.tk.call(
402 'wm', 'overrideredirect', self._w, boolean))
403 def positionfrom(self, who=None):
404 return self.tk.call('wm', 'positionfrom', self._w, who)
405 def protocol(self, name=None, func=None):
406 if _isfunctype(func):
407 command = self._register(func)
408 else:
409 command = func
410 return self.tk.call(
411 'wm', 'protocol', self._w, name, command)
412 def sizefrom(self, who=None):
413 return self.tk.call('wm', 'sizefrom', self._w, who)
414 def state(self):
415 return self.tk.call('wm', 'state', self._w)
416 def title(self, string=None):
417 return self.tk.call('wm', 'title', self._w, string)
418 def transient(self, master=None):
419 return self.tk.call('wm', 'transient', self._w, master)
420 def withdraw(self):
421 return self.tk.call('wm', 'withdraw', self._w)
422
423class Tk(Misc, Wm):
424 _w = '.'
425 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000426 self.master = None
427 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000428 if baseName is None:
429 import sys, os
430 baseName = os.path.basename(sys.argv[0])
431 if baseName[-3:] == '.py': baseName = baseName[:-3]
432 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000433 self.tk.createcommand('tkerror', _tkerror)
434 def destroy(self):
435 for c in self.children.values(): c.destroy()
436 del self.master.children[self._name]
437 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000438 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000439 return self._w
Guido van Rossum18468821994-06-20 07:49:28 +0000440
441class Pack:
442 def config(self, cnf={}):
443 apply(self.tk.call,
444 ('pack', 'configure', self._w)
445 + self._options(cnf))
446 pack = config
447 def __setitem__(self, key, value):
448 Pack.config({key: value})
449 def forget(self):
450 self.tk.call('pack', 'forget', self._w)
451 def newinfo(self):
452 return self.tk.call('pack', 'newinfo', self._w)
453 info = newinfo
454 def propagate(self, boolean=None):
455 if boolean:
456 self.tk.call('pack', 'propagate', self._w)
457 else:
458 return self._getboolean(self.tk.call(
459 'pack', 'propagate', self._w))
460 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000461 return map(self._nametowidget,
462 self.tk.splitlist(
463 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000464
465class Place:
466 def config(self, cnf={}):
467 apply(self.tk.call,
468 ('place', 'configure', self._w)
469 + self._options(cnf))
470 place = config
471 def __setitem__(self, key, value):
472 Place.config({key: value})
473 def forget(self):
474 self.tk.call('place', 'forget', self._w)
475 def info(self):
476 return self.tk.call('place', 'info', self._w)
477 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000478 return map(self._nametowidget,
479 self.tk.splitlist(
480 self.tk.call(
481 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000482
Guido van Rossum45853db1994-06-20 12:19:19 +0000483_default_root = None
Guido van Rossum18468821994-06-20 07:49:28 +0000484
485class Widget(Misc, Pack, Place):
486 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum45853db1994-06-20 12:19:19 +0000487 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000488 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000489 if not _default_root:
490 _default_root = Tk()
491 master = _default_root
492 if not _default_root:
493 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000494 self.master = master
495 self.tk = master.tk
496 if cnf.has_key('name'):
497 name = cnf['name']
498 del cnf['name']
499 else:
500 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000501 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000502 if master._w=='.':
503 self._w = '.' + name
504 else:
505 self._w = master._w + '.' + name
506 self.widgetName = widgetName
507 apply(self.tk.call, (widgetName, self._w) + extra)
508 Widget.config(self, cnf)
Guido van Rossum45853db1994-06-20 12:19:19 +0000509 self.children = {}
510 if master.children.has_key(name):
511 master.children[name].destroy()
512 master.children[name] = self
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000513 def config(self, cnf=None):
514 if cnf is None:
515 cnf = {}
516 for x in self.tk.split(
517 self.tk.call(self._w, 'configure')):
518 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
519 return cnf
520 if type(cnf) == StringType:
521 x = self.tk.split(self.tk.call(
522 self._w, 'configure', '-'+cnf))
523 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000524 for k in cnf.keys():
525 if type(k) == ClassType:
526 k.config(self, cnf[k])
527 del cnf[k]
528 apply(self.tk.call, (self._w, 'configure')
529 + self._options(cnf))
530 def __getitem__(self, key):
Guido van Rossumc2fb7e61994-06-27 07:48:37 +0000531 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000532 self._w, 'configure', '-' + key))
533 return v[4]
534 def __setitem__(self, key, value):
535 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000536 def keys(self):
537 return map(lambda x: x[0][1:],
538 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000539 def __str__(self):
540 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000541 def destroy(self):
542 for c in self.children.values(): c.destroy()
543 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000544 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000545 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000546 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000547
548class Toplevel(Widget, Wm):
549 def __init__(self, master=None, cnf={}):
550 extra = ()
551 if cnf.has_key('screen'):
552 extra = ('-screen', cnf['screen'])
553 del cnf['screen']
554 if cnf.has_key('class'):
555 extra = extra + ('-class', cnf['class'])
556 del cnf['class']
557 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000558 root = self._root()
559 self.iconname(root.iconname())
560 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000561
562class Button(Widget):
563 def __init__(self, master=None, cnf={}):
564 Widget.__init__(self, master, 'button', cnf)
565 def tk_butEnter(self):
566 self.tk.call('tk_butEnter', self._w)
567 def tk_butLeave(self):
568 self.tk.call('tk_butLeave', self._w)
569 def tk_butDown(self):
570 self.tk.call('tk_butDown', self._w)
571 def tk_butUp(self):
572 self.tk.call('tk_butUp', self._w)
573 def flash(self):
574 self.tk.call(self._w, 'flash')
575 def invoke(self):
576 self.tk.call(self._w, 'invoke')
577
578# Indices:
579def AtEnd():
580 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000581def AtInsert(*args):
582 s = 'insert'
583 for a in args:
584 if a: s = s + (' ' + a)
585 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000586def AtSelFirst():
587 return 'sel.first'
588def AtSelLast():
589 return 'sel.last'
590def At(x, y=None):
591 if y:
592 return '@' + `x` + ',' + `y`
593 else:
594 return '@' + `x`
595
596class Canvas(Widget):
597 def __init__(self, master=None, cnf={}):
598 Widget.__init__(self, master, 'canvas', cnf)
599 def addtag(self, *args):
600 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000601 def addtag_above(self, tagOrId):
602 self.addtag('above', tagOrId)
603 def addtag_all(self):
604 self.addtag('all')
605 def addtag_below(self, tagOrId):
606 self.addtag('below', tagOrId)
607 def addtag_closest(self, x, y, halo=None, start=None):
608 self.addtag('closest', x, y, halo, start)
609 def addtag_enclosed(self, x1, y1, x2, y2):
610 self.addtag('enclosed', x1, y1, x2, y2)
611 def addtag_overlapping(self, x1, y1, x2, y2):
612 self.addtag('overlapping', x1, y1, x2, y2)
613 def addtag_withtag(self, tagOrId):
614 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000615 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000616 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000617 def bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000618 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000619 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000620 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000621 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000622 def canvasx(self, screenx, gridspacing=None):
623 return self.tk.getint(self.tk.call(
624 self._w, 'canvasx', screenx, gridspacing))
625 def canvasy(self, screeny, gridspacing=None):
626 return self.tk.getint(self.tk.call(
627 self._w, 'canvasy', screeny, gridspacing))
628 def coords(self, *args):
629 return self._do('coords', args)
630 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000631 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000632 cnf = args[-1]
633 if type(cnf) == type({}):
634 args = args[:-1]
635 else:
636 cnf = {}
Guido van Rossum08a40381994-06-21 11:44:21 +0000637 v = (self._w, 'create', itemType) + args
Guido van Rossum18468821994-06-20 07:49:28 +0000638 for k in cnf.keys():
639 v = v + ('-' + k, cnf[k])
640 return self.tk.getint(apply(self.tk.call, v))
641 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000642 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000643 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000644 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000645 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000646 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000647 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000648 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000649 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000650 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000651 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000652 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000653 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000654 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000655 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000656 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000657 def dchars(self, *args):
658 self._do('dchars', args)
659 def delete(self, *args):
660 self._do('delete', args)
661 def dtag(self, *args):
662 self._do('dtag', args)
663 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000664 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000665 def find_above(self, tagOrId):
666 return self.find('above', tagOrId)
667 def find_all(self):
668 return self.find('all')
669 def find_below(self, tagOrId):
670 return self.find('below', tagOrId)
671 def find_closest(self, x, y, halo=None, start=None):
672 return self.find('closest', x, y, halo, start)
673 def find_enclosed(self, x1, y1, x2, y2):
674 return self.find('enclosed', x1, y1, x2, y2)
675 def find_overlapping(self, x1, y1, x2, y2):
676 return self.find('overlapping', x1, y1, x2, y2)
677 def find_withtag(self, tagOrId):
678 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000679 def focus(self, *args):
680 return self._do('focus', args)
681 def gettags(self, *args):
682 return self.tk.splitlist(self._do('gettags', args))
683 def icursor(self, *args):
684 self._do('icursor', args)
685 def index(self, *args):
686 return self.tk.getint(self._do('index', args))
687 def insert(self, *args):
688 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000689 def itemconfig(self, tagOrId, cnf=None):
690 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000691 cnf = {}
692 for x in self.tk.split(
693 self._do('itemconfigure', (tagOrId))):
694 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
695 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000696 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000697 x = self.tk.split(self._do('itemconfigure',
698 (tagOrId, '-'+cnf,)))
699 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000700 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
701 def lower(self, *args):
702 self._do('lower', args)
703 def move(self, *args):
704 self._do('move', args)
705 def postscript(self, cnf={}):
706 return self._do('postscript', self._options(cnf))
707 def tkraise(self, *args):
708 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000709 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000710 def scale(self, *args):
711 self._do('scale', args)
712 def scan_mark(self, x, y):
713 self.tk.call(self._w, 'scan', 'mark', x, y)
714 def scan_dragto(self, x, y):
715 self.tk.call(self._w, 'scan', 'dragto', x, y)
716 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000717 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000718 def select_clear(self):
719 self.tk.call(self._w, 'select', 'clear')
720 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000721 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000722 def select_item(self):
723 self.tk.call(self._w, 'select', 'item')
724 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000725 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000726 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000727 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000728 def xview(self, index):
729 self.tk.call(self._w, 'xview', index)
730 def yview(self, index):
731 self.tk.call(self._w, 'yview', index)
732
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000733def _flatten(tuple):
734 res = ()
735 for item in tuple:
736 if type(item) in (TupleType, ListType):
737 res = res + _flatten(item)
738 else:
739 res = res + (item,)
740 return res
741
Guido van Rossum18468821994-06-20 07:49:28 +0000742class Checkbutton(Widget):
743 def __init__(self, master=None, cnf={}):
744 Widget.__init__(self, master, 'checkbutton', cnf)
745 def deselect(self):
746 self.tk.call(self._w, 'deselect')
747 def flash(self):
748 self.tk.call(self._w, 'flash')
749 def invoke(self):
750 self.tk.call(self._w, 'invoke')
751 def select(self):
752 self.tk.call(self._w, 'select')
753 def toggle(self):
754 self.tk.call(self._w, 'toggle')
755
756class Entry(Widget):
757 def __init__(self, master=None, cnf={}):
758 Widget.__init__(self, master, 'entry', cnf)
759 def tk_entryBackspace(self):
760 self.tk.call('tk_entryBackspace', self._w)
761 def tk_entryBackword(self):
762 self.tk.call('tk_entryBackword', self._w)
763 def tk_entrySeeCaret(self):
764 self.tk.call('tk_entrySeeCaret', self._w)
765 def delete(self, first, last=None):
766 self.tk.call(self._w, 'delete', first, last)
767 def get(self):
768 return self.tk.call(self._w, 'get')
769 def icursor(self, index):
770 self.tk.call(self._w, 'icursor', index)
771 def index(self, index):
772 return self.tk.getint(self.tk.call(
773 self._w, 'index', index))
774 def insert(self, index, string):
775 self.tk.call(self._w, 'insert', index, string)
776 def scan_mark(self, x):
777 self.tk.call(self._w, 'scan', 'mark', x)
778 def scan_dragto(self, x):
779 self.tk.call(self._w, 'scan', 'dragto', x)
780 def select_adjust(self, index):
781 self.tk.call(self._w, 'select', 'adjust', index)
782 def select_clear(self):
783 self.tk.call(self._w, 'select', 'clear')
784 def select_from(self, index):
785 self.tk.call(self._w, 'select', 'from', index)
786 def select_to(self, index):
787 self.tk.call(self._w, 'select', 'to', index)
788 def select_view(self, index):
789 self.tk.call(self._w, 'select', 'view', index)
790
791class Frame(Widget):
792 def __init__(self, master=None, cnf={}):
793 extra = ()
794 if cnf.has_key('class'):
795 extra = ('-class', cnf['class'])
796 del cnf['class']
797 Widget.__init__(self, master, 'frame', cnf, extra)
798 def tk_menuBar(self, *args):
799 apply(self.tk.call, ('tk_menuBar', self._w) + args)
800
801class Label(Widget):
802 def __init__(self, master=None, cnf={}):
803 Widget.__init__(self, master, 'label', cnf)
804
805class Listbox(Widget):
806 def __init__(self, master=None, cnf={}):
807 Widget.__init__(self, master, 'listbox', cnf)
808 def tk_listboxSingleSelect(self):
809 self.tk.call('tk_listboxSingleSelect', self._w)
810 def curselection(self):
811 return self.tk.splitlist(self.tk.call(
812 self._w, 'curselection'))
813 def delete(self, first, last=None):
814 self.tk.call(self._w, 'delete', first, last)
815 def get(self, index):
816 return self.tk.call(self._w, 'get', index)
817 def insert(self, index, *elements):
818 apply(self.tk.call,
819 (self._w, 'insert', index) + elements)
820 def nearest(self, y):
821 return self.tk.getint(self.tk.call(
822 self._w, 'nearest', y))
823 def scan_mark(self, x, y):
824 self.tk.call(self._w, 'scan', 'mark', x, y)
825 def scan_dragto(self, x, y):
826 self.tk.call(self._w, 'scan', 'dragto', x, y)
827 def select_adjust(self, index):
828 self.tk.call(self._w, 'select', 'adjust', index)
829 def select_clear(self):
830 self.tk.call(self._w, 'select', 'clear')
831 def select_from(self, index):
832 self.tk.call(self._w, 'select', 'from', index)
833 def select_to(self, index):
834 self.tk.call(self._w, 'select', 'to', index)
835 def size(self):
836 return self.tk.getint(self.tk.call(self._w, 'size'))
837 def xview(self, index):
838 self.tk.call(self._w, 'xview', index)
839 def yview(self, index):
840 self.tk.call(self._w, 'yview', index)
841
842class Menu(Widget):
843 def __init__(self, master=None, cnf={}):
844 Widget.__init__(self, master, 'menu', cnf)
845 def tk_menuBar(self, *args):
846 apply(self.tk.call, ('tk_menuBar', self._w) + args)
847 def tk_bindForTraversal(self):
848 self.tk.call('tk_bindForTraversal', self._w)
849 def tk_mbPost(self):
850 self.tk.call('tk_mbPost', self._w)
851 def tk_mbUnpost(self):
852 self.tk.call('tk_mbUnpost')
853 def tk_traverseToMenu(self, char):
854 self.tk.call('tk_traverseToMenu', self._w, char)
855 def tk_traverseWithinMenu(self, char):
856 self.tk.call('tk_traverseWithinMenu', self._w, char)
857 def tk_getMenuButtons(self):
858 return self.tk.call('tk_getMenuButtons', self._w)
859 def tk_nextMenu(self, count):
860 self.tk.call('tk_nextMenu', count)
861 def tk_nextMenuEntry(self, count):
862 self.tk.call('tk_nextMenuEntry', count)
863 def tk_invokeMenu(self):
864 self.tk.call('tk_invokeMenu', self._w)
865 def tk_firstMenu(self):
866 self.tk.call('tk_firstMenu', self._w)
867 def tk_mbButtonDown(self):
868 self.tk.call('tk_mbButtonDown', self._w)
869 def activate(self, index):
870 self.tk.call(self._w, 'activate', index)
871 def add(self, itemType, cnf={}):
872 apply(self.tk.call, (self._w, 'add', itemType)
873 + self._options(cnf))
874 def delete(self, index1, index2=None):
875 self.tk.call(self._w, 'delete', index1, index2)
876 def entryconfig(self, index, cnf={}):
877 apply(self.tk.call, (self._w, 'entryconfigure', index)
878 + self._options(cnf))
879 def index(self, index):
Guido van Rossumc2fb7e61994-06-27 07:48:37 +0000880 i = self.tk.call(self._w, 'index', index)
881 if i == 'none': return None
882 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +0000883 def invoke(self, index):
884 return self.tk.call(self._w, 'invoke', index)
885 def post(self, x, y):
886 self.tk.call(self._w, 'post', x, y)
887 def unpost(self):
888 self.tk.call(self._w, 'unpost')
889 def yposition(self, index):
890 return self.tk.getint(self.tk.call(
891 self._w, 'yposition', index))
892
893class Menubutton(Widget):
894 def __init__(self, master=None, cnf={}):
895 Widget.__init__(self, master, 'menubutton', cnf)
896
897class Message(Widget):
898 def __init__(self, master=None, cnf={}):
899 Widget.__init__(self, master, 'message', cnf)
900
901class Radiobutton(Widget):
902 def __init__(self, master=None, cnf={}):
903 Widget.__init__(self, master, 'radiobutton', cnf)
904 def deselect(self):
905 self.tk.call(self._w, 'deselect')
906 def flash(self):
907 self.tk.call(self._w, 'flash')
908 def invoke(self):
909 self.tk.call(self._w, 'invoke')
910 def select(self):
911 self.tk.call(self._w, 'select')
912
913class Scale(Widget):
914 def __init__(self, master=None, cnf={}):
915 Widget.__init__(self, master, 'scale', cnf)
916 def get(self):
917 return self.tk.getint(self.tk.call(self._w, 'get'))
918 def set(self, value):
919 self.tk.call(self._w, 'set', value)
920
921class Scrollbar(Widget):
922 def __init__(self, master=None, cnf={}):
923 Widget.__init__(self, master, 'scrollbar', cnf)
924 def get(self):
925 return self.tk.getints(self.tk.call(self._w, 'get'))
926 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
927 self.tk.call(self._w, 'set',
928 totalUnits, windowUnits, firstUnit, lastUnit)
929
930class Text(Widget):
931 def __init__(self, master=None, cnf={}):
932 Widget.__init__(self, master, 'text', cnf)
933 def tk_textSelectTo(self, index):
934 self.tk.call('tk_textSelectTo', self._w, index)
935 def tk_textBackspace(self):
936 self.tk.call('tk_textBackspace', self._w)
937 def tk_textIndexCloser(self, a, b, c):
938 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
939 def tk_textResetAnchor(self, index):
940 self.tk.call('tk_textResetAnchor', self._w, index)
941 def compare(self, index1, op, index2):
942 return self.tk.getboolean(self.tk.call(
943 self._w, 'compare', index1, op, index2))
944 def debug(self, boolean=None):
945 return self.tk.getboolean(self.tk.call(
946 self._w, 'debug', boolean))
947 def delete(self, index1, index2=None):
948 self.tk.call(self._w, 'delete', index1, index2)
949 def get(self, index1, index2=None):
950 return self.tk.call(self._w, 'get', index1, index2)
951 def index(self, index):
952 return self.tk.call(self._w, 'index', index)
953 def insert(self, index, chars):
954 self.tk.call(self._w, 'insert', index, chars)
955 def mark_names(self):
956 return self.tk.splitlist(self.tk.call(
957 self._w, 'mark', 'names'))
958 def mark_set(self, markName, index):
959 self.tk.call(self._w, 'mark', 'set', markName, index)
960 def mark_unset(self, markNames):
961 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
962 def scan_mark(self, y):
963 self.tk.call(self._w, 'scan', 'mark', y)
964 def scan_dragto(self, y):
965 self.tk.call(self._w, 'scan', 'dragto', y)
966 def tag_add(self, tagName, index1, index2=None):
967 self.tk.call(
968 self._w, 'tag', 'add', tagName, index1, index2)
969 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000970 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000971 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000972 self.tk.call(self._w, 'tag', 'bind',
973 tagName, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000974 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000975 def tag_config(self, tagName, cnf={}):
976 apply(self.tk.call,
977 (self._w, 'tag', 'configure', tagName)
978 + self._options(cnf))
979 def tag_delete(self, tagNames):
980 apply(self.tk.call, (self._w, 'tag', 'delete')
981 + tagNames)
982 def tag_lower(self, tagName, belowThis=None):
983 self.tk.call(self._w, 'tag', 'lower',
984 tagName, belowThis)
985 def tag_names(self, index=None):
986 return self.tk.splitlist(
987 self.tk.call(self._w, 'tag', 'names', index))
988 def tag_nextrange(self, tagName, index1, index2=None):
989 return self.tk.splitlist(self.tk.call(
990 self._w, 'tag', 'nextrange', index1, index2))
991 def tag_raise(self, tagName, aboveThis=None):
992 self.tk.call(
993 self._w, 'tag', 'raise', tagName, aboveThis)
994 def tag_ranges(self, tagName):
995 return self.tk.splitlist(self.tk.call(
996 self._w, 'tag', 'ranges', tagName))
997 def tag_remove(self, tagName, index1, index2=None):
998 self.tk.call(
999 self._w, 'tag', 'remove', index1, index2)
1000 def yview(self, what):
1001 self.tk.call(self._w, 'yview', what)
1002 def yview_pickplace(self, what):
1003 self.tk.call(self._w, 'yview', '-pickplace', what)
1004
1005#class Dialog:
1006