blob: e75b0d44da14adac5d1e6b7fd4a487f11b0a72ec [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 Rossum27b77a41994-07-12 15:52:32 +0000537 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000538 def destroy(self):
539 for c in self.children.values(): c.destroy()
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 Rossum27b77a41994-07-12 15:52:32 +0000543 def readprofile(self, baseName, className):
544 import os
545 if os.environ.has_key('HOME'): home = os.environ['HOME']
546 else: home = os.curdir
547 class_tcl = os.path.join(home, '.%s.tcl' % className)
548 class_py = os.path.join(home, '.%s.py' % className)
549 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
550 base_py = os.path.join(home, '.%s.py' % baseName)
551 dir = {'self': self}
552 exec 'from Tkinter import *' in dir
553 if os.path.isfile(class_tcl):
554 print 'source', `class_tcl`
555 self.tk.call('source', class_tcl)
556 if os.path.isfile(class_py):
557 print 'execfile', `class_py`
558 execfile(class_py, dir)
559 if os.path.isfile(base_tcl):
560 print 'source', `base_tcl`
561 self.tk.call('source', base_tcl)
562 if os.path.isfile(base_py):
563 print 'execfile', `base_py`
564 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000565
566class Pack:
567 def config(self, cnf={}):
568 apply(self.tk.call,
569 ('pack', 'configure', self._w)
570 + self._options(cnf))
571 pack = config
572 def __setitem__(self, key, value):
573 Pack.config({key: value})
574 def forget(self):
575 self.tk.call('pack', 'forget', self._w)
576 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000577 words = self.tk.splitlist(
578 self.tk.call('pack', 'newinfo', self._w))
579 dict = {}
580 for i in range(0, len(words), 2):
581 key = words[i][1:]
582 value = words[i+1]
583 if value[0] == '.':
584 value = self._nametowidget(value)
585 dict[key] = value
586 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000587 info = newinfo
588 def propagate(self, boolean=None):
589 if boolean:
590 self.tk.call('pack', 'propagate', self._w)
591 else:
592 return self._getboolean(self.tk.call(
593 'pack', 'propagate', self._w))
594 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000595 return map(self._nametowidget,
596 self.tk.splitlist(
597 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000598
599class Place:
600 def config(self, cnf={}):
601 apply(self.tk.call,
602 ('place', 'configure', self._w)
603 + self._options(cnf))
604 place = config
605 def __setitem__(self, key, value):
606 Place.config({key: value})
607 def forget(self):
608 self.tk.call('place', 'forget', self._w)
609 def info(self):
610 return self.tk.call('place', 'info', self._w)
611 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000612 return map(self._nametowidget,
613 self.tk.splitlist(
614 self.tk.call(
615 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000616
Guido van Rossum18468821994-06-20 07:49:28 +0000617class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000618 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000619 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000620 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000621 if not _default_root:
622 _default_root = Tk()
623 master = _default_root
624 if not _default_root:
625 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000626 self.master = master
627 self.tk = master.tk
628 if cnf.has_key('name'):
629 name = cnf['name']
630 del cnf['name']
631 else:
632 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000633 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000634 if master._w=='.':
635 self._w = '.' + name
636 else:
637 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000638 self.children = {}
639 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000640 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000641 self.master.children[self._name] = self
642 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000643 cnf = _cnfmerge(cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000644 Widget._setup(self, master, cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000645 self.widgetName = widgetName
646 apply(self.tk.call, (widgetName, self._w) + extra)
647 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000648 def config(self, cnf=None):
Guido van Rossum2a390311994-07-06 10:20:11 +0000649 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000650 if cnf is None:
651 cnf = {}
652 for x in self.tk.split(
653 self.tk.call(self._w, 'configure')):
654 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
655 return cnf
656 if type(cnf) == StringType:
657 x = self.tk.split(self.tk.call(
658 self._w, 'configure', '-'+cnf))
659 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000660 for k in cnf.keys():
661 if type(k) == ClassType:
662 k.config(self, cnf[k])
663 del cnf[k]
664 apply(self.tk.call, (self._w, 'configure')
665 + self._options(cnf))
666 def __getitem__(self, key):
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000667 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000668 self._w, 'configure', '-' + key))
669 return v[4]
670 def __setitem__(self, key, value):
671 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000672 def keys(self):
673 return map(lambda x: x[0][1:],
674 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000675 def __str__(self):
676 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000677 def destroy(self):
678 for c in self.children.values(): c.destroy()
Guido van Rossum18468821994-06-20 07:49:28 +0000679 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000680 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000681 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000682
683class Toplevel(Widget, Wm):
684 def __init__(self, master=None, cnf={}):
685 extra = ()
686 if cnf.has_key('screen'):
687 extra = ('-screen', cnf['screen'])
688 del cnf['screen']
689 if cnf.has_key('class'):
690 extra = extra + ('-class', cnf['class'])
691 del cnf['class']
692 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000693 root = self._root()
694 self.iconname(root.iconname())
695 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000696
697class Button(Widget):
698 def __init__(self, master=None, cnf={}):
699 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000700 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000701 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000702 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000703 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000704 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000705 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000706 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000707 self.tk.call('tk_butUp', self._w)
708 def flash(self):
709 self.tk.call(self._w, 'flash')
710 def invoke(self):
711 self.tk.call(self._w, 'invoke')
712
713# Indices:
714def AtEnd():
715 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000716def AtInsert(*args):
717 s = 'insert'
718 for a in args:
719 if a: s = s + (' ' + a)
720 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000721def AtSelFirst():
722 return 'sel.first'
723def AtSelLast():
724 return 'sel.last'
725def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000726 if y is None:
727 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000728 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000729 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000730
731class Canvas(Widget):
732 def __init__(self, master=None, cnf={}):
733 Widget.__init__(self, master, 'canvas', cnf)
734 def addtag(self, *args):
735 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000736 def addtag_above(self, tagOrId):
737 self.addtag('above', tagOrId)
738 def addtag_all(self):
739 self.addtag('all')
740 def addtag_below(self, tagOrId):
741 self.addtag('below', tagOrId)
742 def addtag_closest(self, x, y, halo=None, start=None):
743 self.addtag('closest', x, y, halo, start)
744 def addtag_enclosed(self, x1, y1, x2, y2):
745 self.addtag('enclosed', x1, y1, x2, y2)
746 def addtag_overlapping(self, x1, y1, x2, y2):
747 self.addtag('overlapping', x1, y1, x2, y2)
748 def addtag_withtag(self, tagOrId):
749 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000750 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000751 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000752 def bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000753 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000754 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000755 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000756 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000757 def canvasx(self, screenx, gridspacing=None):
758 return self.tk.getint(self.tk.call(
759 self._w, 'canvasx', screenx, gridspacing))
760 def canvasy(self, screeny, gridspacing=None):
761 return self.tk.getint(self.tk.call(
762 self._w, 'canvasy', screeny, gridspacing))
763 def coords(self, *args):
764 return self._do('coords', args)
765 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000766 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000767 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000768 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000769 args = args[:-1]
770 else:
771 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000772 return self.tk.getint(apply(
773 self.tk.call,
774 (self._w, 'create', itemType)
775 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000776 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000777 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000778 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000779 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000780 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000781 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000782 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000783 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000784 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000785 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000786 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000787 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000788 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000789 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000790 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000791 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000792 def dchars(self, *args):
793 self._do('dchars', args)
794 def delete(self, *args):
795 self._do('delete', args)
796 def dtag(self, *args):
797 self._do('dtag', args)
798 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000799 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000800 def find_above(self, tagOrId):
801 return self.find('above', tagOrId)
802 def find_all(self):
803 return self.find('all')
804 def find_below(self, tagOrId):
805 return self.find('below', tagOrId)
806 def find_closest(self, x, y, halo=None, start=None):
807 return self.find('closest', x, y, halo, start)
808 def find_enclosed(self, x1, y1, x2, y2):
809 return self.find('enclosed', x1, y1, x2, y2)
810 def find_overlapping(self, x1, y1, x2, y2):
811 return self.find('overlapping', x1, y1, x2, y2)
812 def find_withtag(self, tagOrId):
813 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000814 def focus(self, *args):
815 return self._do('focus', args)
816 def gettags(self, *args):
817 return self.tk.splitlist(self._do('gettags', args))
818 def icursor(self, *args):
819 self._do('icursor', args)
820 def index(self, *args):
821 return self.tk.getint(self._do('index', args))
822 def insert(self, *args):
823 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000824 def itemconfig(self, tagOrId, cnf=None):
825 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000826 cnf = {}
827 for x in self.tk.split(
828 self._do('itemconfigure', (tagOrId))):
829 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
830 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000831 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000832 x = self.tk.split(self._do('itemconfigure',
833 (tagOrId, '-'+cnf,)))
834 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000835 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
836 def lower(self, *args):
837 self._do('lower', args)
838 def move(self, *args):
839 self._do('move', args)
840 def postscript(self, cnf={}):
841 return self._do('postscript', self._options(cnf))
842 def tkraise(self, *args):
843 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000844 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000845 def scale(self, *args):
846 self._do('scale', args)
847 def scan_mark(self, x, y):
848 self.tk.call(self._w, 'scan', 'mark', x, y)
849 def scan_dragto(self, x, y):
850 self.tk.call(self._w, 'scan', 'dragto', x, y)
851 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000852 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000853 def select_clear(self):
854 self.tk.call(self._w, 'select', 'clear')
855 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000856 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000857 def select_item(self):
858 self.tk.call(self._w, 'select', 'item')
859 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000860 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000861 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000862 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000863 def xview(self, index):
864 self.tk.call(self._w, 'xview', index)
865 def yview(self, index):
866 self.tk.call(self._w, 'yview', index)
867
868class Checkbutton(Widget):
869 def __init__(self, master=None, cnf={}):
870 Widget.__init__(self, master, 'checkbutton', cnf)
871 def deselect(self):
872 self.tk.call(self._w, 'deselect')
873 def flash(self):
874 self.tk.call(self._w, 'flash')
875 def invoke(self):
876 self.tk.call(self._w, 'invoke')
877 def select(self):
878 self.tk.call(self._w, 'select')
879 def toggle(self):
880 self.tk.call(self._w, 'toggle')
881
882class Entry(Widget):
883 def __init__(self, master=None, cnf={}):
884 Widget.__init__(self, master, 'entry', cnf)
885 def tk_entryBackspace(self):
886 self.tk.call('tk_entryBackspace', self._w)
887 def tk_entryBackword(self):
888 self.tk.call('tk_entryBackword', self._w)
889 def tk_entrySeeCaret(self):
890 self.tk.call('tk_entrySeeCaret', self._w)
891 def delete(self, first, last=None):
892 self.tk.call(self._w, 'delete', first, last)
893 def get(self):
894 return self.tk.call(self._w, 'get')
895 def icursor(self, index):
896 self.tk.call(self._w, 'icursor', index)
897 def index(self, index):
898 return self.tk.getint(self.tk.call(
899 self._w, 'index', index))
900 def insert(self, index, string):
901 self.tk.call(self._w, 'insert', index, string)
902 def scan_mark(self, x):
903 self.tk.call(self._w, 'scan', 'mark', x)
904 def scan_dragto(self, x):
905 self.tk.call(self._w, 'scan', 'dragto', x)
906 def select_adjust(self, index):
907 self.tk.call(self._w, 'select', 'adjust', index)
908 def select_clear(self):
909 self.tk.call(self._w, 'select', 'clear')
910 def select_from(self, index):
911 self.tk.call(self._w, 'select', 'from', index)
912 def select_to(self, index):
913 self.tk.call(self._w, 'select', 'to', index)
914 def select_view(self, index):
915 self.tk.call(self._w, 'select', 'view', index)
916
917class Frame(Widget):
918 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000919 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000920 extra = ()
921 if cnf.has_key('class'):
922 extra = ('-class', cnf['class'])
923 del cnf['class']
924 Widget.__init__(self, master, 'frame', cnf, extra)
925 def tk_menuBar(self, *args):
926 apply(self.tk.call, ('tk_menuBar', self._w) + args)
927
928class Label(Widget):
929 def __init__(self, master=None, cnf={}):
930 Widget.__init__(self, master, 'label', cnf)
931
932class Listbox(Widget):
933 def __init__(self, master=None, cnf={}):
934 Widget.__init__(self, master, 'listbox', cnf)
935 def tk_listboxSingleSelect(self):
936 self.tk.call('tk_listboxSingleSelect', self._w)
937 def curselection(self):
938 return self.tk.splitlist(self.tk.call(
939 self._w, 'curselection'))
940 def delete(self, first, last=None):
941 self.tk.call(self._w, 'delete', first, last)
942 def get(self, index):
943 return self.tk.call(self._w, 'get', index)
944 def insert(self, index, *elements):
945 apply(self.tk.call,
946 (self._w, 'insert', index) + elements)
947 def nearest(self, y):
948 return self.tk.getint(self.tk.call(
949 self._w, 'nearest', y))
950 def scan_mark(self, x, y):
951 self.tk.call(self._w, 'scan', 'mark', x, y)
952 def scan_dragto(self, x, y):
953 self.tk.call(self._w, 'scan', 'dragto', x, y)
954 def select_adjust(self, index):
955 self.tk.call(self._w, 'select', 'adjust', index)
956 def select_clear(self):
957 self.tk.call(self._w, 'select', 'clear')
958 def select_from(self, index):
959 self.tk.call(self._w, 'select', 'from', index)
960 def select_to(self, index):
961 self.tk.call(self._w, 'select', 'to', index)
962 def size(self):
963 return self.tk.getint(self.tk.call(self._w, 'size'))
964 def xview(self, index):
965 self.tk.call(self._w, 'xview', index)
966 def yview(self, index):
967 self.tk.call(self._w, 'yview', index)
968
969class Menu(Widget):
970 def __init__(self, master=None, cnf={}):
971 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000972 def tk_bindForTraversal(self):
973 self.tk.call('tk_bindForTraversal', self._w)
974 def tk_mbPost(self):
975 self.tk.call('tk_mbPost', self._w)
976 def tk_mbUnpost(self):
977 self.tk.call('tk_mbUnpost')
978 def tk_traverseToMenu(self, char):
979 self.tk.call('tk_traverseToMenu', self._w, char)
980 def tk_traverseWithinMenu(self, char):
981 self.tk.call('tk_traverseWithinMenu', self._w, char)
982 def tk_getMenuButtons(self):
983 return self.tk.call('tk_getMenuButtons', self._w)
984 def tk_nextMenu(self, count):
985 self.tk.call('tk_nextMenu', count)
986 def tk_nextMenuEntry(self, count):
987 self.tk.call('tk_nextMenuEntry', count)
988 def tk_invokeMenu(self):
989 self.tk.call('tk_invokeMenu', self._w)
990 def tk_firstMenu(self):
991 self.tk.call('tk_firstMenu', self._w)
992 def tk_mbButtonDown(self):
993 self.tk.call('tk_mbButtonDown', self._w)
994 def activate(self, index):
995 self.tk.call(self._w, 'activate', index)
996 def add(self, itemType, cnf={}):
997 apply(self.tk.call, (self._w, 'add', itemType)
998 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000999 def add_cascade(self, cnf={}):
1000 self.add('cascade', cnf)
1001 def add_checkbutton(self, cnf={}):
1002 self.add('checkbutton', cnf)
1003 def add_command(self, cnf={}):
1004 self.add('command', cnf)
1005 def add_radiobutton(self, cnf={}):
1006 self.add('radiobutton', cnf)
1007 def add_separator(self, cnf={}):
1008 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001009 def delete(self, index1, index2=None):
1010 self.tk.call(self._w, 'delete', index1, index2)
1011 def entryconfig(self, index, cnf={}):
1012 apply(self.tk.call, (self._w, 'entryconfigure', index)
1013 + self._options(cnf))
1014 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001015 i = self.tk.call(self._w, 'index', index)
1016 if i == 'none': return None
1017 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001018 def invoke(self, index):
1019 return self.tk.call(self._w, 'invoke', index)
1020 def post(self, x, y):
1021 self.tk.call(self._w, 'post', x, y)
1022 def unpost(self):
1023 self.tk.call(self._w, 'unpost')
1024 def yposition(self, index):
1025 return self.tk.getint(self.tk.call(
1026 self._w, 'yposition', index))
1027
1028class Menubutton(Widget):
1029 def __init__(self, master=None, cnf={}):
1030 Widget.__init__(self, master, 'menubutton', cnf)
1031
1032class Message(Widget):
1033 def __init__(self, master=None, cnf={}):
1034 Widget.__init__(self, master, 'message', cnf)
1035
1036class Radiobutton(Widget):
1037 def __init__(self, master=None, cnf={}):
1038 Widget.__init__(self, master, 'radiobutton', cnf)
1039 def deselect(self):
1040 self.tk.call(self._w, 'deselect')
1041 def flash(self):
1042 self.tk.call(self._w, 'flash')
1043 def invoke(self):
1044 self.tk.call(self._w, 'invoke')
1045 def select(self):
1046 self.tk.call(self._w, 'select')
1047
1048class Scale(Widget):
1049 def __init__(self, master=None, cnf={}):
1050 Widget.__init__(self, master, 'scale', cnf)
1051 def get(self):
1052 return self.tk.getint(self.tk.call(self._w, 'get'))
1053 def set(self, value):
1054 self.tk.call(self._w, 'set', value)
1055
1056class Scrollbar(Widget):
1057 def __init__(self, master=None, cnf={}):
1058 Widget.__init__(self, master, 'scrollbar', cnf)
1059 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001060 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +00001061 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1062 self.tk.call(self._w, 'set',
1063 totalUnits, windowUnits, firstUnit, lastUnit)
1064
1065class Text(Widget):
1066 def __init__(self, master=None, cnf={}):
1067 Widget.__init__(self, master, 'text', cnf)
1068 def tk_textSelectTo(self, index):
1069 self.tk.call('tk_textSelectTo', self._w, index)
1070 def tk_textBackspace(self):
1071 self.tk.call('tk_textBackspace', self._w)
1072 def tk_textIndexCloser(self, a, b, c):
1073 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1074 def tk_textResetAnchor(self, index):
1075 self.tk.call('tk_textResetAnchor', self._w, index)
1076 def compare(self, index1, op, index2):
1077 return self.tk.getboolean(self.tk.call(
1078 self._w, 'compare', index1, op, index2))
1079 def debug(self, boolean=None):
1080 return self.tk.getboolean(self.tk.call(
1081 self._w, 'debug', boolean))
1082 def delete(self, index1, index2=None):
1083 self.tk.call(self._w, 'delete', index1, index2)
1084 def get(self, index1, index2=None):
1085 return self.tk.call(self._w, 'get', index1, index2)
1086 def index(self, index):
1087 return self.tk.call(self._w, 'index', index)
1088 def insert(self, index, chars):
1089 self.tk.call(self._w, 'insert', index, chars)
1090 def mark_names(self):
1091 return self.tk.splitlist(self.tk.call(
1092 self._w, 'mark', 'names'))
1093 def mark_set(self, markName, index):
1094 self.tk.call(self._w, 'mark', 'set', markName, index)
1095 def mark_unset(self, markNames):
1096 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1097 def scan_mark(self, y):
1098 self.tk.call(self._w, 'scan', 'mark', y)
1099 def scan_dragto(self, y):
1100 self.tk.call(self._w, 'scan', 'dragto', y)
1101 def tag_add(self, tagName, index1, index2=None):
1102 self.tk.call(
1103 self._w, 'tag', 'add', tagName, index1, index2)
1104 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001105 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001106 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001107 self.tk.call(self._w, 'tag', 'bind',
1108 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001109 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001110 def tag_config(self, tagName, cnf={}):
1111 apply(self.tk.call,
1112 (self._w, 'tag', 'configure', tagName)
1113 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001114 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001115 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001116 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001117 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001118 def tag_names(self, index=None):
1119 return self.tk.splitlist(
1120 self.tk.call(self._w, 'tag', 'names', index))
1121 def tag_nextrange(self, tagName, index1, index2=None):
1122 return self.tk.splitlist(self.tk.call(
1123 self._w, 'tag', 'nextrange', index1, index2))
1124 def tag_raise(self, tagName, aboveThis=None):
1125 self.tk.call(
1126 self._w, 'tag', 'raise', tagName, aboveThis)
1127 def tag_ranges(self, tagName):
1128 return self.tk.splitlist(self.tk.call(
1129 self._w, 'tag', 'ranges', tagName))
1130 def tag_remove(self, tagName, index1, index2=None):
1131 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001132 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum18468821994-06-20 07:49:28 +00001133 def yview(self, what):
1134 self.tk.call(self._w, 'yview', what)
1135 def yview_pickplace(self, what):
1136 self.tk.call(self._w, 'yview', '-pickplace', what)
1137
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001138######################################################################
1139# Extensions:
1140
1141class Studbutton(Button):
1142 def __init__(self, master=None, cnf={}):
1143 Widget.__init__(self, master, 'studbutton', cnf)
1144 self.bind('<Any-Enter>', self.tk_butEnter)
1145 self.bind('<Any-Leave>', self.tk_butLeave)
1146 self.bind('<1>', self.tk_butDown)
1147 self.bind('<ButtonRelease-1>', self.tk_butUp)
1148
1149class Tributton(Button):
1150 def __init__(self, master=None, cnf={}):
1151 Widget.__init__(self, master, 'tributton', cnf)
1152 self.bind('<Any-Enter>', self.tk_butEnter)
1153 self.bind('<Any-Leave>', self.tk_butLeave)
1154 self.bind('<1>', self.tk_butDown)
1155 self.bind('<ButtonRelease-1>', self.tk_butUp)
1156