blob: cc2316e562245dd5257720245f94c0dc662e18eb [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 Rossum36269991996-05-16 17:11:27 +000027TkVersion = _string.atof(tkinter.TK_VERSION)
28TclVersion = _string.atof(tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000029
Guido van Rossum36269991996-05-16 17:11:27 +000030######################################################################
31# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
32# they are defined here (and not in Tkconstants):
33######################################################################
34if TkVersion >= 4.1:
35 READABLE = 2
36 WRITABLE = 4
37 EXCEPTION = 8
38else:
39 READABLE = 1
40 WRITABLE = 2
41 EXCEPTION = 4
42
43
Guido van Rossum2dcf5291994-07-06 09:23:20 +000044def _flatten(tuple):
45 res = ()
46 for item in tuple:
47 if type(item) in (TupleType, ListType):
48 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000049 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000050 res = res + (item,)
51 return res
52
53def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000054 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000055 return cnfs
56 elif type(cnfs) in (NoneType, StringType):
57
Guido van Rossum2dcf5291994-07-06 09:23:20 +000058 return cnfs
59 else:
60 cnf = {}
61 for c in _flatten(cnfs):
62 for k, v in c.items():
63 cnf[k] = v
64 return cnf
65
66class Event:
67 pass
68
Guido van Rossumaec5dc91994-06-27 07:55:12 +000069_default_root = None
70
Guido van Rossum45853db1994-06-20 12:19:19 +000071def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000072 pass
73
Guido van Rossum97aeca11994-07-07 13:12:12 +000074def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000075 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000076
Guido van Rossumaec5dc91994-06-27 07:55:12 +000077_varnum = 0
78class Variable:
79 def __init__(self, master=None):
80 global _default_root
81 global _varnum
82 if master:
83 self._tk = master.tk
84 else:
85 self._tk = _default_root.tk
86 self._name = 'PY_VAR' + `_varnum`
87 _varnum = _varnum + 1
88 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000089 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000090 def __str__(self):
91 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000092 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000093 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000094
95class StringVar(Variable):
96 def __init__(self, master=None):
97 Variable.__init__(self, master)
98 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000099 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000100
101class IntVar(Variable):
102 def __init__(self, master=None):
103 Variable.__init__(self, master)
104 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000105 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000106
107class DoubleVar(Variable):
108 def __init__(self, master=None):
109 Variable.__init__(self, master)
110 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000111 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000112
113class BooleanVar(Variable):
114 def __init__(self, master=None):
115 Variable.__init__(self, master)
116 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000117 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000118
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000119def mainloop(n=0):
120 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000121
122def getint(s):
123 return _default_root.tk.getint(s)
124
125def getdouble(s):
126 return _default_root.tk.getdouble(s)
127
128def getboolean(s):
129 return _default_root.tk.getboolean(s)
130
Guido van Rossum18468821994-06-20 07:49:28 +0000131class Misc:
132 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000133 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000134 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000135 def tk_menuBar(self, *args):
136 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000137 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000138 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000139 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000140 def wait_window(self, window=None):
141 if window == None:
142 window = self
143 self.tk.call('tkwait', 'window', window._w)
144 def wait_visibility(self, window=None):
145 if window == None:
146 window = self
147 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000148 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000149 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000150 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000151 return self.tk.getvar(name)
152 def getint(self, s):
153 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000154 def getdouble(self, s):
155 return self.tk.getdouble(s)
156 def getboolean(self, s):
157 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000158 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000159 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000160 focus = focus_set # XXX b/w compat?
161 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000162 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000163 def focus_default_none(self):
164 self.tk.call('focus', 'default', 'none')
165 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000166 def focus_none(self):
167 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000168 def focus_get(self):
169 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000170 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000171 return self._nametowidget(name)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000172 def tk_focusNext(self):
173 name = self.tk.call('tk_focusNext', self._w)
174 if not name: return None
175 return self._nametowidget(name)
176 def tk_focusPrev(self):
177 name = self.tk.call('tk_focusPrev', self._w)
178 if not name: return None
179 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000180 def after(self, ms, func=None, *args):
181 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000182 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000183 self.tk.call('after', ms)
184 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000185 # XXX Disgusting hack to clean up after calling func
186 tmp = []
187 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
188 try:
189 apply(func, args)
190 finally:
191 tk.deletecommand(tmp[0])
192 name = self._register(callit)
193 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000194 return self.tk.call('after', ms, name)
195 def after_idle(self, func, *args):
196 return apply(self.after, ('idle', func) + args)
197 def after_cancel(self, id):
198 self.tk.call('after', 'cancel', id)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000199 def bell(self, displayof=None):
200 if displayof:
201 self.tk.call('bell', '-displayof', displayof)
202 else:
203 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000204 # XXX grab current w/o window argument
205 def grab_current(self):
206 name = self.tk.call('grab', 'current', self._w)
207 if not name: return None
208 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000209 def grab_release(self):
210 self.tk.call('grab', 'release', self._w)
211 def grab_set(self):
212 self.tk.call('grab', 'set', self._w)
213 def grab_set_global(self):
214 self.tk.call('grab', 'set', '-global', self._w)
215 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000216 status = self.tk.call('grab', 'status', self._w)
217 if status == 'none': status = None
218 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000219 def lower(self, belowThis=None):
220 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000221 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000222 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000223 def option_clear(self):
224 self.tk.call('option', 'clear')
225 def option_get(self, name, className):
226 return self.tk.call('option', 'get', self._w, name, className)
227 def option_readfile(self, fileName, priority = None):
228 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000229 def selection_clear(self):
230 self.tk.call('selection', 'clear', self._w)
231 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000232 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000233 def selection_handle(self, func, type=None, format=None):
234 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000235 self.tk.call('selection', 'handle', self._w,
236 name, type, format)
237 def selection_own(self, func=None):
238 name = self._register(func)
239 self.tk.call('selection', 'own', self._w, name)
240 def selection_own_get(self):
241 return self._nametowidget(self.tk.call('selection', 'own'))
242 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000243 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000244 def lower(self, belowThis=None):
245 self.tk.call('lift', self._w, belowThis)
246 def tkraise(self, aboveThis=None):
247 self.tk.call('raise', self._w, aboveThis)
248 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000249 def colormodel(self, value=None):
250 return self.tk.call('tk', 'colormodel', self._w, value)
251 def winfo_atom(self, name):
252 return self.tk.getint(self.tk.call('winfo', 'atom', name))
253 def winfo_atomname(self, id):
254 return self.tk.call('winfo', 'atomname', id)
255 def winfo_cells(self):
256 return self.tk.getint(
257 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000258 def winfo_children(self):
259 return map(self._nametowidget,
260 self.tk.splitlist(self.tk.call(
261 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000262 def winfo_class(self):
263 return self.tk.call('winfo', 'class', self._w)
264 def winfo_containing(self, rootX, rootY):
Guido van Rossum36269991996-05-16 17:11:27 +0000265 return self.tk.call('winfo', 'containing', rootX, rootY)
Guido van Rossum18468821994-06-20 07:49:28 +0000266 def winfo_depth(self):
267 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
268 def winfo_exists(self):
269 return self.tk.getint(
270 self.tk.call('winfo', 'exists', self._w))
271 def winfo_fpixels(self, number):
272 return self.tk.getdouble(self.tk.call(
273 'winfo', 'fpixels', self._w, number))
274 def winfo_geometry(self):
275 return self.tk.call('winfo', 'geometry', self._w)
276 def winfo_height(self):
277 return self.tk.getint(
278 self.tk.call('winfo', 'height', self._w))
279 def winfo_id(self):
280 return self.tk.getint(
281 self.tk.call('winfo', 'id', self._w))
282 def winfo_interps(self):
283 return self.tk.splitlist(
284 self.tk.call('winfo', 'interps'))
285 def winfo_ismapped(self):
286 return self.tk.getint(
287 self.tk.call('winfo', 'ismapped', self._w))
288 def winfo_name(self):
289 return self.tk.call('winfo', 'name', self._w)
290 def winfo_parent(self):
291 return self.tk.call('winfo', 'parent', self._w)
292 def winfo_pathname(self, id):
293 return self.tk.call('winfo', 'pathname', id)
294 def winfo_pixels(self, number):
295 return self.tk.getint(
296 self.tk.call('winfo', 'pixels', self._w, number))
297 def winfo_reqheight(self):
298 return self.tk.getint(
299 self.tk.call('winfo', 'reqheight', self._w))
300 def winfo_reqwidth(self):
301 return self.tk.getint(
302 self.tk.call('winfo', 'reqwidth', self._w))
303 def winfo_rgb(self, color):
304 return self._getints(
305 self.tk.call('winfo', 'rgb', self._w, color))
306 def winfo_rootx(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'rootx', self._w))
309 def winfo_rooty(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'rooty', self._w))
312 def winfo_screen(self):
313 return self.tk.call('winfo', 'screen', self._w)
314 def winfo_screencells(self):
315 return self.tk.getint(
316 self.tk.call('winfo', 'screencells', self._w))
317 def winfo_screendepth(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'screendepth', self._w))
320 def winfo_screenheight(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'screenheight', self._w))
323 def winfo_screenmmheight(self):
324 return self.tk.getint(
325 self.tk.call('winfo', 'screenmmheight', self._w))
326 def winfo_screenmmwidth(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'screenmmwidth', self._w))
329 def winfo_screenvisual(self):
330 return self.tk.call('winfo', 'screenvisual', self._w)
331 def winfo_screenwidth(self):
332 return self.tk.getint(
333 self.tk.call('winfo', 'screenwidth', self._w))
334 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000335 return self._nametowidget(self.tk.call(
336 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000337 def winfo_visual(self):
338 return self.tk.call('winfo', 'visual', self._w)
339 def winfo_vrootheight(self):
340 return self.tk.getint(
341 self.tk.call('winfo', 'vrootheight', self._w))
342 def winfo_vrootwidth(self):
343 return self.tk.getint(
344 self.tk.call('winfo', 'vrootwidth', self._w))
345 def winfo_vrootx(self):
346 return self.tk.getint(
347 self.tk.call('winfo', 'vrootx', self._w))
348 def winfo_vrooty(self):
349 return self.tk.getint(
350 self.tk.call('winfo', 'vrooty', self._w))
351 def winfo_width(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'width', self._w))
354 def winfo_x(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'x', self._w))
357 def winfo_y(self):
358 return self.tk.getint(
359 self.tk.call('winfo', 'y', self._w))
360 def update(self):
361 self.tk.call('update')
362 def update_idletasks(self):
363 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000364 def bindtags(self, tagList=None):
365 if tagList is None:
366 return self.tk.splitlist(
367 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000368 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000369 self.tk.call('bindtags', self._w, tagList)
370 def _bind(self, what, sequence, func, add):
371 if func:
372 cmd = ("%sset _tkinter_break [%s %s]\n"
373 'if {"$_tkinter_break" == "break"} break\n') \
374 % (add and '+' or '',
375 self._register(func, self._substitute),
376 _string.join(self._subst_format))
377 apply(self.tk.call, what + (sequence, cmd))
378 elif func == '':
379 apply(self.tk.call, what + (sequence, func))
380 else:
381 return apply(self.tk.call, what + (sequence,))
382 def bind(self, sequence=None, func=None, add=None):
383 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000384 def unbind(self, sequence):
385 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000386 def bind_all(self, sequence=None, func=None, add=None):
387 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000388 def unbind_all(self, sequence):
389 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000390 def bind_class(self, className, sequence=None, func=None, add=None):
391 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000392 def unbind_class(self, className, sequence):
393 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000394 def mainloop(self, n=0):
395 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000396 def quit(self):
397 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000398 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000399 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000400 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
401 def _getdoubles(self, string):
402 if not string: return None
403 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000404 def _getboolean(self, string):
405 if string:
406 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000407 def _options(self, cnf, kw = None):
408 if kw:
409 cnf = _cnfmerge((cnf, kw))
410 else:
411 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000412 res = ()
413 for k, v in cnf.items():
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000414 if k[-1] == '_': k = k[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000415 if callable(v):
Guido van Rossum18468821994-06-20 07:49:28 +0000416 v = self._register(v)
417 res = res + ('-'+k, v)
418 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000419 def _nametowidget(self, name):
420 w = self
421 if name[0] == '.':
422 w = w._root()
423 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000424 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000425 while name:
426 i = find(name, '.')
427 if i >= 0:
428 name, tail = name[:i], name[i+1:]
429 else:
430 tail = ''
431 w = w.children[name]
432 name = tail
433 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000434 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000435 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000436 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000437 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000438 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000439 except AttributeError:
440 pass
441 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000442 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000443 except AttributeError:
444 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000445 self.tk.createcommand(name, f)
446 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000447 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000448 def _root(self):
449 w = self
450 while w.master: w = w.master
451 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000452 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000453 '%s', '%t', '%w', '%x', '%y',
454 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
455 def _substitute(self, *args):
456 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000457 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000458 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
459 # Missing: (a, c, d, m, o, v, B, R)
460 e = Event()
461 e.serial = tk.getint(nsign)
462 e.num = tk.getint(b)
463 try: e.focus = tk.getboolean(f)
464 except TclError: pass
465 e.height = tk.getint(h)
466 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000467 # For Visibility events, event state is a string and
468 # not an integer:
469 try:
470 e.state = tk.getint(s)
471 except TclError:
472 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000473 e.time = tk.getint(t)
474 e.width = tk.getint(w)
475 e.x = tk.getint(x)
476 e.y = tk.getint(y)
477 e.char = A
478 try: e.send_event = tk.getboolean(E)
479 except TclError: pass
480 e.keysym = K
481 e.keysym_num = tk.getint(N)
482 e.type = T
483 e.widget = self._nametowidget(W)
484 e.x_root = tk.getint(X)
485 e.y_root = tk.getint(Y)
486 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000487 def _report_exception(self):
488 import sys
489 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
490 root = self._root()
491 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000492
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000493class CallWrapper:
494 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000495 self.func = func
496 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000497 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000498 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000499 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000500 if self.subst:
501 args = apply(self.subst, args)
502 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000503 except SystemExit, msg:
504 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000505 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000506 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000507
508class Wm:
509 def aspect(self,
510 minNumer=None, minDenom=None,
511 maxNumer=None, maxDenom=None):
512 return self._getints(
513 self.tk.call('wm', 'aspect', self._w,
514 minNumer, minDenom,
515 maxNumer, maxDenom))
516 def client(self, name=None):
517 return self.tk.call('wm', 'client', self._w, name)
518 def command(self, value=None):
519 return self.tk.call('wm', 'command', self._w, value)
520 def deiconify(self):
521 return self.tk.call('wm', 'deiconify', self._w)
522 def focusmodel(self, model=None):
523 return self.tk.call('wm', 'focusmodel', self._w, model)
524 def frame(self):
525 return self.tk.call('wm', 'frame', self._w)
526 def geometry(self, newGeometry=None):
527 return self.tk.call('wm', 'geometry', self._w, newGeometry)
528 def grid(self,
529 baseWidht=None, baseHeight=None,
530 widthInc=None, heightInc=None):
531 return self._getints(self.tk.call(
532 'wm', 'grid', self._w,
533 baseWidht, baseHeight, widthInc, heightInc))
534 def group(self, pathName=None):
535 return self.tk.call('wm', 'group', self._w, pathName)
536 def iconbitmap(self, bitmap=None):
537 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
538 def iconify(self):
539 return self.tk.call('wm', 'iconify', self._w)
540 def iconmask(self, bitmap=None):
541 return self.tk.call('wm', 'iconmask', self._w, bitmap)
542 def iconname(self, newName=None):
543 return self.tk.call('wm', 'iconname', self._w, newName)
544 def iconposition(self, x=None, y=None):
545 return self._getints(self.tk.call(
546 'wm', 'iconposition', self._w, x, y))
547 def iconwindow(self, pathName=None):
548 return self.tk.call('wm', 'iconwindow', self._w, pathName)
549 def maxsize(self, width=None, height=None):
550 return self._getints(self.tk.call(
551 'wm', 'maxsize', self._w, width, height))
552 def minsize(self, width=None, height=None):
553 return self._getints(self.tk.call(
554 'wm', 'minsize', self._w, width, height))
555 def overrideredirect(self, boolean=None):
556 return self._getboolean(self.tk.call(
557 'wm', 'overrideredirect', self._w, boolean))
558 def positionfrom(self, who=None):
559 return self.tk.call('wm', 'positionfrom', self._w, who)
560 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000561 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000562 command = self._register(func)
563 else:
564 command = func
565 return self.tk.call(
566 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000567 def resizable(self, width=None, height=None):
568 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000569 def sizefrom(self, who=None):
570 return self.tk.call('wm', 'sizefrom', self._w, who)
571 def state(self):
572 return self.tk.call('wm', 'state', self._w)
573 def title(self, string=None):
574 return self.tk.call('wm', 'title', self._w, string)
575 def transient(self, master=None):
576 return self.tk.call('wm', 'transient', self._w, master)
577 def withdraw(self):
578 return self.tk.call('wm', 'withdraw', self._w)
579
580class Tk(Misc, Wm):
581 _w = '.'
582 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000583 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000584 self.master = None
585 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000586 if baseName is None:
587 import sys, os
588 baseName = os.path.basename(sys.argv[0])
589 if baseName[-3:] == '.py': baseName = baseName[:-3]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000590 self.tk = tkinter.create(screenName, baseName, className)
591 try:
592 # Disable event scanning except for Command-Period
593 import MacOS
594 MacOS.EnableAppswitch(0)
595 except ImportError:
596 pass
597 else:
598 # Work around nasty MacTk bug
599 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000600 # Version sanity checks
601 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000602 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000603 raise RuntimeError, \
604 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000605 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000606 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000607 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000608 raise RuntimeError, \
609 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000610 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000611 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000612 raise RuntimeError, \
613 "Tk 4.0 or higher is required; found Tk %s" \
614 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000615 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000616 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000617 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000618 if not _default_root:
619 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000620 def destroy(self):
621 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000622 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000623 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000624 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000625 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000626 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000627 if os.environ.has_key('HOME'): home = os.environ['HOME']
628 else: home = os.curdir
629 class_tcl = os.path.join(home, '.%s.tcl' % className)
630 class_py = os.path.join(home, '.%s.py' % className)
631 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
632 base_py = os.path.join(home, '.%s.py' % baseName)
633 dir = {'self': self}
634 exec 'from Tkinter import *' in dir
635 if os.path.isfile(class_tcl):
636 print 'source', `class_tcl`
637 self.tk.call('source', class_tcl)
638 if os.path.isfile(class_py):
639 print 'execfile', `class_py`
640 execfile(class_py, dir)
641 if os.path.isfile(base_tcl):
642 print 'source', `base_tcl`
643 self.tk.call('source', base_tcl)
644 if os.path.isfile(base_py):
645 print 'execfile', `base_py`
646 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000647 def report_callback_exception(self, exc, val, tb):
648 import traceback
649 print "Exception in Tkinter callback"
650 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000651
652class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000653 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000654 apply(self.tk.call,
655 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000656 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000657 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000658 pack = config
659 def __setitem__(self, key, value):
660 Pack.config({key: value})
661 def forget(self):
662 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000663 pack_forget = forget
664 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000665 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000666 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000667 dict = {}
668 for i in range(0, len(words), 2):
669 key = words[i][1:]
670 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000671 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000672 value = self._nametowidget(value)
673 dict[key] = value
674 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000675 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000676 _noarg_ = ['_noarg_']
677 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000678 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000679 return self._getboolean(self.tk.call(
680 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000681 else:
682 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000683 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000684 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000685 return map(self._nametowidget,
686 self.tk.splitlist(
687 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000688 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000689
690class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000691 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000692 for k in ['in_']:
693 if kw.has_key(k):
694 kw[k[:-1]] = kw[k]
695 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000696 apply(self.tk.call,
697 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000698 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000699 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000700 place = config
701 def __setitem__(self, key, value):
702 Place.config({key: value})
703 def forget(self):
704 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000705 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000706 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000707 words = self.tk.splitlist(
708 self.tk.call('place', 'info', self._w))
709 dict = {}
710 for i in range(0, len(words), 2):
711 key = words[i][1:]
712 value = words[i+1]
713 if value[:1] == '.':
714 value = self._nametowidget(value)
715 dict[key] = value
716 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000717 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000718 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000719 return map(self._nametowidget,
720 self.tk.splitlist(
721 self.tk.call(
722 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000723 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000724
Guido van Rossum37dcab11996-05-16 16:00:19 +0000725class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000726 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000727 def config(self, cnf={}, **kw):
728 apply(self.tk.call,
729 ('grid', 'configure', self._w)
730 + self._options(cnf, kw))
731 grid = config
732 def __setitem__(self, key, value):
733 Grid.config({key: value})
734 def bbox(self, column, row):
735 return self._getints(
736 self.tk.call(
737 'grid', 'bbox', self._w, column, row)) or None
738 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000739 def columnconfigure(self, index, cnf={}, **kw):
740 if type(cnf) is not DictionaryType and not kw:
741 options = self._options({cnf: None})
742 else:
743 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000744 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000745 ('grid', 'columnconfigure', self._w, index)
746 + options)
747 if options == ('-minsize', None):
748 return self.tk.getint(res) or None
749 elif options == ('-weight', None):
750 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000751 def forget(self):
752 self.tk.call('grid', 'forget', self._w)
753 grid_forget = forget
754 def info(self):
755 words = self.tk.splitlist(
756 self.tk.call('grid', 'info', self._w))
757 dict = {}
758 for i in range(0, len(words), 2):
759 key = words[i][1:]
760 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000761 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000762 value = self._nametowidget(value)
763 dict[key] = value
764 return dict
765 grid_info = info
766 def location(self, x, y):
767 return self._getints(
768 self.tk.call(
769 'grid', 'location', self._w, x, y)) or None
770 _noarg_ = ['_noarg_']
771 def propagate(self, flag=_noarg_):
772 if flag is Grid._noarg_:
773 return self._getboolean(self.tk.call(
774 'grid', 'propagate', self._w))
775 else:
776 self.tk.call('grid', 'propagate', self._w, flag)
777 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000778 def rowconfigure(self, index, cnf={}, **kw):
779 if type(cnf) is not DictionaryType and not kw:
780 options = self._options({cnf: None})
781 else:
782 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000783 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000784 ('grid', 'rowconfigure', self._w, index)
785 + options)
786 if options == ('-minsize', None):
787 return self.tk.getint(res) or None
788 elif options == ('-weight', None):
789 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000790 def size(self):
791 return self._getints(
792 self.tk.call('grid', 'size', self._w)) or None
793 def slaves(self, *args):
794 return map(self._nametowidget,
795 self.tk.splitlist(
796 apply(self.tk.call,
797 ('grid', 'slaves', self._w) + args)))
798 grid_slaves = slaves
799
800class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000801 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000802 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000803 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000804 if not _default_root:
805 _default_root = Tk()
806 master = _default_root
807 if not _default_root:
808 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000809 self.master = master
810 self.tk = master.tk
811 if cnf.has_key('name'):
812 name = cnf['name']
813 del cnf['name']
814 else:
815 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000816 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000817 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000818 self._w = '.' + name
819 else:
820 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000821 self.children = {}
822 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000823 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000824 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000825 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
826 if kw:
827 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000828 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000829 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000830 classes = []
831 for k in cnf.keys():
832 if type(k) is ClassType:
833 classes.append((k, cnf[k]))
834 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000835 apply(self.tk.call,
836 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000837 for k, v in classes:
838 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000839 def config(self, cnf=None, **kw):
840 # XXX ought to generalize this so tag_config etc. can use it
841 if kw:
842 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000843 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000844 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000845 if cnf is None:
846 cnf = {}
847 for x in self.tk.split(
848 self.tk.call(self._w, 'configure')):
849 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
850 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000851 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000852 x = self.tk.split(self.tk.call(
853 self._w, 'configure', '-'+cnf))
854 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000855 apply(self.tk.call, (self._w, 'configure')
856 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000857 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000858 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000859 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000860 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000861 def __setitem__(self, key, value):
862 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000863 def keys(self):
864 return map(lambda x: x[0][1:],
865 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000866 def __str__(self):
867 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000868 def destroy(self):
869 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000870 if self.master.children.has_key(self._name):
871 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000872 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000873 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000874 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000875
876class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000877 def __init__(self, master=None, cnf={}, **kw):
878 if kw:
879 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000880 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000881 for wmkey in ['screen', 'class_', 'class', 'visual',
882 'colormap']:
883 if cnf.has_key(wmkey):
884 val = cnf[wmkey]
885 # TBD: a hack needed because some keys
886 # are not valid as keyword arguments
887 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
888 else: opt = '-'+wmkey
889 extra = extra + (opt, val)
890 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000891 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000892 root = self._root()
893 self.iconname(root.iconname())
894 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000895
896class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000897 def __init__(self, master=None, cnf={}, **kw):
898 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000899 def tkButtonEnter(self, *dummy):
900 self.tk.call('tkButtonEnter', self._w)
901 def tkButtonLeave(self, *dummy):
902 self.tk.call('tkButtonLeave', self._w)
903 def tkButtonDown(self, *dummy):
904 self.tk.call('tkButtonDown', self._w)
905 def tkButtonUp(self, *dummy):
906 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000907 def tkButtonInvoke(self, *dummy):
908 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000909 def flash(self):
910 self.tk.call(self._w, 'flash')
911 def invoke(self):
912 self.tk.call(self._w, 'invoke')
913
914# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000915# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000916def AtEnd():
917 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000918def AtInsert(*args):
919 s = 'insert'
920 for a in args:
921 if a: s = s + (' ' + a)
922 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000923def AtSelFirst():
924 return 'sel.first'
925def AtSelLast():
926 return 'sel.last'
927def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000928 if y is None:
929 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000930 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000931 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000932
933class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000934 def __init__(self, master=None, cnf={}, **kw):
935 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000936 def addtag(self, *args):
937 self._do('addtag', args)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000938 def addtag_above(self, tagOrId):
939 self.addtag('above', tagOrId)
940 def addtag_all(self):
941 self.addtag('all')
942 def addtag_below(self, tagOrId):
943 self.addtag('below', tagOrId)
944 def addtag_closest(self, x, y, halo=None, start=None):
945 self.addtag('closest', x, y, halo, start)
946 def addtag_enclosed(self, x1, y1, x2, y2):
947 self.addtag('enclosed', x1, y1, x2, y2)
948 def addtag_overlapping(self, x1, y1, x2, y2):
949 self.addtag('overlapping', x1, y1, x2, y2)
950 def addtag_withtag(self, tagOrId):
951 self.addtag('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000952 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000953 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000954 def tag_unbind(self, tagOrId, sequence):
955 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000956 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +0000957 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +0000958 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +0000959 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000960 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000961 self._w, 'canvasx', screenx, gridspacing))
962 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000963 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000964 self._w, 'canvasy', screeny, gridspacing))
965 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +0000966 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +0000967 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000968 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000969 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000970 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000971 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000972 args = args[:-1]
973 else:
974 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000975 return self.tk.getint(apply(
976 self.tk.call,
977 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000978 + args + self._options(cnf, kw)))
979 def create_arc(self, *args, **kw):
980 return self._create('arc', args, kw)
981 def create_bitmap(self, *args, **kw):
982 return self._create('bitmap', args, kw)
983 def create_image(self, *args, **kw):
984 return self._create('image', args, kw)
985 def create_line(self, *args, **kw):
986 return self._create('line', args, kw)
987 def create_oval(self, *args, **kw):
988 return self._create('oval', args, kw)
989 def create_polygon(self, *args, **kw):
990 return self._create('polygon', args, kw)
991 def create_rectangle(self, *args, **kw):
992 return self._create('rectangle', args, kw)
993 def create_text(self, *args, **kw):
994 return self._create('text', args, kw)
995 def create_window(self, *args, **kw):
996 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000997 def dchars(self, *args):
998 self._do('dchars', args)
999 def delete(self, *args):
1000 self._do('delete', args)
1001 def dtag(self, *args):
1002 self._do('dtag', args)
1003 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001004 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001005 def find_above(self, tagOrId):
1006 return self.find('above', tagOrId)
1007 def find_all(self):
1008 return self.find('all')
1009 def find_below(self, tagOrId):
1010 return self.find('below', tagOrId)
1011 def find_closest(self, x, y, halo=None, start=None):
1012 return self.find('closest', x, y, halo, start)
1013 def find_enclosed(self, x1, y1, x2, y2):
1014 return self.find('enclosed', x1, y1, x2, y2)
1015 def find_overlapping(self, x1, y1, x2, y2):
1016 return self.find('overlapping', x1, y1, x2, y2)
1017 def find_withtag(self, tagOrId):
1018 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001019 def focus(self, *args):
1020 return self._do('focus', args)
1021 def gettags(self, *args):
1022 return self.tk.splitlist(self._do('gettags', args))
1023 def icursor(self, *args):
1024 self._do('icursor', args)
1025 def index(self, *args):
1026 return self.tk.getint(self._do('index', args))
1027 def insert(self, *args):
1028 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001029 def itemcget(self, tagOrId, option):
1030 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001031 def itemconfig(self, tagOrId, cnf=None, **kw):
1032 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001033 cnf = {}
1034 for x in self.tk.split(
1035 self._do('itemconfigure', (tagOrId))):
1036 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1037 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001038 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001039 x = self.tk.split(self._do('itemconfigure',
1040 (tagOrId, '-'+cnf,)))
1041 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001042 self._do('itemconfigure', (tagOrId,)
1043 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001044 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001045 def lower(self, *args):
1046 self._do('lower', args)
1047 def move(self, *args):
1048 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001049 def postscript(self, cnf={}, **kw):
1050 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001051 def tkraise(self, *args):
1052 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001053 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001054 def scale(self, *args):
1055 self._do('scale', args)
1056 def scan_mark(self, x, y):
1057 self.tk.call(self._w, 'scan', 'mark', x, y)
1058 def scan_dragto(self, x, y):
1059 self.tk.call(self._w, 'scan', 'dragto', x, y)
1060 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001061 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001062 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001063 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001064 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001065 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001066 def select_item(self):
1067 self.tk.call(self._w, 'select', 'item')
1068 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001069 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001070 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001071 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001072 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001073 if not args:
1074 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001075 apply(self.tk.call, (self._w, 'xview')+args)
1076 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001077 if not args:
1078 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001079 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001080
1081class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001082 def __init__(self, master=None, cnf={}, **kw):
1083 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001084 def deselect(self):
1085 self.tk.call(self._w, 'deselect')
1086 def flash(self):
1087 self.tk.call(self._w, 'flash')
1088 def invoke(self):
1089 self.tk.call(self._w, 'invoke')
1090 def select(self):
1091 self.tk.call(self._w, 'select')
1092 def toggle(self):
1093 self.tk.call(self._w, 'toggle')
1094
1095class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001096 def __init__(self, master=None, cnf={}, **kw):
1097 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001098 def delete(self, first, last=None):
1099 self.tk.call(self._w, 'delete', first, last)
1100 def get(self):
1101 return self.tk.call(self._w, 'get')
1102 def icursor(self, index):
1103 self.tk.call(self._w, 'icursor', index)
1104 def index(self, index):
1105 return self.tk.getint(self.tk.call(
1106 self._w, 'index', index))
1107 def insert(self, index, string):
1108 self.tk.call(self._w, 'insert', index, string)
1109 def scan_mark(self, x):
1110 self.tk.call(self._w, 'scan', 'mark', x)
1111 def scan_dragto(self, x):
1112 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001113 def selection_adjust(self, index):
1114 self.tk.call(self._w, 'selection', 'adjust', index)
1115 select_adjust = selection_adjust
1116 def selection_clear(self):
1117 self.tk.call(self._w, 'selection', 'clear')
1118 select_clear = selection_clear
1119 def selection_from(self, index):
1120 self.tk.call(self._w, 'selection', 'set', index)
1121 select_from = selection_from
1122 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001123 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001124 self.tk.call(self._w, 'selection', 'present'))
1125 select_present = selection_present
1126 def selection_range(self, start, end):
1127 self.tk.call(self._w, 'selection', 'range', start, end)
1128 select_range = selection_range
1129 def selection_to(self, index):
1130 self.tk.call(self._w, 'selection', 'to', index)
1131 select_to = selection_to
1132 def xview(self, index):
1133 self.tk.call(self._w, 'xview', index)
1134 def xview_moveto(self, fraction):
1135 self.tk.call(self._w, 'xview', 'moveto', fraction)
1136 def xview_scroll(self, number, what):
1137 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001138
1139class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001140 def __init__(self, master=None, cnf={}, **kw):
1141 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001142 extra = ()
1143 if cnf.has_key('class'):
1144 extra = ('-class', cnf['class'])
1145 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001146 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001147
1148class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001149 def __init__(self, master=None, cnf={}, **kw):
1150 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001151
Guido van Rossum18468821994-06-20 07:49:28 +00001152class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001153 def __init__(self, master=None, cnf={}, **kw):
1154 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001155 def activate(self, index):
1156 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001157 def bbox(self, *args):
1158 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001159 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001160 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001161 return self.tk.splitlist(self.tk.call(
1162 self._w, 'curselection'))
1163 def delete(self, first, last=None):
1164 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001165 def get(self, first, last=None):
1166 if last:
1167 return self.tk.splitlist(self.tk.call(
1168 self._w, 'get', first, last))
1169 else:
1170 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001171 def insert(self, index, *elements):
1172 apply(self.tk.call,
1173 (self._w, 'insert', index) + elements)
1174 def nearest(self, y):
1175 return self.tk.getint(self.tk.call(
1176 self._w, 'nearest', y))
1177 def scan_mark(self, x, y):
1178 self.tk.call(self._w, 'scan', 'mark', x, y)
1179 def scan_dragto(self, x, y):
1180 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001181 def see(self, index):
1182 self.tk.call(self._w, 'see', index)
1183 def index(self, index):
1184 i = self.tk.call(self._w, 'index', index)
1185 if i == 'none': return None
1186 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001187 def select_adjust(self, index):
1188 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001189 def select_anchor(self, index):
1190 self.tk.call(self._w, 'selection', 'anchor', index)
1191 def select_clear(self, first, last=None):
1192 self.tk.call(self._w,
1193 'selection', 'clear', first, last)
1194 def select_includes(self, index):
1195 return self.tk.getboolean(self.tk.call(
1196 self._w, 'selection', 'includes', index))
1197 def select_set(self, first, last=None):
1198 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001199 def size(self):
1200 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001201 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001202 if not what:
1203 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001204 apply(self.tk.call, (self._w, 'xview')+what)
1205 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001206 if not what:
1207 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001208 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001209
1210class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001211 def __init__(self, master=None, cnf={}, **kw):
1212 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001213 def tk_bindForTraversal(self):
1214 self.tk.call('tk_bindForTraversal', self._w)
1215 def tk_mbPost(self):
1216 self.tk.call('tk_mbPost', self._w)
1217 def tk_mbUnpost(self):
1218 self.tk.call('tk_mbUnpost')
1219 def tk_traverseToMenu(self, char):
1220 self.tk.call('tk_traverseToMenu', self._w, char)
1221 def tk_traverseWithinMenu(self, char):
1222 self.tk.call('tk_traverseWithinMenu', self._w, char)
1223 def tk_getMenuButtons(self):
1224 return self.tk.call('tk_getMenuButtons', self._w)
1225 def tk_nextMenu(self, count):
1226 self.tk.call('tk_nextMenu', count)
1227 def tk_nextMenuEntry(self, count):
1228 self.tk.call('tk_nextMenuEntry', count)
1229 def tk_invokeMenu(self):
1230 self.tk.call('tk_invokeMenu', self._w)
1231 def tk_firstMenu(self):
1232 self.tk.call('tk_firstMenu', self._w)
1233 def tk_mbButtonDown(self):
1234 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001235 def tk_popup(self, x, y, entry=""):
1236 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001237 def activate(self, index):
1238 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001239 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001240 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001241 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001242 def add_cascade(self, cnf={}, **kw):
1243 self.add('cascade', cnf or kw)
1244 def add_checkbutton(self, cnf={}, **kw):
1245 self.add('checkbutton', cnf or kw)
1246 def add_command(self, cnf={}, **kw):
1247 self.add('command', cnf or kw)
1248 def add_radiobutton(self, cnf={}, **kw):
1249 self.add('radiobutton', cnf or kw)
1250 def add_separator(self, cnf={}, **kw):
1251 self.add('separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001252 def delete(self, index1, index2=None):
1253 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001254 def entryconfig(self, index, cnf=None, **kw):
1255 if cnf is None and not kw:
1256 cnf = {}
1257 for x in self.tk.split(apply(self.tk.call,
1258 (self._w, 'entryconfigure', index))):
1259 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1260 return cnf
1261 if type(cnf) == StringType and not kw:
1262 x = self.tk.split(apply(self.tk.call,
1263 (self._w, 'entryconfigure', index, '-'+cnf)))
1264 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001265 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001266 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001267 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001268 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001269 i = self.tk.call(self._w, 'index', index)
1270 if i == 'none': return None
1271 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001272 def invoke(self, index):
1273 return self.tk.call(self._w, 'invoke', index)
1274 def post(self, x, y):
1275 self.tk.call(self._w, 'post', x, y)
1276 def unpost(self):
1277 self.tk.call(self._w, 'unpost')
1278 def yposition(self, index):
1279 return self.tk.getint(self.tk.call(
1280 self._w, 'yposition', index))
1281
1282class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001283 def __init__(self, master=None, cnf={}, **kw):
1284 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001285
1286class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001287 def __init__(self, master=None, cnf={}, **kw):
1288 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001289
1290class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001291 def __init__(self, master=None, cnf={}, **kw):
1292 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001293 def deselect(self):
1294 self.tk.call(self._w, 'deselect')
1295 def flash(self):
1296 self.tk.call(self._w, 'flash')
1297 def invoke(self):
1298 self.tk.call(self._w, 'invoke')
1299 def select(self):
1300 self.tk.call(self._w, 'select')
1301
1302class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001303 def __init__(self, master=None, cnf={}, **kw):
1304 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001305 def get(self):
1306 return self.tk.getint(self.tk.call(self._w, 'get'))
1307 def set(self, value):
1308 self.tk.call(self._w, 'set', value)
1309
1310class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001311 def __init__(self, master=None, cnf={}, **kw):
1312 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001313 def activate(self, index):
1314 self.tk.call(self._w, 'activate', index)
1315 def delta(self, deltax, deltay):
1316 return self.getdouble(self.tk.call(
1317 self._w, 'delta', deltax, deltay))
1318 def fraction(self, x, y):
1319 return self.getdouble(self.tk.call(
1320 self._w, 'fraction', x, y))
1321 def identify(self, x, y):
1322 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001323 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001324 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001325 def set(self, *args):
1326 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001327
1328class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001329 def __init__(self, master=None, cnf={}, **kw):
1330 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001331 self.bind('<Delete>', self.bspace)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001332 def bbox(self, *args):
1333 return self._getints(self._do('bbox', args)) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001334 def bspace(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001335 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001336 def tk_textSelectTo(self, index):
1337 self.tk.call('tk_textSelectTo', self._w, index)
1338 def tk_textBackspace(self):
1339 self.tk.call('tk_textBackspace', self._w)
1340 def tk_textIndexCloser(self, a, b, c):
1341 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1342 def tk_textResetAnchor(self, index):
1343 self.tk.call('tk_textResetAnchor', self._w, index)
1344 def compare(self, index1, op, index2):
1345 return self.tk.getboolean(self.tk.call(
1346 self._w, 'compare', index1, op, index2))
1347 def debug(self, boolean=None):
1348 return self.tk.getboolean(self.tk.call(
1349 self._w, 'debug', boolean))
1350 def delete(self, index1, index2=None):
1351 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001352 def dlineinfo(self, index):
1353 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001354 def get(self, index1, index2=None):
1355 return self.tk.call(self._w, 'get', index1, index2)
1356 def index(self, index):
1357 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001358 def insert(self, index, chars, *args):
1359 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001360 def mark_gravity(self, markName, direction=None):
1361 return apply(self.tk.call,
1362 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001363 def mark_names(self):
1364 return self.tk.splitlist(self.tk.call(
1365 self._w, 'mark', 'names'))
1366 def mark_set(self, markName, index):
1367 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001368 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001369 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001370 def scan_mark(self, x, y):
1371 self.tk.call(self._w, 'scan', 'mark', x, y)
1372 def scan_dragto(self, x, y):
1373 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001374 def search(self, pattern, index, stopindex=None,
1375 forwards=None, backwards=None, exact=None,
1376 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001377 args = [self._w, 'search']
1378 if forwards: args.append('-forwards')
1379 if backwards: args.append('-backwards')
1380 if exact: args.append('-exact')
1381 if regexp: args.append('-regexp')
1382 if nocase: args.append('-nocase')
1383 if count: args.append('-count'); args.append(count)
1384 if pattern[0] == '-': args.append('--')
1385 args.append(pattern)
1386 args.append(index)
1387 if stopindex: args.append(stopindex)
1388 return apply(self.tk.call, tuple(args))
1389 def see(self, index):
1390 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001391 def tag_add(self, tagName, index1, index2=None):
1392 self.tk.call(
1393 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001394 def tag_unbind(self, tagName, sequence):
1395 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001396 def tag_bind(self, tagName, sequence, func, add=None):
1397 return self._bind((self._w, 'tag', 'bind', tagName),
1398 sequence, func, add)
1399 def tag_cget(self, tagName, option):
1400 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001401 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001402 if type(cnf) == StringType:
1403 x = self.tk.split(self.tk.call(
1404 self._w, 'tag', 'configure', tagName, '-'+cnf))
1405 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001406 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001407 (self._w, 'tag', 'configure', tagName)
1408 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001409 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001410 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001411 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001412 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001413 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001414 def tag_names(self, index=None):
1415 return self.tk.splitlist(
1416 self.tk.call(self._w, 'tag', 'names', index))
1417 def tag_nextrange(self, tagName, index1, index2=None):
1418 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001419 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001420 def tag_raise(self, tagName, aboveThis=None):
1421 self.tk.call(
1422 self._w, 'tag', 'raise', tagName, aboveThis)
1423 def tag_ranges(self, tagName):
1424 return self.tk.splitlist(self.tk.call(
1425 self._w, 'tag', 'ranges', tagName))
1426 def tag_remove(self, tagName, index1, index2=None):
1427 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001428 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001429 def window_cget(self, index, option):
1430 return self.tk.call(self._w, 'window', 'cget', index, option)
1431 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001432 if type(cnf) == StringType:
1433 x = self.tk.split(self.tk.call(
1434 self._w, 'window', 'configure',
1435 index, '-'+cnf))
1436 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001437 apply(self.tk.call,
1438 (self._w, 'window', 'configure', index)
1439 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001440 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001441 def window_create(self, index, cnf={}, **kw):
1442 apply(self.tk.call,
1443 (self._w, 'window', 'create', index)
1444 + self._options(cnf, kw))
1445 def window_names(self):
1446 return self.tk.splitlist(
1447 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001448 def xview(self, *what):
1449 if not what:
1450 return self._getdoubles(self.tk.call(self._w, 'xview'))
1451 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001452 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001453 if not what:
1454 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001455 apply(self.tk.call, (self._w, 'yview')+what)
1456 def yview_pickplace(self, *what):
1457 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001458
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001459class OptionMenu(Widget):
1460 def __init__(self, master, variable, value, *values):
1461 self.widgetName = 'tk_optionMenu'
1462 Widget._setup(self, master, {})
1463 self.menuname = apply(
1464 self.tk.call,
1465 (self.widgetName, self._w, variable, value) + values)
1466
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001467class Image:
1468 def __init__(self, imgtype, name=None, cnf={}, **kw):
1469 self.name = None
1470 master = _default_root
1471 if not master: raise RuntimeError, 'Too early to create image'
1472 self.tk = master.tk
1473 if not name: name = `id(self)`
1474 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1475 elif kw: cnf = kw
1476 options = ()
1477 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001478 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001479 v = self._register(v)
1480 options = options + ('-'+k, v)
1481 apply(self.tk.call,
1482 ('image', 'create', imgtype, name,) + options)
1483 self.name = name
1484 def __str__(self): return self.name
1485 def __del__(self):
1486 if self.name:
1487 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001488 def __setitem__(self, key, value):
1489 self.tk.call(self.name, 'configure', '-'+key, value)
1490 def __getitem__(self, key):
1491 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001492 def height(self):
1493 return self.tk.getint(
1494 self.tk.call('image', 'height', self.name))
1495 def type(self):
1496 return self.tk.call('image', 'type', self.name)
1497 def width(self):
1498 return self.tk.getint(
1499 self.tk.call('image', 'width', self.name))
1500
1501class PhotoImage(Image):
1502 def __init__(self, name=None, cnf={}, **kw):
1503 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001504 def blank(self):
1505 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001506 def cget(self, option):
1507 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001508 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001509 def __getitem__(self, key):
1510 return self.tk.call(self.name, 'cget', '-' + key)
1511 def copy(self):
1512 destImage = PhotoImage()
1513 self.tk.call(destImage, 'copy', self.name)
1514 return destImage
1515 def zoom(self,x,y=''):
1516 destImage = PhotoImage()
1517 if y=='': y=x
1518 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1519 return destImage
1520 def subsample(self,x,y=''):
1521 destImage = PhotoImage()
1522 if y=='': y=x
1523 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1524 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001525 def get(self, x, y):
1526 return self.tk.call(self.name, 'get', x, y)
1527 def put(self, data, to=None):
1528 args = (self.name, 'put', data)
1529 if to:
1530 args = args + to
1531 apply(self.tk.call, args)
1532 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001533 def write(self, filename, format=None, from_coords=None):
1534 args = (self.name, 'write', filename)
1535 if format:
1536 args = args + ('-format', format)
1537 if from_coords:
1538 args = args + ('-from',) + tuple(from_coords)
1539 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001540
1541class BitmapImage(Image):
1542 def __init__(self, name=None, cnf={}, **kw):
1543 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1544
1545def image_names(): return _default_root.tk.call('image', 'names')
1546def image_types(): return _default_root.tk.call('image', 'types')
1547
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001548######################################################################
1549# Extensions:
1550
1551class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001552 def __init__(self, master=None, cnf={}, **kw):
1553 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001554 self.bind('<Any-Enter>', self.tkButtonEnter)
1555 self.bind('<Any-Leave>', self.tkButtonLeave)
1556 self.bind('<1>', self.tkButtonDown)
1557 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001558
1559class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001560 def __init__(self, master=None, cnf={}, **kw):
1561 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001562 self.bind('<Any-Enter>', self.tkButtonEnter)
1563 self.bind('<Any-Leave>', self.tkButtonLeave)
1564 self.bind('<1>', self.tkButtonDown)
1565 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1566 self['fg'] = self['bg']
1567 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001568
1569
1570# Emacs cruft
1571# Local Variables:
1572# py-indent-offset: 8
1573# End: