blob: 70e70cd78fd8b87d306b184b8bacd225d9b42cc2 [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 Rossum18468821994-06-20 07:49:28 +0000513 def config(self, cnf={}):
514 for k in cnf.keys():
515 if type(k) == ClassType:
516 k.config(self, cnf[k])
517 del cnf[k]
518 apply(self.tk.call, (self._w, 'configure')
519 + self._options(cnf))
520 def __getitem__(self, key):
521 v = self.tk.split(self.tk.call(
522 self._w, 'configure', '-' + key))
523 return v[4]
524 def __setitem__(self, key, value):
525 Widget.config(self, {key: value})
526 def __str__(self):
527 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000528 def destroy(self):
529 for c in self.children.values(): c.destroy()
530 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000531 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000532 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000533 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000534
535class Toplevel(Widget, Wm):
536 def __init__(self, master=None, cnf={}):
537 extra = ()
538 if cnf.has_key('screen'):
539 extra = ('-screen', cnf['screen'])
540 del cnf['screen']
541 if cnf.has_key('class'):
542 extra = extra + ('-class', cnf['class'])
543 del cnf['class']
544 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000545 root = self._root()
546 self.iconname(root.iconname())
547 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000548
549class Button(Widget):
550 def __init__(self, master=None, cnf={}):
551 Widget.__init__(self, master, 'button', cnf)
552 def tk_butEnter(self):
553 self.tk.call('tk_butEnter', self._w)
554 def tk_butLeave(self):
555 self.tk.call('tk_butLeave', self._w)
556 def tk_butDown(self):
557 self.tk.call('tk_butDown', self._w)
558 def tk_butUp(self):
559 self.tk.call('tk_butUp', self._w)
560 def flash(self):
561 self.tk.call(self._w, 'flash')
562 def invoke(self):
563 self.tk.call(self._w, 'invoke')
564
565# Indices:
566def AtEnd():
567 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000568def AtInsert(*args):
569 s = 'insert'
570 for a in args:
571 if a: s = s + (' ' + a)
572 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000573def AtSelFirst():
574 return 'sel.first'
575def AtSelLast():
576 return 'sel.last'
577def At(x, y=None):
578 if y:
579 return '@' + `x` + ',' + `y`
580 else:
581 return '@' + `x`
582
583class Canvas(Widget):
584 def __init__(self, master=None, cnf={}):
585 Widget.__init__(self, master, 'canvas', cnf)
586 def addtag(self, *args):
587 self._do('addtag', args)
588 def bbox(self, *args):
589 return self._getints(self._do('bbox', args))
590 def bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000591 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000592 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000593 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000594 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000595 def canvasx(self, screenx, gridspacing=None):
596 return self.tk.getint(self.tk.call(
597 self._w, 'canvasx', screenx, gridspacing))
598 def canvasy(self, screeny, gridspacing=None):
599 return self.tk.getint(self.tk.call(
600 self._w, 'canvasy', screeny, gridspacing))
601 def coords(self, *args):
602 return self._do('coords', args)
603 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000604 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000605 cnf = args[-1]
606 if type(cnf) == type({}):
607 args = args[:-1]
608 else:
609 cnf = {}
Guido van Rossum08a40381994-06-21 11:44:21 +0000610 v = (self._w, 'create', itemType) + args
Guido van Rossum18468821994-06-20 07:49:28 +0000611 for k in cnf.keys():
612 v = v + ('-' + k, cnf[k])
613 return self.tk.getint(apply(self.tk.call, v))
614 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000615 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000616 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000617 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000618 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000619 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000620 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000621 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000622 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000623 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000624 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000625 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000626 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000627 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000628 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000629 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000630 def dchars(self, *args):
631 self._do('dchars', args)
632 def delete(self, *args):
633 self._do('delete', args)
634 def dtag(self, *args):
635 self._do('dtag', args)
636 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000637 return self._getints(self._do('find', args))
Guido van Rossum18468821994-06-20 07:49:28 +0000638 def focus(self, *args):
639 return self._do('focus', args)
640 def gettags(self, *args):
641 return self.tk.splitlist(self._do('gettags', args))
642 def icursor(self, *args):
643 self._do('icursor', args)
644 def index(self, *args):
645 return self.tk.getint(self._do('index', args))
646 def insert(self, *args):
647 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000648 def itemconfig(self, tagOrId, cnf=None):
649 if cnf is None:
650 return self.tk.split(self._do('itemconfigure',
651 (tagOrId)))
652 if type(cnf) == StringType:
653 return self.tk.split(self._do('itemconfigure',
654 (tagOrId, '-'+cnf,)))
Guido van Rossum18468821994-06-20 07:49:28 +0000655 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
656 def lower(self, *args):
657 self._do('lower', args)
658 def move(self, *args):
659 self._do('move', args)
660 def postscript(self, cnf={}):
661 return self._do('postscript', self._options(cnf))
662 def tkraise(self, *args):
663 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000664 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000665 def scale(self, *args):
666 self._do('scale', args)
667 def scan_mark(self, x, y):
668 self.tk.call(self._w, 'scan', 'mark', x, y)
669 def scan_dragto(self, x, y):
670 self.tk.call(self._w, 'scan', 'dragto', x, y)
671 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000672 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000673 def select_clear(self):
674 self.tk.call(self._w, 'select', 'clear')
675 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000676 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000677 def select_item(self):
678 self.tk.call(self._w, 'select', 'item')
679 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000680 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000681 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000682 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000683 def xview(self, index):
684 self.tk.call(self._w, 'xview', index)
685 def yview(self, index):
686 self.tk.call(self._w, 'yview', index)
687
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000688def _flatten(tuple):
689 res = ()
690 for item in tuple:
691 if type(item) in (TupleType, ListType):
692 res = res + _flatten(item)
693 else:
694 res = res + (item,)
695 return res
696
Guido van Rossum18468821994-06-20 07:49:28 +0000697class Checkbutton(Widget):
698 def __init__(self, master=None, cnf={}):
699 Widget.__init__(self, master, 'checkbutton', cnf)
700 def deselect(self):
701 self.tk.call(self._w, 'deselect')
702 def flash(self):
703 self.tk.call(self._w, 'flash')
704 def invoke(self):
705 self.tk.call(self._w, 'invoke')
706 def select(self):
707 self.tk.call(self._w, 'select')
708 def toggle(self):
709 self.tk.call(self._w, 'toggle')
710
711class Entry(Widget):
712 def __init__(self, master=None, cnf={}):
713 Widget.__init__(self, master, 'entry', cnf)
714 def tk_entryBackspace(self):
715 self.tk.call('tk_entryBackspace', self._w)
716 def tk_entryBackword(self):
717 self.tk.call('tk_entryBackword', self._w)
718 def tk_entrySeeCaret(self):
719 self.tk.call('tk_entrySeeCaret', self._w)
720 def delete(self, first, last=None):
721 self.tk.call(self._w, 'delete', first, last)
722 def get(self):
723 return self.tk.call(self._w, 'get')
724 def icursor(self, index):
725 self.tk.call(self._w, 'icursor', index)
726 def index(self, index):
727 return self.tk.getint(self.tk.call(
728 self._w, 'index', index))
729 def insert(self, index, string):
730 self.tk.call(self._w, 'insert', index, string)
731 def scan_mark(self, x):
732 self.tk.call(self._w, 'scan', 'mark', x)
733 def scan_dragto(self, x):
734 self.tk.call(self._w, 'scan', 'dragto', x)
735 def select_adjust(self, index):
736 self.tk.call(self._w, 'select', 'adjust', index)
737 def select_clear(self):
738 self.tk.call(self._w, 'select', 'clear')
739 def select_from(self, index):
740 self.tk.call(self._w, 'select', 'from', index)
741 def select_to(self, index):
742 self.tk.call(self._w, 'select', 'to', index)
743 def select_view(self, index):
744 self.tk.call(self._w, 'select', 'view', index)
745
746class Frame(Widget):
747 def __init__(self, master=None, cnf={}):
748 extra = ()
749 if cnf.has_key('class'):
750 extra = ('-class', cnf['class'])
751 del cnf['class']
752 Widget.__init__(self, master, 'frame', cnf, extra)
753 def tk_menuBar(self, *args):
754 apply(self.tk.call, ('tk_menuBar', self._w) + args)
755
756class Label(Widget):
757 def __init__(self, master=None, cnf={}):
758 Widget.__init__(self, master, 'label', cnf)
759
760class Listbox(Widget):
761 def __init__(self, master=None, cnf={}):
762 Widget.__init__(self, master, 'listbox', cnf)
763 def tk_listboxSingleSelect(self):
764 self.tk.call('tk_listboxSingleSelect', self._w)
765 def curselection(self):
766 return self.tk.splitlist(self.tk.call(
767 self._w, 'curselection'))
768 def delete(self, first, last=None):
769 self.tk.call(self._w, 'delete', first, last)
770 def get(self, index):
771 return self.tk.call(self._w, 'get', index)
772 def insert(self, index, *elements):
773 apply(self.tk.call,
774 (self._w, 'insert', index) + elements)
775 def nearest(self, y):
776 return self.tk.getint(self.tk.call(
777 self._w, 'nearest', y))
778 def scan_mark(self, x, y):
779 self.tk.call(self._w, 'scan', 'mark', x, y)
780 def scan_dragto(self, x, y):
781 self.tk.call(self._w, 'scan', 'dragto', x, y)
782 def select_adjust(self, index):
783 self.tk.call(self._w, 'select', 'adjust', index)
784 def select_clear(self):
785 self.tk.call(self._w, 'select', 'clear')
786 def select_from(self, index):
787 self.tk.call(self._w, 'select', 'from', index)
788 def select_to(self, index):
789 self.tk.call(self._w, 'select', 'to', index)
790 def size(self):
791 return self.tk.getint(self.tk.call(self._w, 'size'))
792 def xview(self, index):
793 self.tk.call(self._w, 'xview', index)
794 def yview(self, index):
795 self.tk.call(self._w, 'yview', index)
796
797class Menu(Widget):
798 def __init__(self, master=None, cnf={}):
799 Widget.__init__(self, master, 'menu', cnf)
800 def tk_menuBar(self, *args):
801 apply(self.tk.call, ('tk_menuBar', self._w) + args)
802 def tk_bindForTraversal(self):
803 self.tk.call('tk_bindForTraversal', self._w)
804 def tk_mbPost(self):
805 self.tk.call('tk_mbPost', self._w)
806 def tk_mbUnpost(self):
807 self.tk.call('tk_mbUnpost')
808 def tk_traverseToMenu(self, char):
809 self.tk.call('tk_traverseToMenu', self._w, char)
810 def tk_traverseWithinMenu(self, char):
811 self.tk.call('tk_traverseWithinMenu', self._w, char)
812 def tk_getMenuButtons(self):
813 return self.tk.call('tk_getMenuButtons', self._w)
814 def tk_nextMenu(self, count):
815 self.tk.call('tk_nextMenu', count)
816 def tk_nextMenuEntry(self, count):
817 self.tk.call('tk_nextMenuEntry', count)
818 def tk_invokeMenu(self):
819 self.tk.call('tk_invokeMenu', self._w)
820 def tk_firstMenu(self):
821 self.tk.call('tk_firstMenu', self._w)
822 def tk_mbButtonDown(self):
823 self.tk.call('tk_mbButtonDown', self._w)
824 def activate(self, index):
825 self.tk.call(self._w, 'activate', index)
826 def add(self, itemType, cnf={}):
827 apply(self.tk.call, (self._w, 'add', itemType)
828 + self._options(cnf))
829 def delete(self, index1, index2=None):
830 self.tk.call(self._w, 'delete', index1, index2)
831 def entryconfig(self, index, cnf={}):
832 apply(self.tk.call, (self._w, 'entryconfigure', index)
833 + self._options(cnf))
834 def index(self, index):
835 return self.tk.call(self._w, 'index', index)
836 def invoke(self, index):
837 return self.tk.call(self._w, 'invoke', index)
838 def post(self, x, y):
839 self.tk.call(self._w, 'post', x, y)
840 def unpost(self):
841 self.tk.call(self._w, 'unpost')
842 def yposition(self, index):
843 return self.tk.getint(self.tk.call(
844 self._w, 'yposition', index))
845
846class Menubutton(Widget):
847 def __init__(self, master=None, cnf={}):
848 Widget.__init__(self, master, 'menubutton', cnf)
849
850class Message(Widget):
851 def __init__(self, master=None, cnf={}):
852 Widget.__init__(self, master, 'message', cnf)
853
854class Radiobutton(Widget):
855 def __init__(self, master=None, cnf={}):
856 Widget.__init__(self, master, 'radiobutton', cnf)
857 def deselect(self):
858 self.tk.call(self._w, 'deselect')
859 def flash(self):
860 self.tk.call(self._w, 'flash')
861 def invoke(self):
862 self.tk.call(self._w, 'invoke')
863 def select(self):
864 self.tk.call(self._w, 'select')
865
866class Scale(Widget):
867 def __init__(self, master=None, cnf={}):
868 Widget.__init__(self, master, 'scale', cnf)
869 def get(self):
870 return self.tk.getint(self.tk.call(self._w, 'get'))
871 def set(self, value):
872 self.tk.call(self._w, 'set', value)
873
874class Scrollbar(Widget):
875 def __init__(self, master=None, cnf={}):
876 Widget.__init__(self, master, 'scrollbar', cnf)
877 def get(self):
878 return self.tk.getints(self.tk.call(self._w, 'get'))
879 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
880 self.tk.call(self._w, 'set',
881 totalUnits, windowUnits, firstUnit, lastUnit)
882
883class Text(Widget):
884 def __init__(self, master=None, cnf={}):
885 Widget.__init__(self, master, 'text', cnf)
886 def tk_textSelectTo(self, index):
887 self.tk.call('tk_textSelectTo', self._w, index)
888 def tk_textBackspace(self):
889 self.tk.call('tk_textBackspace', self._w)
890 def tk_textIndexCloser(self, a, b, c):
891 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
892 def tk_textResetAnchor(self, index):
893 self.tk.call('tk_textResetAnchor', self._w, index)
894 def compare(self, index1, op, index2):
895 return self.tk.getboolean(self.tk.call(
896 self._w, 'compare', index1, op, index2))
897 def debug(self, boolean=None):
898 return self.tk.getboolean(self.tk.call(
899 self._w, 'debug', boolean))
900 def delete(self, index1, index2=None):
901 self.tk.call(self._w, 'delete', index1, index2)
902 def get(self, index1, index2=None):
903 return self.tk.call(self._w, 'get', index1, index2)
904 def index(self, index):
905 return self.tk.call(self._w, 'index', index)
906 def insert(self, index, chars):
907 self.tk.call(self._w, 'insert', index, chars)
908 def mark_names(self):
909 return self.tk.splitlist(self.tk.call(
910 self._w, 'mark', 'names'))
911 def mark_set(self, markName, index):
912 self.tk.call(self._w, 'mark', 'set', markName, index)
913 def mark_unset(self, markNames):
914 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
915 def scan_mark(self, y):
916 self.tk.call(self._w, 'scan', 'mark', y)
917 def scan_dragto(self, y):
918 self.tk.call(self._w, 'scan', 'dragto', y)
919 def tag_add(self, tagName, index1, index2=None):
920 self.tk.call(
921 self._w, 'tag', 'add', tagName, index1, index2)
922 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000923 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000924 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000925 self.tk.call(self._w, 'tag', 'bind',
926 tagName, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000927 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000928 def tag_config(self, tagName, cnf={}):
929 apply(self.tk.call,
930 (self._w, 'tag', 'configure', tagName)
931 + self._options(cnf))
932 def tag_delete(self, tagNames):
933 apply(self.tk.call, (self._w, 'tag', 'delete')
934 + tagNames)
935 def tag_lower(self, tagName, belowThis=None):
936 self.tk.call(self._w, 'tag', 'lower',
937 tagName, belowThis)
938 def tag_names(self, index=None):
939 return self.tk.splitlist(
940 self.tk.call(self._w, 'tag', 'names', index))
941 def tag_nextrange(self, tagName, index1, index2=None):
942 return self.tk.splitlist(self.tk.call(
943 self._w, 'tag', 'nextrange', index1, index2))
944 def tag_raise(self, tagName, aboveThis=None):
945 self.tk.call(
946 self._w, 'tag', 'raise', tagName, aboveThis)
947 def tag_ranges(self, tagName):
948 return self.tk.splitlist(self.tk.call(
949 self._w, 'tag', 'ranges', tagName))
950 def tag_remove(self, tagName, index1, index2=None):
951 self.tk.call(
952 self._w, 'tag', 'remove', index1, index2)
953 def yview(self, what):
954 self.tk.call(self._w, 'yview', what)
955 def yview_pickplace(self, what):
956 self.tk.call(self._w, 'yview', '-pickplace', what)
957
958#class Dialog:
959