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