blob: c05704ec2c1026feb0cdac298d647600aa05c981 [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum18468821994-06-20 07:49:28 +00003import tkinter
4from tkinter import TclError
5
6class _Dummy:
7 def meth(self): return
8
Guido van Rossum2dcf5291994-07-06 09:23:20 +00009def _func():
10 pass
Guido van Rossum18468821994-06-20 07:49:28 +000011
Guido van Rossum2dcf5291994-07-06 09:23:20 +000012FunctionType = type(_func)
Guido van Rossum18468821994-06-20 07:49:28 +000013ClassType = type(_Dummy)
14MethodType = type(_Dummy.meth)
Guido van Rossum08a40381994-06-21 11:44:21 +000015StringType = type('')
Guido van Rossum67ef5f31994-06-20 13:39:14 +000016TupleType = type(())
17ListType = type([])
Guido van Rossum2dcf5291994-07-06 09:23:20 +000018DictionaryType = type({})
19NoneType = type(None)
Guido van Rossum08a40381994-06-21 11:44:21 +000020CallableTypes = (FunctionType, MethodType)
Guido van Rossum18468821994-06-20 07:49:28 +000021
Guido van Rossum2dcf5291994-07-06 09:23:20 +000022def _flatten(tuple):
23 res = ()
24 for item in tuple:
25 if type(item) in (TupleType, ListType):
26 res = res + _flatten(item)
27 else:
28 res = res + (item,)
29 return res
30
31def _cnfmerge(cnfs):
32 if type(cnfs) in (NoneType, DictionaryType, StringType):
33 return cnfs
34 else:
35 cnf = {}
36 for c in _flatten(cnfs):
37 for k, v in c.items():
38 cnf[k] = v
39 return cnf
40
41class Event:
42 pass
43
Guido van Rossumaec5dc91994-06-27 07:55:12 +000044_default_root = None
45
Guido van Rossum45853db1994-06-20 12:19:19 +000046def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000047 pass
48
Guido van Rossum97aeca11994-07-07 13:12:12 +000049def _exit(code='0'):
50 import sys
51 sys.exit(getint(code))
52
Guido van Rossumaec5dc91994-06-27 07:55:12 +000053_varnum = 0
54class Variable:
55 def __init__(self, master=None):
56 global _default_root
57 global _varnum
58 if master:
59 self._tk = master.tk
60 else:
61 self._tk = _default_root.tk
62 self._name = 'PY_VAR' + `_varnum`
63 _varnum = _varnum + 1
64 def __del__(self):
65 self._tk.unsetvar(self._name)
66 def __str__(self):
67 return self._name
68 def __call__(self, value=None):
69 if value == None:
70 return self.get()
71 else:
72 self.set(value)
73 def set(self, value):
74 return self._tk.setvar(self._name, value)
75
76class StringVar(Variable):
77 def __init__(self, master=None):
78 Variable.__init__(self, master)
79 def get(self):
80 return self._tk.getvar(self._name)
81
82class IntVar(Variable):
83 def __init__(self, master=None):
84 Variable.__init__(self, master)
85 def get(self):
86 return self._tk.getint(self._tk.getvar(self._name))
87
88class DoubleVar(Variable):
89 def __init__(self, master=None):
90 Variable.__init__(self, master)
91 def get(self):
92 return self._tk.getdouble(self._tk.getvar(self._name))
93
94class BooleanVar(Variable):
95 def __init__(self, master=None):
96 Variable.__init__(self, master)
97 def get(self):
98 return self._tk.getboolean(self._tk.getvar(self._name))
99
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000100def mainloop():
101 _default_root.tk.mainloop()
102
103def getint(s):
104 return _default_root.tk.getint(s)
105
106def getdouble(s):
107 return _default_root.tk.getdouble(s)
108
109def getboolean(s):
110 return _default_root.tk.getboolean(s)
111
Guido van Rossum18468821994-06-20 07:49:28 +0000112class Misc:
113 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000114 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000115 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000116 def tk_menuBar(self, *args):
117 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000118 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000119 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000120 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000121 def wait_window(self, window=None):
122 if window == None:
123 window = self
124 self.tk.call('tkwait', 'window', window._w)
125 def wait_visibility(self, window=None):
126 if window == None:
127 window = self
128 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000129 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000130 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000131 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000132 return self.tk.getvar(name)
133 def getint(self, s):
134 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000135 def getdouble(self, s):
136 return self.tk.getdouble(s)
137 def getboolean(self, s):
138 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000139 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000140 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000141 focus = focus_set # XXX b/w compat?
142 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000143 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000144 def focus_default_none(self):
145 self.tk.call('focus', 'default', 'none')
146 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000147 def focus_none(self):
148 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000149 def focus_get(self):
150 name = self.tk.call('focus')
151 if name == 'none': return None
152 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000153 def after(self, ms, func=None, *args):
154 if not func:
155 self.tk.call('after', ms)
156 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000157 # XXX Disgusting hack to clean up after calling func
158 tmp = []
159 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
160 try:
161 apply(func, args)
162 finally:
163 tk.deletecommand(tmp[0])
164 name = self._register(callit)
165 tmp.append(name)
166 self.tk.call('after', ms, name)
Guido van Rossum45853db1994-06-20 12:19:19 +0000167 # XXX grab current w/o window argument
168 def grab_current(self):
169 name = self.tk.call('grab', 'current', self._w)
170 if not name: return None
171 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000172 def grab_release(self):
173 self.tk.call('grab', 'release', self._w)
174 def grab_set(self):
175 self.tk.call('grab', 'set', self._w)
176 def grab_set_global(self):
177 self.tk.call('grab', 'set', '-global', self._w)
178 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000179 status = self.tk.call('grab', 'status', self._w)
180 if status == 'none': status = None
181 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000182 def lower(self, belowThis=None):
183 self.tk.call('lower', self._w, belowThis)
184 def selection_clear(self):
185 self.tk.call('selection', 'clear', self._w)
186 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000187 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000188 def selection_handle(self, func, type=None, format=None):
189 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000190 self.tk.call('selection', 'handle', self._w,
191 name, type, format)
192 def selection_own(self, func=None):
193 name = self._register(func)
194 self.tk.call('selection', 'own', self._w, name)
195 def selection_own_get(self):
196 return self._nametowidget(self.tk.call('selection', 'own'))
197 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000198 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000199 def lower(self, belowThis=None):
200 self.tk.call('lift', self._w, belowThis)
201 def tkraise(self, aboveThis=None):
202 self.tk.call('raise', self._w, aboveThis)
203 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000204 def colormodel(self, value=None):
205 return self.tk.call('tk', 'colormodel', self._w, value)
206 def winfo_atom(self, name):
207 return self.tk.getint(self.tk.call('winfo', 'atom', name))
208 def winfo_atomname(self, id):
209 return self.tk.call('winfo', 'atomname', id)
210 def winfo_cells(self):
211 return self.tk.getint(
212 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000213 def winfo_children(self):
214 return map(self._nametowidget,
215 self.tk.splitlist(self.tk.call(
216 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000217 def winfo_class(self):
218 return self.tk.call('winfo', 'class', self._w)
219 def winfo_containing(self, rootX, rootY):
220 return self.tk.call('winfo', 'containing', rootx, rootY)
221 def winfo_depth(self):
222 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
223 def winfo_exists(self):
224 return self.tk.getint(
225 self.tk.call('winfo', 'exists', self._w))
226 def winfo_fpixels(self, number):
227 return self.tk.getdouble(self.tk.call(
228 'winfo', 'fpixels', self._w, number))
229 def winfo_geometry(self):
230 return self.tk.call('winfo', 'geometry', self._w)
231 def winfo_height(self):
232 return self.tk.getint(
233 self.tk.call('winfo', 'height', self._w))
234 def winfo_id(self):
235 return self.tk.getint(
236 self.tk.call('winfo', 'id', self._w))
237 def winfo_interps(self):
238 return self.tk.splitlist(
239 self.tk.call('winfo', 'interps'))
240 def winfo_ismapped(self):
241 return self.tk.getint(
242 self.tk.call('winfo', 'ismapped', self._w))
243 def winfo_name(self):
244 return self.tk.call('winfo', 'name', self._w)
245 def winfo_parent(self):
246 return self.tk.call('winfo', 'parent', self._w)
247 def winfo_pathname(self, id):
248 return self.tk.call('winfo', 'pathname', id)
249 def winfo_pixels(self, number):
250 return self.tk.getint(
251 self.tk.call('winfo', 'pixels', self._w, number))
252 def winfo_reqheight(self):
253 return self.tk.getint(
254 self.tk.call('winfo', 'reqheight', self._w))
255 def winfo_reqwidth(self):
256 return self.tk.getint(
257 self.tk.call('winfo', 'reqwidth', self._w))
258 def winfo_rgb(self, color):
259 return self._getints(
260 self.tk.call('winfo', 'rgb', self._w, color))
261 def winfo_rootx(self):
262 return self.tk.getint(
263 self.tk.call('winfo', 'rootx', self._w))
264 def winfo_rooty(self):
265 return self.tk.getint(
266 self.tk.call('winfo', 'rooty', self._w))
267 def winfo_screen(self):
268 return self.tk.call('winfo', 'screen', self._w)
269 def winfo_screencells(self):
270 return self.tk.getint(
271 self.tk.call('winfo', 'screencells', self._w))
272 def winfo_screendepth(self):
273 return self.tk.getint(
274 self.tk.call('winfo', 'screendepth', self._w))
275 def winfo_screenheight(self):
276 return self.tk.getint(
277 self.tk.call('winfo', 'screenheight', self._w))
278 def winfo_screenmmheight(self):
279 return self.tk.getint(
280 self.tk.call('winfo', 'screenmmheight', self._w))
281 def winfo_screenmmwidth(self):
282 return self.tk.getint(
283 self.tk.call('winfo', 'screenmmwidth', self._w))
284 def winfo_screenvisual(self):
285 return self.tk.call('winfo', 'screenvisual', self._w)
286 def winfo_screenwidth(self):
287 return self.tk.getint(
288 self.tk.call('winfo', 'screenwidth', self._w))
289 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000290 return self._nametowidget(self.tk.call(
291 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000292 def winfo_visual(self):
293 return self.tk.call('winfo', 'visual', self._w)
294 def winfo_vrootheight(self):
295 return self.tk.getint(
296 self.tk.call('winfo', 'vrootheight', self._w))
297 def winfo_vrootwidth(self):
298 return self.tk.getint(
299 self.tk.call('winfo', 'vrootwidth', self._w))
300 def winfo_vrootx(self):
301 return self.tk.getint(
302 self.tk.call('winfo', 'vrootx', self._w))
303 def winfo_vrooty(self):
304 return self.tk.getint(
305 self.tk.call('winfo', 'vrooty', self._w))
306 def winfo_width(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'width', self._w))
309 def winfo_x(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'x', self._w))
312 def winfo_y(self):
313 return self.tk.getint(
314 self.tk.call('winfo', 'y', self._w))
315 def update(self):
316 self.tk.call('update')
317 def update_idletasks(self):
318 self.tk.call('update', 'idletasks')
319 def bind(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000320 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000321 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000322 self.tk.call('bind', self._w, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000323 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000325 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000326 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000327 self.tk.call('bind', 'all' , sequence,
Guido van Rossumbd84b041994-07-04 10:48:25 +0000328 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000329 def bind_class(self, className, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000330 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000331 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000332 self.tk.call('bind', className , sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000333 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000334 def mainloop(self):
335 self.tk.mainloop()
336 def quit(self):
337 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000338 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000339 if not string: return None
340 res = ()
341 for v in self.tk.splitlist(string):
342 res = res + (self.tk.getint(v),)
343 return res
Guido van Rossum18468821994-06-20 07:49:28 +0000344 def _getboolean(self, string):
345 if string:
346 return self.tk.getboolean(string)
Guido van Rossum18468821994-06-20 07:49:28 +0000347 def _options(self, cnf):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000348 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000349 res = ()
350 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000351 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000352 v = self._register(v)
353 res = res + ('-'+k, v)
354 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000355 def _nametowidget(self, name):
356 w = self
357 if name[0] == '.':
358 w = w._root()
359 name = name[1:]
360 from string import find
361 while name:
362 i = find(name, '.')
363 if i >= 0:
364 name, tail = name[:i], name[i+1:]
365 else:
366 tail = ''
367 w = w.children[name]
368 name = tail
369 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000370 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000371 f = _CallSafely(func, subst).__call__
372 name = `id(f)`
373 if hasattr(func, 'im_func'):
374 func = func.im_func
375 if hasattr(func, 'func_name') and \
376 type(func.func_name) == type(''):
377 name = name + func.func_name
378 self.tk.createcommand(name, f)
379 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000380 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000381 def _root(self):
382 w = self
383 while w.master: w = w.master
384 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000385 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000386 '%s', '%t', '%w', '%x', '%y',
387 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
388 def _substitute(self, *args):
389 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000390 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000391 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
392 # Missing: (a, c, d, m, o, v, B, R)
393 e = Event()
394 e.serial = tk.getint(nsign)
395 e.num = tk.getint(b)
396 try: e.focus = tk.getboolean(f)
397 except TclError: pass
398 e.height = tk.getint(h)
399 e.keycode = tk.getint(k)
400 e.state = tk.getint(s)
401 e.time = tk.getint(t)
402 e.width = tk.getint(w)
403 e.x = tk.getint(x)
404 e.y = tk.getint(y)
405 e.char = A
406 try: e.send_event = tk.getboolean(E)
407 except TclError: pass
408 e.keysym = K
409 e.keysym_num = tk.getint(N)
410 e.type = T
411 e.widget = self._nametowidget(W)
412 e.x_root = tk.getint(X)
413 e.y_root = tk.getint(Y)
414 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000415
416class _CallSafely:
417 def __init__(self, func, subst=None):
418 self.func = func
419 self.subst = subst
420 def __call__(self, *args):
421 if self.subst:
422 args = self.apply_func(self.subst, args)
423 args = self.apply_func(self.func, args)
424 def apply_func(self, func, args):
425 import sys
426 try:
427 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000428 except SystemExit, msg:
429 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000430 except:
431 try:
432 try:
433 t = sys.exc_traceback
434 while t:
435 sys.stderr.write(
436 ' %s, line %s\n' %
437 (t.tb_frame.f_code,
438 t.tb_lineno))
439 t = t.tb_next
440 finally:
441 sys.stderr.write('%s: %s\n' %
442 (sys.exc_type,
443 sys.exc_value))
Guido van Rossum45853db1994-06-20 12:19:19 +0000444 (sys.last_type,
445 sys.last_value,
446 sys.last_traceback) = (sys.exc_type,
447 sys.exc_value,
448 sys.exc_traceback)
449 import pdb
450 pdb.pm()
Guido van Rossum18468821994-06-20 07:49:28 +0000451 except:
452 print '*** Error in error handling ***'
453 print sys.exc_type, ':', sys.exc_value
454
455class Wm:
456 def aspect(self,
457 minNumer=None, minDenom=None,
458 maxNumer=None, maxDenom=None):
459 return self._getints(
460 self.tk.call('wm', 'aspect', self._w,
461 minNumer, minDenom,
462 maxNumer, maxDenom))
463 def client(self, name=None):
464 return self.tk.call('wm', 'client', self._w, name)
465 def command(self, value=None):
466 return self.tk.call('wm', 'command', self._w, value)
467 def deiconify(self):
468 return self.tk.call('wm', 'deiconify', self._w)
469 def focusmodel(self, model=None):
470 return self.tk.call('wm', 'focusmodel', self._w, model)
471 def frame(self):
472 return self.tk.call('wm', 'frame', self._w)
473 def geometry(self, newGeometry=None):
474 return self.tk.call('wm', 'geometry', self._w, newGeometry)
475 def grid(self,
476 baseWidht=None, baseHeight=None,
477 widthInc=None, heightInc=None):
478 return self._getints(self.tk.call(
479 'wm', 'grid', self._w,
480 baseWidht, baseHeight, widthInc, heightInc))
481 def group(self, pathName=None):
482 return self.tk.call('wm', 'group', self._w, pathName)
483 def iconbitmap(self, bitmap=None):
484 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
485 def iconify(self):
486 return self.tk.call('wm', 'iconify', self._w)
487 def iconmask(self, bitmap=None):
488 return self.tk.call('wm', 'iconmask', self._w, bitmap)
489 def iconname(self, newName=None):
490 return self.tk.call('wm', 'iconname', self._w, newName)
491 def iconposition(self, x=None, y=None):
492 return self._getints(self.tk.call(
493 'wm', 'iconposition', self._w, x, y))
494 def iconwindow(self, pathName=None):
495 return self.tk.call('wm', 'iconwindow', self._w, pathName)
496 def maxsize(self, width=None, height=None):
497 return self._getints(self.tk.call(
498 'wm', 'maxsize', self._w, width, height))
499 def minsize(self, width=None, height=None):
500 return self._getints(self.tk.call(
501 'wm', 'minsize', self._w, width, height))
502 def overrideredirect(self, boolean=None):
503 return self._getboolean(self.tk.call(
504 'wm', 'overrideredirect', self._w, boolean))
505 def positionfrom(self, who=None):
506 return self.tk.call('wm', 'positionfrom', self._w, who)
507 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000508 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000509 command = self._register(func)
510 else:
511 command = func
512 return self.tk.call(
513 'wm', 'protocol', self._w, name, command)
514 def sizefrom(self, who=None):
515 return self.tk.call('wm', 'sizefrom', self._w, who)
516 def state(self):
517 return self.tk.call('wm', 'state', self._w)
518 def title(self, string=None):
519 return self.tk.call('wm', 'title', self._w, string)
520 def transient(self, master=None):
521 return self.tk.call('wm', 'transient', self._w, master)
522 def withdraw(self):
523 return self.tk.call('wm', 'withdraw', self._w)
524
525class Tk(Misc, Wm):
526 _w = '.'
527 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000528 self.master = None
529 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000530 if baseName is None:
531 import sys, os
532 baseName = os.path.basename(sys.argv[0])
533 if baseName[-3:] == '.py': baseName = baseName[:-3]
534 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000535 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000536 self.tk.createcommand('exit', _exit)
Guido van Rossum45853db1994-06-20 12:19:19 +0000537 def destroy(self):
538 for c in self.children.values(): c.destroy()
Guido van Rossumbd84b041994-07-04 10:48:25 +0000539## del self.master.children[self._name]
Guido van Rossum45853db1994-06-20 12:19:19 +0000540 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000541 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000542 return self._w
Guido van Rossum18468821994-06-20 07:49:28 +0000543
544class Pack:
545 def config(self, cnf={}):
546 apply(self.tk.call,
547 ('pack', 'configure', self._w)
548 + self._options(cnf))
549 pack = config
550 def __setitem__(self, key, value):
551 Pack.config({key: value})
552 def forget(self):
553 self.tk.call('pack', 'forget', self._w)
554 def newinfo(self):
555 return self.tk.call('pack', 'newinfo', self._w)
556 info = newinfo
557 def propagate(self, boolean=None):
558 if boolean:
559 self.tk.call('pack', 'propagate', self._w)
560 else:
561 return self._getboolean(self.tk.call(
562 'pack', 'propagate', self._w))
563 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000564 return map(self._nametowidget,
565 self.tk.splitlist(
566 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000567
568class Place:
569 def config(self, cnf={}):
570 apply(self.tk.call,
571 ('place', 'configure', self._w)
572 + self._options(cnf))
573 place = config
574 def __setitem__(self, key, value):
575 Place.config({key: value})
576 def forget(self):
577 self.tk.call('place', 'forget', self._w)
578 def info(self):
579 return self.tk.call('place', 'info', self._w)
580 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000581 return map(self._nametowidget,
582 self.tk.splitlist(
583 self.tk.call(
584 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000585
Guido van Rossum18468821994-06-20 07:49:28 +0000586class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000587 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000588 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000589 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000590 if not _default_root:
591 _default_root = Tk()
592 master = _default_root
593 if not _default_root:
594 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000595 self.master = master
596 self.tk = master.tk
597 if cnf.has_key('name'):
598 name = cnf['name']
599 del cnf['name']
600 else:
601 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000602 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000603 if master._w=='.':
604 self._w = '.' + name
605 else:
606 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000607 self.children = {}
608 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000609 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000610 self.master.children[self._name] = self
611 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000612 cnf = _cnfmerge(cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000613 Widget._setup(self, master, cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000614 self.widgetName = widgetName
615 apply(self.tk.call, (widgetName, self._w) + extra)
616 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000617 def config(self, cnf=None):
Guido van Rossum2a390311994-07-06 10:20:11 +0000618 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000619 if cnf is None:
620 cnf = {}
621 for x in self.tk.split(
622 self.tk.call(self._w, 'configure')):
623 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
624 return cnf
625 if type(cnf) == StringType:
626 x = self.tk.split(self.tk.call(
627 self._w, 'configure', '-'+cnf))
628 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000629 for k in cnf.keys():
630 if type(k) == ClassType:
631 k.config(self, cnf[k])
632 del cnf[k]
633 apply(self.tk.call, (self._w, 'configure')
634 + self._options(cnf))
635 def __getitem__(self, key):
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000636 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000637 self._w, 'configure', '-' + key))
638 return v[4]
639 def __setitem__(self, key, value):
640 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000641 def keys(self):
642 return map(lambda x: x[0][1:],
643 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000644 def __str__(self):
645 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000646 def destroy(self):
647 for c in self.children.values(): c.destroy()
Guido van Rossum18468821994-06-20 07:49:28 +0000648 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000649 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000650 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000651
652class Toplevel(Widget, Wm):
653 def __init__(self, master=None, cnf={}):
654 extra = ()
655 if cnf.has_key('screen'):
656 extra = ('-screen', cnf['screen'])
657 del cnf['screen']
658 if cnf.has_key('class'):
659 extra = extra + ('-class', cnf['class'])
660 del cnf['class']
661 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000662 root = self._root()
663 self.iconname(root.iconname())
664 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000665
666class Button(Widget):
667 def __init__(self, master=None, cnf={}):
668 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000669 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000670 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000671 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000672 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000673 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000674 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000675 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000676 self.tk.call('tk_butUp', self._w)
677 def flash(self):
678 self.tk.call(self._w, 'flash')
679 def invoke(self):
680 self.tk.call(self._w, 'invoke')
681
682# Indices:
683def AtEnd():
684 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000685def AtInsert(*args):
686 s = 'insert'
687 for a in args:
688 if a: s = s + (' ' + a)
689 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000690def AtSelFirst():
691 return 'sel.first'
692def AtSelLast():
693 return 'sel.last'
694def At(x, y=None):
695 if y:
696 return '@' + `x` + ',' + `y`
697 else:
698 return '@' + `x`
699
700class Canvas(Widget):
701 def __init__(self, master=None, cnf={}):
702 Widget.__init__(self, master, 'canvas', cnf)
703 def addtag(self, *args):
704 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000705 def addtag_above(self, tagOrId):
706 self.addtag('above', tagOrId)
707 def addtag_all(self):
708 self.addtag('all')
709 def addtag_below(self, tagOrId):
710 self.addtag('below', tagOrId)
711 def addtag_closest(self, x, y, halo=None, start=None):
712 self.addtag('closest', x, y, halo, start)
713 def addtag_enclosed(self, x1, y1, x2, y2):
714 self.addtag('enclosed', x1, y1, x2, y2)
715 def addtag_overlapping(self, x1, y1, x2, y2):
716 self.addtag('overlapping', x1, y1, x2, y2)
717 def addtag_withtag(self, tagOrId):
718 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000719 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000720 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000721 def bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000722 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000723 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000724 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000725 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000726 def canvasx(self, screenx, gridspacing=None):
727 return self.tk.getint(self.tk.call(
728 self._w, 'canvasx', screenx, gridspacing))
729 def canvasy(self, screeny, gridspacing=None):
730 return self.tk.getint(self.tk.call(
731 self._w, 'canvasy', screeny, gridspacing))
732 def coords(self, *args):
733 return self._do('coords', args)
734 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000735 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000736 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000737 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000738 args = args[:-1]
739 else:
740 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000741 return self.tk.getint(apply(
742 self.tk.call,
743 (self._w, 'create', itemType)
744 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000745 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000746 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000747 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000748 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000749 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000750 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000751 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000752 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000753 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000754 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000755 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000756 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000757 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000758 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000759 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000760 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000761 def dchars(self, *args):
762 self._do('dchars', args)
763 def delete(self, *args):
764 self._do('delete', args)
765 def dtag(self, *args):
766 self._do('dtag', args)
767 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000768 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000769 def find_above(self, tagOrId):
770 return self.find('above', tagOrId)
771 def find_all(self):
772 return self.find('all')
773 def find_below(self, tagOrId):
774 return self.find('below', tagOrId)
775 def find_closest(self, x, y, halo=None, start=None):
776 return self.find('closest', x, y, halo, start)
777 def find_enclosed(self, x1, y1, x2, y2):
778 return self.find('enclosed', x1, y1, x2, y2)
779 def find_overlapping(self, x1, y1, x2, y2):
780 return self.find('overlapping', x1, y1, x2, y2)
781 def find_withtag(self, tagOrId):
782 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000783 def focus(self, *args):
784 return self._do('focus', args)
785 def gettags(self, *args):
786 return self.tk.splitlist(self._do('gettags', args))
787 def icursor(self, *args):
788 self._do('icursor', args)
789 def index(self, *args):
790 return self.tk.getint(self._do('index', args))
791 def insert(self, *args):
792 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000793 def itemconfig(self, tagOrId, cnf=None):
794 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000795 cnf = {}
796 for x in self.tk.split(
797 self._do('itemconfigure', (tagOrId))):
798 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
799 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000800 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000801 x = self.tk.split(self._do('itemconfigure',
802 (tagOrId, '-'+cnf,)))
803 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000804 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
805 def lower(self, *args):
806 self._do('lower', args)
807 def move(self, *args):
808 self._do('move', args)
809 def postscript(self, cnf={}):
810 return self._do('postscript', self._options(cnf))
811 def tkraise(self, *args):
812 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000813 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000814 def scale(self, *args):
815 self._do('scale', args)
816 def scan_mark(self, x, y):
817 self.tk.call(self._w, 'scan', 'mark', x, y)
818 def scan_dragto(self, x, y):
819 self.tk.call(self._w, 'scan', 'dragto', x, y)
820 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000821 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000822 def select_clear(self):
823 self.tk.call(self._w, 'select', 'clear')
824 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000825 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000826 def select_item(self):
827 self.tk.call(self._w, 'select', 'item')
828 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000829 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000830 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000831 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000832 def xview(self, index):
833 self.tk.call(self._w, 'xview', index)
834 def yview(self, index):
835 self.tk.call(self._w, 'yview', index)
836
837class Checkbutton(Widget):
838 def __init__(self, master=None, cnf={}):
839 Widget.__init__(self, master, 'checkbutton', cnf)
840 def deselect(self):
841 self.tk.call(self._w, 'deselect')
842 def flash(self):
843 self.tk.call(self._w, 'flash')
844 def invoke(self):
845 self.tk.call(self._w, 'invoke')
846 def select(self):
847 self.tk.call(self._w, 'select')
848 def toggle(self):
849 self.tk.call(self._w, 'toggle')
850
851class Entry(Widget):
852 def __init__(self, master=None, cnf={}):
853 Widget.__init__(self, master, 'entry', cnf)
854 def tk_entryBackspace(self):
855 self.tk.call('tk_entryBackspace', self._w)
856 def tk_entryBackword(self):
857 self.tk.call('tk_entryBackword', self._w)
858 def tk_entrySeeCaret(self):
859 self.tk.call('tk_entrySeeCaret', self._w)
860 def delete(self, first, last=None):
861 self.tk.call(self._w, 'delete', first, last)
862 def get(self):
863 return self.tk.call(self._w, 'get')
864 def icursor(self, index):
865 self.tk.call(self._w, 'icursor', index)
866 def index(self, index):
867 return self.tk.getint(self.tk.call(
868 self._w, 'index', index))
869 def insert(self, index, string):
870 self.tk.call(self._w, 'insert', index, string)
871 def scan_mark(self, x):
872 self.tk.call(self._w, 'scan', 'mark', x)
873 def scan_dragto(self, x):
874 self.tk.call(self._w, 'scan', 'dragto', x)
875 def select_adjust(self, index):
876 self.tk.call(self._w, 'select', 'adjust', index)
877 def select_clear(self):
878 self.tk.call(self._w, 'select', 'clear')
879 def select_from(self, index):
880 self.tk.call(self._w, 'select', 'from', index)
881 def select_to(self, index):
882 self.tk.call(self._w, 'select', 'to', index)
883 def select_view(self, index):
884 self.tk.call(self._w, 'select', 'view', index)
885
886class Frame(Widget):
887 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000888 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000889 extra = ()
890 if cnf.has_key('class'):
891 extra = ('-class', cnf['class'])
892 del cnf['class']
893 Widget.__init__(self, master, 'frame', cnf, extra)
894 def tk_menuBar(self, *args):
895 apply(self.tk.call, ('tk_menuBar', self._w) + args)
896
897class Label(Widget):
898 def __init__(self, master=None, cnf={}):
899 Widget.__init__(self, master, 'label', cnf)
900
901class Listbox(Widget):
902 def __init__(self, master=None, cnf={}):
903 Widget.__init__(self, master, 'listbox', cnf)
904 def tk_listboxSingleSelect(self):
905 self.tk.call('tk_listboxSingleSelect', self._w)
906 def curselection(self):
907 return self.tk.splitlist(self.tk.call(
908 self._w, 'curselection'))
909 def delete(self, first, last=None):
910 self.tk.call(self._w, 'delete', first, last)
911 def get(self, index):
912 return self.tk.call(self._w, 'get', index)
913 def insert(self, index, *elements):
914 apply(self.tk.call,
915 (self._w, 'insert', index) + elements)
916 def nearest(self, y):
917 return self.tk.getint(self.tk.call(
918 self._w, 'nearest', y))
919 def scan_mark(self, x, y):
920 self.tk.call(self._w, 'scan', 'mark', x, y)
921 def scan_dragto(self, x, y):
922 self.tk.call(self._w, 'scan', 'dragto', x, y)
923 def select_adjust(self, index):
924 self.tk.call(self._w, 'select', 'adjust', index)
925 def select_clear(self):
926 self.tk.call(self._w, 'select', 'clear')
927 def select_from(self, index):
928 self.tk.call(self._w, 'select', 'from', index)
929 def select_to(self, index):
930 self.tk.call(self._w, 'select', 'to', index)
931 def size(self):
932 return self.tk.getint(self.tk.call(self._w, 'size'))
933 def xview(self, index):
934 self.tk.call(self._w, 'xview', index)
935 def yview(self, index):
936 self.tk.call(self._w, 'yview', index)
937
938class Menu(Widget):
939 def __init__(self, master=None, cnf={}):
940 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000941 def tk_bindForTraversal(self):
942 self.tk.call('tk_bindForTraversal', self._w)
943 def tk_mbPost(self):
944 self.tk.call('tk_mbPost', self._w)
945 def tk_mbUnpost(self):
946 self.tk.call('tk_mbUnpost')
947 def tk_traverseToMenu(self, char):
948 self.tk.call('tk_traverseToMenu', self._w, char)
949 def tk_traverseWithinMenu(self, char):
950 self.tk.call('tk_traverseWithinMenu', self._w, char)
951 def tk_getMenuButtons(self):
952 return self.tk.call('tk_getMenuButtons', self._w)
953 def tk_nextMenu(self, count):
954 self.tk.call('tk_nextMenu', count)
955 def tk_nextMenuEntry(self, count):
956 self.tk.call('tk_nextMenuEntry', count)
957 def tk_invokeMenu(self):
958 self.tk.call('tk_invokeMenu', self._w)
959 def tk_firstMenu(self):
960 self.tk.call('tk_firstMenu', self._w)
961 def tk_mbButtonDown(self):
962 self.tk.call('tk_mbButtonDown', self._w)
963 def activate(self, index):
964 self.tk.call(self._w, 'activate', index)
965 def add(self, itemType, cnf={}):
966 apply(self.tk.call, (self._w, 'add', itemType)
967 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000968 def add_cascade(self, cnf={}):
969 self.add('cascade', cnf)
970 def add_checkbutton(self, cnf={}):
971 self.add('checkbutton', cnf)
972 def add_command(self, cnf={}):
973 self.add('command', cnf)
974 def add_radiobutton(self, cnf={}):
975 self.add('radiobutton', cnf)
976 def add_separator(self, cnf={}):
977 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000978 def delete(self, index1, index2=None):
979 self.tk.call(self._w, 'delete', index1, index2)
980 def entryconfig(self, index, cnf={}):
981 apply(self.tk.call, (self._w, 'entryconfigure', index)
982 + self._options(cnf))
983 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000984 i = self.tk.call(self._w, 'index', index)
985 if i == 'none': return None
986 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def invoke(self, index):
988 return self.tk.call(self._w, 'invoke', index)
989 def post(self, x, y):
990 self.tk.call(self._w, 'post', x, y)
991 def unpost(self):
992 self.tk.call(self._w, 'unpost')
993 def yposition(self, index):
994 return self.tk.getint(self.tk.call(
995 self._w, 'yposition', index))
996
997class Menubutton(Widget):
998 def __init__(self, master=None, cnf={}):
999 Widget.__init__(self, master, 'menubutton', cnf)
1000
1001class Message(Widget):
1002 def __init__(self, master=None, cnf={}):
1003 Widget.__init__(self, master, 'message', cnf)
1004
1005class Radiobutton(Widget):
1006 def __init__(self, master=None, cnf={}):
1007 Widget.__init__(self, master, 'radiobutton', cnf)
1008 def deselect(self):
1009 self.tk.call(self._w, 'deselect')
1010 def flash(self):
1011 self.tk.call(self._w, 'flash')
1012 def invoke(self):
1013 self.tk.call(self._w, 'invoke')
1014 def select(self):
1015 self.tk.call(self._w, 'select')
1016
1017class Scale(Widget):
1018 def __init__(self, master=None, cnf={}):
1019 Widget.__init__(self, master, 'scale', cnf)
1020 def get(self):
1021 return self.tk.getint(self.tk.call(self._w, 'get'))
1022 def set(self, value):
1023 self.tk.call(self._w, 'set', value)
1024
1025class Scrollbar(Widget):
1026 def __init__(self, master=None, cnf={}):
1027 Widget.__init__(self, master, 'scrollbar', cnf)
1028 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001029 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +00001030 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1031 self.tk.call(self._w, 'set',
1032 totalUnits, windowUnits, firstUnit, lastUnit)
1033
1034class Text(Widget):
1035 def __init__(self, master=None, cnf={}):
1036 Widget.__init__(self, master, 'text', cnf)
1037 def tk_textSelectTo(self, index):
1038 self.tk.call('tk_textSelectTo', self._w, index)
1039 def tk_textBackspace(self):
1040 self.tk.call('tk_textBackspace', self._w)
1041 def tk_textIndexCloser(self, a, b, c):
1042 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1043 def tk_textResetAnchor(self, index):
1044 self.tk.call('tk_textResetAnchor', self._w, index)
1045 def compare(self, index1, op, index2):
1046 return self.tk.getboolean(self.tk.call(
1047 self._w, 'compare', index1, op, index2))
1048 def debug(self, boolean=None):
1049 return self.tk.getboolean(self.tk.call(
1050 self._w, 'debug', boolean))
1051 def delete(self, index1, index2=None):
1052 self.tk.call(self._w, 'delete', index1, index2)
1053 def get(self, index1, index2=None):
1054 return self.tk.call(self._w, 'get', index1, index2)
1055 def index(self, index):
1056 return self.tk.call(self._w, 'index', index)
1057 def insert(self, index, chars):
1058 self.tk.call(self._w, 'insert', index, chars)
1059 def mark_names(self):
1060 return self.tk.splitlist(self.tk.call(
1061 self._w, 'mark', 'names'))
1062 def mark_set(self, markName, index):
1063 self.tk.call(self._w, 'mark', 'set', markName, index)
1064 def mark_unset(self, markNames):
1065 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1066 def scan_mark(self, y):
1067 self.tk.call(self._w, 'scan', 'mark', y)
1068 def scan_dragto(self, y):
1069 self.tk.call(self._w, 'scan', 'dragto', y)
1070 def tag_add(self, tagName, index1, index2=None):
1071 self.tk.call(
1072 self._w, 'tag', 'add', tagName, index1, index2)
1073 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001074 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001075 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001076 self.tk.call(self._w, 'tag', 'bind',
1077 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001078 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001079 def tag_config(self, tagName, cnf={}):
1080 apply(self.tk.call,
1081 (self._w, 'tag', 'configure', tagName)
1082 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001083 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001084 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001085 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001086 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001087 def tag_names(self, index=None):
1088 return self.tk.splitlist(
1089 self.tk.call(self._w, 'tag', 'names', index))
1090 def tag_nextrange(self, tagName, index1, index2=None):
1091 return self.tk.splitlist(self.tk.call(
1092 self._w, 'tag', 'nextrange', index1, index2))
1093 def tag_raise(self, tagName, aboveThis=None):
1094 self.tk.call(
1095 self._w, 'tag', 'raise', tagName, aboveThis)
1096 def tag_ranges(self, tagName):
1097 return self.tk.splitlist(self.tk.call(
1098 self._w, 'tag', 'ranges', tagName))
1099 def tag_remove(self, tagName, index1, index2=None):
1100 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001101 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum18468821994-06-20 07:49:28 +00001102 def yview(self, what):
1103 self.tk.call(self._w, 'yview', what)
1104 def yview_pickplace(self, what):
1105 self.tk.call(self._w, 'yview', '-pickplace', what)
1106
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001107######################################################################
1108# Extensions:
1109
1110class Studbutton(Button):
1111 def __init__(self, master=None, cnf={}):
1112 Widget.__init__(self, master, 'studbutton', cnf)
1113 self.bind('<Any-Enter>', self.tk_butEnter)
1114 self.bind('<Any-Leave>', self.tk_butLeave)
1115 self.bind('<1>', self.tk_butDown)
1116 self.bind('<ButtonRelease-1>', self.tk_butUp)
1117
1118class Tributton(Button):
1119 def __init__(self, master=None, cnf={}):
1120 Widget.__init__(self, master, 'tributton', cnf)
1121 self.bind('<Any-Enter>', self.tk_butEnter)
1122 self.bind('<Any-Leave>', self.tk_butLeave)
1123 self.bind('<1>', self.tk_butDown)
1124 self.bind('<ButtonRelease-1>', self.tk_butUp)
1125