blob: 4b012f19880d198b3e690b862ad4f0d9afe4ab56 [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 Rossumf023ab01994-08-30 12:13:44 +0000682 if self.master.children.has_key(self._name):
683 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000684 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000685 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000686 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000687
688class Toplevel(Widget, Wm):
689 def __init__(self, master=None, cnf={}):
690 extra = ()
691 if cnf.has_key('screen'):
692 extra = ('-screen', cnf['screen'])
693 del cnf['screen']
694 if cnf.has_key('class'):
695 extra = extra + ('-class', cnf['class'])
696 del cnf['class']
697 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000698 root = self._root()
699 self.iconname(root.iconname())
700 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000701
702class Button(Widget):
703 def __init__(self, master=None, cnf={}):
704 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000705 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000706 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000707 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000708 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000709 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000710 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000711 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000712 self.tk.call('tk_butUp', self._w)
713 def flash(self):
714 self.tk.call(self._w, 'flash')
715 def invoke(self):
716 self.tk.call(self._w, 'invoke')
717
718# Indices:
719def AtEnd():
720 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000721def AtInsert(*args):
722 s = 'insert'
723 for a in args:
724 if a: s = s + (' ' + a)
725 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000726def AtSelFirst():
727 return 'sel.first'
728def AtSelLast():
729 return 'sel.last'
730def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000731 if y is None:
732 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000733 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000734 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000735
736class Canvas(Widget):
737 def __init__(self, master=None, cnf={}):
738 Widget.__init__(self, master, 'canvas', cnf)
739 def addtag(self, *args):
740 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000741 def addtag_above(self, tagOrId):
742 self.addtag('above', tagOrId)
743 def addtag_all(self):
744 self.addtag('all')
745 def addtag_below(self, tagOrId):
746 self.addtag('below', tagOrId)
747 def addtag_closest(self, x, y, halo=None, start=None):
748 self.addtag('closest', x, y, halo, start)
749 def addtag_enclosed(self, x1, y1, x2, y2):
750 self.addtag('enclosed', x1, y1, x2, y2)
751 def addtag_overlapping(self, x1, y1, x2, y2):
752 self.addtag('overlapping', x1, y1, x2, y2)
753 def addtag_withtag(self, tagOrId):
754 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000755 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000756 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000757 def tag_unbind(self, tagOrId, sequence):
758 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
759 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000760 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000761 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000762 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000763 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000764 def canvasx(self, screenx, gridspacing=None):
765 return self.tk.getint(self.tk.call(
766 self._w, 'canvasx', screenx, gridspacing))
767 def canvasy(self, screeny, gridspacing=None):
768 return self.tk.getint(self.tk.call(
769 self._w, 'canvasy', screeny, gridspacing))
770 def coords(self, *args):
771 return self._do('coords', args)
772 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000773 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000774 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000775 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000776 args = args[:-1]
777 else:
778 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000779 return self.tk.getint(apply(
780 self.tk.call,
781 (self._w, 'create', itemType)
782 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000783 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000784 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000785 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000786 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000787 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000788 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000789 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000790 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000791 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000792 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000793 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000794 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000795 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000796 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000797 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000798 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000799 def dchars(self, *args):
800 self._do('dchars', args)
801 def delete(self, *args):
802 self._do('delete', args)
803 def dtag(self, *args):
804 self._do('dtag', args)
805 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000806 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000807 def find_above(self, tagOrId):
808 return self.find('above', tagOrId)
809 def find_all(self):
810 return self.find('all')
811 def find_below(self, tagOrId):
812 return self.find('below', tagOrId)
813 def find_closest(self, x, y, halo=None, start=None):
814 return self.find('closest', x, y, halo, start)
815 def find_enclosed(self, x1, y1, x2, y2):
816 return self.find('enclosed', x1, y1, x2, y2)
817 def find_overlapping(self, x1, y1, x2, y2):
818 return self.find('overlapping', x1, y1, x2, y2)
819 def find_withtag(self, tagOrId):
820 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000821 def focus(self, *args):
822 return self._do('focus', args)
823 def gettags(self, *args):
824 return self.tk.splitlist(self._do('gettags', args))
825 def icursor(self, *args):
826 self._do('icursor', args)
827 def index(self, *args):
828 return self.tk.getint(self._do('index', args))
829 def insert(self, *args):
830 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000831 def itemconfig(self, tagOrId, cnf=None):
832 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000833 cnf = {}
834 for x in self.tk.split(
835 self._do('itemconfigure', (tagOrId))):
836 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
837 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000838 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000839 x = self.tk.split(self._do('itemconfigure',
840 (tagOrId, '-'+cnf,)))
841 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000842 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
843 def lower(self, *args):
844 self._do('lower', args)
845 def move(self, *args):
846 self._do('move', args)
847 def postscript(self, cnf={}):
848 return self._do('postscript', self._options(cnf))
849 def tkraise(self, *args):
850 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000851 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000852 def scale(self, *args):
853 self._do('scale', args)
854 def scan_mark(self, x, y):
855 self.tk.call(self._w, 'scan', 'mark', x, y)
856 def scan_dragto(self, x, y):
857 self.tk.call(self._w, 'scan', 'dragto', x, y)
858 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000859 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000860 def select_clear(self):
861 self.tk.call(self._w, 'select', 'clear')
862 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000863 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000864 def select_item(self):
865 self.tk.call(self._w, 'select', 'item')
866 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000867 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000868 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000869 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000870 def xview(self, index):
871 self.tk.call(self._w, 'xview', index)
872 def yview(self, index):
873 self.tk.call(self._w, 'yview', index)
874
875class Checkbutton(Widget):
876 def __init__(self, master=None, cnf={}):
877 Widget.__init__(self, master, 'checkbutton', cnf)
878 def deselect(self):
879 self.tk.call(self._w, 'deselect')
880 def flash(self):
881 self.tk.call(self._w, 'flash')
882 def invoke(self):
883 self.tk.call(self._w, 'invoke')
884 def select(self):
885 self.tk.call(self._w, 'select')
886 def toggle(self):
887 self.tk.call(self._w, 'toggle')
888
889class Entry(Widget):
890 def __init__(self, master=None, cnf={}):
891 Widget.__init__(self, master, 'entry', cnf)
892 def tk_entryBackspace(self):
893 self.tk.call('tk_entryBackspace', self._w)
894 def tk_entryBackword(self):
895 self.tk.call('tk_entryBackword', self._w)
896 def tk_entrySeeCaret(self):
897 self.tk.call('tk_entrySeeCaret', self._w)
898 def delete(self, first, last=None):
899 self.tk.call(self._w, 'delete', first, last)
900 def get(self):
901 return self.tk.call(self._w, 'get')
902 def icursor(self, index):
903 self.tk.call(self._w, 'icursor', index)
904 def index(self, index):
905 return self.tk.getint(self.tk.call(
906 self._w, 'index', index))
907 def insert(self, index, string):
908 self.tk.call(self._w, 'insert', index, string)
909 def scan_mark(self, x):
910 self.tk.call(self._w, 'scan', 'mark', x)
911 def scan_dragto(self, x):
912 self.tk.call(self._w, 'scan', 'dragto', x)
913 def select_adjust(self, index):
914 self.tk.call(self._w, 'select', 'adjust', index)
915 def select_clear(self):
916 self.tk.call(self._w, 'select', 'clear')
917 def select_from(self, index):
918 self.tk.call(self._w, 'select', 'from', index)
919 def select_to(self, index):
920 self.tk.call(self._w, 'select', 'to', index)
921 def select_view(self, index):
922 self.tk.call(self._w, 'select', 'view', index)
923
924class Frame(Widget):
925 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000926 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000927 extra = ()
928 if cnf.has_key('class'):
929 extra = ('-class', cnf['class'])
930 del cnf['class']
931 Widget.__init__(self, master, 'frame', cnf, extra)
932 def tk_menuBar(self, *args):
933 apply(self.tk.call, ('tk_menuBar', self._w) + args)
934
935class Label(Widget):
936 def __init__(self, master=None, cnf={}):
937 Widget.__init__(self, master, 'label', cnf)
938
939class Listbox(Widget):
940 def __init__(self, master=None, cnf={}):
941 Widget.__init__(self, master, 'listbox', cnf)
942 def tk_listboxSingleSelect(self):
943 self.tk.call('tk_listboxSingleSelect', self._w)
944 def curselection(self):
945 return self.tk.splitlist(self.tk.call(
946 self._w, 'curselection'))
947 def delete(self, first, last=None):
948 self.tk.call(self._w, 'delete', first, last)
949 def get(self, index):
950 return self.tk.call(self._w, 'get', index)
951 def insert(self, index, *elements):
952 apply(self.tk.call,
953 (self._w, 'insert', index) + elements)
954 def nearest(self, y):
955 return self.tk.getint(self.tk.call(
956 self._w, 'nearest', y))
957 def scan_mark(self, x, y):
958 self.tk.call(self._w, 'scan', 'mark', x, y)
959 def scan_dragto(self, x, y):
960 self.tk.call(self._w, 'scan', 'dragto', x, y)
961 def select_adjust(self, index):
962 self.tk.call(self._w, 'select', 'adjust', index)
963 def select_clear(self):
964 self.tk.call(self._w, 'select', 'clear')
965 def select_from(self, index):
966 self.tk.call(self._w, 'select', 'from', index)
967 def select_to(self, index):
968 self.tk.call(self._w, 'select', 'to', index)
969 def size(self):
970 return self.tk.getint(self.tk.call(self._w, 'size'))
971 def xview(self, index):
972 self.tk.call(self._w, 'xview', index)
973 def yview(self, index):
974 self.tk.call(self._w, 'yview', index)
975
976class Menu(Widget):
977 def __init__(self, master=None, cnf={}):
978 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000979 def tk_bindForTraversal(self):
980 self.tk.call('tk_bindForTraversal', self._w)
981 def tk_mbPost(self):
982 self.tk.call('tk_mbPost', self._w)
983 def tk_mbUnpost(self):
984 self.tk.call('tk_mbUnpost')
985 def tk_traverseToMenu(self, char):
986 self.tk.call('tk_traverseToMenu', self._w, char)
987 def tk_traverseWithinMenu(self, char):
988 self.tk.call('tk_traverseWithinMenu', self._w, char)
989 def tk_getMenuButtons(self):
990 return self.tk.call('tk_getMenuButtons', self._w)
991 def tk_nextMenu(self, count):
992 self.tk.call('tk_nextMenu', count)
993 def tk_nextMenuEntry(self, count):
994 self.tk.call('tk_nextMenuEntry', count)
995 def tk_invokeMenu(self):
996 self.tk.call('tk_invokeMenu', self._w)
997 def tk_firstMenu(self):
998 self.tk.call('tk_firstMenu', self._w)
999 def tk_mbButtonDown(self):
1000 self.tk.call('tk_mbButtonDown', self._w)
1001 def activate(self, index):
1002 self.tk.call(self._w, 'activate', index)
1003 def add(self, itemType, cnf={}):
1004 apply(self.tk.call, (self._w, 'add', itemType)
1005 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001006 def add_cascade(self, cnf={}):
1007 self.add('cascade', cnf)
1008 def add_checkbutton(self, cnf={}):
1009 self.add('checkbutton', cnf)
1010 def add_command(self, cnf={}):
1011 self.add('command', cnf)
1012 def add_radiobutton(self, cnf={}):
1013 self.add('radiobutton', cnf)
1014 def add_separator(self, cnf={}):
1015 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001016 def delete(self, index1, index2=None):
1017 self.tk.call(self._w, 'delete', index1, index2)
1018 def entryconfig(self, index, cnf={}):
1019 apply(self.tk.call, (self._w, 'entryconfigure', index)
1020 + self._options(cnf))
1021 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001022 i = self.tk.call(self._w, 'index', index)
1023 if i == 'none': return None
1024 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001025 def invoke(self, index):
1026 return self.tk.call(self._w, 'invoke', index)
1027 def post(self, x, y):
1028 self.tk.call(self._w, 'post', x, y)
1029 def unpost(self):
1030 self.tk.call(self._w, 'unpost')
1031 def yposition(self, index):
1032 return self.tk.getint(self.tk.call(
1033 self._w, 'yposition', index))
1034
1035class Menubutton(Widget):
1036 def __init__(self, master=None, cnf={}):
1037 Widget.__init__(self, master, 'menubutton', cnf)
1038
1039class Message(Widget):
1040 def __init__(self, master=None, cnf={}):
1041 Widget.__init__(self, master, 'message', cnf)
1042
1043class Radiobutton(Widget):
1044 def __init__(self, master=None, cnf={}):
1045 Widget.__init__(self, master, 'radiobutton', cnf)
1046 def deselect(self):
1047 self.tk.call(self._w, 'deselect')
1048 def flash(self):
1049 self.tk.call(self._w, 'flash')
1050 def invoke(self):
1051 self.tk.call(self._w, 'invoke')
1052 def select(self):
1053 self.tk.call(self._w, 'select')
1054
1055class Scale(Widget):
1056 def __init__(self, master=None, cnf={}):
1057 Widget.__init__(self, master, 'scale', cnf)
1058 def get(self):
1059 return self.tk.getint(self.tk.call(self._w, 'get'))
1060 def set(self, value):
1061 self.tk.call(self._w, 'set', value)
1062
1063class Scrollbar(Widget):
1064 def __init__(self, master=None, cnf={}):
1065 Widget.__init__(self, master, 'scrollbar', cnf)
1066 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001067 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +00001068 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1069 self.tk.call(self._w, 'set',
1070 totalUnits, windowUnits, firstUnit, lastUnit)
1071
1072class Text(Widget):
1073 def __init__(self, master=None, cnf={}):
1074 Widget.__init__(self, master, 'text', cnf)
1075 def tk_textSelectTo(self, index):
1076 self.tk.call('tk_textSelectTo', self._w, index)
1077 def tk_textBackspace(self):
1078 self.tk.call('tk_textBackspace', self._w)
1079 def tk_textIndexCloser(self, a, b, c):
1080 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1081 def tk_textResetAnchor(self, index):
1082 self.tk.call('tk_textResetAnchor', self._w, index)
1083 def compare(self, index1, op, index2):
1084 return self.tk.getboolean(self.tk.call(
1085 self._w, 'compare', index1, op, index2))
1086 def debug(self, boolean=None):
1087 return self.tk.getboolean(self.tk.call(
1088 self._w, 'debug', boolean))
1089 def delete(self, index1, index2=None):
1090 self.tk.call(self._w, 'delete', index1, index2)
1091 def get(self, index1, index2=None):
1092 return self.tk.call(self._w, 'get', index1, index2)
1093 def index(self, index):
1094 return self.tk.call(self._w, 'index', index)
1095 def insert(self, index, chars):
1096 self.tk.call(self._w, 'insert', index, chars)
1097 def mark_names(self):
1098 return self.tk.splitlist(self.tk.call(
1099 self._w, 'mark', 'names'))
1100 def mark_set(self, markName, index):
1101 self.tk.call(self._w, 'mark', 'set', markName, index)
1102 def mark_unset(self, markNames):
1103 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1104 def scan_mark(self, y):
1105 self.tk.call(self._w, 'scan', 'mark', y)
1106 def scan_dragto(self, y):
1107 self.tk.call(self._w, 'scan', 'dragto', y)
1108 def tag_add(self, tagName, index1, index2=None):
1109 self.tk.call(
1110 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001111 def tag_unbind(self, tagName, sequence):
1112 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001113 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001114 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001115 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001116 self.tk.call(self._w, 'tag', 'bind',
1117 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001118 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001119 def tag_config(self, tagName, cnf={}):
1120 apply(self.tk.call,
1121 (self._w, 'tag', 'configure', tagName)
1122 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001123 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001124 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001125 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001126 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def tag_names(self, index=None):
1128 return self.tk.splitlist(
1129 self.tk.call(self._w, 'tag', 'names', index))
1130 def tag_nextrange(self, tagName, index1, index2=None):
1131 return self.tk.splitlist(self.tk.call(
1132 self._w, 'tag', 'nextrange', index1, index2))
1133 def tag_raise(self, tagName, aboveThis=None):
1134 self.tk.call(
1135 self._w, 'tag', 'raise', tagName, aboveThis)
1136 def tag_ranges(self, tagName):
1137 return self.tk.splitlist(self.tk.call(
1138 self._w, 'tag', 'ranges', tagName))
1139 def tag_remove(self, tagName, index1, index2=None):
1140 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001141 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum18468821994-06-20 07:49:28 +00001142 def yview(self, what):
1143 self.tk.call(self._w, 'yview', what)
1144 def yview_pickplace(self, what):
1145 self.tk.call(self._w, 'yview', '-pickplace', what)
1146
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001147######################################################################
1148# Extensions:
1149
1150class Studbutton(Button):
1151 def __init__(self, master=None, cnf={}):
1152 Widget.__init__(self, master, 'studbutton', cnf)
1153 self.bind('<Any-Enter>', self.tk_butEnter)
1154 self.bind('<Any-Leave>', self.tk_butLeave)
1155 self.bind('<1>', self.tk_butDown)
1156 self.bind('<ButtonRelease-1>', self.tk_butUp)
1157
1158class Tributton(Button):
1159 def __init__(self, master=None, cnf={}):
1160 Widget.__init__(self, master, 'tributton', cnf)
1161 self.bind('<Any-Enter>', self.tk_butEnter)
1162 self.bind('<Any-Leave>', self.tk_butLeave)
1163 self.bind('<1>', self.tk_butDown)
1164 self.bind('<ButtonRelease-1>', self.tk_butUp)
1165