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