blob: de081ecf1bed49c4cf8cdae4185e95f90c6fc6a8 [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)
Guido van Rossum780044f1994-10-20 22:02:27 +0000184 def option_add(self, pattern, value, priority = None):
185 self.tk.call('option', 'add', pattern, priority)
186 def option_clear(self):
187 self.tk.call('option', 'clear')
188 def option_get(self, name, className):
189 return self.tk.call('option', 'get', self._w, name, className)
190 def option_readfile(self, fileName, priority = None):
191 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000192 def selection_clear(self):
193 self.tk.call('selection', 'clear', self._w)
194 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000195 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000196 def selection_handle(self, func, type=None, format=None):
197 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000198 self.tk.call('selection', 'handle', self._w,
199 name, type, format)
200 def selection_own(self, func=None):
201 name = self._register(func)
202 self.tk.call('selection', 'own', self._w, name)
203 def selection_own_get(self):
204 return self._nametowidget(self.tk.call('selection', 'own'))
205 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000206 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000207 def lower(self, belowThis=None):
208 self.tk.call('lift', self._w, belowThis)
209 def tkraise(self, aboveThis=None):
210 self.tk.call('raise', self._w, aboveThis)
211 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000212 def colormodel(self, value=None):
213 return self.tk.call('tk', 'colormodel', self._w, value)
214 def winfo_atom(self, name):
215 return self.tk.getint(self.tk.call('winfo', 'atom', name))
216 def winfo_atomname(self, id):
217 return self.tk.call('winfo', 'atomname', id)
218 def winfo_cells(self):
219 return self.tk.getint(
220 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000221 def winfo_children(self):
222 return map(self._nametowidget,
223 self.tk.splitlist(self.tk.call(
224 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000225 def winfo_class(self):
226 return self.tk.call('winfo', 'class', self._w)
227 def winfo_containing(self, rootX, rootY):
228 return self.tk.call('winfo', 'containing', rootx, rootY)
229 def winfo_depth(self):
230 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
231 def winfo_exists(self):
232 return self.tk.getint(
233 self.tk.call('winfo', 'exists', self._w))
234 def winfo_fpixels(self, number):
235 return self.tk.getdouble(self.tk.call(
236 'winfo', 'fpixels', self._w, number))
237 def winfo_geometry(self):
238 return self.tk.call('winfo', 'geometry', self._w)
239 def winfo_height(self):
240 return self.tk.getint(
241 self.tk.call('winfo', 'height', self._w))
242 def winfo_id(self):
243 return self.tk.getint(
244 self.tk.call('winfo', 'id', self._w))
245 def winfo_interps(self):
246 return self.tk.splitlist(
247 self.tk.call('winfo', 'interps'))
248 def winfo_ismapped(self):
249 return self.tk.getint(
250 self.tk.call('winfo', 'ismapped', self._w))
251 def winfo_name(self):
252 return self.tk.call('winfo', 'name', self._w)
253 def winfo_parent(self):
254 return self.tk.call('winfo', 'parent', self._w)
255 def winfo_pathname(self, id):
256 return self.tk.call('winfo', 'pathname', id)
257 def winfo_pixels(self, number):
258 return self.tk.getint(
259 self.tk.call('winfo', 'pixels', self._w, number))
260 def winfo_reqheight(self):
261 return self.tk.getint(
262 self.tk.call('winfo', 'reqheight', self._w))
263 def winfo_reqwidth(self):
264 return self.tk.getint(
265 self.tk.call('winfo', 'reqwidth', self._w))
266 def winfo_rgb(self, color):
267 return self._getints(
268 self.tk.call('winfo', 'rgb', self._w, color))
269 def winfo_rootx(self):
270 return self.tk.getint(
271 self.tk.call('winfo', 'rootx', self._w))
272 def winfo_rooty(self):
273 return self.tk.getint(
274 self.tk.call('winfo', 'rooty', self._w))
275 def winfo_screen(self):
276 return self.tk.call('winfo', 'screen', self._w)
277 def winfo_screencells(self):
278 return self.tk.getint(
279 self.tk.call('winfo', 'screencells', self._w))
280 def winfo_screendepth(self):
281 return self.tk.getint(
282 self.tk.call('winfo', 'screendepth', self._w))
283 def winfo_screenheight(self):
284 return self.tk.getint(
285 self.tk.call('winfo', 'screenheight', self._w))
286 def winfo_screenmmheight(self):
287 return self.tk.getint(
288 self.tk.call('winfo', 'screenmmheight', self._w))
289 def winfo_screenmmwidth(self):
290 return self.tk.getint(
291 self.tk.call('winfo', 'screenmmwidth', self._w))
292 def winfo_screenvisual(self):
293 return self.tk.call('winfo', 'screenvisual', self._w)
294 def winfo_screenwidth(self):
295 return self.tk.getint(
296 self.tk.call('winfo', 'screenwidth', self._w))
297 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000298 return self._nametowidget(self.tk.call(
299 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000300 def winfo_visual(self):
301 return self.tk.call('winfo', 'visual', self._w)
302 def winfo_vrootheight(self):
303 return self.tk.getint(
304 self.tk.call('winfo', 'vrootheight', self._w))
305 def winfo_vrootwidth(self):
306 return self.tk.getint(
307 self.tk.call('winfo', 'vrootwidth', self._w))
308 def winfo_vrootx(self):
309 return self.tk.getint(
310 self.tk.call('winfo', 'vrootx', self._w))
311 def winfo_vrooty(self):
312 return self.tk.getint(
313 self.tk.call('winfo', 'vrooty', self._w))
314 def winfo_width(self):
315 return self.tk.getint(
316 self.tk.call('winfo', 'width', self._w))
317 def winfo_x(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'x', self._w))
320 def winfo_y(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'y', self._w))
323 def update(self):
324 self.tk.call('update')
325 def update_idletasks(self):
326 self.tk.call('update', 'idletasks')
Guido van Rossumef8f8811994-08-08 12:47:33 +0000327 def unbind(self, sequence):
328 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +0000329 def bind(self, 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', self._w, 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 bind_all(self, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000335 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000336 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000337 self.tk.call('bind', 'all' , sequence,
Guido van Rossumbd84b041994-07-04 10:48:25 +0000338 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000339 def bind_class(self, className, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000340 if add: add = '+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000341 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000342 self.tk.call('bind', className , sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000343 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000344 def mainloop(self):
345 self.tk.mainloop()
346 def quit(self):
347 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000348 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000349 if not string: return None
350 res = ()
351 for v in self.tk.splitlist(string):
352 res = res + (self.tk.getint(v),)
353 return res
Guido van Rossum18468821994-06-20 07:49:28 +0000354 def _getboolean(self, string):
355 if string:
356 return self.tk.getboolean(string)
Guido van Rossum18468821994-06-20 07:49:28 +0000357 def _options(self, cnf):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000358 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000359 res = ()
360 for k, v in cnf.items():
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000361 if type(v) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000362 v = self._register(v)
363 res = res + ('-'+k, v)
364 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000365 def _nametowidget(self, name):
366 w = self
367 if name[0] == '.':
368 w = w._root()
369 name = name[1:]
370 from string import find
371 while name:
372 i = find(name, '.')
373 if i >= 0:
374 name, tail = name[:i], name[i+1:]
375 else:
376 tail = ''
377 w = w.children[name]
378 name = tail
379 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000380 def _register(self, func, subst=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000381 f = _CallSafely(func, subst).__call__
382 name = `id(f)`
383 if hasattr(func, 'im_func'):
384 func = func.im_func
385 if hasattr(func, 'func_name') and \
386 type(func.func_name) == type(''):
387 name = name + func.func_name
388 self.tk.createcommand(name, f)
389 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000390 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000391 def _root(self):
392 w = self
393 while w.master: w = w.master
394 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000395 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000396 '%s', '%t', '%w', '%x', '%y',
397 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
398 def _substitute(self, *args):
399 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000400 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000401 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
402 # Missing: (a, c, d, m, o, v, B, R)
403 e = Event()
404 e.serial = tk.getint(nsign)
405 e.num = tk.getint(b)
406 try: e.focus = tk.getboolean(f)
407 except TclError: pass
408 e.height = tk.getint(h)
409 e.keycode = tk.getint(k)
410 e.state = tk.getint(s)
411 e.time = tk.getint(t)
412 e.width = tk.getint(w)
413 e.x = tk.getint(x)
414 e.y = tk.getint(y)
415 e.char = A
416 try: e.send_event = tk.getboolean(E)
417 except TclError: pass
418 e.keysym = K
419 e.keysym_num = tk.getint(N)
420 e.type = T
421 e.widget = self._nametowidget(W)
422 e.x_root = tk.getint(X)
423 e.y_root = tk.getint(Y)
424 return (e,)
Guido van Rossum18468821994-06-20 07:49:28 +0000425
426class _CallSafely:
427 def __init__(self, func, subst=None):
428 self.func = func
429 self.subst = subst
430 def __call__(self, *args):
431 if self.subst:
432 args = self.apply_func(self.subst, args)
433 args = self.apply_func(self.func, args)
434 def apply_func(self, func, args):
435 import sys
436 try:
437 return apply(func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000438 except SystemExit, msg:
439 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000440 except:
441 try:
442 try:
443 t = sys.exc_traceback
444 while t:
445 sys.stderr.write(
446 ' %s, line %s\n' %
447 (t.tb_frame.f_code,
448 t.tb_lineno))
449 t = t.tb_next
450 finally:
451 sys.stderr.write('%s: %s\n' %
452 (sys.exc_type,
453 sys.exc_value))
Guido van Rossum45853db1994-06-20 12:19:19 +0000454 (sys.last_type,
455 sys.last_value,
456 sys.last_traceback) = (sys.exc_type,
457 sys.exc_value,
458 sys.exc_traceback)
459 import pdb
460 pdb.pm()
Guido van Rossum18468821994-06-20 07:49:28 +0000461 except:
462 print '*** Error in error handling ***'
463 print sys.exc_type, ':', sys.exc_value
464
465class Wm:
466 def aspect(self,
467 minNumer=None, minDenom=None,
468 maxNumer=None, maxDenom=None):
469 return self._getints(
470 self.tk.call('wm', 'aspect', self._w,
471 minNumer, minDenom,
472 maxNumer, maxDenom))
473 def client(self, name=None):
474 return self.tk.call('wm', 'client', self._w, name)
475 def command(self, value=None):
476 return self.tk.call('wm', 'command', self._w, value)
477 def deiconify(self):
478 return self.tk.call('wm', 'deiconify', self._w)
479 def focusmodel(self, model=None):
480 return self.tk.call('wm', 'focusmodel', self._w, model)
481 def frame(self):
482 return self.tk.call('wm', 'frame', self._w)
483 def geometry(self, newGeometry=None):
484 return self.tk.call('wm', 'geometry', self._w, newGeometry)
485 def grid(self,
486 baseWidht=None, baseHeight=None,
487 widthInc=None, heightInc=None):
488 return self._getints(self.tk.call(
489 'wm', 'grid', self._w,
490 baseWidht, baseHeight, widthInc, heightInc))
491 def group(self, pathName=None):
492 return self.tk.call('wm', 'group', self._w, pathName)
493 def iconbitmap(self, bitmap=None):
494 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
495 def iconify(self):
496 return self.tk.call('wm', 'iconify', self._w)
497 def iconmask(self, bitmap=None):
498 return self.tk.call('wm', 'iconmask', self._w, bitmap)
499 def iconname(self, newName=None):
500 return self.tk.call('wm', 'iconname', self._w, newName)
501 def iconposition(self, x=None, y=None):
502 return self._getints(self.tk.call(
503 'wm', 'iconposition', self._w, x, y))
504 def iconwindow(self, pathName=None):
505 return self.tk.call('wm', 'iconwindow', self._w, pathName)
506 def maxsize(self, width=None, height=None):
507 return self._getints(self.tk.call(
508 'wm', 'maxsize', self._w, width, height))
509 def minsize(self, width=None, height=None):
510 return self._getints(self.tk.call(
511 'wm', 'minsize', self._w, width, height))
512 def overrideredirect(self, boolean=None):
513 return self._getboolean(self.tk.call(
514 'wm', 'overrideredirect', self._w, boolean))
515 def positionfrom(self, who=None):
516 return self.tk.call('wm', 'positionfrom', self._w, who)
517 def protocol(self, name=None, func=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000518 if type(func) in CallableTypes:
Guido van Rossum18468821994-06-20 07:49:28 +0000519 command = self._register(func)
520 else:
521 command = func
522 return self.tk.call(
523 'wm', 'protocol', self._w, name, command)
524 def sizefrom(self, who=None):
525 return self.tk.call('wm', 'sizefrom', self._w, who)
526 def state(self):
527 return self.tk.call('wm', 'state', self._w)
528 def title(self, string=None):
529 return self.tk.call('wm', 'title', self._w, string)
530 def transient(self, master=None):
531 return self.tk.call('wm', 'transient', self._w, master)
532 def withdraw(self):
533 return self.tk.call('wm', 'withdraw', self._w)
534
535class Tk(Misc, Wm):
536 _w = '.'
537 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum45853db1994-06-20 12:19:19 +0000538 self.master = None
539 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000540 if baseName is None:
541 import sys, os
542 baseName = os.path.basename(sys.argv[0])
543 if baseName[-3:] == '.py': baseName = baseName[:-3]
544 self.tk = tkinter.create(screenName, baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000545 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000546 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000547 self.readprofile(baseName, className)
Guido van Rossum45853db1994-06-20 12:19:19 +0000548 def destroy(self):
549 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000550 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000551 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000552 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000553 def readprofile(self, baseName, className):
554 import os
555 if os.environ.has_key('HOME'): home = os.environ['HOME']
556 else: home = os.curdir
557 class_tcl = os.path.join(home, '.%s.tcl' % className)
558 class_py = os.path.join(home, '.%s.py' % className)
559 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
560 base_py = os.path.join(home, '.%s.py' % baseName)
561 dir = {'self': self}
562 exec 'from Tkinter import *' in dir
563 if os.path.isfile(class_tcl):
564 print 'source', `class_tcl`
565 self.tk.call('source', class_tcl)
566 if os.path.isfile(class_py):
567 print 'execfile', `class_py`
568 execfile(class_py, dir)
569 if os.path.isfile(base_tcl):
570 print 'source', `base_tcl`
571 self.tk.call('source', base_tcl)
572 if os.path.isfile(base_py):
573 print 'execfile', `base_py`
574 execfile(base_py, dir)
Guido van Rossum18468821994-06-20 07:49:28 +0000575
576class Pack:
577 def config(self, cnf={}):
578 apply(self.tk.call,
579 ('pack', 'configure', self._w)
580 + self._options(cnf))
581 pack = config
582 def __setitem__(self, key, value):
583 Pack.config({key: value})
584 def forget(self):
585 self.tk.call('pack', 'forget', self._w)
586 def newinfo(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000587 words = self.tk.splitlist(
588 self.tk.call('pack', 'newinfo', self._w))
589 dict = {}
590 for i in range(0, len(words), 2):
591 key = words[i][1:]
592 value = words[i+1]
593 if value[0] == '.':
594 value = self._nametowidget(value)
595 dict[key] = value
596 return dict
Guido van Rossum18468821994-06-20 07:49:28 +0000597 info = newinfo
598 def propagate(self, boolean=None):
599 if boolean:
600 self.tk.call('pack', 'propagate', self._w)
601 else:
602 return self._getboolean(self.tk.call(
603 'pack', 'propagate', self._w))
604 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000605 return map(self._nametowidget,
606 self.tk.splitlist(
607 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000608
609class Place:
610 def config(self, cnf={}):
611 apply(self.tk.call,
612 ('place', 'configure', self._w)
613 + self._options(cnf))
614 place = config
615 def __setitem__(self, key, value):
616 Place.config({key: value})
617 def forget(self):
618 self.tk.call('place', 'forget', self._w)
619 def info(self):
620 return self.tk.call('place', 'info', self._w)
621 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000622 return map(self._nametowidget,
623 self.tk.splitlist(
624 self.tk.call(
625 'place', 'slaves', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000626
Guido van Rossum18468821994-06-20 07:49:28 +0000627class Widget(Misc, Pack, Place):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000628 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000629 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000630 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000631 if not _default_root:
632 _default_root = Tk()
633 master = _default_root
634 if not _default_root:
635 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000636 self.master = master
637 self.tk = master.tk
638 if cnf.has_key('name'):
639 name = cnf['name']
640 del cnf['name']
641 else:
642 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000643 self._name = name
Guido van Rossum18468821994-06-20 07:49:28 +0000644 if master._w=='.':
645 self._w = '.' + name
646 else:
647 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000648 self.children = {}
649 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000650 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000651 self.master.children[self._name] = self
652 def __init__(self, master, widgetName, cnf={}, extra=()):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000653 cnf = _cnfmerge(cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000654 Widget._setup(self, master, cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000655 self.widgetName = widgetName
656 apply(self.tk.call, (widgetName, self._w) + extra)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000657 if cnf:
658 Widget.config(self, cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000659 def config(self, cnf=None):
Guido van Rossum2a390311994-07-06 10:20:11 +0000660 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000661 if cnf is None:
662 cnf = {}
663 for x in self.tk.split(
664 self.tk.call(self._w, 'configure')):
665 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
666 return cnf
667 if type(cnf) == StringType:
668 x = self.tk.split(self.tk.call(
669 self._w, 'configure', '-'+cnf))
670 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000671 for k in cnf.keys():
672 if type(k) == ClassType:
673 k.config(self, cnf[k])
674 del cnf[k]
675 apply(self.tk.call, (self._w, 'configure')
676 + self._options(cnf))
677 def __getitem__(self, key):
Guido van Rossum535cf0c1994-06-27 07:55:59 +0000678 v = self.tk.splitlist(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000679 self._w, 'configure', '-' + key))
680 return v[4]
681 def __setitem__(self, key, value):
682 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000683 def keys(self):
684 return map(lambda x: x[0][1:],
685 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000686 def __str__(self):
687 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000688 def destroy(self):
689 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000690 if self.master.children.has_key(self._name):
691 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000692 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000693 def _do(self, name, args=()):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000694 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000695
696class Toplevel(Widget, Wm):
697 def __init__(self, master=None, cnf={}):
698 extra = ()
699 if cnf.has_key('screen'):
700 extra = ('-screen', cnf['screen'])
701 del cnf['screen']
702 if cnf.has_key('class'):
703 extra = extra + ('-class', cnf['class'])
704 del cnf['class']
705 Widget.__init__(self, master, 'toplevel', cnf, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000706 root = self._root()
707 self.iconname(root.iconname())
708 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000709
710class Button(Widget):
711 def __init__(self, master=None, cnf={}):
712 Widget.__init__(self, master, 'button', cnf)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000713 def tk_butEnter(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000714 self.tk.call('tk_butEnter', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000715 def tk_butLeave(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000716 self.tk.call('tk_butLeave', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000717 def tk_butDown(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000718 self.tk.call('tk_butDown', self._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000719 def tk_butUp(self, *dummy):
Guido van Rossum18468821994-06-20 07:49:28 +0000720 self.tk.call('tk_butUp', self._w)
721 def flash(self):
722 self.tk.call(self._w, 'flash')
723 def invoke(self):
724 self.tk.call(self._w, 'invoke')
725
726# Indices:
727def AtEnd():
728 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000729def AtInsert(*args):
730 s = 'insert'
731 for a in args:
732 if a: s = s + (' ' + a)
733 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000734def AtSelFirst():
735 return 'sel.first'
736def AtSelLast():
737 return 'sel.last'
738def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000739 if y is None:
740 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000741 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000742 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000743
744class Canvas(Widget):
745 def __init__(self, master=None, cnf={}):
746 Widget.__init__(self, master, 'canvas', cnf)
747 def addtag(self, *args):
748 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000749 def addtag_above(self, tagOrId):
750 self.addtag('above', tagOrId)
751 def addtag_all(self):
752 self.addtag('all')
753 def addtag_below(self, tagOrId):
754 self.addtag('below', tagOrId)
755 def addtag_closest(self, x, y, halo=None, start=None):
756 self.addtag('closest', x, y, halo, start)
757 def addtag_enclosed(self, x1, y1, x2, y2):
758 self.addtag('enclosed', x1, y1, x2, y2)
759 def addtag_overlapping(self, x1, y1, x2, y2):
760 self.addtag('overlapping', x1, y1, x2, y2)
761 def addtag_withtag(self, tagOrId):
762 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000763 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000764 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000765 def tag_unbind(self, tagOrId, sequence):
766 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
767 def tag_bind(self, tagOrId, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +0000768 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +0000769 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +0000770 self.tk.call(self._w, 'bind', tagOrId, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000771 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +0000772 def canvasx(self, screenx, gridspacing=None):
773 return self.tk.getint(self.tk.call(
774 self._w, 'canvasx', screenx, gridspacing))
775 def canvasy(self, screeny, gridspacing=None):
776 return self.tk.getint(self.tk.call(
777 self._w, 'canvasy', screeny, gridspacing))
778 def coords(self, *args):
779 return self._do('coords', args)
780 def _create(self, itemType, args): # Args: (value, value, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000781 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000782 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000783 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000784 args = args[:-1]
785 else:
786 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000787 return self.tk.getint(apply(
788 self.tk.call,
789 (self._w, 'create', itemType)
790 + args + self._options(cnf)))
Guido van Rossum18468821994-06-20 07:49:28 +0000791 def create_arc(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000792 return Canvas._create(self, 'arc', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000793 def create_bitmap(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000794 return Canvas._create(self, 'bitmap', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000795 def create_line(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000796 return Canvas._create(self, 'line', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000797 def create_oval(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000798 return Canvas._create(self, 'oval', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000799 def create_polygon(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000800 return Canvas._create(self, 'polygon', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000801 def create_rectangle(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000802 return Canvas._create(self, 'rectangle', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000803 def create_text(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000804 return Canvas._create(self, 'text', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000805 def create_window(self, *args):
Guido van Rossum67ef5f31994-06-20 13:39:14 +0000806 return Canvas._create(self, 'window', args)
Guido van Rossum18468821994-06-20 07:49:28 +0000807 def dchars(self, *args):
808 self._do('dchars', args)
809 def delete(self, *args):
810 self._do('delete', args)
811 def dtag(self, *args):
812 self._do('dtag', args)
813 def find(self, *args):
Guido van Rossum08a40381994-06-21 11:44:21 +0000814 return self._getints(self._do('find', args))
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000815 def find_above(self, tagOrId):
816 return self.find('above', tagOrId)
817 def find_all(self):
818 return self.find('all')
819 def find_below(self, tagOrId):
820 return self.find('below', tagOrId)
821 def find_closest(self, x, y, halo=None, start=None):
822 return self.find('closest', x, y, halo, start)
823 def find_enclosed(self, x1, y1, x2, y2):
824 return self.find('enclosed', x1, y1, x2, y2)
825 def find_overlapping(self, x1, y1, x2, y2):
826 return self.find('overlapping', x1, y1, x2, y2)
827 def find_withtag(self, tagOrId):
828 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000829 def focus(self, *args):
830 return self._do('focus', args)
831 def gettags(self, *args):
832 return self.tk.splitlist(self._do('gettags', args))
833 def icursor(self, *args):
834 self._do('icursor', args)
835 def index(self, *args):
836 return self.tk.getint(self._do('index', args))
837 def insert(self, *args):
838 self._do('insert', args)
Guido van Rossum08a40381994-06-21 11:44:21 +0000839 def itemconfig(self, tagOrId, cnf=None):
840 if cnf is None:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000841 cnf = {}
842 for x in self.tk.split(
843 self._do('itemconfigure', (tagOrId))):
844 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
845 return cnf
Guido van Rossum08a40381994-06-21 11:44:21 +0000846 if type(cnf) == StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000847 x = self.tk.split(self._do('itemconfigure',
848 (tagOrId, '-'+cnf,)))
849 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000850 self._do('itemconfigure', (tagOrId,) + self._options(cnf))
851 def lower(self, *args):
852 self._do('lower', args)
853 def move(self, *args):
854 self._do('move', args)
855 def postscript(self, cnf={}):
856 return self._do('postscript', self._options(cnf))
857 def tkraise(self, *args):
858 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000859 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000860 def scale(self, *args):
861 self._do('scale', args)
862 def scan_mark(self, x, y):
863 self.tk.call(self._w, 'scan', 'mark', x, y)
864 def scan_dragto(self, x, y):
865 self.tk.call(self._w, 'scan', 'dragto', x, y)
866 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000867 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000868 def select_clear(self):
869 self.tk.call(self._w, 'select', 'clear')
870 def select_from(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000871 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000872 def select_item(self):
873 self.tk.call(self._w, 'select', 'item')
874 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +0000875 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +0000876 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +0000877 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum18468821994-06-20 07:49:28 +0000878 def xview(self, index):
879 self.tk.call(self._w, 'xview', index)
880 def yview(self, index):
881 self.tk.call(self._w, 'yview', index)
882
883class Checkbutton(Widget):
884 def __init__(self, master=None, cnf={}):
885 Widget.__init__(self, master, 'checkbutton', cnf)
886 def deselect(self):
887 self.tk.call(self._w, 'deselect')
888 def flash(self):
889 self.tk.call(self._w, 'flash')
890 def invoke(self):
891 self.tk.call(self._w, 'invoke')
892 def select(self):
893 self.tk.call(self._w, 'select')
894 def toggle(self):
895 self.tk.call(self._w, 'toggle')
896
897class Entry(Widget):
898 def __init__(self, master=None, cnf={}):
899 Widget.__init__(self, master, 'entry', cnf)
900 def tk_entryBackspace(self):
901 self.tk.call('tk_entryBackspace', self._w)
902 def tk_entryBackword(self):
903 self.tk.call('tk_entryBackword', self._w)
904 def tk_entrySeeCaret(self):
905 self.tk.call('tk_entrySeeCaret', self._w)
906 def delete(self, first, last=None):
907 self.tk.call(self._w, 'delete', first, last)
908 def get(self):
909 return self.tk.call(self._w, 'get')
910 def icursor(self, index):
911 self.tk.call(self._w, 'icursor', index)
912 def index(self, index):
913 return self.tk.getint(self.tk.call(
914 self._w, 'index', index))
915 def insert(self, index, string):
916 self.tk.call(self._w, 'insert', index, string)
917 def scan_mark(self, x):
918 self.tk.call(self._w, 'scan', 'mark', x)
919 def scan_dragto(self, x):
920 self.tk.call(self._w, 'scan', 'dragto', x)
921 def select_adjust(self, index):
922 self.tk.call(self._w, 'select', 'adjust', index)
923 def select_clear(self):
924 self.tk.call(self._w, 'select', 'clear')
925 def select_from(self, index):
926 self.tk.call(self._w, 'select', 'from', index)
927 def select_to(self, index):
928 self.tk.call(self._w, 'select', 'to', index)
929 def select_view(self, index):
930 self.tk.call(self._w, 'select', 'view', index)
931
932class Frame(Widget):
933 def __init__(self, master=None, cnf={}):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000934 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000935 extra = ()
936 if cnf.has_key('class'):
937 extra = ('-class', cnf['class'])
938 del cnf['class']
939 Widget.__init__(self, master, 'frame', cnf, extra)
940 def tk_menuBar(self, *args):
941 apply(self.tk.call, ('tk_menuBar', self._w) + args)
942
943class Label(Widget):
944 def __init__(self, master=None, cnf={}):
945 Widget.__init__(self, master, 'label', cnf)
946
947class Listbox(Widget):
948 def __init__(self, master=None, cnf={}):
949 Widget.__init__(self, master, 'listbox', cnf)
950 def tk_listboxSingleSelect(self):
951 self.tk.call('tk_listboxSingleSelect', self._w)
952 def curselection(self):
953 return self.tk.splitlist(self.tk.call(
954 self._w, 'curselection'))
955 def delete(self, first, last=None):
956 self.tk.call(self._w, 'delete', first, last)
957 def get(self, index):
958 return self.tk.call(self._w, 'get', index)
959 def insert(self, index, *elements):
960 apply(self.tk.call,
961 (self._w, 'insert', index) + elements)
962 def nearest(self, y):
963 return self.tk.getint(self.tk.call(
964 self._w, 'nearest', y))
965 def scan_mark(self, x, y):
966 self.tk.call(self._w, 'scan', 'mark', x, y)
967 def scan_dragto(self, x, y):
968 self.tk.call(self._w, 'scan', 'dragto', x, y)
969 def select_adjust(self, index):
970 self.tk.call(self._w, 'select', 'adjust', index)
971 def select_clear(self):
972 self.tk.call(self._w, 'select', 'clear')
973 def select_from(self, index):
974 self.tk.call(self._w, 'select', 'from', index)
975 def select_to(self, index):
976 self.tk.call(self._w, 'select', 'to', index)
977 def size(self):
978 return self.tk.getint(self.tk.call(self._w, 'size'))
979 def xview(self, index):
980 self.tk.call(self._w, 'xview', index)
981 def yview(self, index):
982 self.tk.call(self._w, 'yview', index)
983
984class Menu(Widget):
985 def __init__(self, master=None, cnf={}):
986 Widget.__init__(self, master, 'menu', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000987 def tk_bindForTraversal(self):
988 self.tk.call('tk_bindForTraversal', self._w)
989 def tk_mbPost(self):
990 self.tk.call('tk_mbPost', self._w)
991 def tk_mbUnpost(self):
992 self.tk.call('tk_mbUnpost')
993 def tk_traverseToMenu(self, char):
994 self.tk.call('tk_traverseToMenu', self._w, char)
995 def tk_traverseWithinMenu(self, char):
996 self.tk.call('tk_traverseWithinMenu', self._w, char)
997 def tk_getMenuButtons(self):
998 return self.tk.call('tk_getMenuButtons', self._w)
999 def tk_nextMenu(self, count):
1000 self.tk.call('tk_nextMenu', count)
1001 def tk_nextMenuEntry(self, count):
1002 self.tk.call('tk_nextMenuEntry', count)
1003 def tk_invokeMenu(self):
1004 self.tk.call('tk_invokeMenu', self._w)
1005 def tk_firstMenu(self):
1006 self.tk.call('tk_firstMenu', self._w)
1007 def tk_mbButtonDown(self):
1008 self.tk.call('tk_mbButtonDown', self._w)
1009 def activate(self, index):
1010 self.tk.call(self._w, 'activate', index)
1011 def add(self, itemType, cnf={}):
1012 apply(self.tk.call, (self._w, 'add', itemType)
1013 + self._options(cnf))
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001014 def add_cascade(self, cnf={}):
1015 self.add('cascade', cnf)
1016 def add_checkbutton(self, cnf={}):
1017 self.add('checkbutton', cnf)
1018 def add_command(self, cnf={}):
1019 self.add('command', cnf)
1020 def add_radiobutton(self, cnf={}):
1021 self.add('radiobutton', cnf)
1022 def add_separator(self, cnf={}):
1023 self.add('separator', cnf)
Guido van Rossum18468821994-06-20 07:49:28 +00001024 def delete(self, index1, index2=None):
1025 self.tk.call(self._w, 'delete', index1, index2)
1026 def entryconfig(self, index, cnf={}):
1027 apply(self.tk.call, (self._w, 'entryconfigure', index)
1028 + self._options(cnf))
1029 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001030 i = self.tk.call(self._w, 'index', index)
1031 if i == 'none': return None
1032 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001033 def invoke(self, index):
1034 return self.tk.call(self._w, 'invoke', index)
1035 def post(self, x, y):
1036 self.tk.call(self._w, 'post', x, y)
1037 def unpost(self):
1038 self.tk.call(self._w, 'unpost')
1039 def yposition(self, index):
1040 return self.tk.getint(self.tk.call(
1041 self._w, 'yposition', index))
1042
1043class Menubutton(Widget):
1044 def __init__(self, master=None, cnf={}):
1045 Widget.__init__(self, master, 'menubutton', cnf)
1046
1047class Message(Widget):
1048 def __init__(self, master=None, cnf={}):
1049 Widget.__init__(self, master, 'message', cnf)
1050
1051class Radiobutton(Widget):
1052 def __init__(self, master=None, cnf={}):
1053 Widget.__init__(self, master, 'radiobutton', cnf)
1054 def deselect(self):
1055 self.tk.call(self._w, 'deselect')
1056 def flash(self):
1057 self.tk.call(self._w, 'flash')
1058 def invoke(self):
1059 self.tk.call(self._w, 'invoke')
1060 def select(self):
1061 self.tk.call(self._w, 'select')
1062
1063class Scale(Widget):
1064 def __init__(self, master=None, cnf={}):
1065 Widget.__init__(self, master, 'scale', cnf)
1066 def get(self):
1067 return self.tk.getint(self.tk.call(self._w, 'get'))
1068 def set(self, value):
1069 self.tk.call(self._w, 'set', value)
1070
1071class Scrollbar(Widget):
1072 def __init__(self, master=None, cnf={}):
1073 Widget.__init__(self, master, 'scrollbar', cnf)
1074 def get(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001075 return self._getints(self.tk.call(self._w, 'get'))
Guido van Rossum18468821994-06-20 07:49:28 +00001076 def set(self, totalUnits, windowUnits, firstUnit, lastUnit):
1077 self.tk.call(self._w, 'set',
1078 totalUnits, windowUnits, firstUnit, lastUnit)
1079
1080class Text(Widget):
1081 def __init__(self, master=None, cnf={}):
1082 Widget.__init__(self, master, 'text', cnf)
1083 def tk_textSelectTo(self, index):
1084 self.tk.call('tk_textSelectTo', self._w, index)
1085 def tk_textBackspace(self):
1086 self.tk.call('tk_textBackspace', self._w)
1087 def tk_textIndexCloser(self, a, b, c):
1088 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1089 def tk_textResetAnchor(self, index):
1090 self.tk.call('tk_textResetAnchor', self._w, index)
1091 def compare(self, index1, op, index2):
1092 return self.tk.getboolean(self.tk.call(
1093 self._w, 'compare', index1, op, index2))
1094 def debug(self, boolean=None):
1095 return self.tk.getboolean(self.tk.call(
1096 self._w, 'debug', boolean))
1097 def delete(self, index1, index2=None):
1098 self.tk.call(self._w, 'delete', index1, index2)
1099 def get(self, index1, index2=None):
1100 return self.tk.call(self._w, 'get', index1, index2)
1101 def index(self, index):
1102 return self.tk.call(self._w, 'index', index)
1103 def insert(self, index, chars):
1104 self.tk.call(self._w, 'insert', index, chars)
1105 def mark_names(self):
1106 return self.tk.splitlist(self.tk.call(
1107 self._w, 'mark', 'names'))
1108 def mark_set(self, markName, index):
1109 self.tk.call(self._w, 'mark', 'set', markName, index)
1110 def mark_unset(self, markNames):
1111 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
1112 def scan_mark(self, y):
1113 self.tk.call(self._w, 'scan', 'mark', y)
1114 def scan_dragto(self, y):
1115 self.tk.call(self._w, 'scan', 'dragto', y)
1116 def tag_add(self, tagName, index1, index2=None):
1117 self.tk.call(
1118 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001119 def tag_unbind(self, tagName, sequence):
1120 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum18468821994-06-20 07:49:28 +00001121 def tag_bind(self, tagName, sequence, func, add=''):
Guido van Rossum18468821994-06-20 07:49:28 +00001122 if add: add='+'
Guido van Rossum45853db1994-06-20 12:19:19 +00001123 name = self._register(func, self._substitute)
Guido van Rossum18468821994-06-20 07:49:28 +00001124 self.tk.call(self._w, 'tag', 'bind',
1125 tagName, sequence,
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001126 (add + name,) + self._subst_format)
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def tag_config(self, tagName, cnf={}):
1128 apply(self.tk.call,
1129 (self._w, 'tag', 'configure', tagName)
1130 + self._options(cnf))
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001131 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001132 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001133 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001134 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001135 def tag_names(self, index=None):
1136 return self.tk.splitlist(
1137 self.tk.call(self._w, 'tag', 'names', index))
1138 def tag_nextrange(self, tagName, index1, index2=None):
1139 return self.tk.splitlist(self.tk.call(
1140 self._w, 'tag', 'nextrange', index1, index2))
1141 def tag_raise(self, tagName, aboveThis=None):
1142 self.tk.call(
1143 self._w, 'tag', 'raise', tagName, aboveThis)
1144 def tag_ranges(self, tagName):
1145 return self.tk.splitlist(self.tk.call(
1146 self._w, 'tag', 'ranges', tagName))
1147 def tag_remove(self, tagName, index1, index2=None):
1148 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001149 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def yview(self, what):
1151 self.tk.call(self._w, 'yview', what)
1152 def yview_pickplace(self, what):
1153 self.tk.call(self._w, 'yview', '-pickplace', what)
1154
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001155######################################################################
1156# Extensions:
1157
1158class Studbutton(Button):
1159 def __init__(self, master=None, cnf={}):
1160 Widget.__init__(self, master, 'studbutton', cnf)
1161 self.bind('<Any-Enter>', self.tk_butEnter)
1162 self.bind('<Any-Leave>', self.tk_butLeave)
1163 self.bind('<1>', self.tk_butDown)
1164 self.bind('<ButtonRelease-1>', self.tk_butUp)
1165
1166class Tributton(Button):
1167 def __init__(self, master=None, cnf={}):
1168 Widget.__init__(self, master, 'tributton', cnf)
1169 self.bind('<Any-Enter>', self.tk_butEnter)
1170 self.bind('<Any-Leave>', self.tk_butLeave)
1171 self.bind('<1>', self.tk_butDown)
1172 self.bind('<ButtonRelease-1>', self.tk_butUp)
1173