blob: 4d2dd3ab1a1ebb0731309d73013fc29da1735b2f [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 Rossumaec5dc91994-06-27 07:55:12 +000019_default_root = None
20
Guido van Rossum45853db1994-06-20 12:19:19 +000021def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000022 pass
23
24class Event:
25 pass
26
Guido van Rossumaec5dc91994-06-27 07:55:12 +000027_varnum = 0
28class Variable:
29 def __init__(self, master=None):
30 global _default_root
31 global _varnum
32 if master:
33 self._tk = master.tk
34 else:
35 self._tk = _default_root.tk
36 self._name = 'PY_VAR' + `_varnum`
37 _varnum = _varnum + 1
38 def __del__(self):
39 self._tk.unsetvar(self._name)
40 def __str__(self):
41 return self._name
42 def __call__(self, value=None):
43 if value == None:
44 return self.get()
45 else:
46 self.set(value)
47 def set(self, value):
48 return self._tk.setvar(self._name, value)
49
50class StringVar(Variable):
51 def __init__(self, master=None):
52 Variable.__init__(self, master)
53 def get(self):
54 return self._tk.getvar(self._name)
55
56class IntVar(Variable):
57 def __init__(self, master=None):
58 Variable.__init__(self, master)
59 def get(self):
60 return self._tk.getint(self._tk.getvar(self._name))
61
62class DoubleVar(Variable):
63 def __init__(self, master=None):
64 Variable.__init__(self, master)
65 def get(self):
66 return self._tk.getdouble(self._tk.getvar(self._name))
67
68class BooleanVar(Variable):
69 def __init__(self, master=None):
70 Variable.__init__(self, master)
71 def get(self):
72 return self._tk.getboolean(self._tk.getvar(self._name))
73
Guido van Rossum18468821994-06-20 07:49:28 +000074class Misc:
75 def tk_strictMotif(self, boolean=None):
76 self.tk.getboolean(self.tk.call(
77 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 def tk_menuBar(self, *args):
79 apply(self.tk.call, ('tk_menuBar', self._w) + args)
80 def waitvar(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +000081 self.tk.call('tkwait', 'variable', name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000082 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +000083 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000084 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +000085 return self.tk.getvar(name)
86 def getint(self, s):
87 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +000088 def getdouble(self, s):
89 return self.tk.getdouble(s)
90 def getboolean(self, s):
91 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +000092 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +000093 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +000094 focus = focus_set # XXX b/w compat?
95 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +000096 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +000097 def focus_default_none(self):
98 self.tk.call('focus', 'default', 'none')
99 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000100 def focus_none(self):
101 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000102 def focus_get(self):
103 name = self.tk.call('focus')
104 if name == 'none': return None
105 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000106 def after(self, ms, func=None, *args):
107 if not func:
108 self.tk.call('after', ms)
109 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000110 # XXX Disgusting hack to clean up after calling func
111 tmp = []
112 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
113 try:
114 apply(func, args)
115 finally:
116 tk.deletecommand(tmp[0])
117 name = self._register(callit)
118 tmp.append(name)
119 self.tk.call('after', ms, name)
Guido van Rossum45853db1994-06-20 12:19:19 +0000120 # XXX grab current w/o window argument
121 def grab_current(self):
122 name = self.tk.call('grab', 'current', self._w)
123 if not name: return None
124 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000125 def grab_release(self):
126 self.tk.call('grab', 'release', self._w)
127 def grab_set(self):
128 self.tk.call('grab', 'set', self._w)
129 def grab_set_global(self):
130 self.tk.call('grab', 'set', '-global', self._w)
131 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000132 status = self.tk.call('grab', 'status', self._w)
133 if status == 'none': status = None
134 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000135 def lower(self, belowThis=None):
136 self.tk.call('lower', self._w, belowThis)
137 def selection_clear(self):
138 self.tk.call('selection', 'clear', self._w)
139 def selection_get(self, type=None):
140 self.tk.call('selection', 'get', type)
141 def selection_handle(self, func, type=None, format=None):
142 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000143 self.tk.call('selection', 'handle', self._w,
144 name, type, format)
145 def selection_own(self, func=None):
146 name = self._register(func)
147 self.tk.call('selection', 'own', self._w, name)
148 def selection_own_get(self):
149 return self._nametowidget(self.tk.call('selection', 'own'))
150 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000151 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000152 def lower(self, belowThis=None):
153 self.tk.call('lift', self._w, belowThis)
154 def tkraise(self, aboveThis=None):
155 self.tk.call('raise', self._w, aboveThis)
156 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000157 def colormodel(self, value=None):
158 return self.tk.call('tk', 'colormodel', self._w, value)
159 def winfo_atom(self, name):
160 return self.tk.getint(self.tk.call('winfo', 'atom', name))
161 def winfo_atomname(self, id):
162 return self.tk.call('winfo', 'atomname', id)
163 def winfo_cells(self):
164 return self.tk.getint(
165 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000166 def winfo_children(self):
167 return map(self._nametowidget,
168 self.tk.splitlist(self.tk.call(
169 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000170 def winfo_class(self):
171 return self.tk.call('winfo', 'class', self._w)
172 def winfo_containing(self, rootX, rootY):
173 return self.tk.call('winfo', 'containing', rootx, rootY)
174 def winfo_depth(self):
175 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
176 def winfo_exists(self):
177 return self.tk.getint(
178 self.tk.call('winfo', 'exists', self._w))
179 def winfo_fpixels(self, number):
180 return self.tk.getdouble(self.tk.call(
181 'winfo', 'fpixels', self._w, number))
182 def winfo_geometry(self):
183 return self.tk.call('winfo', 'geometry', self._w)
184 def winfo_height(self):
185 return self.tk.getint(
186 self.tk.call('winfo', 'height', self._w))
187 def winfo_id(self):
188 return self.tk.getint(
189 self.tk.call('winfo', 'id', self._w))
190 def winfo_interps(self):
191 return self.tk.splitlist(
192 self.tk.call('winfo', 'interps'))
193 def winfo_ismapped(self):
194 return self.tk.getint(
195 self.tk.call('winfo', 'ismapped', self._w))
196 def winfo_name(self):
197 return self.tk.call('winfo', 'name', self._w)
198 def winfo_parent(self):
199 return self.tk.call('winfo', 'parent', self._w)
200 def winfo_pathname(self, id):
201 return self.tk.call('winfo', 'pathname', id)
202 def winfo_pixels(self, number):
203 return self.tk.getint(
204 self.tk.call('winfo', 'pixels', self._w, number))
205 def winfo_reqheight(self):
206 return self.tk.getint(
207 self.tk.call('winfo', 'reqheight', self._w))
208 def winfo_reqwidth(self):
209 return self.tk.getint(
210 self.tk.call('winfo', 'reqwidth', self._w))
211 def winfo_rgb(self, color):
212 return self._getints(
213 self.tk.call('winfo', 'rgb', self._w, color))
214 def winfo_rootx(self):
215 return self.tk.getint(
216 self.tk.call('winfo', 'rootx', self._w))
217 def winfo_rooty(self):
218 return self.tk.getint(
219 self.tk.call('winfo', 'rooty', self._w))
220 def winfo_screen(self):
221 return self.tk.call('winfo', 'screen', self._w)
222 def winfo_screencells(self):
223 return self.tk.getint(
224 self.tk.call('winfo', 'screencells', self._w))
225 def winfo_screendepth(self):
226 return self.tk.getint(
227 self.tk.call('winfo', 'screendepth', self._w))
228 def winfo_screenheight(self):
229 return self.tk.getint(
230 self.tk.call('winfo', 'screenheight', self._w))
231 def winfo_screenmmheight(self):
232 return self.tk.getint(
233 self.tk.call('winfo', 'screenmmheight', self._w))
234 def winfo_screenmmwidth(self):
235 return self.tk.getint(
236 self.tk.call('winfo', 'screenmmwidth', self._w))
237 def winfo_screenvisual(self):
238 return self.tk.call('winfo', 'screenvisual', self._w)
239 def winfo_screenwidth(self):
240 return self.tk.getint(
241 self.tk.call('winfo', 'screenwidth', self._w))
242 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000243 return self._nametowidget(self.tk.call(
244 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000245 def winfo_visual(self):
246 return self.tk.call('winfo', 'visual', self._w)
247 def winfo_vrootheight(self):
248 return self.tk.getint(
249 self.tk.call('winfo', 'vrootheight', self._w))
250 def winfo_vrootwidth(self):
251 return self.tk.getint(
252 self.tk.call('winfo', 'vrootwidth', self._w))
253 def winfo_vrootx(self):
254 return self.tk.getint(
255 self.tk.call('winfo', 'vrootx', self._w))
256 def winfo_vrooty(self):
257 return self.tk.getint(
258 self.tk.call('winfo', 'vrooty', self._w))
259 def winfo_width(self):
260 return self.tk.getint(
261 self.tk.call('winfo', 'width', self._w))
262 def winfo_x(self):
263 return self.tk.getint(
264 self.tk.call('winfo', 'x', self._w))
265 def winfo_y(self):
266 return self.tk.getint(
267 self.tk.call('winfo', 'y', self._w))
268 def update(self):
269 self.tk.call('update')
270 def update_idletasks(self):
271 self.tk.call('update', 'idletasks')
272 def bind(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000273 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000274 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000275 self.tk.call('bind', self._w, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000276 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000277 def bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000278 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000279 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000280 self.tk.call('bind', 'all' , sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000281 (add + `name`,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000282 def bind_class(self, className, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000283 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000284 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000285 self.tk.call('bind', className , sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000286 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000287 def mainloop(self):
288 self.tk.mainloop()
289 def quit(self):
290 self.tk.quit()
291 # Utilities
292 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000293 if not string: return None
294 res = ()
295 for v in self.tk.splitlist(string):
296 res = res + (self.tk.getint(v),)
297 return res
Guido van Rossum18468821994-06-20 07:49:28 +0000298 def _getboolean(self, string):
299 if string:
300 return self.tk.getboolean(string)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000301 # else return None
Guido van Rossum18468821994-06-20 07:49:28 +0000302 def _options(self, cnf):
303 res = ()
304 for k, v in cnf.items():
305 if _isfunctype(v):
306 v = self._register(v)
307 res = res + ('-'+k, v)
308 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000309 def _nametowidget(self, name):
310 w = self
311 if name[0] == '.':
312 w = w._root()
313 name = name[1:]
314 from string import find
315 while name:
316 i = find(name, '.')
317 if i >= 0:
318 name, tail = name[:i], name[i+1:]
319 else:
320 tail = ''
321 w = w.children[name]
322 name = tail
323 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000325 f = _CallSafely(func, subst).__call__
326 name = `id(f)`
327 if hasattr(func, 'im_func'):
328 func = func.im_func
329 if hasattr(func, 'func_name') and \
330 type(func.func_name) == type(''):
331 name = name + func.func_name
332 self.tk.createcommand(name, f)
333 return name
Guido van Rossum45853db1994-06-20 12:19:19 +0000334 def _root(self):
335 w = self
336 while w.master: w = w.master
337 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000338 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000339 '%s', '%t', '%w', '%x', '%y',
340 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
341 def _substitute(self, *args):
342 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000343 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000344 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
345 # Missing: (a, c, d, m, o, v, B, R)
346 e = Event()
347 e.serial = tk.getint(nsign)
348 e.num = tk.getint(b)
349 try: e.focus = tk.getboolean(f)
350 except TclError: pass
351 e.height = tk.getint(h)
352 e.keycode = tk.getint(k)
353 e.state = tk.getint(s)
354 e.time = tk.getint(t)
355 e.width = tk.getint(w)
356 e.x = tk.getint(x)
357 e.y = tk.getint(y)
358 e.char = A
359 try: e.send_event = tk.getboolean(E)
360 except TclError: pass
361 e.keysym = K
362 e.keysym_num = tk.getint(N)
363 e.type = T
364 e.widget = self._nametowidget(W)
365 e.x_root = tk.getint(X)
366 e.y_root = tk.getint(Y)
367 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000368
369class _CallSafely:
370 def __init__(self, func, subst=None):
371 self.func = func
372 self.subst = subst
373 def __call__(self, *args):
374 if self.subst:
375 args = self.apply_func(self.subst, args)
376 args = self.apply_func(self.func, args)
377 def apply_func(self, func, args):
378 import sys
379 try:
380 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000381 except SystemExit, msg:
382 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000383 except:
384 try:
385 try:
386 t = sys.exc_traceback
387 while t:
388 sys.stderr.write(
389 ' %s, line %s\n' %
390 (t.tb_frame.f_code,
391 t.tb_lineno))
392 t = t.tb_next
393 finally:
394 sys.stderr.write('%s: %s\n' %
395 (sys.exc_type,
396 sys.exc_value))
Guido van Rossum45853db1994-06-20 12:19:19 +0000397 (sys.last_type,
398 sys.last_value,
399 sys.last_traceback) = (sys.exc_type,
400 sys.exc_value,
401 sys.exc_traceback)
402 import pdb
403 pdb.pm()
Guido van Rossum18468821994-06-20 07:49:28 +0000404 except:
405 print '*** Error in error handling ***'
406 print sys.exc_type, ':', sys.exc_value
407
408class Wm:
409 def aspect(self,
410 minNumer=None, minDenom=None,
411 maxNumer=None, maxDenom=None):
412 return self._getints(
413 self.tk.call('wm', 'aspect', self._w,
414 minNumer, minDenom,
415 maxNumer, maxDenom))
416 def client(self, name=None):
417 return self.tk.call('wm', 'client', self._w, name)
418 def command(self, value=None):
419 return self.tk.call('wm', 'command', self._w, value)
420 def deiconify(self):
421 return self.tk.call('wm', 'deiconify', self._w)
422 def focusmodel(self, model=None):
423 return self.tk.call('wm', 'focusmodel', self._w, model)
424 def frame(self):
425 return self.tk.call('wm', 'frame', self._w)
426 def geometry(self, newGeometry=None):
427 return self.tk.call('wm', 'geometry', self._w, newGeometry)
428 def grid(self,
429 baseWidht=None, baseHeight=None,
430 widthInc=None, heightInc=None):
431 return self._getints(self.tk.call(
432 'wm', 'grid', self._w,
433 baseWidht, baseHeight, widthInc, heightInc))
434 def group(self, pathName=None):
435 return self.tk.call('wm', 'group', self._w, pathName)
436 def iconbitmap(self, bitmap=None):
437 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
438 def iconify(self):
439 return self.tk.call('wm', 'iconify', self._w)
440 def iconmask(self, bitmap=None):
441 return self.tk.call('wm', 'iconmask', self._w, bitmap)
442 def iconname(self, newName=None):
443 return self.tk.call('wm', 'iconname', self._w, newName)
444 def iconposition(self, x=None, y=None):
445 return self._getints(self.tk.call(
446 'wm', 'iconposition', self._w, x, y))
447 def iconwindow(self, pathName=None):
448 return self.tk.call('wm', 'iconwindow', self._w, pathName)
449 def maxsize(self, width=None, height=None):
450 return self._getints(self.tk.call(
451 'wm', 'maxsize', self._w, width, height))
452 def minsize(self, width=None, height=None):
453 return self._getints(self.tk.call(
454 'wm', 'minsize', self._w, width, height))
455 def overrideredirect(self, boolean=None):
456 return self._getboolean(self.tk.call(
457 'wm', 'overrideredirect', self._w, boolean))
458 def positionfrom(self, who=None):
459 return self.tk.call('wm', 'positionfrom', self._w, who)
460 def protocol(self, name=None, func=None):
461 if _isfunctype(func):
462 command = self._register(func)
463 else:
464 command = func
465 return self.tk.call(
466 'wm', 'protocol', self._w, name, command)
467 def sizefrom(self, who=None):
468 return self.tk.call('wm', 'sizefrom', self._w, who)
469 def state(self):
470 return self.tk.call('wm', 'state', self._w)
471 def title(self, string=None):
472 return self.tk.call('wm', 'title', self._w, string)
473 def transient(self, master=None):
474 return self.tk.call('wm', 'transient', self._w, master)
475 def withdraw(self):
476 return self.tk.call('wm', 'withdraw', self._w)
477
478class Tk(Misc, Wm):
479 _w = '.'
480 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000481 self.master = None
482 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000483 if baseName is None:
484 import sys, os
485 baseName = os.path.basename(sys.argv[0])
486 if baseName[-3:] == '.py': baseName = baseName[:-3]
487 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000488 self.tk.createcommand('tkerror', _tkerror)
489 def destroy(self):
490 for c in self.children.values(): c.destroy()
491 del self.master.children[self._name]
492 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000493 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000494 return self._w
Guido van Rossum18468821994-06-20 07:49:28 +0000495
496class Pack:
497 def config(self, cnf={}):
498 apply(self.tk.call,
499 ('pack', 'configure', self._w)
500 + self._options(cnf))
501 pack = config
502 def __setitem__(self, key, value):
503 Pack.config({key: value})
504 def forget(self):
505 self.tk.call('pack', 'forget', self._w)
506 def newinfo(self):
507 return self.tk.call('pack', 'newinfo', self._w)
508 info = newinfo
509 def propagate(self, boolean=None):
510 if boolean:
511 self.tk.call('pack', 'propagate', self._w)
512 else:
513 return self._getboolean(self.tk.call(
514 'pack', 'propagate', self._w))
515 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000516 return map(self._nametowidget,
517 self.tk.splitlist(
518 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000519
520class Place:
521 def config(self, cnf={}):
522 apply(self.tk.call,
523 ('place', 'configure', self._w)
524 + self._options(cnf))
525 place = config
526 def __setitem__(self, key, value):
527 Place.config({key: value})
528 def forget(self):
529 self.tk.call('place', 'forget', self._w)
530 def info(self):
531 return self.tk.call('place', 'info', self._w)
532 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000533 return map(self._nametowidget,
534 self.tk.splitlist(
535 self.tk.call(
536 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000537
Guido van Rossum18468821994-06-20 07:49:28 +0000538class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000539 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000540 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000541 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000542 if not _default_root:
543 _default_root = Tk()
544 master = _default_root
545 if not _default_root:
546 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000547 self.master = master
548 self.tk = master.tk
549 if cnf.has_key('name'):
550 name = cnf['name']
551 del cnf['name']
552 else:
553 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000554 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000555 if master._w=='.':
556 self._w = '.' + name
557 else:
558 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000559 self.children = {}
560 if self.master.children.has_key(self._name):
561 self.master.children[self._name].destroy()
562 self.master.children[self._name] = self
563 def __init__(self, master, widgetName, cnf={}, extra=()):
564 Widget._setup(self, master, cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000565 self.widgetName = widgetName
566 apply(self.tk.call, (widgetName, self._w) + extra)
567 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000568 def config(self, cnf=None):
569 if cnf is None:
570 cnf = {}
571 for x in self.tk.split(
572 self.tk.call(self._w, 'configure')):
573 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
574 return cnf
575 if type(cnf) == StringType:
576 x = self.tk.split(self.tk.call(
577 self._w, 'configure', '-'+cnf))
578 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000579 for k in cnf.keys():
580 if type(k) == ClassType:
581 k.config(self, cnf[k])
582 del cnf[k]
583 apply(self.tk.call, (self._w, 'configure')
584 + self._options(cnf))
585 def __getitem__(self, key):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000586 v = self.tk.split(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000587 self._w, 'configure', '-' + key))
588 return v[4]
589 def __setitem__(self, key, value):
590 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000591 def keys(self):
592 return map(lambda x: x[0][1:],
593 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000594 def __str__(self):
595 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000596 def destroy(self):
597 for c in self.children.values(): c.destroy()
598 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000599 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000600 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000601 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000602
603class Toplevel(Widget, Wm):
604 def __init__(self, master=None, cnf={}):
605 extra = ()
606 if cnf.has_key('screen'):
607 extra = ('-screen', cnf['screen'])
608 del cnf['screen']
609 if cnf.has_key('class'):
610 extra = extra + ('-class', cnf['class'])
611 del cnf['class']
612 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000613 root = self._root()
614 self.iconname(root.iconname())
615 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000616
617class Button(Widget):
618 def __init__(self, master=None, cnf={}):
619 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000620 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000621 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000622 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000623 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000624 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000625 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000626 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000627 self.tk.call('tk_butUp', self._w)
628 def flash(self):
629 self.tk.call(self._w, 'flash')
630 def invoke(self):
631 self.tk.call(self._w, 'invoke')
632
633# Indices:
634def AtEnd():
635 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000636def AtInsert(*args):
637 s = 'insert'
638 for a in args:
639 if a: s = s + (' ' + a)
640 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000641def AtSelFirst():
642 return 'sel.first'
643def AtSelLast():
644 return 'sel.last'
645def At(x, y=None):
646 if y:
647 return '@' + `x` + ',' + `y`
648 else:
649 return '@' + `x`
650
651class Canvas(Widget):
652 def __init__(self, master=None, cnf={}):
653 Widget.__init__(self, master, 'canvas', cnf)
654 def addtag(self, *args):
655 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000656 def addtag_above(self, tagOrId):
657 self.addtag('above', tagOrId)
658 def addtag_all(self):
659 self.addtag('all')
660 def addtag_below(self, tagOrId):
661 self.addtag('below', tagOrId)
662 def addtag_closest(self, x, y, halo=None, start=None):
663 self.addtag('closest', x, y, halo, start)
664 def addtag_enclosed(self, x1, y1, x2, y2):
665 self.addtag('enclosed', x1, y1, x2, y2)
666 def addtag_overlapping(self, x1, y1, x2, y2):
667 self.addtag('overlapping', x1, y1, x2, y2)
668 def addtag_withtag(self, tagOrId):
669 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000670 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000671 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000672 def bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000673 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000674 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000675 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000676 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000677 def canvasx(self, screenx, gridspacing=None):
678 return self.tk.getint(self.tk.call(
679 self._w, 'canvasx', screenx, gridspacing))
680 def canvasy(self, screeny, gridspacing=None):
681 return self.tk.getint(self.tk.call(
682 self._w, 'canvasy', screeny, gridspacing))
683 def coords(self, *args):
684 return self._do('coords', args)
685 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000686 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000687 cnf = args[-1]
688 if type(cnf) == type({}):
689 args = args[:-1]
690 else:
691 cnf = {}
Guido van Rossum08a40381994-06-21 11:44:21 +0000692 v = (self._w, 'create', itemType) + args
Guido van Rossum18468821994-06-20 07:49:28 +0000693 for k in cnf.keys():
694 v = v + ('-' + k, cnf[k])
695 return self.tk.getint(apply(self.tk.call, v))
696 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000697 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000698 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000699 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000700 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000701 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000702 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000703 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000704 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000705 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000706 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000707 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000708 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000709 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000710 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000711 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000712 def dchars(self, *args):
713 self._do('dchars', args)
714 def delete(self, *args):
715 self._do('delete', args)
716 def dtag(self, *args):
717 self._do('dtag', args)
718 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000719 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000720 def find_above(self, tagOrId):
721 return self.find('above', tagOrId)
722 def find_all(self):
723 return self.find('all')
724 def find_below(self, tagOrId):
725 return self.find('below', tagOrId)
726 def find_closest(self, x, y, halo=None, start=None):
727 return self.find('closest', x, y, halo, start)
728 def find_enclosed(self, x1, y1, x2, y2):
729 return self.find('enclosed', x1, y1, x2, y2)
730 def find_overlapping(self, x1, y1, x2, y2):
731 return self.find('overlapping', x1, y1, x2, y2)
732 def find_withtag(self, tagOrId):
733 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000734 def focus(self, *args):
735 return self._do('focus', args)
736 def gettags(self, *args):
737 return self.tk.splitlist(self._do('gettags', args))
738 def icursor(self, *args):
739 self._do('icursor', args)
740 def index(self, *args):
741 return self.tk.getint(self._do('index', args))
742 def insert(self, *args):
743 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000744 def itemconfig(self, tagOrId, cnf=None):
745 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000746 cnf = {}
747 for x in self.tk.split(
748 self._do('itemconfigure', (tagOrId))):
749 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
750 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000751 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000752 x = self.tk.split(self._do('itemconfigure',
753 (tagOrId, '-'+cnf,)))
754 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000755 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
756 def lower(self, *args):
757 self._do('lower', args)
758 def move(self, *args):
759 self._do('move', args)
760 def postscript(self, cnf={}):
761 return self._do('postscript', self._options(cnf))
762 def tkraise(self, *args):
763 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000764 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000765 def scale(self, *args):
766 self._do('scale', args)
767 def scan_mark(self, x, y):
768 self.tk.call(self._w, 'scan', 'mark', x, y)
769 def scan_dragto(self, x, y):
770 self.tk.call(self._w, 'scan', 'dragto', x, y)
771 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000772 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000773 def select_clear(self):
774 self.tk.call(self._w, 'select', 'clear')
775 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000776 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000777 def select_item(self):
778 self.tk.call(self._w, 'select', 'item')
779 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000780 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000781 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000782 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000783 def xview(self, index):
784 self.tk.call(self._w, 'xview', index)
785 def yview(self, index):
786 self.tk.call(self._w, 'yview', index)
787
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000788def _flatten(tuple):
789 res = ()
790 for item in tuple:
791 if type(item) in (TupleType, ListType):
792 res = res + _flatten(item)
793 else:
794 res = res + (item,)
795 return res
796
Guido van Rossum18468821994-06-20 07:49:28 +0000797class Checkbutton(Widget):
798 def __init__(self, master=None, cnf={}):
799 Widget.__init__(self, master, 'checkbutton', cnf)
800 def deselect(self):
801 self.tk.call(self._w, 'deselect')
802 def flash(self):
803 self.tk.call(self._w, 'flash')
804 def invoke(self):
805 self.tk.call(self._w, 'invoke')
806 def select(self):
807 self.tk.call(self._w, 'select')
808 def toggle(self):
809 self.tk.call(self._w, 'toggle')
810
811class Entry(Widget):
812 def __init__(self, master=None, cnf={}):
813 Widget.__init__(self, master, 'entry', cnf)
814 def tk_entryBackspace(self):
815 self.tk.call('tk_entryBackspace', self._w)
816 def tk_entryBackword(self):
817 self.tk.call('tk_entryBackword', self._w)
818 def tk_entrySeeCaret(self):
819 self.tk.call('tk_entrySeeCaret', self._w)
820 def delete(self, first, last=None):
821 self.tk.call(self._w, 'delete', first, last)
822 def get(self):
823 return self.tk.call(self._w, 'get')
824 def icursor(self, index):
825 self.tk.call(self._w, 'icursor', index)
826 def index(self, index):
827 return self.tk.getint(self.tk.call(
828 self._w, 'index', index))
829 def insert(self, index, string):
830 self.tk.call(self._w, 'insert', index, string)
831 def scan_mark(self, x):
832 self.tk.call(self._w, 'scan', 'mark', x)
833 def scan_dragto(self, x):
834 self.tk.call(self._w, 'scan', 'dragto', x)
835 def select_adjust(self, index):
836 self.tk.call(self._w, 'select', 'adjust', index)
837 def select_clear(self):
838 self.tk.call(self._w, 'select', 'clear')
839 def select_from(self, index):
840 self.tk.call(self._w, 'select', 'from', index)
841 def select_to(self, index):
842 self.tk.call(self._w, 'select', 'to', index)
843 def select_view(self, index):
844 self.tk.call(self._w, 'select', 'view', index)
845
846class Frame(Widget):
847 def __init__(self, master=None, cnf={}):
848 extra = ()
849 if cnf.has_key('class'):
850 extra = ('-class', cnf['class'])
851 del cnf['class']
852 Widget.__init__(self, master, 'frame', cnf, extra)
853 def tk_menuBar(self, *args):
854 apply(self.tk.call, ('tk_menuBar', self._w) + args)
855
856class Label(Widget):
857 def __init__(self, master=None, cnf={}):
858 Widget.__init__(self, master, 'label', cnf)
859
860class Listbox(Widget):
861 def __init__(self, master=None, cnf={}):
862 Widget.__init__(self, master, 'listbox', cnf)
863 def tk_listboxSingleSelect(self):
864 self.tk.call('tk_listboxSingleSelect', self._w)
865 def curselection(self):
866 return self.tk.splitlist(self.tk.call(
867 self._w, 'curselection'))
868 def delete(self, first, last=None):
869 self.tk.call(self._w, 'delete', first, last)
870 def get(self, index):
871 return self.tk.call(self._w, 'get', index)
872 def insert(self, index, *elements):
873 apply(self.tk.call,
874 (self._w, 'insert', index) + elements)
875 def nearest(self, y):
876 return self.tk.getint(self.tk.call(
877 self._w, 'nearest', y))
878 def scan_mark(self, x, y):
879 self.tk.call(self._w, 'scan', 'mark', x, y)
880 def scan_dragto(self, x, y):
881 self.tk.call(self._w, 'scan', 'dragto', x, y)
882 def select_adjust(self, index):
883 self.tk.call(self._w, 'select', 'adjust', index)
884 def select_clear(self):
885 self.tk.call(self._w, 'select', 'clear')
886 def select_from(self, index):
887 self.tk.call(self._w, 'select', 'from', index)
888 def select_to(self, index):
889 self.tk.call(self._w, 'select', 'to', index)
890 def size(self):
891 return self.tk.getint(self.tk.call(self._w, 'size'))
892 def xview(self, index):
893 self.tk.call(self._w, 'xview', index)
894 def yview(self, index):
895 self.tk.call(self._w, 'yview', index)
896
897class Menu(Widget):
898 def __init__(self, master=None, cnf={}):
899 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000900 def tk_bindForTraversal(self):
901 self.tk.call('tk_bindForTraversal', self._w)
902 def tk_mbPost(self):
903 self.tk.call('tk_mbPost', self._w)
904 def tk_mbUnpost(self):
905 self.tk.call('tk_mbUnpost')
906 def tk_traverseToMenu(self, char):
907 self.tk.call('tk_traverseToMenu', self._w, char)
908 def tk_traverseWithinMenu(self, char):
909 self.tk.call('tk_traverseWithinMenu', self._w, char)
910 def tk_getMenuButtons(self):
911 return self.tk.call('tk_getMenuButtons', self._w)
912 def tk_nextMenu(self, count):
913 self.tk.call('tk_nextMenu', count)
914 def tk_nextMenuEntry(self, count):
915 self.tk.call('tk_nextMenuEntry', count)
916 def tk_invokeMenu(self):
917 self.tk.call('tk_invokeMenu', self._w)
918 def tk_firstMenu(self):
919 self.tk.call('tk_firstMenu', self._w)
920 def tk_mbButtonDown(self):
921 self.tk.call('tk_mbButtonDown', self._w)
922 def activate(self, index):
923 self.tk.call(self._w, 'activate', index)
924 def add(self, itemType, cnf={}):
925 apply(self.tk.call, (self._w, 'add', itemType)
926 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000927 def add_cascade(self, cnf={}):
928 self.add('cascade', cnf)
929 def add_checkbutton(self, cnf={}):
930 self.add('checkbutton', cnf)
931 def add_command(self, cnf={}):
932 self.add('command', cnf)
933 def add_radiobutton(self, cnf={}):
934 self.add('radiobutton', cnf)
935 def add_separator(self, cnf={}):
936 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000937 def delete(self, index1, index2=None):
938 self.tk.call(self._w, 'delete', index1, index2)
939 def entryconfig(self, index, cnf={}):
940 apply(self.tk.call, (self._w, 'entryconfigure', index)
941 + self._options(cnf))
942 def index(self, index):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000943 return self.tk.call(self._w, 'index', index)
Guido van Rossum18468821994-06-20 07:49:28 +0000944 def invoke(self, index):
945 return self.tk.call(self._w, 'invoke', index)
946 def post(self, x, y):
947 self.tk.call(self._w, 'post', x, y)
948 def unpost(self):
949 self.tk.call(self._w, 'unpost')
950 def yposition(self, index):
951 return self.tk.getint(self.tk.call(
952 self._w, 'yposition', index))
953
954class Menubutton(Widget):
955 def __init__(self, master=None, cnf={}):
956 Widget.__init__(self, master, 'menubutton', cnf)
957
958class Message(Widget):
959 def __init__(self, master=None, cnf={}):
960 Widget.__init__(self, master, 'message', cnf)
961
962class Radiobutton(Widget):
963 def __init__(self, master=None, cnf={}):
964 Widget.__init__(self, master, 'radiobutton', cnf)
965 def deselect(self):
966 self.tk.call(self._w, 'deselect')
967 def flash(self):
968 self.tk.call(self._w, 'flash')
969 def invoke(self):
970 self.tk.call(self._w, 'invoke')
971 def select(self):
972 self.tk.call(self._w, 'select')
973
974class Scale(Widget):
975 def __init__(self, master=None, cnf={}):
976 Widget.__init__(self, master, 'scale', cnf)
977 def get(self):
978 return self.tk.getint(self.tk.call(self._w, 'get'))
979 def set(self, value):
980 self.tk.call(self._w, 'set', value)
981
982class Scrollbar(Widget):
983 def __init__(self, master=None, cnf={}):
984 Widget.__init__(self, master, 'scrollbar', cnf)
985 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000986 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
988 self.tk.call(self._w, 'set',
989 totalUnits, windowUnits, firstUnit, lastUnit)
990
991class Text(Widget):
992 def __init__(self, master=None, cnf={}):
993 Widget.__init__(self, master, 'text', cnf)
994 def tk_textSelectTo(self, index):
995 self.tk.call('tk_textSelectTo', self._w, index)
996 def tk_textBackspace(self):
997 self.tk.call('tk_textBackspace', self._w)
998 def tk_textIndexCloser(self, a, b, c):
999 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1000 def tk_textResetAnchor(self, index):
1001 self.tk.call('tk_textResetAnchor', self._w, index)
1002 def compare(self, index1, op, index2):
1003 return self.tk.getboolean(self.tk.call(
1004 self._w, 'compare', index1, op, index2))
1005 def debug(self, boolean=None):
1006 return self.tk.getboolean(self.tk.call(
1007 self._w, 'debug', boolean))
1008 def delete(self, index1, index2=None):
1009 self.tk.call(self._w, 'delete', index1, index2)
1010 def get(self, index1, index2=None):
1011 return self.tk.call(self._w, 'get', index1, index2)
1012 def index(self, index):
1013 return self.tk.call(self._w, 'index', index)
1014 def insert(self, index, chars):
1015 self.tk.call(self._w, 'insert', index, chars)
1016 def mark_names(self):
1017 return self.tk.splitlist(self.tk.call(
1018 self._w, 'mark', 'names'))
1019 def mark_set(self, markName, index):
1020 self.tk.call(self._w, 'mark', 'set', markName, index)
1021 def mark_unset(self, markNames):
1022 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1023 def scan_mark(self, y):
1024 self.tk.call(self._w, 'scan', 'mark', y)
1025 def scan_dragto(self, y):
1026 self.tk.call(self._w, 'scan', 'dragto', y)
1027 def tag_add(self, tagName, index1, index2=None):
1028 self.tk.call(
1029 self._w, 'tag', 'add', tagName, index1, index2)
1030 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001031 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001032 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001033 self.tk.call(self._w, 'tag', 'bind',
1034 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001035 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001036 def tag_config(self, tagName, cnf={}):
1037 apply(self.tk.call,
1038 (self._w, 'tag', 'configure', tagName)
1039 + self._options(cnf))
1040 def tag_delete(self, tagNames):
1041 apply(self.tk.call, (self._w, 'tag', 'delete')
1042 + tagNames)
1043 def tag_lower(self, tagName, belowThis=None):
1044 self.tk.call(self._w, 'tag', 'lower',
1045 tagName, belowThis)
1046 def tag_names(self, index=None):
1047 return self.tk.splitlist(
1048 self.tk.call(self._w, 'tag', 'names', index))
1049 def tag_nextrange(self, tagName, index1, index2=None):
1050 return self.tk.splitlist(self.tk.call(
1051 self._w, 'tag', 'nextrange', index1, index2))
1052 def tag_raise(self, tagName, aboveThis=None):
1053 self.tk.call(
1054 self._w, 'tag', 'raise', tagName, aboveThis)
1055 def tag_ranges(self, tagName):
1056 return self.tk.splitlist(self.tk.call(
1057 self._w, 'tag', 'ranges', tagName))
1058 def tag_remove(self, tagName, index1, index2=None):
1059 self.tk.call(
1060 self._w, 'tag', 'remove', index1, index2)
1061 def yview(self, what):
1062 self.tk.call(self._w, 'yview', what)
1063 def yview_pickplace(self, what):
1064 self.tk.call(self._w, 'yview', '-pickplace', what)
1065
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001066######################################################################
1067# Extensions:
1068
1069class Studbutton(Button):
1070 def __init__(self, master=None, cnf={}):
1071 Widget.__init__(self, master, 'studbutton', cnf)
1072 self.bind('<Any-Enter>', self.tk_butEnter)
1073 self.bind('<Any-Leave>', self.tk_butLeave)
1074 self.bind('<1>', self.tk_butDown)
1075 self.bind('<ButtonRelease-1>', self.tk_butUp)
1076
1077class Tributton(Button):
1078 def __init__(self, master=None, cnf={}):
1079 Widget.__init__(self, master, 'tributton', cnf)
1080 self.bind('<Any-Enter>', self.tk_butEnter)
1081 self.bind('<Any-Leave>', self.tk_butLeave)
1082 self.bind('<1>', self.tk_butDown)
1083 self.bind('<ButtonRelease-1>', self.tk_butUp)
1084