blob: 5a2e6b28e11be28c897ce426dab7bc0f06c55656 [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')
Guido van Rossumef8f8811994-08-08 12:47:33 +0000319 def unbind(self, sequence):
320 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +0000321 def bind(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000322 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000323 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000324 self.tk.call('bind', self._w, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000325 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000326 def bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000327 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000328 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000329 self.tk.call('bind', 'all' , sequence,
Guido van Rossumbd84b041994-07-04 10:48:25 +0000330 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000331 def bind_class(self, className, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000332 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000333 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000334 self.tk.call('bind', className , sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000335 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000336 def mainloop(self):
337 self.tk.mainloop()
338 def quit(self):
339 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000340 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000341 if not string: return None
342 res = ()
343 for v in self.tk.splitlist(string):
344 res = res + (self.tk.getint(v),)
345 return res
Guido van Rossum18468821994-06-20 07:49:28 +0000346 def _getboolean(self, string):
347 if string:
348 return self.tk.getboolean(string)
Guido van Rossum18468821994-06-20 07:49:28 +0000349 def _options(self, cnf):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000350 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000351 res = ()
352 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000353 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000354 v = self._register(v)
355 res = res + ('-'+k, v)
356 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000357 def _nametowidget(self, name):
358 w = self
359 if name[0] == '.':
360 w = w._root()
361 name = name[1:]
362 from string import find
363 while name:
364 i = find(name, '.')
365 if i >= 0:
366 name, tail = name[:i], name[i+1:]
367 else:
368 tail = ''
369 w = w.children[name]
370 name = tail
371 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000372 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000373 f = _CallSafely(func, subst).__call__
374 name = `id(f)`
375 if hasattr(func, 'im_func'):
376 func = func.im_func
377 if hasattr(func, 'func_name') and \
378 type(func.func_name) == type(''):
379 name = name + func.func_name
380 self.tk.createcommand(name, f)
381 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000382 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000383 def _root(self):
384 w = self
385 while w.master: w = w.master
386 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000387 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000388 '%s', '%t', '%w', '%x', '%y',
389 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
390 def _substitute(self, *args):
391 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000392 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000393 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
394 # Missing: (a, c, d, m, o, v, B, R)
395 e = Event()
396 e.serial = tk.getint(nsign)
397 e.num = tk.getint(b)
398 try: e.focus = tk.getboolean(f)
399 except TclError: pass
400 e.height = tk.getint(h)
401 e.keycode = tk.getint(k)
402 e.state = tk.getint(s)
403 e.time = tk.getint(t)
404 e.width = tk.getint(w)
405 e.x = tk.getint(x)
406 e.y = tk.getint(y)
407 e.char = A
408 try: e.send_event = tk.getboolean(E)
409 except TclError: pass
410 e.keysym = K
411 e.keysym_num = tk.getint(N)
412 e.type = T
413 e.widget = self._nametowidget(W)
414 e.x_root = tk.getint(X)
415 e.y_root = tk.getint(Y)
416 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000417
418class _CallSafely:
419 def __init__(self, func, subst=None):
420 self.func = func
421 self.subst = subst
422 def __call__(self, *args):
423 if self.subst:
424 args = self.apply_func(self.subst, args)
425 args = self.apply_func(self.func, args)
426 def apply_func(self, func, args):
427 import sys
428 try:
429 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000430 except SystemExit, msg:
431 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000432 except:
433 try:
434 try:
435 t = sys.exc_traceback
436 while t:
437 sys.stderr.write(
438 ' %s, line %s\n' %
439 (t.tb_frame.f_code,
440 t.tb_lineno))
441 t = t.tb_next
442 finally:
443 sys.stderr.write('%s: %s\n' %
444 (sys.exc_type,
445 sys.exc_value))
Guido van Rossum45853db1994-06-20 12:19:19 +0000446 (sys.last_type,
447 sys.last_value,
448 sys.last_traceback) = (sys.exc_type,
449 sys.exc_value,
450 sys.exc_traceback)
451 import pdb
452 pdb.pm()
Guido van Rossum18468821994-06-20 07:49:28 +0000453 except:
454 print '*** Error in error handling ***'
455 print sys.exc_type, ':', sys.exc_value
456
457class Wm:
458 def aspect(self,
459 minNumer=None, minDenom=None,
460 maxNumer=None, maxDenom=None):
461 return self._getints(
462 self.tk.call('wm', 'aspect', self._w,
463 minNumer, minDenom,
464 maxNumer, maxDenom))
465 def client(self, name=None):
466 return self.tk.call('wm', 'client', self._w, name)
467 def command(self, value=None):
468 return self.tk.call('wm', 'command', self._w, value)
469 def deiconify(self):
470 return self.tk.call('wm', 'deiconify', self._w)
471 def focusmodel(self, model=None):
472 return self.tk.call('wm', 'focusmodel', self._w, model)
473 def frame(self):
474 return self.tk.call('wm', 'frame', self._w)
475 def geometry(self, newGeometry=None):
476 return self.tk.call('wm', 'geometry', self._w, newGeometry)
477 def grid(self,
478 baseWidht=None, baseHeight=None,
479 widthInc=None, heightInc=None):
480 return self._getints(self.tk.call(
481 'wm', 'grid', self._w,
482 baseWidht, baseHeight, widthInc, heightInc))
483 def group(self, pathName=None):
484 return self.tk.call('wm', 'group', self._w, pathName)
485 def iconbitmap(self, bitmap=None):
486 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
487 def iconify(self):
488 return self.tk.call('wm', 'iconify', self._w)
489 def iconmask(self, bitmap=None):
490 return self.tk.call('wm', 'iconmask', self._w, bitmap)
491 def iconname(self, newName=None):
492 return self.tk.call('wm', 'iconname', self._w, newName)
493 def iconposition(self, x=None, y=None):
494 return self._getints(self.tk.call(
495 'wm', 'iconposition', self._w, x, y))
496 def iconwindow(self, pathName=None):
497 return self.tk.call('wm', 'iconwindow', self._w, pathName)
498 def maxsize(self, width=None, height=None):
499 return self._getints(self.tk.call(
500 'wm', 'maxsize', self._w, width, height))
501 def minsize(self, width=None, height=None):
502 return self._getints(self.tk.call(
503 'wm', 'minsize', self._w, width, height))
504 def overrideredirect(self, boolean=None):
505 return self._getboolean(self.tk.call(
506 'wm', 'overrideredirect', self._w, boolean))
507 def positionfrom(self, who=None):
508 return self.tk.call('wm', 'positionfrom', self._w, who)
509 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000510 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000511 command = self._register(func)
512 else:
513 command = func
514 return self.tk.call(
515 'wm', 'protocol', self._w, name, command)
516 def sizefrom(self, who=None):
517 return self.tk.call('wm', 'sizefrom', self._w, who)
518 def state(self):
519 return self.tk.call('wm', 'state', self._w)
520 def title(self, string=None):
521 return self.tk.call('wm', 'title', self._w, string)
522 def transient(self, master=None):
523 return self.tk.call('wm', 'transient', self._w, master)
524 def withdraw(self):
525 return self.tk.call('wm', 'withdraw', self._w)
526
527class Tk(Misc, Wm):
528 _w = '.'
529 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000530 self.master = None
531 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000532 if baseName is None:
533 import sys, os
534 baseName = os.path.basename(sys.argv[0])
535 if baseName[-3:] == '.py': baseName = baseName[:-3]
536 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000537 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000538 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000539 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000540 def destroy(self):
541 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000542 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000543 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000544 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000545 def readprofile(self, baseName, className):
546 import os
547 if os.environ.has_key('HOME'): home = os.environ['HOME']
548 else: home = os.curdir
549 class_tcl = os.path.join(home, '.%s.tcl' % className)
550 class_py = os.path.join(home, '.%s.py' % className)
551 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
552 base_py = os.path.join(home, '.%s.py' % baseName)
553 dir = {'self': self}
554 exec 'from Tkinter import *' in dir
555 if os.path.isfile(class_tcl):
556 print 'source', `class_tcl`
557 self.tk.call('source', class_tcl)
558 if os.path.isfile(class_py):
559 print 'execfile', `class_py`
560 execfile(class_py, dir)
561 if os.path.isfile(base_tcl):
562 print 'source', `base_tcl`
563 self.tk.call('source', base_tcl)
564 if os.path.isfile(base_py):
565 print 'execfile', `base_py`
566 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000567
568class Pack:
569 def config(self, cnf={}):
570 apply(self.tk.call,
571 ('pack', 'configure', self._w)
572 + self._options(cnf))
573 pack = config
574 def __setitem__(self, key, value):
575 Pack.config({key: value})
576 def forget(self):
577 self.tk.call('pack', 'forget', self._w)
578 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000579 words = self.tk.splitlist(
580 self.tk.call('pack', 'newinfo', self._w))
581 dict = {}
582 for i in range(0, len(words), 2):
583 key = words[i][1:]
584 value = words[i+1]
585 if value[0] == '.':
586 value = self._nametowidget(value)
587 dict[key] = value
588 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000589 info = newinfo
590 def propagate(self, boolean=None):
591 if boolean:
592 self.tk.call('pack', 'propagate', self._w)
593 else:
594 return self._getboolean(self.tk.call(
595 'pack', 'propagate', self._w))
596 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000597 return map(self._nametowidget,
598 self.tk.splitlist(
599 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000600
601class Place:
602 def config(self, cnf={}):
603 apply(self.tk.call,
604 ('place', 'configure', self._w)
605 + self._options(cnf))
606 place = config
607 def __setitem__(self, key, value):
608 Place.config({key: value})
609 def forget(self):
610 self.tk.call('place', 'forget', self._w)
611 def info(self):
612 return self.tk.call('place', 'info', self._w)
613 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000614 return map(self._nametowidget,
615 self.tk.splitlist(
616 self.tk.call(
617 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000618
Guido van Rossum18468821994-06-20 07:49:28 +0000619class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000620 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000621 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000622 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000623 if not _default_root:
624 _default_root = Tk()
625 master = _default_root
626 if not _default_root:
627 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000628 self.master = master
629 self.tk = master.tk
630 if cnf.has_key('name'):
631 name = cnf['name']
632 del cnf['name']
633 else:
634 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000635 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000636 if master._w=='.':
637 self._w = '.' + name
638 else:
639 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000640 self.children = {}
641 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000642 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000643 self.master.children[self._name] = self
644 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000645 cnf = _cnfmerge(cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000646 Widget._setup(self, master, cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000647 self.widgetName = widgetName
648 apply(self.tk.call, (widgetName, self._w) + extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000649 if cnf:
650 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000651 def config(self, cnf=None):
Guido van Rossum2a390311994-07-06 10:20:11 +0000652 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000653 if cnf is None:
654 cnf = {}
655 for x in self.tk.split(
656 self.tk.call(self._w, 'configure')):
657 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
658 return cnf
659 if type(cnf) == StringType:
660 x = self.tk.split(self.tk.call(
661 self._w, 'configure', '-'+cnf))
662 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000663 for k in cnf.keys():
664 if type(k) == ClassType:
665 k.config(self, cnf[k])
666 del cnf[k]
667 apply(self.tk.call, (self._w, 'configure')
668 + self._options(cnf))
669 def __getitem__(self, key):
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000670 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000671 self._w, 'configure', '-' + key))
672 return v[4]
673 def __setitem__(self, key, value):
674 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000675 def keys(self):
676 return map(lambda x: x[0][1:],
677 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000678 def __str__(self):
679 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000680 def destroy(self):
681 for c in self.children.values(): c.destroy()
Guido van Rossum18468821994-06-20 07:49:28 +0000682 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000683 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000684 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000685
686class Toplevel(Widget, Wm):
687 def __init__(self, master=None, cnf={}):
688 extra = ()
689 if cnf.has_key('screen'):
690 extra = ('-screen', cnf['screen'])
691 del cnf['screen']
692 if cnf.has_key('class'):
693 extra = extra + ('-class', cnf['class'])
694 del cnf['class']
695 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000696 root = self._root()
697 self.iconname(root.iconname())
698 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000699
700class Button(Widget):
701 def __init__(self, master=None, cnf={}):
702 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000703 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000704 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000705 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000706 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000707 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000708 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000709 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000710 self.tk.call('tk_butUp', self._w)
711 def flash(self):
712 self.tk.call(self._w, 'flash')
713 def invoke(self):
714 self.tk.call(self._w, 'invoke')
715
716# Indices:
717def AtEnd():
718 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000719def AtInsert(*args):
720 s = 'insert'
721 for a in args:
722 if a: s = s + (' ' + a)
723 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000724def AtSelFirst():
725 return 'sel.first'
726def AtSelLast():
727 return 'sel.last'
728def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000729 if y is None:
730 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000731 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000732 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000733
734class Canvas(Widget):
735 def __init__(self, master=None, cnf={}):
736 Widget.__init__(self, master, 'canvas', cnf)
737 def addtag(self, *args):
738 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000739 def addtag_above(self, tagOrId):
740 self.addtag('above', tagOrId)
741 def addtag_all(self):
742 self.addtag('all')
743 def addtag_below(self, tagOrId):
744 self.addtag('below', tagOrId)
745 def addtag_closest(self, x, y, halo=None, start=None):
746 self.addtag('closest', x, y, halo, start)
747 def addtag_enclosed(self, x1, y1, x2, y2):
748 self.addtag('enclosed', x1, y1, x2, y2)
749 def addtag_overlapping(self, x1, y1, x2, y2):
750 self.addtag('overlapping', x1, y1, x2, y2)
751 def addtag_withtag(self, tagOrId):
752 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000753 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000754 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000755 def tag_unbind(self, tagOrId, sequence):
756 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
757 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000758 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000759 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000760 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000761 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000762 def canvasx(self, screenx, gridspacing=None):
763 return self.tk.getint(self.tk.call(
764 self._w, 'canvasx', screenx, gridspacing))
765 def canvasy(self, screeny, gridspacing=None):
766 return self.tk.getint(self.tk.call(
767 self._w, 'canvasy', screeny, gridspacing))
768 def coords(self, *args):
769 return self._do('coords', args)
770 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000771 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000772 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000773 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000774 args = args[:-1]
775 else:
776 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000777 return self.tk.getint(apply(
778 self.tk.call,
779 (self._w, 'create', itemType)
780 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000781 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000782 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000783 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000784 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000785 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000786 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000787 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000788 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000789 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000790 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000791 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000792 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000793 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000794 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000795 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000796 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000797 def dchars(self, *args):
798 self._do('dchars', args)
799 def delete(self, *args):
800 self._do('delete', args)
801 def dtag(self, *args):
802 self._do('dtag', args)
803 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000804 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000805 def find_above(self, tagOrId):
806 return self.find('above', tagOrId)
807 def find_all(self):
808 return self.find('all')
809 def find_below(self, tagOrId):
810 return self.find('below', tagOrId)
811 def find_closest(self, x, y, halo=None, start=None):
812 return self.find('closest', x, y, halo, start)
813 def find_enclosed(self, x1, y1, x2, y2):
814 return self.find('enclosed', x1, y1, x2, y2)
815 def find_overlapping(self, x1, y1, x2, y2):
816 return self.find('overlapping', x1, y1, x2, y2)
817 def find_withtag(self, tagOrId):
818 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000819 def focus(self, *args):
820 return self._do('focus', args)
821 def gettags(self, *args):
822 return self.tk.splitlist(self._do('gettags', args))
823 def icursor(self, *args):
824 self._do('icursor', args)
825 def index(self, *args):
826 return self.tk.getint(self._do('index', args))
827 def insert(self, *args):
828 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000829 def itemconfig(self, tagOrId, cnf=None):
830 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000831 cnf = {}
832 for x in self.tk.split(
833 self._do('itemconfigure', (tagOrId))):
834 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
835 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000836 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000837 x = self.tk.split(self._do('itemconfigure',
838 (tagOrId, '-'+cnf,)))
839 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000840 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
841 def lower(self, *args):
842 self._do('lower', args)
843 def move(self, *args):
844 self._do('move', args)
845 def postscript(self, cnf={}):
846 return self._do('postscript', self._options(cnf))
847 def tkraise(self, *args):
848 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000849 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000850 def scale(self, *args):
851 self._do('scale', args)
852 def scan_mark(self, x, y):
853 self.tk.call(self._w, 'scan', 'mark', x, y)
854 def scan_dragto(self, x, y):
855 self.tk.call(self._w, 'scan', 'dragto', x, y)
856 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000857 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000858 def select_clear(self):
859 self.tk.call(self._w, 'select', 'clear')
860 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000861 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000862 def select_item(self):
863 self.tk.call(self._w, 'select', 'item')
864 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000865 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000866 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000867 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000868 def xview(self, index):
869 self.tk.call(self._w, 'xview', index)
870 def yview(self, index):
871 self.tk.call(self._w, 'yview', index)
872
873class Checkbutton(Widget):
874 def __init__(self, master=None, cnf={}):
875 Widget.__init__(self, master, 'checkbutton', cnf)
876 def deselect(self):
877 self.tk.call(self._w, 'deselect')
878 def flash(self):
879 self.tk.call(self._w, 'flash')
880 def invoke(self):
881 self.tk.call(self._w, 'invoke')
882 def select(self):
883 self.tk.call(self._w, 'select')
884 def toggle(self):
885 self.tk.call(self._w, 'toggle')
886
887class Entry(Widget):
888 def __init__(self, master=None, cnf={}):
889 Widget.__init__(self, master, 'entry', cnf)
890 def tk_entryBackspace(self):
891 self.tk.call('tk_entryBackspace', self._w)
892 def tk_entryBackword(self):
893 self.tk.call('tk_entryBackword', self._w)
894 def tk_entrySeeCaret(self):
895 self.tk.call('tk_entrySeeCaret', self._w)
896 def delete(self, first, last=None):
897 self.tk.call(self._w, 'delete', first, last)
898 def get(self):
899 return self.tk.call(self._w, 'get')
900 def icursor(self, index):
901 self.tk.call(self._w, 'icursor', index)
902 def index(self, index):
903 return self.tk.getint(self.tk.call(
904 self._w, 'index', index))
905 def insert(self, index, string):
906 self.tk.call(self._w, 'insert', index, string)
907 def scan_mark(self, x):
908 self.tk.call(self._w, 'scan', 'mark', x)
909 def scan_dragto(self, x):
910 self.tk.call(self._w, 'scan', 'dragto', x)
911 def select_adjust(self, index):
912 self.tk.call(self._w, 'select', 'adjust', index)
913 def select_clear(self):
914 self.tk.call(self._w, 'select', 'clear')
915 def select_from(self, index):
916 self.tk.call(self._w, 'select', 'from', index)
917 def select_to(self, index):
918 self.tk.call(self._w, 'select', 'to', index)
919 def select_view(self, index):
920 self.tk.call(self._w, 'select', 'view', index)
921
922class Frame(Widget):
923 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000924 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000925 extra = ()
926 if cnf.has_key('class'):
927 extra = ('-class', cnf['class'])
928 del cnf['class']
929 Widget.__init__(self, master, 'frame', cnf, extra)
930 def tk_menuBar(self, *args):
931 apply(self.tk.call, ('tk_menuBar', self._w) + args)
932
933class Label(Widget):
934 def __init__(self, master=None, cnf={}):
935 Widget.__init__(self, master, 'label', cnf)
936
937class Listbox(Widget):
938 def __init__(self, master=None, cnf={}):
939 Widget.__init__(self, master, 'listbox', cnf)
940 def tk_listboxSingleSelect(self):
941 self.tk.call('tk_listboxSingleSelect', self._w)
942 def curselection(self):
943 return self.tk.splitlist(self.tk.call(
944 self._w, 'curselection'))
945 def delete(self, first, last=None):
946 self.tk.call(self._w, 'delete', first, last)
947 def get(self, index):
948 return self.tk.call(self._w, 'get', index)
949 def insert(self, index, *elements):
950 apply(self.tk.call,
951 (self._w, 'insert', index) + elements)
952 def nearest(self, y):
953 return self.tk.getint(self.tk.call(
954 self._w, 'nearest', y))
955 def scan_mark(self, x, y):
956 self.tk.call(self._w, 'scan', 'mark', x, y)
957 def scan_dragto(self, x, y):
958 self.tk.call(self._w, 'scan', 'dragto', x, y)
959 def select_adjust(self, index):
960 self.tk.call(self._w, 'select', 'adjust', index)
961 def select_clear(self):
962 self.tk.call(self._w, 'select', 'clear')
963 def select_from(self, index):
964 self.tk.call(self._w, 'select', 'from', index)
965 def select_to(self, index):
966 self.tk.call(self._w, 'select', 'to', index)
967 def size(self):
968 return self.tk.getint(self.tk.call(self._w, 'size'))
969 def xview(self, index):
970 self.tk.call(self._w, 'xview', index)
971 def yview(self, index):
972 self.tk.call(self._w, 'yview', index)
973
974class Menu(Widget):
975 def __init__(self, master=None, cnf={}):
976 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000977 def tk_bindForTraversal(self):
978 self.tk.call('tk_bindForTraversal', self._w)
979 def tk_mbPost(self):
980 self.tk.call('tk_mbPost', self._w)
981 def tk_mbUnpost(self):
982 self.tk.call('tk_mbUnpost')
983 def tk_traverseToMenu(self, char):
984 self.tk.call('tk_traverseToMenu', self._w, char)
985 def tk_traverseWithinMenu(self, char):
986 self.tk.call('tk_traverseWithinMenu', self._w, char)
987 def tk_getMenuButtons(self):
988 return self.tk.call('tk_getMenuButtons', self._w)
989 def tk_nextMenu(self, count):
990 self.tk.call('tk_nextMenu', count)
991 def tk_nextMenuEntry(self, count):
992 self.tk.call('tk_nextMenuEntry', count)
993 def tk_invokeMenu(self):
994 self.tk.call('tk_invokeMenu', self._w)
995 def tk_firstMenu(self):
996 self.tk.call('tk_firstMenu', self._w)
997 def tk_mbButtonDown(self):
998 self.tk.call('tk_mbButtonDown', self._w)
999 def activate(self, index):
1000 self.tk.call(self._w, 'activate', index)
1001 def add(self, itemType, cnf={}):
1002 apply(self.tk.call, (self._w, 'add', itemType)
1003 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001004 def add_cascade(self, cnf={}):
1005 self.add('cascade', cnf)
1006 def add_checkbutton(self, cnf={}):
1007 self.add('checkbutton', cnf)
1008 def add_command(self, cnf={}):
1009 self.add('command', cnf)
1010 def add_radiobutton(self, cnf={}):
1011 self.add('radiobutton', cnf)
1012 def add_separator(self, cnf={}):
1013 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001014 def delete(self, index1, index2=None):
1015 self.tk.call(self._w, 'delete', index1, index2)
1016 def entryconfig(self, index, cnf={}):
1017 apply(self.tk.call, (self._w, 'entryconfigure', index)
1018 + self._options(cnf))
1019 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001020 i = self.tk.call(self._w, 'index', index)
1021 if i == 'none': return None
1022 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001023 def invoke(self, index):
1024 return self.tk.call(self._w, 'invoke', index)
1025 def post(self, x, y):
1026 self.tk.call(self._w, 'post', x, y)
1027 def unpost(self):
1028 self.tk.call(self._w, 'unpost')
1029 def yposition(self, index):
1030 return self.tk.getint(self.tk.call(
1031 self._w, 'yposition', index))
1032
1033class Menubutton(Widget):
1034 def __init__(self, master=None, cnf={}):
1035 Widget.__init__(self, master, 'menubutton', cnf)
1036
1037class Message(Widget):
1038 def __init__(self, master=None, cnf={}):
1039 Widget.__init__(self, master, 'message', cnf)
1040
1041class Radiobutton(Widget):
1042 def __init__(self, master=None, cnf={}):
1043 Widget.__init__(self, master, 'radiobutton', cnf)
1044 def deselect(self):
1045 self.tk.call(self._w, 'deselect')
1046 def flash(self):
1047 self.tk.call(self._w, 'flash')
1048 def invoke(self):
1049 self.tk.call(self._w, 'invoke')
1050 def select(self):
1051 self.tk.call(self._w, 'select')
1052
1053class Scale(Widget):
1054 def __init__(self, master=None, cnf={}):
1055 Widget.__init__(self, master, 'scale', cnf)
1056 def get(self):
1057 return self.tk.getint(self.tk.call(self._w, 'get'))
1058 def set(self, value):
1059 self.tk.call(self._w, 'set', value)
1060
1061class Scrollbar(Widget):
1062 def __init__(self, master=None, cnf={}):
1063 Widget.__init__(self, master, 'scrollbar', cnf)
1064 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001065 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +00001066 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1067 self.tk.call(self._w, 'set',
1068 totalUnits, windowUnits, firstUnit, lastUnit)
1069
1070class Text(Widget):
1071 def __init__(self, master=None, cnf={}):
1072 Widget.__init__(self, master, 'text', cnf)
1073 def tk_textSelectTo(self, index):
1074 self.tk.call('tk_textSelectTo', self._w, index)
1075 def tk_textBackspace(self):
1076 self.tk.call('tk_textBackspace', self._w)
1077 def tk_textIndexCloser(self, a, b, c):
1078 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1079 def tk_textResetAnchor(self, index):
1080 self.tk.call('tk_textResetAnchor', self._w, index)
1081 def compare(self, index1, op, index2):
1082 return self.tk.getboolean(self.tk.call(
1083 self._w, 'compare', index1, op, index2))
1084 def debug(self, boolean=None):
1085 return self.tk.getboolean(self.tk.call(
1086 self._w, 'debug', boolean))
1087 def delete(self, index1, index2=None):
1088 self.tk.call(self._w, 'delete', index1, index2)
1089 def get(self, index1, index2=None):
1090 return self.tk.call(self._w, 'get', index1, index2)
1091 def index(self, index):
1092 return self.tk.call(self._w, 'index', index)
1093 def insert(self, index, chars):
1094 self.tk.call(self._w, 'insert', index, chars)
1095 def mark_names(self):
1096 return self.tk.splitlist(self.tk.call(
1097 self._w, 'mark', 'names'))
1098 def mark_set(self, markName, index):
1099 self.tk.call(self._w, 'mark', 'set', markName, index)
1100 def mark_unset(self, markNames):
1101 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1102 def scan_mark(self, y):
1103 self.tk.call(self._w, 'scan', 'mark', y)
1104 def scan_dragto(self, y):
1105 self.tk.call(self._w, 'scan', 'dragto', y)
1106 def tag_add(self, tagName, index1, index2=None):
1107 self.tk.call(
1108 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001109 def tag_unbind(self, tagName, sequence):
1110 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001111 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001112 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001113 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001114 self.tk.call(self._w, 'tag', 'bind',
1115 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001116 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001117 def tag_config(self, tagName, cnf={}):
1118 apply(self.tk.call,
1119 (self._w, 'tag', 'configure', tagName)
1120 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001121 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001122 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001123 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001124 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001125 def tag_names(self, index=None):
1126 return self.tk.splitlist(
1127 self.tk.call(self._w, 'tag', 'names', index))
1128 def tag_nextrange(self, tagName, index1, index2=None):
1129 return self.tk.splitlist(self.tk.call(
1130 self._w, 'tag', 'nextrange', index1, index2))
1131 def tag_raise(self, tagName, aboveThis=None):
1132 self.tk.call(
1133 self._w, 'tag', 'raise', tagName, aboveThis)
1134 def tag_ranges(self, tagName):
1135 return self.tk.splitlist(self.tk.call(
1136 self._w, 'tag', 'ranges', tagName))
1137 def tag_remove(self, tagName, index1, index2=None):
1138 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001139 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum18468821994-06-20 07:49:28 +00001140 def yview(self, what):
1141 self.tk.call(self._w, 'yview', what)
1142 def yview_pickplace(self, what):
1143 self.tk.call(self._w, 'yview', '-pickplace', what)
1144
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001145######################################################################
1146# Extensions:
1147
1148class Studbutton(Button):
1149 def __init__(self, master=None, cnf={}):
1150 Widget.__init__(self, master, 'studbutton', cnf)
1151 self.bind('<Any-Enter>', self.tk_butEnter)
1152 self.bind('<Any-Leave>', self.tk_butLeave)
1153 self.bind('<1>', self.tk_butDown)
1154 self.bind('<ButtonRelease-1>', self.tk_butUp)
1155
1156class Tributton(Button):
1157 def __init__(self, master=None, cnf={}):
1158 Widget.__init__(self, master, 'tributton', cnf)
1159 self.bind('<Any-Enter>', self.tk_butEnter)
1160 self.bind('<Any-Leave>', self.tk_butLeave)
1161 self.bind('<1>', self.tk_butDown)
1162 self.bind('<ButtonRelease-1>', self.tk_butUp)
1163