blob: d31ca9fd25686aa36848aa71197749d84bf2b0ba [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 Rossum37dcab11996-05-16 16:00:19 +00003__version__ = "$Revision$"
4
5try:
6 # See if modern _tkinter is present
7 import _tkinter
8 tkinter = _tkinter # b/w compat
9except ImportError:
10 # No modern _tkinter -- try oldfashioned tkinter
11 import tkinter
12 if hasattr(tkinter, "__path__"):
13 import sys, os
14 # Append standard platform specific directory
15 p = tkinter.__path__
16 for dir in sys.path:
17 if (dir not in p and
18 os.path.basename(dir) == sys.platform):
19 p.append(dir)
20 del sys, os, p, dir
21 from tkinter import tkinter
22TclError = tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +000023from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +000024from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000025import string; _string = string; del string
Guido van Rossum18468821994-06-20 07:49:28 +000026
Guido van Rossum37dcab11996-05-16 16:00:19 +000027TkVersion = eval(tkinter.TK_VERSION)
28TclVersion = eval(tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000029
Guido van Rossuma22a70a1995-08-04 03:51:48 +000030
Guido van Rossum2dcf5291994-07-06 09:23:20 +000031def _flatten(tuple):
32 res = ()
33 for item in tuple:
34 if type(item) in (TupleType, ListType):
35 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000036 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000037 res = res + (item,)
38 return res
39
40def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000041 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000042 return cnfs
43 elif type(cnfs) in (NoneType, StringType):
44
Guido van Rossum2dcf5291994-07-06 09:23:20 +000045 return cnfs
46 else:
47 cnf = {}
48 for c in _flatten(cnfs):
49 for k, v in c.items():
50 cnf[k] = v
51 return cnf
52
53class Event:
54 pass
55
Guido van Rossumaec5dc91994-06-27 07:55:12 +000056_default_root = None
57
Guido van Rossum45853db1994-06-20 12:19:19 +000058def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000059 pass
60
Guido van Rossum97aeca11994-07-07 13:12:12 +000061def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000062 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000063
Guido van Rossumaec5dc91994-06-27 07:55:12 +000064_varnum = 0
65class Variable:
66 def __init__(self, master=None):
67 global _default_root
68 global _varnum
69 if master:
70 self._tk = master.tk
71 else:
72 self._tk = _default_root.tk
73 self._name = 'PY_VAR' + `_varnum`
74 _varnum = _varnum + 1
75 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000076 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000077 def __str__(self):
78 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000079 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000080 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000081
82class StringVar(Variable):
83 def __init__(self, master=None):
84 Variable.__init__(self, master)
85 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000086 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000087
88class IntVar(Variable):
89 def __init__(self, master=None):
90 Variable.__init__(self, master)
91 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000092 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000093
94class DoubleVar(Variable):
95 def __init__(self, master=None):
96 Variable.__init__(self, master)
97 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000098 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000099
100class BooleanVar(Variable):
101 def __init__(self, master=None):
102 Variable.__init__(self, master)
103 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000104 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000105
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000106def mainloop(n=0):
107 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000108
109def getint(s):
110 return _default_root.tk.getint(s)
111
112def getdouble(s):
113 return _default_root.tk.getdouble(s)
114
115def getboolean(s):
116 return _default_root.tk.getboolean(s)
117
Guido van Rossum18468821994-06-20 07:49:28 +0000118class Misc:
119 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000120 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000121 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000122 def tk_menuBar(self, *args):
123 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000124 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000125 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000126 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000127 def wait_window(self, window=None):
128 if window == None:
129 window = self
130 self.tk.call('tkwait', 'window', window._w)
131 def wait_visibility(self, window=None):
132 if window == None:
133 window = self
134 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000135 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000136 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000137 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000138 return self.tk.getvar(name)
139 def getint(self, s):
140 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000141 def getdouble(self, s):
142 return self.tk.getdouble(s)
143 def getboolean(self, s):
144 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000145 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000146 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000147 focus = focus_set # XXX b/w compat?
148 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000149 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000150 def focus_default_none(self):
151 self.tk.call('focus', 'default', 'none')
152 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000153 def focus_none(self):
154 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000155 def focus_get(self):
156 name = self.tk.call('focus')
157 if name == 'none': return None
158 return self._nametowidget(name)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000159 def tk_focusNext(self):
160 name = self.tk.call('tk_focusNext', self._w)
161 if not name: return None
162 return self._nametowidget(name)
163 def tk_focusPrev(self):
164 name = self.tk.call('tk_focusPrev', self._w)
165 if not name: return None
166 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000167 def after(self, ms, func=None, *args):
168 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000169 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000170 self.tk.call('after', ms)
171 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000172 # XXX Disgusting hack to clean up after calling func
173 tmp = []
174 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
175 try:
176 apply(func, args)
177 finally:
178 tk.deletecommand(tmp[0])
179 name = self._register(callit)
180 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000181 return self.tk.call('after', ms, name)
182 def after_idle(self, func, *args):
183 return apply(self.after, ('idle', func) + args)
184 def after_cancel(self, id):
185 self.tk.call('after', 'cancel', id)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000186 def bell(self, displayof=None):
187 if displayof:
188 self.tk.call('bell', '-displayof', displayof)
189 else:
190 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000191 # XXX grab current w/o window argument
192 def grab_current(self):
193 name = self.tk.call('grab', 'current', self._w)
194 if not name: return None
195 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000196 def grab_release(self):
197 self.tk.call('grab', 'release', self._w)
198 def grab_set(self):
199 self.tk.call('grab', 'set', self._w)
200 def grab_set_global(self):
201 self.tk.call('grab', 'set', '-global', self._w)
202 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000203 status = self.tk.call('grab', 'status', self._w)
204 if status == 'none': status = None
205 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000206 def lower(self, belowThis=None):
207 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000208 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000209 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000210 def option_clear(self):
211 self.tk.call('option', 'clear')
212 def option_get(self, name, className):
213 return self.tk.call('option', 'get', self._w, name, className)
214 def option_readfile(self, fileName, priority = None):
215 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000216 def selection_clear(self):
217 self.tk.call('selection', 'clear', self._w)
218 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000219 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000220 def selection_handle(self, func, type=None, format=None):
221 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000222 self.tk.call('selection', 'handle', self._w,
223 name, type, format)
224 def selection_own(self, func=None):
225 name = self._register(func)
226 self.tk.call('selection', 'own', self._w, name)
227 def selection_own_get(self):
228 return self._nametowidget(self.tk.call('selection', 'own'))
229 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000230 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000231 def lower(self, belowThis=None):
232 self.tk.call('lift', self._w, belowThis)
233 def tkraise(self, aboveThis=None):
234 self.tk.call('raise', self._w, aboveThis)
235 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000236 def colormodel(self, value=None):
237 return self.tk.call('tk', 'colormodel', self._w, value)
238 def winfo_atom(self, name):
239 return self.tk.getint(self.tk.call('winfo', 'atom', name))
240 def winfo_atomname(self, id):
241 return self.tk.call('winfo', 'atomname', id)
242 def winfo_cells(self):
243 return self.tk.getint(
244 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000245 def winfo_children(self):
246 return map(self._nametowidget,
247 self.tk.splitlist(self.tk.call(
248 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000249 def winfo_class(self):
250 return self.tk.call('winfo', 'class', self._w)
251 def winfo_containing(self, rootX, rootY):
252 return self.tk.call('winfo', 'containing', rootx, rootY)
253 def winfo_depth(self):
254 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
255 def winfo_exists(self):
256 return self.tk.getint(
257 self.tk.call('winfo', 'exists', self._w))
258 def winfo_fpixels(self, number):
259 return self.tk.getdouble(self.tk.call(
260 'winfo', 'fpixels', self._w, number))
261 def winfo_geometry(self):
262 return self.tk.call('winfo', 'geometry', self._w)
263 def winfo_height(self):
264 return self.tk.getint(
265 self.tk.call('winfo', 'height', self._w))
266 def winfo_id(self):
267 return self.tk.getint(
268 self.tk.call('winfo', 'id', self._w))
269 def winfo_interps(self):
270 return self.tk.splitlist(
271 self.tk.call('winfo', 'interps'))
272 def winfo_ismapped(self):
273 return self.tk.getint(
274 self.tk.call('winfo', 'ismapped', self._w))
275 def winfo_name(self):
276 return self.tk.call('winfo', 'name', self._w)
277 def winfo_parent(self):
278 return self.tk.call('winfo', 'parent', self._w)
279 def winfo_pathname(self, id):
280 return self.tk.call('winfo', 'pathname', id)
281 def winfo_pixels(self, number):
282 return self.tk.getint(
283 self.tk.call('winfo', 'pixels', self._w, number))
284 def winfo_reqheight(self):
285 return self.tk.getint(
286 self.tk.call('winfo', 'reqheight', self._w))
287 def winfo_reqwidth(self):
288 return self.tk.getint(
289 self.tk.call('winfo', 'reqwidth', self._w))
290 def winfo_rgb(self, color):
291 return self._getints(
292 self.tk.call('winfo', 'rgb', self._w, color))
293 def winfo_rootx(self):
294 return self.tk.getint(
295 self.tk.call('winfo', 'rootx', self._w))
296 def winfo_rooty(self):
297 return self.tk.getint(
298 self.tk.call('winfo', 'rooty', self._w))
299 def winfo_screen(self):
300 return self.tk.call('winfo', 'screen', self._w)
301 def winfo_screencells(self):
302 return self.tk.getint(
303 self.tk.call('winfo', 'screencells', self._w))
304 def winfo_screendepth(self):
305 return self.tk.getint(
306 self.tk.call('winfo', 'screendepth', self._w))
307 def winfo_screenheight(self):
308 return self.tk.getint(
309 self.tk.call('winfo', 'screenheight', self._w))
310 def winfo_screenmmheight(self):
311 return self.tk.getint(
312 self.tk.call('winfo', 'screenmmheight', self._w))
313 def winfo_screenmmwidth(self):
314 return self.tk.getint(
315 self.tk.call('winfo', 'screenmmwidth', self._w))
316 def winfo_screenvisual(self):
317 return self.tk.call('winfo', 'screenvisual', self._w)
318 def winfo_screenwidth(self):
319 return self.tk.getint(
320 self.tk.call('winfo', 'screenwidth', self._w))
321 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000322 return self._nametowidget(self.tk.call(
323 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def winfo_visual(self):
325 return self.tk.call('winfo', 'visual', self._w)
326 def winfo_vrootheight(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'vrootheight', self._w))
329 def winfo_vrootwidth(self):
330 return self.tk.getint(
331 self.tk.call('winfo', 'vrootwidth', self._w))
332 def winfo_vrootx(self):
333 return self.tk.getint(
334 self.tk.call('winfo', 'vrootx', self._w))
335 def winfo_vrooty(self):
336 return self.tk.getint(
337 self.tk.call('winfo', 'vrooty', self._w))
338 def winfo_width(self):
339 return self.tk.getint(
340 self.tk.call('winfo', 'width', self._w))
341 def winfo_x(self):
342 return self.tk.getint(
343 self.tk.call('winfo', 'x', self._w))
344 def winfo_y(self):
345 return self.tk.getint(
346 self.tk.call('winfo', 'y', self._w))
347 def update(self):
348 self.tk.call('update')
349 def update_idletasks(self):
350 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000351 def bindtags(self, tagList=None):
352 if tagList is None:
353 return self.tk.splitlist(
354 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000355 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000356 self.tk.call('bindtags', self._w, tagList)
357 def _bind(self, what, sequence, func, add):
358 if func:
359 cmd = ("%sset _tkinter_break [%s %s]\n"
360 'if {"$_tkinter_break" == "break"} break\n') \
361 % (add and '+' or '',
362 self._register(func, self._substitute),
363 _string.join(self._subst_format))
364 apply(self.tk.call, what + (sequence, cmd))
365 elif func == '':
366 apply(self.tk.call, what + (sequence, func))
367 else:
368 return apply(self.tk.call, what + (sequence,))
369 def bind(self, sequence=None, func=None, add=None):
370 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000371 def unbind(self, sequence):
372 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000373 def bind_all(self, sequence=None, func=None, add=None):
374 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000375 def unbind_all(self, sequence):
376 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000377 def bind_class(self, className, sequence=None, func=None, add=None):
378 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000379 def unbind_class(self, className, sequence):
380 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000381 def mainloop(self, n=0):
382 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000383 def quit(self):
384 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000385 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000386 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000387 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
388 def _getdoubles(self, string):
389 if not string: return None
390 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000391 def _getboolean(self, string):
392 if string:
393 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000394 def _options(self, cnf, kw = None):
395 if kw:
396 cnf = _cnfmerge((cnf, kw))
397 else:
398 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000399 res = ()
400 for k, v in cnf.items():
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000401 if k[-1] == '_': k = k[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000402 if callable(v):
Guido van Rossum18468821994-06-20 07:49:28 +0000403 v = self._register(v)
404 res = res + ('-'+k, v)
405 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000406 def _nametowidget(self, name):
407 w = self
408 if name[0] == '.':
409 w = w._root()
410 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000411 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000412 while name:
413 i = find(name, '.')
414 if i >= 0:
415 name, tail = name[:i], name[i+1:]
416 else:
417 tail = ''
418 w = w.children[name]
419 name = tail
420 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000421 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000422 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000423 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000424 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000425 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000426 except AttributeError:
427 pass
428 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000429 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000430 except AttributeError:
431 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000432 self.tk.createcommand(name, f)
433 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000434 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000435 def _root(self):
436 w = self
437 while w.master: w = w.master
438 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000439 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000440 '%s', '%t', '%w', '%x', '%y',
441 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
442 def _substitute(self, *args):
443 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000444 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000445 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
446 # Missing: (a, c, d, m, o, v, B, R)
447 e = Event()
448 e.serial = tk.getint(nsign)
449 e.num = tk.getint(b)
450 try: e.focus = tk.getboolean(f)
451 except TclError: pass
452 e.height = tk.getint(h)
453 e.keycode = tk.getint(k)
454 e.state = tk.getint(s)
455 e.time = tk.getint(t)
456 e.width = tk.getint(w)
457 e.x = tk.getint(x)
458 e.y = tk.getint(y)
459 e.char = A
460 try: e.send_event = tk.getboolean(E)
461 except TclError: pass
462 e.keysym = K
463 e.keysym_num = tk.getint(N)
464 e.type = T
465 e.widget = self._nametowidget(W)
466 e.x_root = tk.getint(X)
467 e.y_root = tk.getint(Y)
468 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000469 def _report_exception(self):
470 import sys
471 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
472 root = self._root()
473 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000474
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000475class CallWrapper:
476 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000477 self.func = func
478 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000479 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000480 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000481 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000482 if self.subst:
483 args = apply(self.subst, args)
484 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000485 except SystemExit, msg:
486 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000487 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000488 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000489
490class Wm:
491 def aspect(self,
492 minNumer=None, minDenom=None,
493 maxNumer=None, maxDenom=None):
494 return self._getints(
495 self.tk.call('wm', 'aspect', self._w,
496 minNumer, minDenom,
497 maxNumer, maxDenom))
498 def client(self, name=None):
499 return self.tk.call('wm', 'client', self._w, name)
500 def command(self, value=None):
501 return self.tk.call('wm', 'command', self._w, value)
502 def deiconify(self):
503 return self.tk.call('wm', 'deiconify', self._w)
504 def focusmodel(self, model=None):
505 return self.tk.call('wm', 'focusmodel', self._w, model)
506 def frame(self):
507 return self.tk.call('wm', 'frame', self._w)
508 def geometry(self, newGeometry=None):
509 return self.tk.call('wm', 'geometry', self._w, newGeometry)
510 def grid(self,
511 baseWidht=None, baseHeight=None,
512 widthInc=None, heightInc=None):
513 return self._getints(self.tk.call(
514 'wm', 'grid', self._w,
515 baseWidht, baseHeight, widthInc, heightInc))
516 def group(self, pathName=None):
517 return self.tk.call('wm', 'group', self._w, pathName)
518 def iconbitmap(self, bitmap=None):
519 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
520 def iconify(self):
521 return self.tk.call('wm', 'iconify', self._w)
522 def iconmask(self, bitmap=None):
523 return self.tk.call('wm', 'iconmask', self._w, bitmap)
524 def iconname(self, newName=None):
525 return self.tk.call('wm', 'iconname', self._w, newName)
526 def iconposition(self, x=None, y=None):
527 return self._getints(self.tk.call(
528 'wm', 'iconposition', self._w, x, y))
529 def iconwindow(self, pathName=None):
530 return self.tk.call('wm', 'iconwindow', self._w, pathName)
531 def maxsize(self, width=None, height=None):
532 return self._getints(self.tk.call(
533 'wm', 'maxsize', self._w, width, height))
534 def minsize(self, width=None, height=None):
535 return self._getints(self.tk.call(
536 'wm', 'minsize', self._w, width, height))
537 def overrideredirect(self, boolean=None):
538 return self._getboolean(self.tk.call(
539 'wm', 'overrideredirect', self._w, boolean))
540 def positionfrom(self, who=None):
541 return self.tk.call('wm', 'positionfrom', self._w, who)
542 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000543 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000544 command = self._register(func)
545 else:
546 command = func
547 return self.tk.call(
548 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000549 def resizable(self, width=None, height=None):
550 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000551 def sizefrom(self, who=None):
552 return self.tk.call('wm', 'sizefrom', self._w, who)
553 def state(self):
554 return self.tk.call('wm', 'state', self._w)
555 def title(self, string=None):
556 return self.tk.call('wm', 'title', self._w, string)
557 def transient(self, master=None):
558 return self.tk.call('wm', 'transient', self._w, master)
559 def withdraw(self):
560 return self.tk.call('wm', 'withdraw', self._w)
561
562class Tk(Misc, Wm):
563 _w = '.'
564 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000565 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000566 self.master = None
567 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000568 if baseName is None:
569 import sys, os
570 baseName = os.path.basename(sys.argv[0])
571 if baseName[-3:] == '.py': baseName = baseName[:-3]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000572 self.tk = tkinter.create(screenName, baseName, className)
573 try:
574 # Disable event scanning except for Command-Period
575 import MacOS
576 MacOS.EnableAppswitch(0)
577 except ImportError:
578 pass
579 else:
580 # Work around nasty MacTk bug
581 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000582 # Version sanity checks
583 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000584 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000585 raise RuntimeError, \
586 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000587 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000588 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000589 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000590 raise RuntimeError, \
591 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000592 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000593 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000594 raise RuntimeError, \
595 "Tk 4.0 or higher is required; found Tk %s" \
596 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000597 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000598 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000599 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000600 if not _default_root:
601 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000602 def destroy(self):
603 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000604 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000605 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000606 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000607 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000608 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000609 if os.environ.has_key('HOME'): home = os.environ['HOME']
610 else: home = os.curdir
611 class_tcl = os.path.join(home, '.%s.tcl' % className)
612 class_py = os.path.join(home, '.%s.py' % className)
613 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
614 base_py = os.path.join(home, '.%s.py' % baseName)
615 dir = {'self': self}
616 exec 'from Tkinter import *' in dir
617 if os.path.isfile(class_tcl):
618 print 'source', `class_tcl`
619 self.tk.call('source', class_tcl)
620 if os.path.isfile(class_py):
621 print 'execfile', `class_py`
622 execfile(class_py, dir)
623 if os.path.isfile(base_tcl):
624 print 'source', `base_tcl`
625 self.tk.call('source', base_tcl)
626 if os.path.isfile(base_py):
627 print 'execfile', `base_py`
628 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000629 def report_callback_exception(self, exc, val, tb):
630 import traceback
631 print "Exception in Tkinter callback"
632 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000633
634class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000635 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000636 apply(self.tk.call,
637 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000638 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000639 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000640 pack = config
641 def __setitem__(self, key, value):
642 Pack.config({key: value})
643 def forget(self):
644 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000645 pack_forget = forget
646 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000647 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000648 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000649 dict = {}
650 for i in range(0, len(words), 2):
651 key = words[i][1:]
652 value = words[i+1]
653 if value[0] == '.':
654 value = self._nametowidget(value)
655 dict[key] = value
656 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000657 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000658 _noarg_ = ['_noarg_']
659 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000660 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000661 return self._getboolean(self.tk.call(
662 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000663 else:
664 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000665 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000666 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000667 return map(self._nametowidget,
668 self.tk.splitlist(
669 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000670 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000671
672class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000673 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000674 for k in ['in_']:
675 if kw.has_key(k):
676 kw[k[:-1]] = kw[k]
677 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000678 apply(self.tk.call,
679 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000680 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000681 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000682 place = config
683 def __setitem__(self, key, value):
684 Place.config({key: value})
685 def forget(self):
686 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000687 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000688 def info(self):
689 return self.tk.call('place', 'info', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000690 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000691 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000692 return map(self._nametowidget,
693 self.tk.splitlist(
694 self.tk.call(
695 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000696 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000697
Guido van Rossum37dcab11996-05-16 16:00:19 +0000698class Grid:
699 # Thanks to Masa Yoshikawa (yosikawa@isi.edu)
700 def config(self, cnf={}, **kw):
701 apply(self.tk.call,
702 ('grid', 'configure', self._w)
703 + self._options(cnf, kw))
704 grid = config
705 def __setitem__(self, key, value):
706 Grid.config({key: value})
707 def bbox(self, column, row):
708 return self._getints(
709 self.tk.call(
710 'grid', 'bbox', self._w, column, row)) or None
711 grid_bbox = bbox
712 def columnconfigure(self, index, *args):
713 res = apply(self.tk.call,
714 ('grid', 'columnconfigure', self._w)
715 + args)
716 if args == ['minsize']:
717 return self._getint(res) or None
718 elif args == ['wieght']:
719 return self._getdouble(res) or None
720 def forget(self):
721 self.tk.call('grid', 'forget', self._w)
722 grid_forget = forget
723 def info(self):
724 words = self.tk.splitlist(
725 self.tk.call('grid', 'info', self._w))
726 dict = {}
727 for i in range(0, len(words), 2):
728 key = words[i][1:]
729 value = words[i+1]
730 if value[0] == '.':
731 value = self._nametowidget(value)
732 dict[key] = value
733 return dict
734 grid_info = info
735 def location(self, x, y):
736 return self._getints(
737 self.tk.call(
738 'grid', 'location', self._w, x, y)) or None
739 _noarg_ = ['_noarg_']
740 def propagate(self, flag=_noarg_):
741 if flag is Grid._noarg_:
742 return self._getboolean(self.tk.call(
743 'grid', 'propagate', self._w))
744 else:
745 self.tk.call('grid', 'propagate', self._w, flag)
746 grid_propagate = propagate
747 def rowconfigure(self, index, *args):
748 res = apply(self.tk.call,
749 ('grid', 'rowconfigure', self._w)
750 + args)
751 if args == ['minsize']:
752 return self._getint(res) or None
753 elif args == ['wieght']:
754 return self._getdouble(res) or None
755 def size(self):
756 return self._getints(
757 self.tk.call('grid', 'size', self._w)) or None
758 def slaves(self, *args):
759 return map(self._nametowidget,
760 self.tk.splitlist(
761 apply(self.tk.call,
762 ('grid', 'slaves', self._w) + args)))
763 grid_slaves = slaves
764
765class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000766 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000767 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000768 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000769 if not _default_root:
770 _default_root = Tk()
771 master = _default_root
772 if not _default_root:
773 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000774 self.master = master
775 self.tk = master.tk
776 if cnf.has_key('name'):
777 name = cnf['name']
778 del cnf['name']
779 else:
780 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000781 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000782 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000783 self._w = '.' + name
784 else:
785 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000786 self.children = {}
787 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000788 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000789 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000790 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
791 if kw:
792 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000793 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000794 Widget._setup(self, master, cnf)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000795 apply(self.tk.call,
796 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000797 def config(self, cnf=None, **kw):
798 # XXX ought to generalize this so tag_config etc. can use it
799 if kw:
800 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000801 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000802 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000803 if cnf is None:
804 cnf = {}
805 for x in self.tk.split(
806 self.tk.call(self._w, 'configure')):
807 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
808 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000809 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000810 x = self.tk.split(self.tk.call(
811 self._w, 'configure', '-'+cnf))
812 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000813 for k in cnf.keys():
Guido van Rossum37dcab11996-05-16 16:00:19 +0000814 if type(k) is ClassType:
Guido van Rossum18468821994-06-20 07:49:28 +0000815 k.config(self, cnf[k])
816 del cnf[k]
817 apply(self.tk.call, (self._w, 'configure')
818 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000819 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000820 def __getitem__(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000821 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum18468821994-06-20 07:49:28 +0000822 def __setitem__(self, key, value):
823 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000824 def keys(self):
825 return map(lambda x: x[0][1:],
826 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000827 def __str__(self):
828 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000829 def destroy(self):
830 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000831 if self.master.children.has_key(self._name):
832 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000833 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000834 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000835 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000836
837class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000838 def __init__(self, master=None, cnf={}, **kw):
839 if kw:
840 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000841 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 for wmkey in ['screen', 'class_', 'class', 'visual',
843 'colormap']:
844 if cnf.has_key(wmkey):
845 val = cnf[wmkey]
846 # TBD: a hack needed because some keys
847 # are not valid as keyword arguments
848 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
849 else: opt = '-'+wmkey
850 extra = extra + (opt, val)
851 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000852 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000853 root = self._root()
854 self.iconname(root.iconname())
855 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000856
857class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000858 def __init__(self, master=None, cnf={}, **kw):
859 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000860 def tkButtonEnter(self, *dummy):
861 self.tk.call('tkButtonEnter', self._w)
862 def tkButtonLeave(self, *dummy):
863 self.tk.call('tkButtonLeave', self._w)
864 def tkButtonDown(self, *dummy):
865 self.tk.call('tkButtonDown', self._w)
866 def tkButtonUp(self, *dummy):
867 self.tk.call('tkButtonUp', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000868 def flash(self):
869 self.tk.call(self._w, 'flash')
870 def invoke(self):
871 self.tk.call(self._w, 'invoke')
872
873# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000874# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000875def AtEnd():
876 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000877def AtInsert(*args):
878 s = 'insert'
879 for a in args:
880 if a: s = s + (' ' + a)
881 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000882def AtSelFirst():
883 return 'sel.first'
884def AtSelLast():
885 return 'sel.last'
886def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000887 if y is None:
888 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000889 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000890 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000891
892class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000893 def __init__(self, master=None, cnf={}, **kw):
894 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000895 def addtag(self, *args):
896 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000897 def addtag_above(self, tagOrId):
898 self.addtag('above', tagOrId)
899 def addtag_all(self):
900 self.addtag('all')
901 def addtag_below(self, tagOrId):
902 self.addtag('below', tagOrId)
903 def addtag_closest(self, x, y, halo=None, start=None):
904 self.addtag('closest', x, y, halo, start)
905 def addtag_enclosed(self, x1, y1, x2, y2):
906 self.addtag('enclosed', x1, y1, x2, y2)
907 def addtag_overlapping(self, x1, y1, x2, y2):
908 self.addtag('overlapping', x1, y1, x2, y2)
909 def addtag_withtag(self, tagOrId):
910 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000911 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000912 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000913 def tag_unbind(self, tagOrId, sequence):
914 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000915 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
916 return self._bind((self._w, 'tag', 'bind', tagOrId),
917 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +0000918 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000919 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000920 self._w, 'canvasx', screenx, gridspacing))
921 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000922 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000923 self._w, 'canvasy', screeny, gridspacing))
924 def coords(self, *args):
925 return self._do('coords', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000926 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000927 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000928 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000929 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000930 args = args[:-1]
931 else:
932 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000933 return self.tk.getint(apply(
934 self.tk.call,
935 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000936 + args + self._options(cnf, kw)))
937 def create_arc(self, *args, **kw):
938 return self._create('arc', args, kw)
939 def create_bitmap(self, *args, **kw):
940 return self._create('bitmap', args, kw)
941 def create_image(self, *args, **kw):
942 return self._create('image', args, kw)
943 def create_line(self, *args, **kw):
944 return self._create('line', args, kw)
945 def create_oval(self, *args, **kw):
946 return self._create('oval', args, kw)
947 def create_polygon(self, *args, **kw):
948 return self._create('polygon', args, kw)
949 def create_rectangle(self, *args, **kw):
950 return self._create('rectangle', args, kw)
951 def create_text(self, *args, **kw):
952 return self._create('text', args, kw)
953 def create_window(self, *args, **kw):
954 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000955 def dchars(self, *args):
956 self._do('dchars', args)
957 def delete(self, *args):
958 self._do('delete', args)
959 def dtag(self, *args):
960 self._do('dtag', args)
961 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000962 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000963 def find_above(self, tagOrId):
964 return self.find('above', tagOrId)
965 def find_all(self):
966 return self.find('all')
967 def find_below(self, tagOrId):
968 return self.find('below', tagOrId)
969 def find_closest(self, x, y, halo=None, start=None):
970 return self.find('closest', x, y, halo, start)
971 def find_enclosed(self, x1, y1, x2, y2):
972 return self.find('enclosed', x1, y1, x2, y2)
973 def find_overlapping(self, x1, y1, x2, y2):
974 return self.find('overlapping', x1, y1, x2, y2)
975 def find_withtag(self, tagOrId):
976 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000977 def focus(self, *args):
978 return self._do('focus', args)
979 def gettags(self, *args):
980 return self.tk.splitlist(self._do('gettags', args))
981 def icursor(self, *args):
982 self._do('icursor', args)
983 def index(self, *args):
984 return self.tk.getint(self._do('index', args))
985 def insert(self, *args):
986 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000987 def itemcget(self, tagOrId, option):
988 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000989 def itemconfig(self, tagOrId, cnf=None, **kw):
990 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000991 cnf = {}
992 for x in self.tk.split(
993 self._do('itemconfigure', (tagOrId))):
994 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
995 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000996 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000997 x = self.tk.split(self._do('itemconfigure',
998 (tagOrId, '-'+cnf,)))
999 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001000 self._do('itemconfigure', (tagOrId,)
1001 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001002 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001003 def lower(self, *args):
1004 self._do('lower', args)
1005 def move(self, *args):
1006 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001007 def postscript(self, cnf={}, **kw):
1008 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001009 def tkraise(self, *args):
1010 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001011 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001012 def scale(self, *args):
1013 self._do('scale', args)
1014 def scan_mark(self, x, y):
1015 self.tk.call(self._w, 'scan', 'mark', x, y)
1016 def scan_dragto(self, x, y):
1017 self.tk.call(self._w, 'scan', 'dragto', x, y)
1018 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001019 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001020 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001021 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001022 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001023 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001024 def select_item(self):
1025 self.tk.call(self._w, 'select', 'item')
1026 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001027 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001028 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001029 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001030 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001031 if not args:
1032 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001033 apply(self.tk.call, (self._w, 'xview')+args)
1034 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001035 if not args:
1036 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001037 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001038
1039class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001040 def __init__(self, master=None, cnf={}, **kw):
1041 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001042 def deselect(self):
1043 self.tk.call(self._w, 'deselect')
1044 def flash(self):
1045 self.tk.call(self._w, 'flash')
1046 def invoke(self):
1047 self.tk.call(self._w, 'invoke')
1048 def select(self):
1049 self.tk.call(self._w, 'select')
1050 def toggle(self):
1051 self.tk.call(self._w, 'toggle')
1052
1053class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001054 def __init__(self, master=None, cnf={}, **kw):
1055 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001056 def delete(self, first, last=None):
1057 self.tk.call(self._w, 'delete', first, last)
1058 def get(self):
1059 return self.tk.call(self._w, 'get')
1060 def icursor(self, index):
1061 self.tk.call(self._w, 'icursor', index)
1062 def index(self, index):
1063 return self.tk.getint(self.tk.call(
1064 self._w, 'index', index))
1065 def insert(self, index, string):
1066 self.tk.call(self._w, 'insert', index, string)
1067 def scan_mark(self, x):
1068 self.tk.call(self._w, 'scan', 'mark', x)
1069 def scan_dragto(self, x):
1070 self.tk.call(self._w, 'scan', 'dragto', x)
1071 def select_adjust(self, index):
1072 self.tk.call(self._w, 'select', 'adjust', index)
1073 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001074 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001075 def select_from(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001076 self.tk.call(self._w, 'select', 'set', index)
Guido van Rossum1d59df21995-08-11 14:21:06 +00001077 def select_present(self):
1078 return self.tk.getboolean(
1079 self.tk.call(self._w, 'select', 'present'))
1080 def select_range(self, start, end):
1081 self.tk.call(self._w, 'select', 'range', start, end)
Guido van Rossum18468821994-06-20 07:49:28 +00001082 def select_to(self, index):
1083 self.tk.call(self._w, 'select', 'to', index)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001084 def view(self, index):
1085 self.tk.call(self._w, 'view', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001086
1087class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001088 def __init__(self, master=None, cnf={}, **kw):
1089 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001090 extra = ()
1091 if cnf.has_key('class'):
1092 extra = ('-class', cnf['class'])
1093 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001094 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001095
1096class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001097 def __init__(self, master=None, cnf={}, **kw):
1098 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001099
Guido van Rossum18468821994-06-20 07:49:28 +00001100class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001101 def __init__(self, master=None, cnf={}, **kw):
1102 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001103 def activate(self, index):
1104 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001105 def bbox(self, *args):
1106 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001107 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001108 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001109 return self.tk.splitlist(self.tk.call(
1110 self._w, 'curselection'))
1111 def delete(self, first, last=None):
1112 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001113 def get(self, first, last=None):
1114 if last:
1115 return self.tk.splitlist(self.tk.call(
1116 self._w, 'get', first, last))
1117 else:
1118 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001119 def insert(self, index, *elements):
1120 apply(self.tk.call,
1121 (self._w, 'insert', index) + elements)
1122 def nearest(self, y):
1123 return self.tk.getint(self.tk.call(
1124 self._w, 'nearest', y))
1125 def scan_mark(self, x, y):
1126 self.tk.call(self._w, 'scan', 'mark', x, y)
1127 def scan_dragto(self, x, y):
1128 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001129 def see(self, index):
1130 self.tk.call(self._w, 'see', index)
1131 def index(self, index):
1132 i = self.tk.call(self._w, 'index', index)
1133 if i == 'none': return None
1134 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001135 def select_adjust(self, index):
1136 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001137 def select_anchor(self, index):
1138 self.tk.call(self._w, 'selection', 'anchor', index)
1139 def select_clear(self, first, last=None):
1140 self.tk.call(self._w,
1141 'selection', 'clear', first, last)
1142 def select_includes(self, index):
1143 return self.tk.getboolean(self.tk.call(
1144 self._w, 'selection', 'includes', index))
1145 def select_set(self, first, last=None):
1146 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001147 def size(self):
1148 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001149 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001150 if not what:
1151 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001152 apply(self.tk.call, (self._w, 'xview')+what)
1153 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001154 if not what:
1155 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001156 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001157
1158class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001159 def __init__(self, master=None, cnf={}, **kw):
1160 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001161 def tk_bindForTraversal(self):
1162 self.tk.call('tk_bindForTraversal', self._w)
1163 def tk_mbPost(self):
1164 self.tk.call('tk_mbPost', self._w)
1165 def tk_mbUnpost(self):
1166 self.tk.call('tk_mbUnpost')
1167 def tk_traverseToMenu(self, char):
1168 self.tk.call('tk_traverseToMenu', self._w, char)
1169 def tk_traverseWithinMenu(self, char):
1170 self.tk.call('tk_traverseWithinMenu', self._w, char)
1171 def tk_getMenuButtons(self):
1172 return self.tk.call('tk_getMenuButtons', self._w)
1173 def tk_nextMenu(self, count):
1174 self.tk.call('tk_nextMenu', count)
1175 def tk_nextMenuEntry(self, count):
1176 self.tk.call('tk_nextMenuEntry', count)
1177 def tk_invokeMenu(self):
1178 self.tk.call('tk_invokeMenu', self._w)
1179 def tk_firstMenu(self):
1180 self.tk.call('tk_firstMenu', self._w)
1181 def tk_mbButtonDown(self):
1182 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001183 def tk_popup(self, x, y, entry=""):
1184 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001185 def activate(self, index):
1186 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001187 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001188 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001189 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001190 def add_cascade(self, cnf={}, **kw):
1191 self.add('cascade', cnf or kw)
1192 def add_checkbutton(self, cnf={}, **kw):
1193 self.add('checkbutton', cnf or kw)
1194 def add_command(self, cnf={}, **kw):
1195 self.add('command', cnf or kw)
1196 def add_radiobutton(self, cnf={}, **kw):
1197 self.add('radiobutton', cnf or kw)
1198 def add_separator(self, cnf={}, **kw):
1199 self.add('separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001200 def delete(self, index1, index2=None):
1201 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001202 def entryconfig(self, index, cnf=None, **kw):
1203 if cnf is None and not kw:
1204 cnf = {}
1205 for x in self.tk.split(apply(self.tk.call,
1206 (self._w, 'entryconfigure', index))):
1207 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1208 return cnf
1209 if type(cnf) == StringType and not kw:
1210 x = self.tk.split(apply(self.tk.call,
1211 (self._w, 'entryconfigure', index, '-'+cnf)))
1212 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001213 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001214 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001215 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001216 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001217 i = self.tk.call(self._w, 'index', index)
1218 if i == 'none': return None
1219 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001220 def invoke(self, index):
1221 return self.tk.call(self._w, 'invoke', index)
1222 def post(self, x, y):
1223 self.tk.call(self._w, 'post', x, y)
1224 def unpost(self):
1225 self.tk.call(self._w, 'unpost')
1226 def yposition(self, index):
1227 return self.tk.getint(self.tk.call(
1228 self._w, 'yposition', index))
1229
1230class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001231 def __init__(self, master=None, cnf={}, **kw):
1232 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001233
1234class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001235 def __init__(self, master=None, cnf={}, **kw):
1236 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001237
1238class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001239 def __init__(self, master=None, cnf={}, **kw):
1240 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001241 def deselect(self):
1242 self.tk.call(self._w, 'deselect')
1243 def flash(self):
1244 self.tk.call(self._w, 'flash')
1245 def invoke(self):
1246 self.tk.call(self._w, 'invoke')
1247 def select(self):
1248 self.tk.call(self._w, 'select')
1249
1250class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001251 def __init__(self, master=None, cnf={}, **kw):
1252 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001253 def get(self):
1254 return self.tk.getint(self.tk.call(self._w, 'get'))
1255 def set(self, value):
1256 self.tk.call(self._w, 'set', value)
1257
1258class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001259 def __init__(self, master=None, cnf={}, **kw):
1260 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001261 def activate(self, index):
1262 self.tk.call(self._w, 'activate', index)
1263 def delta(self, deltax, deltay):
1264 return self.getdouble(self.tk.call(
1265 self._w, 'delta', deltax, deltay))
1266 def fraction(self, x, y):
1267 return self.getdouble(self.tk.call(
1268 self._w, 'fraction', x, y))
1269 def identify(self, x, y):
1270 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001271 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001272 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001273 def set(self, *args):
1274 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001275
1276class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001277 def __init__(self, master=None, cnf={}, **kw):
1278 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001279 self.bind('<Delete>', self.bspace)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001280 def bbox(self, *args):
1281 return self._getints(self._do('bbox', args)) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001282 def bspace(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001283 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001284 def tk_textSelectTo(self, index):
1285 self.tk.call('tk_textSelectTo', self._w, index)
1286 def tk_textBackspace(self):
1287 self.tk.call('tk_textBackspace', self._w)
1288 def tk_textIndexCloser(self, a, b, c):
1289 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1290 def tk_textResetAnchor(self, index):
1291 self.tk.call('tk_textResetAnchor', self._w, index)
1292 def compare(self, index1, op, index2):
1293 return self.tk.getboolean(self.tk.call(
1294 self._w, 'compare', index1, op, index2))
1295 def debug(self, boolean=None):
1296 return self.tk.getboolean(self.tk.call(
1297 self._w, 'debug', boolean))
1298 def delete(self, index1, index2=None):
1299 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001300 def dlineinfo(self, index):
1301 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001302 def get(self, index1, index2=None):
1303 return self.tk.call(self._w, 'get', index1, index2)
1304 def index(self, index):
1305 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001306 def insert(self, index, chars, *args):
1307 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001308 def mark_gravity(self, markName, direction=None):
1309 return apply(self.tk.call,
1310 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001311 def mark_names(self):
1312 return self.tk.splitlist(self.tk.call(
1313 self._w, 'mark', 'names'))
1314 def mark_set(self, markName, index):
1315 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001316 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001317 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001318 def scan_mark(self, x, y):
1319 self.tk.call(self._w, 'scan', 'mark', x, y)
1320 def scan_dragto(self, x, y):
1321 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001322 def search(self, pattern, index, stopindex=None,
1323 forwards=None, backwards=None, exact=None,
1324 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001325 args = [self._w, 'search']
1326 if forwards: args.append('-forwards')
1327 if backwards: args.append('-backwards')
1328 if exact: args.append('-exact')
1329 if regexp: args.append('-regexp')
1330 if nocase: args.append('-nocase')
1331 if count: args.append('-count'); args.append(count)
1332 if pattern[0] == '-': args.append('--')
1333 args.append(pattern)
1334 args.append(index)
1335 if stopindex: args.append(stopindex)
1336 return apply(self.tk.call, tuple(args))
1337 def see(self, index):
1338 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001339 def tag_add(self, tagName, index1, index2=None):
1340 self.tk.call(
1341 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001342 def tag_unbind(self, tagName, sequence):
1343 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001344 def tag_bind(self, tagName, sequence, func, add=None):
1345 return self._bind((self._w, 'tag', 'bind', tagName),
1346 sequence, func, add)
1347 def tag_cget(self, tagName, option):
1348 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001349 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001350 if type(cnf) == StringType:
1351 x = self.tk.split(self.tk.call(
1352 self._w, 'tag', 'configure', tagName, '-'+cnf))
1353 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001354 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001355 (self._w, 'tag', 'configure', tagName)
1356 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001357 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001358 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001359 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001360 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001361 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001362 def tag_names(self, index=None):
1363 return self.tk.splitlist(
1364 self.tk.call(self._w, 'tag', 'names', index))
1365 def tag_nextrange(self, tagName, index1, index2=None):
1366 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001367 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001368 def tag_raise(self, tagName, aboveThis=None):
1369 self.tk.call(
1370 self._w, 'tag', 'raise', tagName, aboveThis)
1371 def tag_ranges(self, tagName):
1372 return self.tk.splitlist(self.tk.call(
1373 self._w, 'tag', 'ranges', tagName))
1374 def tag_remove(self, tagName, index1, index2=None):
1375 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001376 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001377 def window_cget(self, index, option):
1378 return self.tk.call(self._w, 'window', 'cget', index, option)
1379 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001380 if type(cnf) == StringType:
1381 x = self.tk.split(self.tk.call(
1382 self._w, 'window', 'configure',
1383 index, '-'+cnf))
1384 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001385 apply(self.tk.call,
1386 (self._w, 'window', 'configure', index)
1387 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001388 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001389 def window_create(self, index, cnf={}, **kw):
1390 apply(self.tk.call,
1391 (self._w, 'window', 'create', index)
1392 + self._options(cnf, kw))
1393 def window_names(self):
1394 return self.tk.splitlist(
1395 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001396 def xview(self, *what):
1397 if not what:
1398 return self._getdoubles(self.tk.call(self._w, 'xview'))
1399 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001400 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001401 if not what:
1402 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001403 apply(self.tk.call, (self._w, 'yview')+what)
1404 def yview_pickplace(self, *what):
1405 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001406
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001407class OptionMenu(Widget):
1408 def __init__(self, master, variable, value, *values):
1409 self.widgetName = 'tk_optionMenu'
1410 Widget._setup(self, master, {})
1411 self.menuname = apply(
1412 self.tk.call,
1413 (self.widgetName, self._w, variable, value) + values)
1414
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001415class Image:
1416 def __init__(self, imgtype, name=None, cnf={}, **kw):
1417 self.name = None
1418 master = _default_root
1419 if not master: raise RuntimeError, 'Too early to create image'
1420 self.tk = master.tk
1421 if not name: name = `id(self)`
1422 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1423 elif kw: cnf = kw
1424 options = ()
1425 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001426 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001427 v = self._register(v)
1428 options = options + ('-'+k, v)
1429 apply(self.tk.call,
1430 ('image', 'create', imgtype, name,) + options)
1431 self.name = name
1432 def __str__(self): return self.name
1433 def __del__(self):
1434 if self.name:
1435 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001436 def __setitem__(self, key, value):
1437 self.tk.call(self.name, 'configure', '-'+key, value)
1438 def __getitem__(self, key):
1439 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001440 def height(self):
1441 return self.tk.getint(
1442 self.tk.call('image', 'height', self.name))
1443 def type(self):
1444 return self.tk.call('image', 'type', self.name)
1445 def width(self):
1446 return self.tk.getint(
1447 self.tk.call('image', 'width', self.name))
1448
1449class PhotoImage(Image):
1450 def __init__(self, name=None, cnf={}, **kw):
1451 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001452 def blank(self):
1453 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001454 def cget(self, option):
1455 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001456 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001457 def __getitem__(self, key):
1458 return self.tk.call(self.name, 'cget', '-' + key)
1459 def copy(self):
1460 destImage = PhotoImage()
1461 self.tk.call(destImage, 'copy', self.name)
1462 return destImage
1463 def zoom(self,x,y=''):
1464 destImage = PhotoImage()
1465 if y=='': y=x
1466 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1467 return destImage
1468 def subsample(self,x,y=''):
1469 destImage = PhotoImage()
1470 if y=='': y=x
1471 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1472 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001473 def get(self, x, y):
1474 return self.tk.call(self.name, 'get', x, y)
1475 def put(self, data, to=None):
1476 args = (self.name, 'put', data)
1477 if to:
1478 args = args + to
1479 apply(self.tk.call, args)
1480 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001481 def write(self, filename, format=None, from_coords=None):
1482 args = (self.name, 'write', filename)
1483 if format:
1484 args = args + ('-format', format)
1485 if from_coords:
1486 args = args + ('-from',) + tuple(from_coords)
1487 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001488
1489class BitmapImage(Image):
1490 def __init__(self, name=None, cnf={}, **kw):
1491 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1492
1493def image_names(): return _default_root.tk.call('image', 'names')
1494def image_types(): return _default_root.tk.call('image', 'types')
1495
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001496######################################################################
1497# Extensions:
1498
1499class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001500 def __init__(self, master=None, cnf={}, **kw):
1501 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001502 self.bind('<Any-Enter>', self.tkButtonEnter)
1503 self.bind('<Any-Leave>', self.tkButtonLeave)
1504 self.bind('<1>', self.tkButtonDown)
1505 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001506
1507class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001508 def __init__(self, master=None, cnf={}, **kw):
1509 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001510 self.bind('<Any-Enter>', self.tkButtonEnter)
1511 self.bind('<Any-Leave>', self.tkButtonLeave)
1512 self.bind('<1>', self.tkButtonDown)
1513 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1514 self['fg'] = self['bg']
1515 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001516
1517
1518# Emacs cruft
1519# Local Variables:
1520# py-indent-offset: 8
1521# End: