blob: 8b5d94f15b428c1fea6b432cd4487e1e5a75dc98 [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):
9 return type(func) in (type(_Dummy.meth), type(_isfunctype))
10
11FunctionType = type(_isfunctype)
12ClassType = type(_Dummy)
13MethodType = type(_Dummy.meth)
Guido van Rossum67ef5f31994-06-20 13:39:14 +000014TupleType = type(())
15ListType = type([])
Guido van Rossum18468821994-06-20 07:49:28 +000016
Guido van Rossum45853db1994-06-20 12:19:19 +000017def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000018 pass
19
20class Event:
21 pass
22
23class Misc:
24 def tk_strictMotif(self, boolean=None):
25 self.tk.getboolean(self.tk.call(
26 'set', 'tk_strictMotif', boolean))
27 def waitvar(self, name='VAR'):
28 self.tk.call('tkwait', 'variable', name)
29 def setvar(self, name='VAR', value='1'):
30 self.tk.setvar(name, value)
Guido van Rossum5e8d3721994-06-20 08:12:01 +000031 def getvar(self, name='VAR'):
32 return self.tk.getvar(name)
33 def getint(self, s):
34 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +000035 def getdouble(self, s):
36 return self.tk.getdouble(s)
37 def getboolean(self, s):
38 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +000039 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +000040 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +000041 focus = focus_set # XXX b/w compat?
42 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +000043 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +000044 def focus_default_none(self):
45 self.tk.call('focus', 'default', 'none')
46 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +000047 def focus_none(self):
48 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +000049 def focus_get(self):
50 name = self.tk.call('focus')
51 if name == 'none': return None
52 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +000053 def after(self, ms, func=None, *args):
54 if not func:
55 self.tk.call('after', ms)
56 else:
57 name = self._register(func)
58 apply(self.tk.call, ('after', ms, name) + args)
Guido van Rossum45853db1994-06-20 12:19:19 +000059 # XXX grab current w/o window argument
60 def grab_current(self):
61 name = self.tk.call('grab', 'current', self._w)
62 if not name: return None
63 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +000064 def grab_release(self):
65 self.tk.call('grab', 'release', self._w)
66 def grab_set(self):
67 self.tk.call('grab', 'set', self._w)
68 def grab_set_global(self):
69 self.tk.call('grab', 'set', '-global', self._w)
70 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +000071 status = self.tk.call('grab', 'status', self._w)
72 if status == 'none': status = None
73 return status
Guido van Rossum18468821994-06-20 07:49:28 +000074 def lower(self, belowThis=None):
75 self.tk.call('lower', self._w, belowThis)
76 def selection_clear(self):
77 self.tk.call('selection', 'clear', self._w)
78 def selection_get(self, type=None):
79 self.tk.call('selection', 'get', type)
80 def selection_handle(self, func, type=None, format=None):
81 name = self._register(func)
82 self.tk.call('selection', 'handle',
83 self._w, name, type, format)
84 #XXX def selection_own(self):
85 # self.tk.call('selection', 'own', self._w)
86 def send(self, interp, cmd, *args): #XXX
87 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +000088 def lower(self, belowThis=None):
89 self.tk.call('lift', self._w, belowThis)
90 def tkraise(self, aboveThis=None):
91 self.tk.call('raise', self._w, aboveThis)
92 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +000093 def colormodel(self, value=None):
94 return self.tk.call('tk', 'colormodel', self._w, value)
95 def winfo_atom(self, name):
96 return self.tk.getint(self.tk.call('winfo', 'atom', name))
97 def winfo_atomname(self, id):
98 return self.tk.call('winfo', 'atomname', id)
99 def winfo_cells(self):
100 return self.tk.getint(
101 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000102 def winfo_children(self):
103 return map(self._nametowidget,
104 self.tk.splitlist(self.tk.call(
105 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000106 def winfo_class(self):
107 return self.tk.call('winfo', 'class', self._w)
108 def winfo_containing(self, rootX, rootY):
109 return self.tk.call('winfo', 'containing', rootx, rootY)
110 def winfo_depth(self):
111 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
112 def winfo_exists(self):
113 return self.tk.getint(
114 self.tk.call('winfo', 'exists', self._w))
115 def winfo_fpixels(self, number):
116 return self.tk.getdouble(self.tk.call(
117 'winfo', 'fpixels', self._w, number))
118 def winfo_geometry(self):
119 return self.tk.call('winfo', 'geometry', self._w)
120 def winfo_height(self):
121 return self.tk.getint(
122 self.tk.call('winfo', 'height', self._w))
123 def winfo_id(self):
124 return self.tk.getint(
125 self.tk.call('winfo', 'id', self._w))
126 def winfo_interps(self):
127 return self.tk.splitlist(
128 self.tk.call('winfo', 'interps'))
129 def winfo_ismapped(self):
130 return self.tk.getint(
131 self.tk.call('winfo', 'ismapped', self._w))
132 def winfo_name(self):
133 return self.tk.call('winfo', 'name', self._w)
134 def winfo_parent(self):
135 return self.tk.call('winfo', 'parent', self._w)
136 def winfo_pathname(self, id):
137 return self.tk.call('winfo', 'pathname', id)
138 def winfo_pixels(self, number):
139 return self.tk.getint(
140 self.tk.call('winfo', 'pixels', self._w, number))
141 def winfo_reqheight(self):
142 return self.tk.getint(
143 self.tk.call('winfo', 'reqheight', self._w))
144 def winfo_reqwidth(self):
145 return self.tk.getint(
146 self.tk.call('winfo', 'reqwidth', self._w))
147 def winfo_rgb(self, color):
148 return self._getints(
149 self.tk.call('winfo', 'rgb', self._w, color))
150 def winfo_rootx(self):
151 return self.tk.getint(
152 self.tk.call('winfo', 'rootx', self._w))
153 def winfo_rooty(self):
154 return self.tk.getint(
155 self.tk.call('winfo', 'rooty', self._w))
156 def winfo_screen(self):
157 return self.tk.call('winfo', 'screen', self._w)
158 def winfo_screencells(self):
159 return self.tk.getint(
160 self.tk.call('winfo', 'screencells', self._w))
161 def winfo_screendepth(self):
162 return self.tk.getint(
163 self.tk.call('winfo', 'screendepth', self._w))
164 def winfo_screenheight(self):
165 return self.tk.getint(
166 self.tk.call('winfo', 'screenheight', self._w))
167 def winfo_screenmmheight(self):
168 return self.tk.getint(
169 self.tk.call('winfo', 'screenmmheight', self._w))
170 def winfo_screenmmwidth(self):
171 return self.tk.getint(
172 self.tk.call('winfo', 'screenmmwidth', self._w))
173 def winfo_screenvisual(self):
174 return self.tk.call('winfo', 'screenvisual', self._w)
175 def winfo_screenwidth(self):
176 return self.tk.getint(
177 self.tk.call('winfo', 'screenwidth', self._w))
178 def winfo_toplevel(self):
179 return self.tk.call('winfo', 'toplevel', self._w)
180 def winfo_visual(self):
181 return self.tk.call('winfo', 'visual', self._w)
182 def winfo_vrootheight(self):
183 return self.tk.getint(
184 self.tk.call('winfo', 'vrootheight', self._w))
185 def winfo_vrootwidth(self):
186 return self.tk.getint(
187 self.tk.call('winfo', 'vrootwidth', self._w))
188 def winfo_vrootx(self):
189 return self.tk.getint(
190 self.tk.call('winfo', 'vrootx', self._w))
191 def winfo_vrooty(self):
192 return self.tk.getint(
193 self.tk.call('winfo', 'vrooty', self._w))
194 def winfo_width(self):
195 return self.tk.getint(
196 self.tk.call('winfo', 'width', self._w))
197 def winfo_x(self):
198 return self.tk.getint(
199 self.tk.call('winfo', 'x', self._w))
200 def winfo_y(self):
201 return self.tk.getint(
202 self.tk.call('winfo', 'y', self._w))
203 def update(self):
204 self.tk.call('update')
205 def update_idletasks(self):
206 self.tk.call('update', 'idletasks')
207 def bind(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000208 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000209 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000210 self.tk.call('bind', self._w, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000211 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000212 def bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000213 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000214 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000215 self.tk.call('bind', 'all' , sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000216 (add + `name`,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000217 def bind_class(self, className, 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', className , 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 mainloop(self):
223 self.tk.mainloop()
224 def quit(self):
225 self.tk.quit()
226 # Utilities
227 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000228 if not string: return None
229 res = ()
230 for v in self.tk.splitlist(string):
231 res = res + (self.tk.getint(v),)
232 return res
Guido van Rossum18468821994-06-20 07:49:28 +0000233 def _getboolean(self, string):
234 if string:
235 return self.tk.getboolean(string)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000236 # else return None
Guido van Rossum18468821994-06-20 07:49:28 +0000237 def _options(self, cnf):
238 res = ()
239 for k, v in cnf.items():
240 if _isfunctype(v):
241 v = self._register(v)
242 res = res + ('-'+k, v)
243 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000244 def _nametowidget(self, name):
245 w = self
246 if name[0] == '.':
247 w = w._root()
248 name = name[1:]
249 from string import find
250 while name:
251 i = find(name, '.')
252 if i >= 0:
253 name, tail = name[:i], name[i+1:]
254 else:
255 tail = ''
256 w = w.children[name]
257 name = tail
258 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000259 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000260 f = _CallSafely(func, subst).__call__
261 name = `id(f)`
262 if hasattr(func, 'im_func'):
263 func = func.im_func
264 if hasattr(func, 'func_name') and \
265 type(func.func_name) == type(''):
266 name = name + func.func_name
267 self.tk.createcommand(name, f)
268 return name
Guido van Rossum45853db1994-06-20 12:19:19 +0000269 def _root(self):
270 w = self
271 while w.master: w = w.master
272 return w
273 _subst_prefix = ('%#', '%b', '%f', '%h', '%k',
274 '%s', '%t', '%w', '%x', '%y',
275 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
276 def _substitute(self, *args):
277 tk = self.tk
278 if len(args) != len(self._subst_prefix): return args
279 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
280 # Missing: (a, c, d, m, o, v, B, R)
281 e = Event()
282 e.serial = tk.getint(nsign)
283 e.num = tk.getint(b)
284 try: e.focus = tk.getboolean(f)
285 except TclError: pass
286 e.height = tk.getint(h)
287 e.keycode = tk.getint(k)
288 e.state = tk.getint(s)
289 e.time = tk.getint(t)
290 e.width = tk.getint(w)
291 e.x = tk.getint(x)
292 e.y = tk.getint(y)
293 e.char = A
294 try: e.send_event = tk.getboolean(E)
295 except TclError: pass
296 e.keysym = K
297 e.keysym_num = tk.getint(N)
298 e.type = T
299 e.widget = self._nametowidget(W)
300 e.x_root = tk.getint(X)
301 e.y_root = tk.getint(Y)
302 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000303
304class _CallSafely:
305 def __init__(self, func, subst=None):
306 self.func = func
307 self.subst = subst
308 def __call__(self, *args):
309 if self.subst:
310 args = self.apply_func(self.subst, args)
311 args = self.apply_func(self.func, args)
312 def apply_func(self, func, args):
313 import sys
314 try:
315 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000316 except SystemExit, msg:
317 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000318 except:
319 try:
320 try:
321 t = sys.exc_traceback
322 while t:
323 sys.stderr.write(
324 ' %s, line %s\n' %
325 (t.tb_frame.f_code,
326 t.tb_lineno))
327 t = t.tb_next
328 finally:
329 sys.stderr.write('%s: %s\n' %
330 (sys.exc_type,
331 sys.exc_value))
Guido van Rossum45853db1994-06-20 12:19:19 +0000332 (sys.last_type,
333 sys.last_value,
334 sys.last_traceback) = (sys.exc_type,
335 sys.exc_value,
336 sys.exc_traceback)
337 import pdb
338 pdb.pm()
Guido van Rossum18468821994-06-20 07:49:28 +0000339 except:
340 print '*** Error in error handling ***'
341 print sys.exc_type, ':', sys.exc_value
342
343class Wm:
344 def aspect(self,
345 minNumer=None, minDenom=None,
346 maxNumer=None, maxDenom=None):
347 return self._getints(
348 self.tk.call('wm', 'aspect', self._w,
349 minNumer, minDenom,
350 maxNumer, maxDenom))
351 def client(self, name=None):
352 return self.tk.call('wm', 'client', self._w, name)
353 def command(self, value=None):
354 return self.tk.call('wm', 'command', self._w, value)
355 def deiconify(self):
356 return self.tk.call('wm', 'deiconify', self._w)
357 def focusmodel(self, model=None):
358 return self.tk.call('wm', 'focusmodel', self._w, model)
359 def frame(self):
360 return self.tk.call('wm', 'frame', self._w)
361 def geometry(self, newGeometry=None):
362 return self.tk.call('wm', 'geometry', self._w, newGeometry)
363 def grid(self,
364 baseWidht=None, baseHeight=None,
365 widthInc=None, heightInc=None):
366 return self._getints(self.tk.call(
367 'wm', 'grid', self._w,
368 baseWidht, baseHeight, widthInc, heightInc))
369 def group(self, pathName=None):
370 return self.tk.call('wm', 'group', self._w, pathName)
371 def iconbitmap(self, bitmap=None):
372 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
373 def iconify(self):
374 return self.tk.call('wm', 'iconify', self._w)
375 def iconmask(self, bitmap=None):
376 return self.tk.call('wm', 'iconmask', self._w, bitmap)
377 def iconname(self, newName=None):
378 return self.tk.call('wm', 'iconname', self._w, newName)
379 def iconposition(self, x=None, y=None):
380 return self._getints(self.tk.call(
381 'wm', 'iconposition', self._w, x, y))
382 def iconwindow(self, pathName=None):
383 return self.tk.call('wm', 'iconwindow', self._w, pathName)
384 def maxsize(self, width=None, height=None):
385 return self._getints(self.tk.call(
386 'wm', 'maxsize', self._w, width, height))
387 def minsize(self, width=None, height=None):
388 return self._getints(self.tk.call(
389 'wm', 'minsize', self._w, width, height))
390 def overrideredirect(self, boolean=None):
391 return self._getboolean(self.tk.call(
392 'wm', 'overrideredirect', self._w, boolean))
393 def positionfrom(self, who=None):
394 return self.tk.call('wm', 'positionfrom', self._w, who)
395 def protocol(self, name=None, func=None):
396 if _isfunctype(func):
397 command = self._register(func)
398 else:
399 command = func
400 return self.tk.call(
401 'wm', 'protocol', self._w, name, command)
402 def sizefrom(self, who=None):
403 return self.tk.call('wm', 'sizefrom', self._w, who)
404 def state(self):
405 return self.tk.call('wm', 'state', self._w)
406 def title(self, string=None):
407 return self.tk.call('wm', 'title', self._w, string)
408 def transient(self, master=None):
409 return self.tk.call('wm', 'transient', self._w, master)
410 def withdraw(self):
411 return self.tk.call('wm', 'withdraw', self._w)
412
413class Tk(Misc, Wm):
414 _w = '.'
415 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000416 self.master = None
417 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000418 if baseName is None:
419 import sys, os
420 baseName = os.path.basename(sys.argv[0])
421 if baseName[-3:] == '.py': baseName = baseName[:-3]
422 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000423 self.tk.createcommand('tkerror', _tkerror)
424 def destroy(self):
425 for c in self.children.values(): c.destroy()
426 del self.master.children[self._name]
427 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000428 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000429 return self._w
Guido van Rossum18468821994-06-20 07:49:28 +0000430
431class Pack:
432 def config(self, cnf={}):
433 apply(self.tk.call,
434 ('pack', 'configure', self._w)
435 + self._options(cnf))
436 pack = config
437 def __setitem__(self, key, value):
438 Pack.config({key: value})
439 def forget(self):
440 self.tk.call('pack', 'forget', self._w)
441 def newinfo(self):
442 return self.tk.call('pack', 'newinfo', self._w)
443 info = newinfo
444 def propagate(self, boolean=None):
445 if boolean:
446 self.tk.call('pack', 'propagate', self._w)
447 else:
448 return self._getboolean(self.tk.call(
449 'pack', 'propagate', self._w))
450 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000451 return map(self._nametowidget,
452 self.tk.splitlist(
453 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000454
455class Place:
456 def config(self, cnf={}):
457 apply(self.tk.call,
458 ('place', 'configure', self._w)
459 + self._options(cnf))
460 place = config
461 def __setitem__(self, key, value):
462 Place.config({key: value})
463 def forget(self):
464 self.tk.call('place', 'forget', self._w)
465 def info(self):
466 return self.tk.call('place', 'info', self._w)
467 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000468 return map(self._nametowidget,
469 self.tk.splitlist(
470 self.tk.call(
471 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000472
Guido van Rossum45853db1994-06-20 12:19:19 +0000473_default_root = None
Guido van Rossum18468821994-06-20 07:49:28 +0000474
475class Widget(Misc, Pack, Place):
476 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum45853db1994-06-20 12:19:19 +0000477 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000478 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000479 if not _default_root:
480 _default_root = Tk()
481 master = _default_root
482 if not _default_root:
483 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000484 self.master = master
485 self.tk = master.tk
486 if cnf.has_key('name'):
487 name = cnf['name']
488 del cnf['name']
489 else:
490 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000491 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000492 if master._w=='.':
493 self._w = '.' + name
494 else:
495 self._w = master._w + '.' + name
496 self.widgetName = widgetName
497 apply(self.tk.call, (widgetName, self._w) + extra)
498 Widget.config(self, cnf)
Guido van Rossum45853db1994-06-20 12:19:19 +0000499 self.children = {}
500 if master.children.has_key(name):
501 master.children[name].destroy()
502 master.children[name] = self
Guido van Rossum18468821994-06-20 07:49:28 +0000503 def config(self, cnf={}):
504 for k in cnf.keys():
505 if type(k) == ClassType:
506 k.config(self, cnf[k])
507 del cnf[k]
508 apply(self.tk.call, (self._w, 'configure')
509 + self._options(cnf))
510 def __getitem__(self, key):
511 v = self.tk.split(self.tk.call(
512 self._w, 'configure', '-' + key))
513 return v[4]
514 def __setitem__(self, key, value):
515 Widget.config(self, {key: value})
516 def __str__(self):
517 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000518 def destroy(self):
519 for c in self.children.values(): c.destroy()
520 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000521 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000522 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000523 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000524
525class Toplevel(Widget, Wm):
526 def __init__(self, master=None, cnf={}):
527 extra = ()
528 if cnf.has_key('screen'):
529 extra = ('-screen', cnf['screen'])
530 del cnf['screen']
531 if cnf.has_key('class'):
532 extra = extra + ('-class', cnf['class'])
533 del cnf['class']
534 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000535 root = self._root()
536 self.iconname(root.iconname())
537 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000538
539class Button(Widget):
540 def __init__(self, master=None, cnf={}):
541 Widget.__init__(self, master, 'button', cnf)
542 def tk_butEnter(self):
543 self.tk.call('tk_butEnter', self._w)
544 def tk_butLeave(self):
545 self.tk.call('tk_butLeave', self._w)
546 def tk_butDown(self):
547 self.tk.call('tk_butDown', self._w)
548 def tk_butUp(self):
549 self.tk.call('tk_butUp', self._w)
550 def flash(self):
551 self.tk.call(self._w, 'flash')
552 def invoke(self):
553 self.tk.call(self._w, 'invoke')
554
555# Indices:
556def AtEnd():
557 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000558def AtInsert(*args):
559 s = 'insert'
560 for a in args:
561 if a: s = s + (' ' + a)
562 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000563def AtSelFirst():
564 return 'sel.first'
565def AtSelLast():
566 return 'sel.last'
567def At(x, y=None):
568 if y:
569 return '@' + `x` + ',' + `y`
570 else:
571 return '@' + `x`
572
573class Canvas(Widget):
574 def __init__(self, master=None, cnf={}):
575 Widget.__init__(self, master, 'canvas', cnf)
576 def addtag(self, *args):
577 self._do('addtag', args)
578 def bbox(self, *args):
579 return self._getints(self._do('bbox', args))
580 def bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000581 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000582 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000583 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000584 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000585 def canvasx(self, screenx, gridspacing=None):
586 return self.tk.getint(self.tk.call(
587 self._w, 'canvasx', screenx, gridspacing))
588 def canvasy(self, screeny, gridspacing=None):
589 return self.tk.getint(self.tk.call(
590 self._w, 'canvasy', screeny, gridspacing))
591 def coords(self, *args):
592 return self._do('coords', args)
593 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
594 cnf = args[-1]
595 if type(cnf) == type({}):
596 args = args[:-1]
597 else:
598 cnf = {}
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000599 v = (self._w, 'create', itemType) + _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000600 for k in cnf.keys():
601 v = v + ('-' + k, cnf[k])
602 return self.tk.getint(apply(self.tk.call, v))
603 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000604 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000605 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000606 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000607 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000608 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000609 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000610 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000611 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000612 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000613 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000614 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000615 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000616 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000617 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000618 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000619 def dchars(self, *args):
620 self._do('dchars', args)
621 def delete(self, *args):
622 self._do('delete', args)
623 def dtag(self, *args):
624 self._do('dtag', args)
625 def find(self, *args):
Guido van Rossum45853db1994-06-20 12:19:19 +0000626 return self.tk.splitlist(self._do('find', args))
Guido van Rossum18468821994-06-20 07:49:28 +0000627 def focus(self, *args):
628 return self._do('focus', args)
629 def gettags(self, *args):
630 return self.tk.splitlist(self._do('gettags', args))
631 def icursor(self, *args):
632 self._do('icursor', args)
633 def index(self, *args):
634 return self.tk.getint(self._do('index', args))
635 def insert(self, *args):
636 self._do('insert', args)
637 def itemconfig(self, tagOrId, cnf={}):
638 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
639 def lower(self, *args):
640 self._do('lower', args)
641 def move(self, *args):
642 self._do('move', args)
643 def postscript(self, cnf={}):
644 return self._do('postscript', self._options(cnf))
645 def tkraise(self, *args):
646 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000647 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000648 def scale(self, *args):
649 self._do('scale', args)
650 def scan_mark(self, x, y):
651 self.tk.call(self._w, 'scan', 'mark', x, y)
652 def scan_dragto(self, x, y):
653 self.tk.call(self._w, 'scan', 'dragto', x, y)
654 def select_adjust(self, tagOrId, index):
655 self.tk.call(
656 self._w, 'select', 'adjust', tagOrId, index)
657 def select_clear(self):
658 self.tk.call(self._w, 'select', 'clear')
659 def select_from(self, tagOrId, index):
660 self.tk.call(
661 self._w, 'select', 'from', tagOrId, index)
662 def select_item(self):
663 self.tk.call(self._w, 'select', 'item')
664 def select_to(self, tagOrId, index):
665 self.tk.call(
666 self._w, 'select', 'to', tagOrId, index)
667 def type(self, tagOrId):
668 return self.tk.splitlist(self.tk.call(
669 self._w, 'type', tagOrId))
670 def xview(self, index):
671 self.tk.call(self._w, 'xview', index)
672 def yview(self, index):
673 self.tk.call(self._w, 'yview', index)
674
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000675def _flatten(tuple):
676 res = ()
677 for item in tuple:
678 if type(item) in (TupleType, ListType):
679 res = res + _flatten(item)
680 else:
681 res = res + (item,)
682 return res
683
Guido van Rossum18468821994-06-20 07:49:28 +0000684class Checkbutton(Widget):
685 def __init__(self, master=None, cnf={}):
686 Widget.__init__(self, master, 'checkbutton', cnf)
687 def deselect(self):
688 self.tk.call(self._w, 'deselect')
689 def flash(self):
690 self.tk.call(self._w, 'flash')
691 def invoke(self):
692 self.tk.call(self._w, 'invoke')
693 def select(self):
694 self.tk.call(self._w, 'select')
695 def toggle(self):
696 self.tk.call(self._w, 'toggle')
697
698class Entry(Widget):
699 def __init__(self, master=None, cnf={}):
700 Widget.__init__(self, master, 'entry', cnf)
701 def tk_entryBackspace(self):
702 self.tk.call('tk_entryBackspace', self._w)
703 def tk_entryBackword(self):
704 self.tk.call('tk_entryBackword', self._w)
705 def tk_entrySeeCaret(self):
706 self.tk.call('tk_entrySeeCaret', self._w)
707 def delete(self, first, last=None):
708 self.tk.call(self._w, 'delete', first, last)
709 def get(self):
710 return self.tk.call(self._w, 'get')
711 def icursor(self, index):
712 self.tk.call(self._w, 'icursor', index)
713 def index(self, index):
714 return self.tk.getint(self.tk.call(
715 self._w, 'index', index))
716 def insert(self, index, string):
717 self.tk.call(self._w, 'insert', index, string)
718 def scan_mark(self, x):
719 self.tk.call(self._w, 'scan', 'mark', x)
720 def scan_dragto(self, x):
721 self.tk.call(self._w, 'scan', 'dragto', x)
722 def select_adjust(self, index):
723 self.tk.call(self._w, 'select', 'adjust', index)
724 def select_clear(self):
725 self.tk.call(self._w, 'select', 'clear')
726 def select_from(self, index):
727 self.tk.call(self._w, 'select', 'from', index)
728 def select_to(self, index):
729 self.tk.call(self._w, 'select', 'to', index)
730 def select_view(self, index):
731 self.tk.call(self._w, 'select', 'view', index)
732
733class Frame(Widget):
734 def __init__(self, master=None, cnf={}):
735 extra = ()
736 if cnf.has_key('class'):
737 extra = ('-class', cnf['class'])
738 del cnf['class']
739 Widget.__init__(self, master, 'frame', cnf, extra)
740 def tk_menuBar(self, *args):
741 apply(self.tk.call, ('tk_menuBar', self._w) + args)
742
743class Label(Widget):
744 def __init__(self, master=None, cnf={}):
745 Widget.__init__(self, master, 'label', cnf)
746
747class Listbox(Widget):
748 def __init__(self, master=None, cnf={}):
749 Widget.__init__(self, master, 'listbox', cnf)
750 def tk_listboxSingleSelect(self):
751 self.tk.call('tk_listboxSingleSelect', self._w)
752 def curselection(self):
753 return self.tk.splitlist(self.tk.call(
754 self._w, 'curselection'))
755 def delete(self, first, last=None):
756 self.tk.call(self._w, 'delete', first, last)
757 def get(self, index):
758 return self.tk.call(self._w, 'get', index)
759 def insert(self, index, *elements):
760 apply(self.tk.call,
761 (self._w, 'insert', index) + elements)
762 def nearest(self, y):
763 return self.tk.getint(self.tk.call(
764 self._w, 'nearest', y))
765 def scan_mark(self, x, y):
766 self.tk.call(self._w, 'scan', 'mark', x, y)
767 def scan_dragto(self, x, y):
768 self.tk.call(self._w, 'scan', 'dragto', x, y)
769 def select_adjust(self, index):
770 self.tk.call(self._w, 'select', 'adjust', index)
771 def select_clear(self):
772 self.tk.call(self._w, 'select', 'clear')
773 def select_from(self, index):
774 self.tk.call(self._w, 'select', 'from', index)
775 def select_to(self, index):
776 self.tk.call(self._w, 'select', 'to', index)
777 def size(self):
778 return self.tk.getint(self.tk.call(self._w, 'size'))
779 def xview(self, index):
780 self.tk.call(self._w, 'xview', index)
781 def yview(self, index):
782 self.tk.call(self._w, 'yview', index)
783
784class Menu(Widget):
785 def __init__(self, master=None, cnf={}):
786 Widget.__init__(self, master, 'menu', cnf)
787 def tk_menuBar(self, *args):
788 apply(self.tk.call, ('tk_menuBar', self._w) + args)
789 def tk_bindForTraversal(self):
790 self.tk.call('tk_bindForTraversal', self._w)
791 def tk_mbPost(self):
792 self.tk.call('tk_mbPost', self._w)
793 def tk_mbUnpost(self):
794 self.tk.call('tk_mbUnpost')
795 def tk_traverseToMenu(self, char):
796 self.tk.call('tk_traverseToMenu', self._w, char)
797 def tk_traverseWithinMenu(self, char):
798 self.tk.call('tk_traverseWithinMenu', self._w, char)
799 def tk_getMenuButtons(self):
800 return self.tk.call('tk_getMenuButtons', self._w)
801 def tk_nextMenu(self, count):
802 self.tk.call('tk_nextMenu', count)
803 def tk_nextMenuEntry(self, count):
804 self.tk.call('tk_nextMenuEntry', count)
805 def tk_invokeMenu(self):
806 self.tk.call('tk_invokeMenu', self._w)
807 def tk_firstMenu(self):
808 self.tk.call('tk_firstMenu', self._w)
809 def tk_mbButtonDown(self):
810 self.tk.call('tk_mbButtonDown', self._w)
811 def activate(self, index):
812 self.tk.call(self._w, 'activate', index)
813 def add(self, itemType, cnf={}):
814 apply(self.tk.call, (self._w, 'add', itemType)
815 + self._options(cnf))
816 def delete(self, index1, index2=None):
817 self.tk.call(self._w, 'delete', index1, index2)
818 def entryconfig(self, index, cnf={}):
819 apply(self.tk.call, (self._w, 'entryconfigure', index)
820 + self._options(cnf))
821 def index(self, index):
822 return self.tk.call(self._w, 'index', index)
823 def invoke(self, index):
824 return self.tk.call(self._w, 'invoke', index)
825 def post(self, x, y):
826 self.tk.call(self._w, 'post', x, y)
827 def unpost(self):
828 self.tk.call(self._w, 'unpost')
829 def yposition(self, index):
830 return self.tk.getint(self.tk.call(
831 self._w, 'yposition', index))
832
833class Menubutton(Widget):
834 def __init__(self, master=None, cnf={}):
835 Widget.__init__(self, master, 'menubutton', cnf)
836
837class Message(Widget):
838 def __init__(self, master=None, cnf={}):
839 Widget.__init__(self, master, 'message', cnf)
840
841class Radiobutton(Widget):
842 def __init__(self, master=None, cnf={}):
843 Widget.__init__(self, master, 'radiobutton', cnf)
844 def deselect(self):
845 self.tk.call(self._w, 'deselect')
846 def flash(self):
847 self.tk.call(self._w, 'flash')
848 def invoke(self):
849 self.tk.call(self._w, 'invoke')
850 def select(self):
851 self.tk.call(self._w, 'select')
852
853class Scale(Widget):
854 def __init__(self, master=None, cnf={}):
855 Widget.__init__(self, master, 'scale', cnf)
856 def get(self):
857 return self.tk.getint(self.tk.call(self._w, 'get'))
858 def set(self, value):
859 self.tk.call(self._w, 'set', value)
860
861class Scrollbar(Widget):
862 def __init__(self, master=None, cnf={}):
863 Widget.__init__(self, master, 'scrollbar', cnf)
864 def get(self):
865 return self.tk.getints(self.tk.call(self._w, 'get'))
866 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
867 self.tk.call(self._w, 'set',
868 totalUnits, windowUnits, firstUnit, lastUnit)
869
870class Text(Widget):
871 def __init__(self, master=None, cnf={}):
872 Widget.__init__(self, master, 'text', cnf)
873 def tk_textSelectTo(self, index):
874 self.tk.call('tk_textSelectTo', self._w, index)
875 def tk_textBackspace(self):
876 self.tk.call('tk_textBackspace', self._w)
877 def tk_textIndexCloser(self, a, b, c):
878 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
879 def tk_textResetAnchor(self, index):
880 self.tk.call('tk_textResetAnchor', self._w, index)
881 def compare(self, index1, op, index2):
882 return self.tk.getboolean(self.tk.call(
883 self._w, 'compare', index1, op, index2))
884 def debug(self, boolean=None):
885 return self.tk.getboolean(self.tk.call(
886 self._w, 'debug', boolean))
887 def delete(self, index1, index2=None):
888 self.tk.call(self._w, 'delete', index1, index2)
889 def get(self, index1, index2=None):
890 return self.tk.call(self._w, 'get', index1, index2)
891 def index(self, index):
892 return self.tk.call(self._w, 'index', index)
893 def insert(self, index, chars):
894 self.tk.call(self._w, 'insert', index, chars)
895 def mark_names(self):
896 return self.tk.splitlist(self.tk.call(
897 self._w, 'mark', 'names'))
898 def mark_set(self, markName, index):
899 self.tk.call(self._w, 'mark', 'set', markName, index)
900 def mark_unset(self, markNames):
901 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
902 def scan_mark(self, y):
903 self.tk.call(self._w, 'scan', 'mark', y)
904 def scan_dragto(self, y):
905 self.tk.call(self._w, 'scan', 'dragto', y)
906 def tag_add(self, tagName, index1, index2=None):
907 self.tk.call(
908 self._w, 'tag', 'add', tagName, index1, index2)
909 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000910 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000911 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000912 self.tk.call(self._w, 'tag', 'bind',
913 tagName, sequence,
Guido van Rossum45853db1994-06-20 12:19:19 +0000914 (add + name,) + self._subst_prefix)
Guido van Rossum18468821994-06-20 07:49:28 +0000915 def tag_config(self, tagName, cnf={}):
916 apply(self.tk.call,
917 (self._w, 'tag', 'configure', tagName)
918 + self._options(cnf))
919 def tag_delete(self, tagNames):
920 apply(self.tk.call, (self._w, 'tag', 'delete')
921 + tagNames)
922 def tag_lower(self, tagName, belowThis=None):
923 self.tk.call(self._w, 'tag', 'lower',
924 tagName, belowThis)
925 def tag_names(self, index=None):
926 return self.tk.splitlist(
927 self.tk.call(self._w, 'tag', 'names', index))
928 def tag_nextrange(self, tagName, index1, index2=None):
929 return self.tk.splitlist(self.tk.call(
930 self._w, 'tag', 'nextrange', index1, index2))
931 def tag_raise(self, tagName, aboveThis=None):
932 self.tk.call(
933 self._w, 'tag', 'raise', tagName, aboveThis)
934 def tag_ranges(self, tagName):
935 return self.tk.splitlist(self.tk.call(
936 self._w, 'tag', 'ranges', tagName))
937 def tag_remove(self, tagName, index1, index2=None):
938 self.tk.call(
939 self._w, 'tag', 'remove', index1, index2)
940 def yview(self, what):
941 self.tk.call(self._w, 'yview', what)
942 def yview_pickplace(self, what):
943 self.tk.call(self._w, 'yview', '-pickplace', what)
944
945#class Dialog:
946