blob: 0af4bce3a48c31541dbfeab64111a5f361755e16 [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
Guido van Rossum7e9394a1995-03-17 16:21:33 +00005from types import *
6CallableTypes = (FunctionType, MethodType,
7 BuiltinFunctionType, BuiltinMethodType,
8 ClassType, InstanceType)
Guido van Rossum18468821994-06-20 07:49:28 +00009
Guido van Rossum7e9394a1995-03-17 16:21:33 +000010# Older versions of tkinter don't define these variables
11try:
12 TkVersion = eval(tkinter.TK_VERSION)
13except AttributeError:
14 TkVersion = 3.6
15try:
16 TclVersion = eval(tkinter.TCL_VERSION)
17except AttributeError:
18 TclVersion = 7.3
Guido van Rossum18468821994-06-20 07:49:28 +000019
Guido van Rossum2dcf5291994-07-06 09:23:20 +000020def _flatten(tuple):
21 res = ()
22 for item in tuple:
23 if type(item) in (TupleType, ListType):
24 res = res + _flatten(item)
25 else:
26 res = res + (item,)
27 return res
28
29def _cnfmerge(cnfs):
30 if type(cnfs) in (NoneType, DictionaryType, StringType):
31 return cnfs
32 else:
33 cnf = {}
34 for c in _flatten(cnfs):
35 for k, v in c.items():
36 cnf[k] = v
37 return cnf
38
39class Event:
40 pass
41
Guido van Rossumaec5dc91994-06-27 07:55:12 +000042_default_root = None
43
Guido van Rossum45853db1994-06-20 12:19:19 +000044def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000045 pass
46
Guido van Rossum97aeca11994-07-07 13:12:12 +000047def _exit(code='0'):
48 import sys
49 sys.exit(getint(code))
50
Guido van Rossumaec5dc91994-06-27 07:55:12 +000051_varnum = 0
52class Variable:
53 def __init__(self, master=None):
54 global _default_root
55 global _varnum
56 if master:
57 self._tk = master.tk
58 else:
59 self._tk = _default_root.tk
60 self._name = 'PY_VAR' + `_varnum`
61 _varnum = _varnum + 1
62 def __del__(self):
63 self._tk.unsetvar(self._name)
64 def __str__(self):
65 return self._name
66 def __call__(self, value=None):
67 if value == None:
68 return self.get()
69 else:
70 self.set(value)
71 def set(self, value):
72 return self._tk.setvar(self._name, value)
73
74class StringVar(Variable):
75 def __init__(self, master=None):
76 Variable.__init__(self, master)
77 def get(self):
78 return self._tk.getvar(self._name)
79
80class IntVar(Variable):
81 def __init__(self, master=None):
82 Variable.__init__(self, master)
83 def get(self):
84 return self._tk.getint(self._tk.getvar(self._name))
85
86class DoubleVar(Variable):
87 def __init__(self, master=None):
88 Variable.__init__(self, master)
89 def get(self):
90 return self._tk.getdouble(self._tk.getvar(self._name))
91
92class BooleanVar(Variable):
93 def __init__(self, master=None):
94 Variable.__init__(self, master)
95 def get(self):
96 return self._tk.getboolean(self._tk.getvar(self._name))
97
Guido van Rossum2dcf5291994-07-06 09:23:20 +000098def mainloop():
99 _default_root.tk.mainloop()
100
101def getint(s):
102 return _default_root.tk.getint(s)
103
104def getdouble(s):
105 return _default_root.tk.getdouble(s)
106
107def getboolean(s):
108 return _default_root.tk.getboolean(s)
109
Guido van Rossum18468821994-06-20 07:49:28 +0000110class Misc:
111 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000112 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000113 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000114 def tk_menuBar(self, *args):
115 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000116 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000117 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000118 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000119 def wait_window(self, window=None):
120 if window == None:
121 window = self
122 self.tk.call('tkwait', 'window', window._w)
123 def wait_visibility(self, window=None):
124 if window == None:
125 window = self
126 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000127 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000128 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000129 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000130 return self.tk.getvar(name)
131 def getint(self, s):
132 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000133 def getdouble(self, s):
134 return self.tk.getdouble(s)
135 def getboolean(self, s):
136 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000137 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000138 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000139 focus = focus_set # XXX b/w compat?
140 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000141 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000142 def focus_default_none(self):
143 self.tk.call('focus', 'default', 'none')
144 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000145 def focus_none(self):
146 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000147 def focus_get(self):
148 name = self.tk.call('focus')
149 if name == 'none': return None
150 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000151 def after(self, ms, func=None, *args):
152 if not func:
153 self.tk.call('after', ms)
154 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000155 # XXX Disgusting hack to clean up after calling func
156 tmp = []
157 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
158 try:
159 apply(func, args)
160 finally:
161 tk.deletecommand(tmp[0])
162 name = self._register(callit)
163 tmp.append(name)
164 self.tk.call('after', ms, name)
Guido van Rossum45853db1994-06-20 12:19:19 +0000165 # XXX grab current w/o window argument
166 def grab_current(self):
167 name = self.tk.call('grab', 'current', self._w)
168 if not name: return None
169 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000170 def grab_release(self):
171 self.tk.call('grab', 'release', self._w)
172 def grab_set(self):
173 self.tk.call('grab', 'set', self._w)
174 def grab_set_global(self):
175 self.tk.call('grab', 'set', '-global', self._w)
176 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000177 status = self.tk.call('grab', 'status', self._w)
178 if status == 'none': status = None
179 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000180 def lower(self, belowThis=None):
181 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000182 def option_add(self, pattern, value, priority = None):
183 self.tk.call('option', 'add', pattern, priority)
184 def option_clear(self):
185 self.tk.call('option', 'clear')
186 def option_get(self, name, className):
187 return self.tk.call('option', 'get', self._w, name, className)
188 def option_readfile(self, fileName, priority = None):
189 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000190 def selection_clear(self):
191 self.tk.call('selection', 'clear', self._w)
192 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000193 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000194 def selection_handle(self, func, type=None, format=None):
195 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000196 self.tk.call('selection', 'handle', self._w,
197 name, type, format)
198 def selection_own(self, func=None):
199 name = self._register(func)
200 self.tk.call('selection', 'own', self._w, name)
201 def selection_own_get(self):
202 return self._nametowidget(self.tk.call('selection', 'own'))
203 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000204 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000205 def lower(self, belowThis=None):
206 self.tk.call('lift', self._w, belowThis)
207 def tkraise(self, aboveThis=None):
208 self.tk.call('raise', self._w, aboveThis)
209 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000210 def colormodel(self, value=None):
211 return self.tk.call('tk', 'colormodel', self._w, value)
212 def winfo_atom(self, name):
213 return self.tk.getint(self.tk.call('winfo', 'atom', name))
214 def winfo_atomname(self, id):
215 return self.tk.call('winfo', 'atomname', id)
216 def winfo_cells(self):
217 return self.tk.getint(
218 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000219 def winfo_children(self):
220 return map(self._nametowidget,
221 self.tk.splitlist(self.tk.call(
222 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000223 def winfo_class(self):
224 return self.tk.call('winfo', 'class', self._w)
225 def winfo_containing(self, rootX, rootY):
226 return self.tk.call('winfo', 'containing', rootx, rootY)
227 def winfo_depth(self):
228 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
229 def winfo_exists(self):
230 return self.tk.getint(
231 self.tk.call('winfo', 'exists', self._w))
232 def winfo_fpixels(self, number):
233 return self.tk.getdouble(self.tk.call(
234 'winfo', 'fpixels', self._w, number))
235 def winfo_geometry(self):
236 return self.tk.call('winfo', 'geometry', self._w)
237 def winfo_height(self):
238 return self.tk.getint(
239 self.tk.call('winfo', 'height', self._w))
240 def winfo_id(self):
241 return self.tk.getint(
242 self.tk.call('winfo', 'id', self._w))
243 def winfo_interps(self):
244 return self.tk.splitlist(
245 self.tk.call('winfo', 'interps'))
246 def winfo_ismapped(self):
247 return self.tk.getint(
248 self.tk.call('winfo', 'ismapped', self._w))
249 def winfo_name(self):
250 return self.tk.call('winfo', 'name', self._w)
251 def winfo_parent(self):
252 return self.tk.call('winfo', 'parent', self._w)
253 def winfo_pathname(self, id):
254 return self.tk.call('winfo', 'pathname', id)
255 def winfo_pixels(self, number):
256 return self.tk.getint(
257 self.tk.call('winfo', 'pixels', self._w, number))
258 def winfo_reqheight(self):
259 return self.tk.getint(
260 self.tk.call('winfo', 'reqheight', self._w))
261 def winfo_reqwidth(self):
262 return self.tk.getint(
263 self.tk.call('winfo', 'reqwidth', self._w))
264 def winfo_rgb(self, color):
265 return self._getints(
266 self.tk.call('winfo', 'rgb', self._w, color))
267 def winfo_rootx(self):
268 return self.tk.getint(
269 self.tk.call('winfo', 'rootx', self._w))
270 def winfo_rooty(self):
271 return self.tk.getint(
272 self.tk.call('winfo', 'rooty', self._w))
273 def winfo_screen(self):
274 return self.tk.call('winfo', 'screen', self._w)
275 def winfo_screencells(self):
276 return self.tk.getint(
277 self.tk.call('winfo', 'screencells', self._w))
278 def winfo_screendepth(self):
279 return self.tk.getint(
280 self.tk.call('winfo', 'screendepth', self._w))
281 def winfo_screenheight(self):
282 return self.tk.getint(
283 self.tk.call('winfo', 'screenheight', self._w))
284 def winfo_screenmmheight(self):
285 return self.tk.getint(
286 self.tk.call('winfo', 'screenmmheight', self._w))
287 def winfo_screenmmwidth(self):
288 return self.tk.getint(
289 self.tk.call('winfo', 'screenmmwidth', self._w))
290 def winfo_screenvisual(self):
291 return self.tk.call('winfo', 'screenvisual', self._w)
292 def winfo_screenwidth(self):
293 return self.tk.getint(
294 self.tk.call('winfo', 'screenwidth', self._w))
295 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000296 return self._nametowidget(self.tk.call(
297 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000298 def winfo_visual(self):
299 return self.tk.call('winfo', 'visual', self._w)
300 def winfo_vrootheight(self):
301 return self.tk.getint(
302 self.tk.call('winfo', 'vrootheight', self._w))
303 def winfo_vrootwidth(self):
304 return self.tk.getint(
305 self.tk.call('winfo', 'vrootwidth', self._w))
306 def winfo_vrootx(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'vrootx', self._w))
309 def winfo_vrooty(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'vrooty', self._w))
312 def winfo_width(self):
313 return self.tk.getint(
314 self.tk.call('winfo', 'width', self._w))
315 def winfo_x(self):
316 return self.tk.getint(
317 self.tk.call('winfo', 'x', self._w))
318 def winfo_y(self):
319 return self.tk.getint(
320 self.tk.call('winfo', 'y', self._w))
321 def update(self):
322 self.tk.call('update')
323 def update_idletasks(self):
324 self.tk.call('update', 'idletasks')
Guido van Rossumef8f8811994-08-08 12:47:33 +0000325 def unbind(self, sequence):
326 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +0000327 def bind(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000328 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000329 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000330 self.tk.call('bind', self._w, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000331 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000332 def bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000333 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000334 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000335 self.tk.call('bind', 'all' , sequence,
Guido van Rossumbd84b041994-07-04 10:48:25 +0000336 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000337 def bind_class(self, className, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000338 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000339 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000340 self.tk.call('bind', className , sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000341 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000342 def mainloop(self):
343 self.tk.mainloop()
344 def quit(self):
345 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000346 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000347 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000348 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
349 def _getdoubles(self, string):
350 if not string: return None
351 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000352 def _getboolean(self, string):
353 if string:
354 return self.tk.getboolean(string)
Guido van Rossum18468821994-06-20 07:49:28 +0000355 def _options(self, cnf):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000356 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000357 res = ()
358 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000359 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000360 v = self._register(v)
361 res = res + ('-'+k, v)
362 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000363 def _nametowidget(self, name):
364 w = self
365 if name[0] == '.':
366 w = w._root()
367 name = name[1:]
368 from string import find
369 while name:
370 i = find(name, '.')
371 if i >= 0:
372 name, tail = name[:i], name[i+1:]
373 else:
374 tail = ''
375 w = w.children[name]
376 name = tail
377 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000378 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000379 f = _CallSafely(func, subst).__call__
380 name = `id(f)`
381 if hasattr(func, 'im_func'):
382 func = func.im_func
383 if hasattr(func, 'func_name') and \
384 type(func.func_name) == type(''):
385 name = name + func.func_name
386 self.tk.createcommand(name, f)
387 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000388 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000389 def _root(self):
390 w = self
391 while w.master: w = w.master
392 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000393 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000394 '%s', '%t', '%w', '%x', '%y',
395 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
396 def _substitute(self, *args):
397 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000398 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000399 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
400 # Missing: (a, c, d, m, o, v, B, R)
401 e = Event()
402 e.serial = tk.getint(nsign)
403 e.num = tk.getint(b)
404 try: e.focus = tk.getboolean(f)
405 except TclError: pass
406 e.height = tk.getint(h)
407 e.keycode = tk.getint(k)
408 e.state = tk.getint(s)
409 e.time = tk.getint(t)
410 e.width = tk.getint(w)
411 e.x = tk.getint(x)
412 e.y = tk.getint(y)
413 e.char = A
414 try: e.send_event = tk.getboolean(E)
415 except TclError: pass
416 e.keysym = K
417 e.keysym_num = tk.getint(N)
418 e.type = T
419 e.widget = self._nametowidget(W)
420 e.x_root = tk.getint(X)
421 e.y_root = tk.getint(Y)
422 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000423
424class _CallSafely:
425 def __init__(self, func, subst=None):
426 self.func = func
427 self.subst = subst
428 def __call__(self, *args):
429 if self.subst:
430 args = self.apply_func(self.subst, args)
431 args = self.apply_func(self.func, args)
432 def apply_func(self, func, args):
433 import sys
434 try:
435 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000436 except SystemExit, msg:
437 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000438 except:
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000439 import traceback
440 print "Exception in Tkinter callback"
441 traceback.print_exc()
Guido van Rossum18468821994-06-20 07:49:28 +0000442
443class Wm:
444 def aspect(self,
445 minNumer=None, minDenom=None,
446 maxNumer=None, maxDenom=None):
447 return self._getints(
448 self.tk.call('wm', 'aspect', self._w,
449 minNumer, minDenom,
450 maxNumer, maxDenom))
451 def client(self, name=None):
452 return self.tk.call('wm', 'client', self._w, name)
453 def command(self, value=None):
454 return self.tk.call('wm', 'command', self._w, value)
455 def deiconify(self):
456 return self.tk.call('wm', 'deiconify', self._w)
457 def focusmodel(self, model=None):
458 return self.tk.call('wm', 'focusmodel', self._w, model)
459 def frame(self):
460 return self.tk.call('wm', 'frame', self._w)
461 def geometry(self, newGeometry=None):
462 return self.tk.call('wm', 'geometry', self._w, newGeometry)
463 def grid(self,
464 baseWidht=None, baseHeight=None,
465 widthInc=None, heightInc=None):
466 return self._getints(self.tk.call(
467 'wm', 'grid', self._w,
468 baseWidht, baseHeight, widthInc, heightInc))
469 def group(self, pathName=None):
470 return self.tk.call('wm', 'group', self._w, pathName)
471 def iconbitmap(self, bitmap=None):
472 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
473 def iconify(self):
474 return self.tk.call('wm', 'iconify', self._w)
475 def iconmask(self, bitmap=None):
476 return self.tk.call('wm', 'iconmask', self._w, bitmap)
477 def iconname(self, newName=None):
478 return self.tk.call('wm', 'iconname', self._w, newName)
479 def iconposition(self, x=None, y=None):
480 return self._getints(self.tk.call(
481 'wm', 'iconposition', self._w, x, y))
482 def iconwindow(self, pathName=None):
483 return self.tk.call('wm', 'iconwindow', self._w, pathName)
484 def maxsize(self, width=None, height=None):
485 return self._getints(self.tk.call(
486 'wm', 'maxsize', self._w, width, height))
487 def minsize(self, width=None, height=None):
488 return self._getints(self.tk.call(
489 'wm', 'minsize', self._w, width, height))
490 def overrideredirect(self, boolean=None):
491 return self._getboolean(self.tk.call(
492 'wm', 'overrideredirect', self._w, boolean))
493 def positionfrom(self, who=None):
494 return self.tk.call('wm', 'positionfrom', self._w, who)
495 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000496 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000497 command = self._register(func)
498 else:
499 command = func
500 return self.tk.call(
501 'wm', 'protocol', self._w, name, command)
502 def sizefrom(self, who=None):
503 return self.tk.call('wm', 'sizefrom', self._w, who)
504 def state(self):
505 return self.tk.call('wm', 'state', self._w)
506 def title(self, string=None):
507 return self.tk.call('wm', 'title', self._w, string)
508 def transient(self, master=None):
509 return self.tk.call('wm', 'transient', self._w, master)
510 def withdraw(self):
511 return self.tk.call('wm', 'withdraw', self._w)
512
513class Tk(Misc, Wm):
514 _w = '.'
515 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000516 self.master = None
517 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000518 if baseName is None:
519 import sys, os
520 baseName = os.path.basename(sys.argv[0])
521 if baseName[-3:] == '.py': baseName = baseName[:-3]
522 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000523 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000524 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000525 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000526 def destroy(self):
527 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000528 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000529 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000530 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000531 def readprofile(self, baseName, className):
532 import os
533 if os.environ.has_key('HOME'): home = os.environ['HOME']
534 else: home = os.curdir
535 class_tcl = os.path.join(home, '.%s.tcl' % className)
536 class_py = os.path.join(home, '.%s.py' % className)
537 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
538 base_py = os.path.join(home, '.%s.py' % baseName)
539 dir = {'self': self}
540 exec 'from Tkinter import *' in dir
541 if os.path.isfile(class_tcl):
542 print 'source', `class_tcl`
543 self.tk.call('source', class_tcl)
544 if os.path.isfile(class_py):
545 print 'execfile', `class_py`
546 execfile(class_py, dir)
547 if os.path.isfile(base_tcl):
548 print 'source', `base_tcl`
549 self.tk.call('source', base_tcl)
550 if os.path.isfile(base_py):
551 print 'execfile', `base_py`
552 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000553
554class Pack:
555 def config(self, cnf={}):
556 apply(self.tk.call,
557 ('pack', 'configure', self._w)
558 + self._options(cnf))
559 pack = config
560 def __setitem__(self, key, value):
561 Pack.config({key: value})
562 def forget(self):
563 self.tk.call('pack', 'forget', self._w)
564 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000565 words = self.tk.splitlist(
566 self.tk.call('pack', 'newinfo', self._w))
567 dict = {}
568 for i in range(0, len(words), 2):
569 key = words[i][1:]
570 value = words[i+1]
571 if value[0] == '.':
572 value = self._nametowidget(value)
573 dict[key] = value
574 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000575 info = newinfo
Guido van Rossum5505d561994-12-30 17:16:35 +0000576 _noarg_ = ['_noarg_']
577 def propagate(self, flag=_noarg_):
578 if boolean is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000579 return self._getboolean(self.tk.call(
580 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000581 else:
582 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum18468821994-06-20 07:49:28 +0000583 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000584 return map(self._nametowidget,
585 self.tk.splitlist(
586 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000587
588class Place:
589 def config(self, cnf={}):
590 apply(self.tk.call,
591 ('place', 'configure', self._w)
592 + self._options(cnf))
593 place = config
594 def __setitem__(self, key, value):
595 Place.config({key: value})
596 def forget(self):
597 self.tk.call('place', 'forget', self._w)
598 def info(self):
599 return self.tk.call('place', 'info', self._w)
600 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000601 return map(self._nametowidget,
602 self.tk.splitlist(
603 self.tk.call(
604 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000605
Guido van Rossum18468821994-06-20 07:49:28 +0000606class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000607 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000608 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000609 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000610 if not _default_root:
611 _default_root = Tk()
612 master = _default_root
613 if not _default_root:
614 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000615 self.master = master
616 self.tk = master.tk
617 if cnf.has_key('name'):
618 name = cnf['name']
619 del cnf['name']
620 else:
621 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000622 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000623 if master._w=='.':
624 self._w = '.' + name
625 else:
626 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000627 self.children = {}
628 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000629 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000630 self.master.children[self._name] = self
631 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000632 cnf = _cnfmerge(cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000633 Widget._setup(self, master, cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000634 self.widgetName = widgetName
635 apply(self.tk.call, (widgetName, self._w) + extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000636 if cnf:
637 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000638 def config(self, cnf=None):
Guido van Rossum2a390311994-07-06 10:20:11 +0000639 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000640 if cnf is None:
641 cnf = {}
642 for x in self.tk.split(
643 self.tk.call(self._w, 'configure')):
644 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
645 return cnf
646 if type(cnf) == StringType:
647 x = self.tk.split(self.tk.call(
648 self._w, 'configure', '-'+cnf))
649 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000650 for k in cnf.keys():
651 if type(k) == ClassType:
652 k.config(self, cnf[k])
653 del cnf[k]
654 apply(self.tk.call, (self._w, 'configure')
655 + self._options(cnf))
656 def __getitem__(self, key):
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000657 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000658 self._w, 'configure', '-' + key))
659 return v[4]
660 def __setitem__(self, key, value):
661 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000662 def keys(self):
663 return map(lambda x: x[0][1:],
664 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000665 def __str__(self):
666 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000667 def destroy(self):
668 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000669 if self.master.children.has_key(self._name):
670 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000671 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000672 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000673 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000674
675class Toplevel(Widget, Wm):
676 def __init__(self, master=None, cnf={}):
677 extra = ()
678 if cnf.has_key('screen'):
679 extra = ('-screen', cnf['screen'])
680 del cnf['screen']
681 if cnf.has_key('class'):
682 extra = extra + ('-class', cnf['class'])
683 del cnf['class']
684 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000685 root = self._root()
686 self.iconname(root.iconname())
687 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000688
689class Button(Widget):
690 def __init__(self, master=None, cnf={}):
691 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000692 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000693 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000694 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000695 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000696 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000697 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000698 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000699 self.tk.call('tk_butUp', self._w)
700 def flash(self):
701 self.tk.call(self._w, 'flash')
702 def invoke(self):
703 self.tk.call(self._w, 'invoke')
704
705# Indices:
706def AtEnd():
707 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000708def AtInsert(*args):
709 s = 'insert'
710 for a in args:
711 if a: s = s + (' ' + a)
712 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000713def AtSelFirst():
714 return 'sel.first'
715def AtSelLast():
716 return 'sel.last'
717def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000718 if y is None:
719 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000720 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000721 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000722
723class Canvas(Widget):
724 def __init__(self, master=None, cnf={}):
725 Widget.__init__(self, master, 'canvas', cnf)
726 def addtag(self, *args):
727 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000728 def addtag_above(self, tagOrId):
729 self.addtag('above', tagOrId)
730 def addtag_all(self):
731 self.addtag('all')
732 def addtag_below(self, tagOrId):
733 self.addtag('below', tagOrId)
734 def addtag_closest(self, x, y, halo=None, start=None):
735 self.addtag('closest', x, y, halo, start)
736 def addtag_enclosed(self, x1, y1, x2, y2):
737 self.addtag('enclosed', x1, y1, x2, y2)
738 def addtag_overlapping(self, x1, y1, x2, y2):
739 self.addtag('overlapping', x1, y1, x2, y2)
740 def addtag_withtag(self, tagOrId):
741 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000742 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000743 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000744 def tag_unbind(self, tagOrId, sequence):
745 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
746 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000747 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000748 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000749 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000750 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000751 def canvasx(self, screenx, gridspacing=None):
752 return self.tk.getint(self.tk.call(
753 self._w, 'canvasx', screenx, gridspacing))
754 def canvasy(self, screeny, gridspacing=None):
755 return self.tk.getint(self.tk.call(
756 self._w, 'canvasy', screeny, gridspacing))
757 def coords(self, *args):
758 return self._do('coords', args)
759 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000760 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000761 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000762 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000763 args = args[:-1]
764 else:
765 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000766 return self.tk.getint(apply(
767 self.tk.call,
768 (self._w, 'create', itemType)
769 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000770 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000771 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000772 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000773 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000774 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000775 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000776 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000777 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000778 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000779 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000780 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000781 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000782 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000783 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000784 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000785 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000786 def dchars(self, *args):
787 self._do('dchars', args)
788 def delete(self, *args):
789 self._do('delete', args)
790 def dtag(self, *args):
791 self._do('dtag', args)
792 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000793 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000794 def find_above(self, tagOrId):
795 return self.find('above', tagOrId)
796 def find_all(self):
797 return self.find('all')
798 def find_below(self, tagOrId):
799 return self.find('below', tagOrId)
800 def find_closest(self, x, y, halo=None, start=None):
801 return self.find('closest', x, y, halo, start)
802 def find_enclosed(self, x1, y1, x2, y2):
803 return self.find('enclosed', x1, y1, x2, y2)
804 def find_overlapping(self, x1, y1, x2, y2):
805 return self.find('overlapping', x1, y1, x2, y2)
806 def find_withtag(self, tagOrId):
807 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000808 def focus(self, *args):
809 return self._do('focus', args)
810 def gettags(self, *args):
811 return self.tk.splitlist(self._do('gettags', args))
812 def icursor(self, *args):
813 self._do('icursor', args)
814 def index(self, *args):
815 return self.tk.getint(self._do('index', args))
816 def insert(self, *args):
817 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000818 def itemconfig(self, tagOrId, cnf=None):
819 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000820 cnf = {}
821 for x in self.tk.split(
822 self._do('itemconfigure', (tagOrId))):
823 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
824 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000825 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000826 x = self.tk.split(self._do('itemconfigure',
827 (tagOrId, '-'+cnf,)))
828 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000829 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
830 def lower(self, *args):
831 self._do('lower', args)
832 def move(self, *args):
833 self._do('move', args)
834 def postscript(self, cnf={}):
835 return self._do('postscript', self._options(cnf))
836 def tkraise(self, *args):
837 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000838 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000839 def scale(self, *args):
840 self._do('scale', args)
841 def scan_mark(self, x, y):
842 self.tk.call(self._w, 'scan', 'mark', x, y)
843 def scan_dragto(self, x, y):
844 self.tk.call(self._w, 'scan', 'dragto', x, y)
845 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000846 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000847 def select_clear(self):
848 self.tk.call(self._w, 'select', 'clear')
849 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000850 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000851 def select_item(self):
852 self.tk.call(self._w, 'select', 'item')
853 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000854 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000855 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000856 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000857 def xview(self, index):
858 self.tk.call(self._w, 'xview', index)
859 def yview(self, index):
860 self.tk.call(self._w, 'yview', index)
861
862class Checkbutton(Widget):
863 def __init__(self, master=None, cnf={}):
864 Widget.__init__(self, master, 'checkbutton', cnf)
865 def deselect(self):
866 self.tk.call(self._w, 'deselect')
867 def flash(self):
868 self.tk.call(self._w, 'flash')
869 def invoke(self):
870 self.tk.call(self._w, 'invoke')
871 def select(self):
872 self.tk.call(self._w, 'select')
873 def toggle(self):
874 self.tk.call(self._w, 'toggle')
875
876class Entry(Widget):
877 def __init__(self, master=None, cnf={}):
878 Widget.__init__(self, master, 'entry', cnf)
879 def tk_entryBackspace(self):
880 self.tk.call('tk_entryBackspace', self._w)
881 def tk_entryBackword(self):
882 self.tk.call('tk_entryBackword', self._w)
883 def tk_entrySeeCaret(self):
884 self.tk.call('tk_entrySeeCaret', self._w)
885 def delete(self, first, last=None):
886 self.tk.call(self._w, 'delete', first, last)
887 def get(self):
888 return self.tk.call(self._w, 'get')
889 def icursor(self, index):
890 self.tk.call(self._w, 'icursor', index)
891 def index(self, index):
892 return self.tk.getint(self.tk.call(
893 self._w, 'index', index))
894 def insert(self, index, string):
895 self.tk.call(self._w, 'insert', index, string)
896 def scan_mark(self, x):
897 self.tk.call(self._w, 'scan', 'mark', x)
898 def scan_dragto(self, x):
899 self.tk.call(self._w, 'scan', 'dragto', x)
900 def select_adjust(self, index):
901 self.tk.call(self._w, 'select', 'adjust', index)
902 def select_clear(self):
903 self.tk.call(self._w, 'select', 'clear')
904 def select_from(self, index):
905 self.tk.call(self._w, 'select', 'from', index)
906 def select_to(self, index):
907 self.tk.call(self._w, 'select', 'to', index)
908 def select_view(self, index):
909 self.tk.call(self._w, 'select', 'view', index)
910
911class Frame(Widget):
912 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000913 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000914 extra = ()
915 if cnf.has_key('class'):
916 extra = ('-class', cnf['class'])
917 del cnf['class']
918 Widget.__init__(self, master, 'frame', cnf, extra)
919 def tk_menuBar(self, *args):
920 apply(self.tk.call, ('tk_menuBar', self._w) + args)
921
922class Label(Widget):
923 def __init__(self, master=None, cnf={}):
924 Widget.__init__(self, master, 'label', cnf)
925
926class Listbox(Widget):
927 def __init__(self, master=None, cnf={}):
928 Widget.__init__(self, master, 'listbox', cnf)
929 def tk_listboxSingleSelect(self):
930 self.tk.call('tk_listboxSingleSelect', self._w)
931 def curselection(self):
932 return self.tk.splitlist(self.tk.call(
933 self._w, 'curselection'))
934 def delete(self, first, last=None):
935 self.tk.call(self._w, 'delete', first, last)
936 def get(self, index):
937 return self.tk.call(self._w, 'get', index)
938 def insert(self, index, *elements):
939 apply(self.tk.call,
940 (self._w, 'insert', index) + elements)
941 def nearest(self, y):
942 return self.tk.getint(self.tk.call(
943 self._w, 'nearest', y))
944 def scan_mark(self, x, y):
945 self.tk.call(self._w, 'scan', 'mark', x, y)
946 def scan_dragto(self, x, y):
947 self.tk.call(self._w, 'scan', 'dragto', x, y)
948 def select_adjust(self, index):
949 self.tk.call(self._w, 'select', 'adjust', index)
950 def select_clear(self):
951 self.tk.call(self._w, 'select', 'clear')
952 def select_from(self, index):
953 self.tk.call(self._w, 'select', 'from', index)
954 def select_to(self, index):
955 self.tk.call(self._w, 'select', 'to', index)
956 def size(self):
957 return self.tk.getint(self.tk.call(self._w, 'size'))
958 def xview(self, index):
959 self.tk.call(self._w, 'xview', index)
960 def yview(self, index):
961 self.tk.call(self._w, 'yview', index)
962
963class Menu(Widget):
964 def __init__(self, master=None, cnf={}):
965 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000966 def tk_bindForTraversal(self):
967 self.tk.call('tk_bindForTraversal', self._w)
968 def tk_mbPost(self):
969 self.tk.call('tk_mbPost', self._w)
970 def tk_mbUnpost(self):
971 self.tk.call('tk_mbUnpost')
972 def tk_traverseToMenu(self, char):
973 self.tk.call('tk_traverseToMenu', self._w, char)
974 def tk_traverseWithinMenu(self, char):
975 self.tk.call('tk_traverseWithinMenu', self._w, char)
976 def tk_getMenuButtons(self):
977 return self.tk.call('tk_getMenuButtons', self._w)
978 def tk_nextMenu(self, count):
979 self.tk.call('tk_nextMenu', count)
980 def tk_nextMenuEntry(self, count):
981 self.tk.call('tk_nextMenuEntry', count)
982 def tk_invokeMenu(self):
983 self.tk.call('tk_invokeMenu', self._w)
984 def tk_firstMenu(self):
985 self.tk.call('tk_firstMenu', self._w)
986 def tk_mbButtonDown(self):
987 self.tk.call('tk_mbButtonDown', self._w)
988 def activate(self, index):
989 self.tk.call(self._w, 'activate', index)
990 def add(self, itemType, cnf={}):
991 apply(self.tk.call, (self._w, 'add', itemType)
992 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000993 def add_cascade(self, cnf={}):
994 self.add('cascade', cnf)
995 def add_checkbutton(self, cnf={}):
996 self.add('checkbutton', cnf)
997 def add_command(self, cnf={}):
998 self.add('command', cnf)
999 def add_radiobutton(self, cnf={}):
1000 self.add('radiobutton', cnf)
1001 def add_separator(self, cnf={}):
1002 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001003 def delete(self, index1, index2=None):
1004 self.tk.call(self._w, 'delete', index1, index2)
1005 def entryconfig(self, index, cnf={}):
1006 apply(self.tk.call, (self._w, 'entryconfigure', index)
1007 + self._options(cnf))
1008 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001009 i = self.tk.call(self._w, 'index', index)
1010 if i == 'none': return None
1011 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001012 def invoke(self, index):
1013 return self.tk.call(self._w, 'invoke', index)
1014 def post(self, x, y):
1015 self.tk.call(self._w, 'post', x, y)
1016 def unpost(self):
1017 self.tk.call(self._w, 'unpost')
1018 def yposition(self, index):
1019 return self.tk.getint(self.tk.call(
1020 self._w, 'yposition', index))
1021
1022class Menubutton(Widget):
1023 def __init__(self, master=None, cnf={}):
1024 Widget.__init__(self, master, 'menubutton', cnf)
1025
1026class Message(Widget):
1027 def __init__(self, master=None, cnf={}):
1028 Widget.__init__(self, master, 'message', cnf)
1029
1030class Radiobutton(Widget):
1031 def __init__(self, master=None, cnf={}):
1032 Widget.__init__(self, master, 'radiobutton', cnf)
1033 def deselect(self):
1034 self.tk.call(self._w, 'deselect')
1035 def flash(self):
1036 self.tk.call(self._w, 'flash')
1037 def invoke(self):
1038 self.tk.call(self._w, 'invoke')
1039 def select(self):
1040 self.tk.call(self._w, 'select')
1041
1042class Scale(Widget):
1043 def __init__(self, master=None, cnf={}):
1044 Widget.__init__(self, master, 'scale', cnf)
1045 def get(self):
1046 return self.tk.getint(self.tk.call(self._w, 'get'))
1047 def set(self, value):
1048 self.tk.call(self._w, 'set', value)
1049
1050class Scrollbar(Widget):
1051 def __init__(self, master=None, cnf={}):
1052 Widget.__init__(self, master, 'scrollbar', cnf)
1053 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001054 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +00001055 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1056 self.tk.call(self._w, 'set',
1057 totalUnits, windowUnits, firstUnit, lastUnit)
1058
1059class Text(Widget):
1060 def __init__(self, master=None, cnf={}):
1061 Widget.__init__(self, master, 'text', cnf)
1062 def tk_textSelectTo(self, index):
1063 self.tk.call('tk_textSelectTo', self._w, index)
1064 def tk_textBackspace(self):
1065 self.tk.call('tk_textBackspace', self._w)
1066 def tk_textIndexCloser(self, a, b, c):
1067 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1068 def tk_textResetAnchor(self, index):
1069 self.tk.call('tk_textResetAnchor', self._w, index)
1070 def compare(self, index1, op, index2):
1071 return self.tk.getboolean(self.tk.call(
1072 self._w, 'compare', index1, op, index2))
1073 def debug(self, boolean=None):
1074 return self.tk.getboolean(self.tk.call(
1075 self._w, 'debug', boolean))
1076 def delete(self, index1, index2=None):
1077 self.tk.call(self._w, 'delete', index1, index2)
1078 def get(self, index1, index2=None):
1079 return self.tk.call(self._w, 'get', index1, index2)
1080 def index(self, index):
1081 return self.tk.call(self._w, 'index', index)
1082 def insert(self, index, chars):
1083 self.tk.call(self._w, 'insert', index, chars)
1084 def mark_names(self):
1085 return self.tk.splitlist(self.tk.call(
1086 self._w, 'mark', 'names'))
1087 def mark_set(self, markName, index):
1088 self.tk.call(self._w, 'mark', 'set', markName, index)
1089 def mark_unset(self, markNames):
1090 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1091 def scan_mark(self, y):
1092 self.tk.call(self._w, 'scan', 'mark', y)
1093 def scan_dragto(self, y):
1094 self.tk.call(self._w, 'scan', 'dragto', y)
1095 def tag_add(self, tagName, index1, index2=None):
1096 self.tk.call(
1097 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001098 def tag_unbind(self, tagName, sequence):
1099 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001100 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001101 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001102 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001103 self.tk.call(self._w, 'tag', 'bind',
1104 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001105 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001106 def tag_config(self, tagName, cnf={}):
1107 apply(self.tk.call,
1108 (self._w, 'tag', 'configure', tagName)
1109 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001110 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001111 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001112 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001113 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001114 def tag_names(self, index=None):
1115 return self.tk.splitlist(
1116 self.tk.call(self._w, 'tag', 'names', index))
1117 def tag_nextrange(self, tagName, index1, index2=None):
1118 return self.tk.splitlist(self.tk.call(
1119 self._w, 'tag', 'nextrange', index1, index2))
1120 def tag_raise(self, tagName, aboveThis=None):
1121 self.tk.call(
1122 self._w, 'tag', 'raise', tagName, aboveThis)
1123 def tag_ranges(self, tagName):
1124 return self.tk.splitlist(self.tk.call(
1125 self._w, 'tag', 'ranges', tagName))
1126 def tag_remove(self, tagName, index1, index2=None):
1127 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001128 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum18468821994-06-20 07:49:28 +00001129 def yview(self, what):
1130 self.tk.call(self._w, 'yview', what)
1131 def yview_pickplace(self, what):
1132 self.tk.call(self._w, 'yview', '-pickplace', what)
1133
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001134######################################################################
1135# Extensions:
1136
1137class Studbutton(Button):
1138 def __init__(self, master=None, cnf={}):
1139 Widget.__init__(self, master, 'studbutton', cnf)
1140 self.bind('<Any-Enter>', self.tk_butEnter)
1141 self.bind('<Any-Leave>', self.tk_butLeave)
1142 self.bind('<1>', self.tk_butDown)
1143 self.bind('<ButtonRelease-1>', self.tk_butUp)
1144
1145class Tributton(Button):
1146 def __init__(self, master=None, cnf={}):
1147 Widget.__init__(self, master, 'tributton', cnf)
1148 self.bind('<Any-Enter>', self.tk_butEnter)
1149 self.bind('<Any-Leave>', self.tk_butLeave)
1150 self.bind('<1>', self.tk_butDown)
1151 self.bind('<ButtonRelease-1>', self.tk_butUp)
1152