blob: c482f46122654871448b5e8137086eeb8a0202f5 [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):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000057 return cnfs
58 else:
59 cnf = {}
60 for c in _flatten(cnfs):
61 for k, v in c.items():
62 cnf[k] = v
63 return cnf
64
65class Event:
66 pass
67
Guido van Rossumaec5dc91994-06-27 07:55:12 +000068_default_root = None
69
Guido van Rossum45853db1994-06-20 12:19:19 +000070def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000071 pass
72
Guido van Rossum97aeca11994-07-07 13:12:12 +000073def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000074 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000075
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076_varnum = 0
77class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000078 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000079 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
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000088 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000089 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000090 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000091 def __str__(self):
92 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000093 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000094 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000095
96class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000097 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000098 def __init__(self, master=None):
99 Variable.__init__(self, master)
100 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000101 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000102
103class IntVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000104 _default = "0"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000105 def __init__(self, master=None):
106 Variable.__init__(self, master)
107 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000108 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000109
110class DoubleVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000111 _default = "0.0"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000112 def __init__(self, master=None):
113 Variable.__init__(self, master)
114 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000115 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000116
117class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000118 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000119 def __init__(self, master=None):
120 Variable.__init__(self, master)
121 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000122 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000123
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000124def mainloop(n=0):
125 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000126
127def getint(s):
128 return _default_root.tk.getint(s)
129
130def getdouble(s):
131 return _default_root.tk.getdouble(s)
132
133def getboolean(s):
134 return _default_root.tk.getboolean(s)
135
Guido van Rossum18468821994-06-20 07:49:28 +0000136class Misc:
137 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000138 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000139 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000140 def tk_bisque(self):
141 self.tk.call('tk_bisque')
142 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000143 apply(self.tk.call, ('tk_setPalette',)
144 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000145 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000146 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000147 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000148 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000149 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000150 def wait_window(self, window=None):
151 if window == None:
152 window = self
153 self.tk.call('tkwait', 'window', window._w)
154 def wait_visibility(self, window=None):
155 if window == None:
156 window = self
157 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000158 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000159 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000160 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000161 return self.tk.getvar(name)
162 def getint(self, s):
163 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000164 def getdouble(self, s):
165 return self.tk.getdouble(s)
166 def getboolean(self, s):
167 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000168 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000169 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000170 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000171 def focus_force(self):
172 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000173 def focus_get(self):
174 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000175 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000176 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000177 def focus_displayof(self):
178 name = self.tk.call('focus', '-displayof', self._w)
179 if name == 'none' or not name: return None
180 return self._nametowidget(name)
181 def focus_lastfor(self):
182 name = self.tk.call('focus', '-lastfor', self._w)
183 if name == 'none' or not name: return None
184 return self._nametowidget(name)
185 def tk_focusFollowsMouse(self):
186 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000187 def tk_focusNext(self):
188 name = self.tk.call('tk_focusNext', self._w)
189 if not name: return None
190 return self._nametowidget(name)
191 def tk_focusPrev(self):
192 name = self.tk.call('tk_focusPrev', self._w)
193 if not name: return None
194 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000195 def after(self, ms, func=None, *args):
196 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000197 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000198 self.tk.call('after', ms)
199 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000200 # XXX Disgusting hack to clean up after calling func
201 tmp = []
202 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
203 try:
204 apply(func, args)
205 finally:
206 tk.deletecommand(tmp[0])
207 name = self._register(callit)
208 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000209 return self.tk.call('after', ms, name)
210 def after_idle(self, func, *args):
211 return apply(self.after, ('idle', func) + args)
212 def after_cancel(self, id):
213 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000214 def bell(self, displayof=0):
215 apply(self.tk.call, ('bell',) + self._displayof(displayof))
216 # Clipboard handling:
217 def clipboard_clear(self, **kw):
218 if not kw.has_key('displayof'): kw['displayof'] = self._w
219 apply(self.tk.call,
220 ('clipboard', 'clear') + self._options(kw))
221 def clipboard_append(self, string, **kw):
222 if not kw.has_key('displayof'): kw['displayof'] = self._w
223 apply(self.tk.call,
224 ('clipboard', 'append') + self._options(kw)
225 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000226 # XXX grab current w/o window argument
227 def grab_current(self):
228 name = self.tk.call('grab', 'current', self._w)
229 if not name: return None
230 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000231 def grab_release(self):
232 self.tk.call('grab', 'release', self._w)
233 def grab_set(self):
234 self.tk.call('grab', 'set', self._w)
235 def grab_set_global(self):
236 self.tk.call('grab', 'set', '-global', self._w)
237 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000238 status = self.tk.call('grab', 'status', self._w)
239 if status == 'none': status = None
240 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000241 def lower(self, belowThis=None):
242 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000243 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000244 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000245 def option_clear(self):
246 self.tk.call('option', 'clear')
247 def option_get(self, name, className):
248 return self.tk.call('option', 'get', self._w, name, className)
249 def option_readfile(self, fileName, priority = None):
250 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000251 def selection_clear(self, **kw):
252 if not kw.has_key('displayof'): kw['displayof'] = self._w
253 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
254 def selection_get(self, **kw):
255 if not kw.has_key('displayof'): kw['displayof'] = self._w
256 return apply(self.tk.call,
257 ('selection', 'get') + self._options(kw))
258 def selection_handle(self, command, **kw):
259 name = self._register(command)
260 apply(self.tk.call,
261 ('selection', 'handle') + self._options(kw)
262 + (self._w, name))
263 def selection_own(self, **kw):
264 "Become owner of X selection."
265 apply(self.tk.call,
266 ('selection', 'own') + self._options(kw) + (self._w,))
267 def selection_own_get(self, **kw):
268 "Find owner of X selection."
269 if not kw.has_key('displayof'): kw['displayof'] = self._w
270 return self._nametowidget(
271 apply(self.tk.call,
272 ('selection', 'own') + self._options(kw)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000273 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000274 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000275 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000276 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000277 def tkraise(self, aboveThis=None):
278 self.tk.call('raise', self._w, aboveThis)
279 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000280 def colormodel(self, value=None):
281 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000282 def winfo_atom(self, name, displayof=0):
283 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
284 return self.tk.getint(apply(self.tk.call, args))
285 def winfo_atomname(self, id, displayof=0):
286 args = ('winfo', 'atomname') \
287 + self._displayof(displayof) + (id,)
288 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000289 def winfo_cells(self):
290 return self.tk.getint(
291 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000292 def winfo_children(self):
293 return map(self._nametowidget,
294 self.tk.splitlist(self.tk.call(
295 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000296 def winfo_class(self):
297 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000298 def winfo_colormapfull(self):
299 return self.tk.getboolean(
300 self.tk.call('winfo', 'colormapfull'))
301 def winfo_containing(self, rootX, rootY, displayof=0):
302 args = ('winfo', 'containing') \
303 + self._displayof(displayof) + (rootX, rootY)
304 return self._nametowidget(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000305 def winfo_depth(self):
306 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
307 def winfo_exists(self):
308 return self.tk.getint(
309 self.tk.call('winfo', 'exists', self._w))
310 def winfo_fpixels(self, number):
311 return self.tk.getdouble(self.tk.call(
312 'winfo', 'fpixels', self._w, number))
313 def winfo_geometry(self):
314 return self.tk.call('winfo', 'geometry', self._w)
315 def winfo_height(self):
316 return self.tk.getint(
317 self.tk.call('winfo', 'height', self._w))
318 def winfo_id(self):
319 return self.tk.getint(
320 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000321 def winfo_interps(self, displayof=0):
322 args = ('winfo', 'interps') + self._displayof(displayof)
323 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def winfo_ismapped(self):
325 return self.tk.getint(
326 self.tk.call('winfo', 'ismapped', self._w))
327 def winfo_name(self):
328 return self.tk.call('winfo', 'name', self._w)
329 def winfo_parent(self):
330 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000331 def winfo_pathname(self, id, displayof=0):
332 args = ('winfo', 'pathname') \
333 + self._displayof(displayof) + (id,)
334 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000335 def winfo_pixels(self, number):
336 return self.tk.getint(
337 self.tk.call('winfo', 'pixels', self._w, number))
338 def winfo_reqheight(self):
339 return self.tk.getint(
340 self.tk.call('winfo', 'reqheight', self._w))
341 def winfo_reqwidth(self):
342 return self.tk.getint(
343 self.tk.call('winfo', 'reqwidth', self._w))
344 def winfo_rgb(self, color):
345 return self._getints(
346 self.tk.call('winfo', 'rgb', self._w, color))
347 def winfo_rootx(self):
348 return self.tk.getint(
349 self.tk.call('winfo', 'rootx', self._w))
350 def winfo_rooty(self):
351 return self.tk.getint(
352 self.tk.call('winfo', 'rooty', self._w))
353 def winfo_screen(self):
354 return self.tk.call('winfo', 'screen', self._w)
355 def winfo_screencells(self):
356 return self.tk.getint(
357 self.tk.call('winfo', 'screencells', self._w))
358 def winfo_screendepth(self):
359 return self.tk.getint(
360 self.tk.call('winfo', 'screendepth', self._w))
361 def winfo_screenheight(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'screenheight', self._w))
364 def winfo_screenmmheight(self):
365 return self.tk.getint(
366 self.tk.call('winfo', 'screenmmheight', self._w))
367 def winfo_screenmmwidth(self):
368 return self.tk.getint(
369 self.tk.call('winfo', 'screenmmwidth', self._w))
370 def winfo_screenvisual(self):
371 return self.tk.call('winfo', 'screenvisual', self._w)
372 def winfo_screenwidth(self):
373 return self.tk.getint(
374 self.tk.call('winfo', 'screenwidth', self._w))
375 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000376 return self._nametowidget(self.tk.call(
377 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000378 def winfo_visual(self):
379 return self.tk.call('winfo', 'visual', self._w)
380 def winfo_vrootheight(self):
381 return self.tk.getint(
382 self.tk.call('winfo', 'vrootheight', self._w))
383 def winfo_vrootwidth(self):
384 return self.tk.getint(
385 self.tk.call('winfo', 'vrootwidth', self._w))
386 def winfo_vrootx(self):
387 return self.tk.getint(
388 self.tk.call('winfo', 'vrootx', self._w))
389 def winfo_vrooty(self):
390 return self.tk.getint(
391 self.tk.call('winfo', 'vrooty', self._w))
392 def winfo_width(self):
393 return self.tk.getint(
394 self.tk.call('winfo', 'width', self._w))
395 def winfo_x(self):
396 return self.tk.getint(
397 self.tk.call('winfo', 'x', self._w))
398 def winfo_y(self):
399 return self.tk.getint(
400 self.tk.call('winfo', 'y', self._w))
401 def update(self):
402 self.tk.call('update')
403 def update_idletasks(self):
404 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000405 def bindtags(self, tagList=None):
406 if tagList is None:
407 return self.tk.splitlist(
408 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000409 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000410 self.tk.call('bindtags', self._w, tagList)
411 def _bind(self, what, sequence, func, add):
412 if func:
413 cmd = ("%sset _tkinter_break [%s %s]\n"
414 'if {"$_tkinter_break" == "break"} break\n') \
415 % (add and '+' or '',
416 self._register(func, self._substitute),
417 _string.join(self._subst_format))
418 apply(self.tk.call, what + (sequence, cmd))
419 elif func == '':
420 apply(self.tk.call, what + (sequence, func))
421 else:
422 return apply(self.tk.call, what + (sequence,))
423 def bind(self, sequence=None, func=None, add=None):
424 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000425 def unbind(self, sequence):
426 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000427 def bind_all(self, sequence=None, func=None, add=None):
428 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000429 def unbind_all(self, sequence):
430 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000431 def bind_class(self, className, sequence=None, func=None, add=None):
432 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000433 def unbind_class(self, className, sequence):
434 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000435 def mainloop(self, n=0):
436 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000437 def quit(self):
438 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000439 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000440 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000441 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
442 def _getdoubles(self, string):
443 if not string: return None
444 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000445 def _getboolean(self, string):
446 if string:
447 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000448 def _displayof(self, displayof):
449 if displayof:
450 return ('-displayof', displayof)
451 if displayof is None:
452 return ('-displayof', self._w)
453 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000454 def _options(self, cnf, kw = None):
455 if kw:
456 cnf = _cnfmerge((cnf, kw))
457 else:
458 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000459 res = ()
460 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000461 if v is not None:
462 if k[-1] == '_': k = k[:-1]
463 if callable(v):
464 v = self._register(v)
465 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000466 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000467 def _nametowidget(self, name):
468 w = self
469 if name[0] == '.':
470 w = w._root()
471 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000472 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000473 while name:
474 i = find(name, '.')
475 if i >= 0:
476 name, tail = name[:i], name[i+1:]
477 else:
478 tail = ''
479 w = w.children[name]
480 name = tail
481 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000482 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000483 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000484 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000485 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000486 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000487 except AttributeError:
488 pass
489 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000490 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000491 except AttributeError:
492 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000493 self.tk.createcommand(name, f)
494 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000495 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000496 def _root(self):
497 w = self
498 while w.master: w = w.master
499 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000500 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000501 '%s', '%t', '%w', '%x', '%y',
502 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
503 def _substitute(self, *args):
504 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000505 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000506 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
507 # Missing: (a, c, d, m, o, v, B, R)
508 e = Event()
509 e.serial = tk.getint(nsign)
510 e.num = tk.getint(b)
511 try: e.focus = tk.getboolean(f)
512 except TclError: pass
513 e.height = tk.getint(h)
514 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000515 # For Visibility events, event state is a string and
516 # not an integer:
517 try:
518 e.state = tk.getint(s)
519 except TclError:
520 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000521 e.time = tk.getint(t)
522 e.width = tk.getint(w)
523 e.x = tk.getint(x)
524 e.y = tk.getint(y)
525 e.char = A
526 try: e.send_event = tk.getboolean(E)
527 except TclError: pass
528 e.keysym = K
529 e.keysym_num = tk.getint(N)
530 e.type = T
531 e.widget = self._nametowidget(W)
532 e.x_root = tk.getint(X)
533 e.y_root = tk.getint(Y)
534 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000535 def _report_exception(self):
536 import sys
537 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
538 root = self._root()
539 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000540
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000541class CallWrapper:
542 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000543 self.func = func
544 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000545 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000546 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000547 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000548 if self.subst:
549 args = apply(self.subst, args)
550 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000551 except SystemExit, msg:
552 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000553 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000554 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000555
556class Wm:
557 def aspect(self,
558 minNumer=None, minDenom=None,
559 maxNumer=None, maxDenom=None):
560 return self._getints(
561 self.tk.call('wm', 'aspect', self._w,
562 minNumer, minDenom,
563 maxNumer, maxDenom))
564 def client(self, name=None):
565 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000566 def colormapwindows(self, *wlist):
567 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
568 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000569 def command(self, value=None):
570 return self.tk.call('wm', 'command', self._w, value)
571 def deiconify(self):
572 return self.tk.call('wm', 'deiconify', self._w)
573 def focusmodel(self, model=None):
574 return self.tk.call('wm', 'focusmodel', self._w, model)
575 def frame(self):
576 return self.tk.call('wm', 'frame', self._w)
577 def geometry(self, newGeometry=None):
578 return self.tk.call('wm', 'geometry', self._w, newGeometry)
579 def grid(self,
580 baseWidht=None, baseHeight=None,
581 widthInc=None, heightInc=None):
582 return self._getints(self.tk.call(
583 'wm', 'grid', self._w,
584 baseWidht, baseHeight, widthInc, heightInc))
585 def group(self, pathName=None):
586 return self.tk.call('wm', 'group', self._w, pathName)
587 def iconbitmap(self, bitmap=None):
588 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
589 def iconify(self):
590 return self.tk.call('wm', 'iconify', self._w)
591 def iconmask(self, bitmap=None):
592 return self.tk.call('wm', 'iconmask', self._w, bitmap)
593 def iconname(self, newName=None):
594 return self.tk.call('wm', 'iconname', self._w, newName)
595 def iconposition(self, x=None, y=None):
596 return self._getints(self.tk.call(
597 'wm', 'iconposition', self._w, x, y))
598 def iconwindow(self, pathName=None):
599 return self.tk.call('wm', 'iconwindow', self._w, pathName)
600 def maxsize(self, width=None, height=None):
601 return self._getints(self.tk.call(
602 'wm', 'maxsize', self._w, width, height))
603 def minsize(self, width=None, height=None):
604 return self._getints(self.tk.call(
605 'wm', 'minsize', self._w, width, height))
606 def overrideredirect(self, boolean=None):
607 return self._getboolean(self.tk.call(
608 'wm', 'overrideredirect', self._w, boolean))
609 def positionfrom(self, who=None):
610 return self.tk.call('wm', 'positionfrom', self._w, who)
611 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000612 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000613 command = self._register(func)
614 else:
615 command = func
616 return self.tk.call(
617 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000618 def resizable(self, width=None, height=None):
619 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000620 def sizefrom(self, who=None):
621 return self.tk.call('wm', 'sizefrom', self._w, who)
622 def state(self):
623 return self.tk.call('wm', 'state', self._w)
624 def title(self, string=None):
625 return self.tk.call('wm', 'title', self._w, string)
626 def transient(self, master=None):
627 return self.tk.call('wm', 'transient', self._w, master)
628 def withdraw(self):
629 return self.tk.call('wm', 'withdraw', self._w)
630
631class Tk(Misc, Wm):
632 _w = '.'
633 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000634 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000635 self.master = None
636 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000637 if baseName is None:
638 import sys, os
639 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000640 baseName, ext = os.path.splitext(baseName)
641 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum37dcab11996-05-16 16:00:19 +0000642 self.tk = tkinter.create(screenName, baseName, className)
643 try:
644 # Disable event scanning except for Command-Period
645 import MacOS
646 MacOS.EnableAppswitch(0)
647 except ImportError:
648 pass
649 else:
650 # Work around nasty MacTk bug
651 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000652 # Version sanity checks
653 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000654 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000655 raise RuntimeError, \
656 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000657 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000658 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000659 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000660 raise RuntimeError, \
661 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000662 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000663 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000664 raise RuntimeError, \
665 "Tk 4.0 or higher is required; found Tk %s" \
666 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000667 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000668 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000669 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000670 if not _default_root:
671 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000672 def destroy(self):
673 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000674 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000675 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000676 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000677 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000678 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000679 if os.environ.has_key('HOME'): home = os.environ['HOME']
680 else: home = os.curdir
681 class_tcl = os.path.join(home, '.%s.tcl' % className)
682 class_py = os.path.join(home, '.%s.py' % className)
683 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
684 base_py = os.path.join(home, '.%s.py' % baseName)
685 dir = {'self': self}
686 exec 'from Tkinter import *' in dir
687 if os.path.isfile(class_tcl):
688 print 'source', `class_tcl`
689 self.tk.call('source', class_tcl)
690 if os.path.isfile(class_py):
691 print 'execfile', `class_py`
692 execfile(class_py, dir)
693 if os.path.isfile(base_tcl):
694 print 'source', `base_tcl`
695 self.tk.call('source', base_tcl)
696 if os.path.isfile(base_py):
697 print 'execfile', `base_py`
698 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000699 def report_callback_exception(self, exc, val, tb):
700 import traceback
701 print "Exception in Tkinter callback"
702 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000703
704class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000705 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000706 apply(self.tk.call,
707 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000708 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000709 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000710 pack = config
711 def __setitem__(self, key, value):
712 Pack.config({key: value})
713 def forget(self):
714 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000715 pack_forget = forget
716 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000717 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000718 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000719 dict = {}
720 for i in range(0, len(words), 2):
721 key = words[i][1:]
722 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000723 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000724 value = self._nametowidget(value)
725 dict[key] = value
726 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000727 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000728 _noarg_ = ['_noarg_']
729 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000730 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000731 return self._getboolean(self.tk.call(
732 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000733 else:
734 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000735 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000736 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000737 return map(self._nametowidget,
738 self.tk.splitlist(
739 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000740 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000741
742class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000743 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000744 for k in ['in_']:
745 if kw.has_key(k):
746 kw[k[:-1]] = kw[k]
747 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000748 apply(self.tk.call,
749 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000750 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000751 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000752 place = config
753 def __setitem__(self, key, value):
754 Place.config({key: value})
755 def forget(self):
756 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000757 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000758 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000759 words = self.tk.splitlist(
760 self.tk.call('place', 'info', self._w))
761 dict = {}
762 for i in range(0, len(words), 2):
763 key = words[i][1:]
764 value = words[i+1]
765 if value[:1] == '.':
766 value = self._nametowidget(value)
767 dict[key] = value
768 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000769 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000770 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000771 return map(self._nametowidget,
772 self.tk.splitlist(
773 self.tk.call(
774 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000775 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000776
Guido van Rossum37dcab11996-05-16 16:00:19 +0000777class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000778 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000779 def config(self, cnf={}, **kw):
780 apply(self.tk.call,
781 ('grid', 'configure', self._w)
782 + self._options(cnf, kw))
783 grid = config
784 def __setitem__(self, key, value):
785 Grid.config({key: value})
786 def bbox(self, column, row):
787 return self._getints(
788 self.tk.call(
789 'grid', 'bbox', self._w, column, row)) or None
790 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000791 def columnconfigure(self, index, cnf={}, **kw):
792 if type(cnf) is not DictionaryType and not kw:
793 options = self._options({cnf: None})
794 else:
795 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000796 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000797 ('grid', 'columnconfigure', self._w, index)
798 + options)
799 if options == ('-minsize', None):
800 return self.tk.getint(res) or None
801 elif options == ('-weight', None):
802 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 def forget(self):
804 self.tk.call('grid', 'forget', self._w)
805 grid_forget = forget
806 def info(self):
807 words = self.tk.splitlist(
808 self.tk.call('grid', 'info', self._w))
809 dict = {}
810 for i in range(0, len(words), 2):
811 key = words[i][1:]
812 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000813 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000814 value = self._nametowidget(value)
815 dict[key] = value
816 return dict
817 grid_info = info
818 def location(self, x, y):
819 return self._getints(
820 self.tk.call(
821 'grid', 'location', self._w, x, y)) or None
822 _noarg_ = ['_noarg_']
823 def propagate(self, flag=_noarg_):
824 if flag is Grid._noarg_:
825 return self._getboolean(self.tk.call(
826 'grid', 'propagate', self._w))
827 else:
828 self.tk.call('grid', 'propagate', self._w, flag)
829 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000830 def rowconfigure(self, index, cnf={}, **kw):
831 if type(cnf) is not DictionaryType and not kw:
832 options = self._options({cnf: None})
833 else:
834 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000835 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000836 ('grid', 'rowconfigure', self._w, index)
837 + options)
838 if options == ('-minsize', None):
839 return self.tk.getint(res) or None
840 elif options == ('-weight', None):
841 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 def size(self):
843 return self._getints(
844 self.tk.call('grid', 'size', self._w)) or None
845 def slaves(self, *args):
846 return map(self._nametowidget,
847 self.tk.splitlist(
848 apply(self.tk.call,
849 ('grid', 'slaves', self._w) + args)))
850 grid_slaves = slaves
851
852class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000853 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000854 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000855 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000856 if not _default_root:
857 _default_root = Tk()
858 master = _default_root
859 if not _default_root:
860 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000861 self.master = master
862 self.tk = master.tk
863 if cnf.has_key('name'):
864 name = cnf['name']
865 del cnf['name']
866 else:
867 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000868 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000869 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000870 self._w = '.' + name
871 else:
872 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000873 self.children = {}
874 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000875 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000876 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000877 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
878 if kw:
879 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000880 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000881 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000882 classes = []
883 for k in cnf.keys():
884 if type(k) is ClassType:
885 classes.append((k, cnf[k]))
886 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000887 apply(self.tk.call,
888 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000889 for k, v in classes:
890 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000891 def config(self, cnf=None, **kw):
892 # XXX ought to generalize this so tag_config etc. can use it
893 if kw:
894 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000895 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000896 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000897 if cnf is None:
898 cnf = {}
899 for x in self.tk.split(
900 self.tk.call(self._w, 'configure')):
901 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
902 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000903 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000904 x = self.tk.split(self.tk.call(
905 self._w, 'configure', '-'+cnf))
906 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000907 apply(self.tk.call, (self._w, 'configure')
908 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000909 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000910 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000911 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000912 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000913 def __setitem__(self, key, value):
914 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000915 def keys(self):
916 return map(lambda x: x[0][1:],
917 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000918 def __str__(self):
919 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000920 def destroy(self):
921 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000922 if self.master.children.has_key(self._name):
923 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000924 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000925 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000926 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000927
928class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000929 def __init__(self, master=None, cnf={}, **kw):
930 if kw:
931 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000932 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000933 for wmkey in ['screen', 'class_', 'class', 'visual',
934 'colormap']:
935 if cnf.has_key(wmkey):
936 val = cnf[wmkey]
937 # TBD: a hack needed because some keys
938 # are not valid as keyword arguments
939 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
940 else: opt = '-'+wmkey
941 extra = extra + (opt, val)
942 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000943 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000944 root = self._root()
945 self.iconname(root.iconname())
946 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000947
948class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000949 def __init__(self, master=None, cnf={}, **kw):
950 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000951 def tkButtonEnter(self, *dummy):
952 self.tk.call('tkButtonEnter', self._w)
953 def tkButtonLeave(self, *dummy):
954 self.tk.call('tkButtonLeave', self._w)
955 def tkButtonDown(self, *dummy):
956 self.tk.call('tkButtonDown', self._w)
957 def tkButtonUp(self, *dummy):
958 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000959 def tkButtonInvoke(self, *dummy):
960 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000961 def flash(self):
962 self.tk.call(self._w, 'flash')
963 def invoke(self):
964 self.tk.call(self._w, 'invoke')
965
966# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000967# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000968def AtEnd():
969 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000970def AtInsert(*args):
971 s = 'insert'
972 for a in args:
973 if a: s = s + (' ' + a)
974 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000975def AtSelFirst():
976 return 'sel.first'
977def AtSelLast():
978 return 'sel.last'
979def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000980 if y is None:
981 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000982 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000983 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000984
985class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000986 def __init__(self, master=None, cnf={}, **kw):
987 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000988 def addtag(self, *args):
989 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +0000990 def addtag_above(self, newtag, tagOrId):
991 self.addtag(newtag, 'above', tagOrId)
992 def addtag_all(self, newtag):
993 self.addtag(newtag, 'all')
994 def addtag_below(self, newtag, tagOrId):
995 self.addtag(newtag, 'below', tagOrId)
996 def addtag_closest(self, newtag, x, y, halo=None, start=None):
997 self.addtag(newtag, 'closest', x, y, halo, start)
998 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
999 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1000 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1001 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1002 def addtag_withtag(self, newtag, tagOrId):
1003 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001004 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001005 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001006 def tag_unbind(self, tagOrId, sequence):
1007 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001008 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001009 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001010 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001011 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001012 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001013 self._w, 'canvasx', screenx, gridspacing))
1014 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001015 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001016 self._w, 'canvasy', screeny, gridspacing))
1017 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001018 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001019 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001020 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001021 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001022 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001023 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001024 args = args[:-1]
1025 else:
1026 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001027 return self.tk.getint(apply(
1028 self.tk.call,
1029 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001030 + args + self._options(cnf, kw)))
1031 def create_arc(self, *args, **kw):
1032 return self._create('arc', args, kw)
1033 def create_bitmap(self, *args, **kw):
1034 return self._create('bitmap', args, kw)
1035 def create_image(self, *args, **kw):
1036 return self._create('image', args, kw)
1037 def create_line(self, *args, **kw):
1038 return self._create('line', args, kw)
1039 def create_oval(self, *args, **kw):
1040 return self._create('oval', args, kw)
1041 def create_polygon(self, *args, **kw):
1042 return self._create('polygon', args, kw)
1043 def create_rectangle(self, *args, **kw):
1044 return self._create('rectangle', args, kw)
1045 def create_text(self, *args, **kw):
1046 return self._create('text', args, kw)
1047 def create_window(self, *args, **kw):
1048 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001049 def dchars(self, *args):
1050 self._do('dchars', args)
1051 def delete(self, *args):
1052 self._do('delete', args)
1053 def dtag(self, *args):
1054 self._do('dtag', args)
1055 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001056 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001057 def find_above(self, tagOrId):
1058 return self.find('above', tagOrId)
1059 def find_all(self):
1060 return self.find('all')
1061 def find_below(self, tagOrId):
1062 return self.find('below', tagOrId)
1063 def find_closest(self, x, y, halo=None, start=None):
1064 return self.find('closest', x, y, halo, start)
1065 def find_enclosed(self, x1, y1, x2, y2):
1066 return self.find('enclosed', x1, y1, x2, y2)
1067 def find_overlapping(self, x1, y1, x2, y2):
1068 return self.find('overlapping', x1, y1, x2, y2)
1069 def find_withtag(self, tagOrId):
1070 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001071 def focus(self, *args):
1072 return self._do('focus', args)
1073 def gettags(self, *args):
1074 return self.tk.splitlist(self._do('gettags', args))
1075 def icursor(self, *args):
1076 self._do('icursor', args)
1077 def index(self, *args):
1078 return self.tk.getint(self._do('index', args))
1079 def insert(self, *args):
1080 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001081 def itemcget(self, tagOrId, option):
1082 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001083 def itemconfig(self, tagOrId, cnf=None, **kw):
1084 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001085 cnf = {}
1086 for x in self.tk.split(
1087 self._do('itemconfigure', (tagOrId))):
1088 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1089 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001090 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001091 x = self.tk.split(self._do('itemconfigure',
1092 (tagOrId, '-'+cnf,)))
1093 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001094 self._do('itemconfigure', (tagOrId,)
1095 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001096 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001097 def lower(self, *args):
1098 self._do('lower', args)
1099 def move(self, *args):
1100 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001101 def postscript(self, cnf={}, **kw):
1102 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001103 def tkraise(self, *args):
1104 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001105 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001106 def scale(self, *args):
1107 self._do('scale', args)
1108 def scan_mark(self, x, y):
1109 self.tk.call(self._w, 'scan', 'mark', x, y)
1110 def scan_dragto(self, x, y):
1111 self.tk.call(self._w, 'scan', 'dragto', x, y)
1112 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001113 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001114 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001115 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001116 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001117 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001118 def select_item(self):
1119 self.tk.call(self._w, 'select', 'item')
1120 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001121 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001122 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001123 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001124 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001125 if not args:
1126 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001127 apply(self.tk.call, (self._w, 'xview')+args)
1128 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001129 if not args:
1130 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001131 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001132
1133class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001134 def __init__(self, master=None, cnf={}, **kw):
1135 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001136 def deselect(self):
1137 self.tk.call(self._w, 'deselect')
1138 def flash(self):
1139 self.tk.call(self._w, 'flash')
1140 def invoke(self):
1141 self.tk.call(self._w, 'invoke')
1142 def select(self):
1143 self.tk.call(self._w, 'select')
1144 def toggle(self):
1145 self.tk.call(self._w, 'toggle')
1146
1147class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001148 def __init__(self, master=None, cnf={}, **kw):
1149 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def delete(self, first, last=None):
1151 self.tk.call(self._w, 'delete', first, last)
1152 def get(self):
1153 return self.tk.call(self._w, 'get')
1154 def icursor(self, index):
1155 self.tk.call(self._w, 'icursor', index)
1156 def index(self, index):
1157 return self.tk.getint(self.tk.call(
1158 self._w, 'index', index))
1159 def insert(self, index, string):
1160 self.tk.call(self._w, 'insert', index, string)
1161 def scan_mark(self, x):
1162 self.tk.call(self._w, 'scan', 'mark', x)
1163 def scan_dragto(self, x):
1164 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001165 def selection_adjust(self, index):
1166 self.tk.call(self._w, 'selection', 'adjust', index)
1167 select_adjust = selection_adjust
1168 def selection_clear(self):
1169 self.tk.call(self._w, 'selection', 'clear')
1170 select_clear = selection_clear
1171 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001172 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001173 select_from = selection_from
1174 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001175 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001176 self.tk.call(self._w, 'selection', 'present'))
1177 select_present = selection_present
1178 def selection_range(self, start, end):
1179 self.tk.call(self._w, 'selection', 'range', start, end)
1180 select_range = selection_range
1181 def selection_to(self, index):
1182 self.tk.call(self._w, 'selection', 'to', index)
1183 select_to = selection_to
1184 def xview(self, index):
1185 self.tk.call(self._w, 'xview', index)
1186 def xview_moveto(self, fraction):
1187 self.tk.call(self._w, 'xview', 'moveto', fraction)
1188 def xview_scroll(self, number, what):
1189 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001190
1191class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001192 def __init__(self, master=None, cnf={}, **kw):
1193 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001194 extra = ()
1195 if cnf.has_key('class'):
1196 extra = ('-class', cnf['class'])
1197 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001198 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001199
1200class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001201 def __init__(self, master=None, cnf={}, **kw):
1202 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001203
Guido van Rossum18468821994-06-20 07:49:28 +00001204class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001205 def __init__(self, master=None, cnf={}, **kw):
1206 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001207 def activate(self, index):
1208 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001209 def bbox(self, *args):
1210 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001211 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001212 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001213 return self.tk.splitlist(self.tk.call(
1214 self._w, 'curselection'))
1215 def delete(self, first, last=None):
1216 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001217 def get(self, first, last=None):
1218 if last:
1219 return self.tk.splitlist(self.tk.call(
1220 self._w, 'get', first, last))
1221 else:
1222 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001223 def insert(self, index, *elements):
1224 apply(self.tk.call,
1225 (self._w, 'insert', index) + elements)
1226 def nearest(self, y):
1227 return self.tk.getint(self.tk.call(
1228 self._w, 'nearest', y))
1229 def scan_mark(self, x, y):
1230 self.tk.call(self._w, 'scan', 'mark', x, y)
1231 def scan_dragto(self, x, y):
1232 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001233 def see(self, index):
1234 self.tk.call(self._w, 'see', index)
1235 def index(self, index):
1236 i = self.tk.call(self._w, 'index', index)
1237 if i == 'none': return None
1238 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001239 def select_adjust(self, index):
1240 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001241 def select_anchor(self, index):
1242 self.tk.call(self._w, 'selection', 'anchor', index)
1243 def select_clear(self, first, last=None):
1244 self.tk.call(self._w,
1245 'selection', 'clear', first, last)
1246 def select_includes(self, index):
1247 return self.tk.getboolean(self.tk.call(
1248 self._w, 'selection', 'includes', index))
1249 def select_set(self, first, last=None):
1250 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001251 def size(self):
1252 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001253 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001254 if not what:
1255 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001256 apply(self.tk.call, (self._w, 'xview')+what)
1257 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001258 if not what:
1259 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001260 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001261
1262class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001263 def __init__(self, master=None, cnf={}, **kw):
1264 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001265 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001266 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001267 def tk_mbPost(self):
1268 self.tk.call('tk_mbPost', self._w)
1269 def tk_mbUnpost(self):
1270 self.tk.call('tk_mbUnpost')
1271 def tk_traverseToMenu(self, char):
1272 self.tk.call('tk_traverseToMenu', self._w, char)
1273 def tk_traverseWithinMenu(self, char):
1274 self.tk.call('tk_traverseWithinMenu', self._w, char)
1275 def tk_getMenuButtons(self):
1276 return self.tk.call('tk_getMenuButtons', self._w)
1277 def tk_nextMenu(self, count):
1278 self.tk.call('tk_nextMenu', count)
1279 def tk_nextMenuEntry(self, count):
1280 self.tk.call('tk_nextMenuEntry', count)
1281 def tk_invokeMenu(self):
1282 self.tk.call('tk_invokeMenu', self._w)
1283 def tk_firstMenu(self):
1284 self.tk.call('tk_firstMenu', self._w)
1285 def tk_mbButtonDown(self):
1286 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001287 def tk_popup(self, x, y, entry=""):
1288 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001289 def activate(self, index):
1290 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001291 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001292 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001293 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001294 def add_cascade(self, cnf={}, **kw):
1295 self.add('cascade', cnf or kw)
1296 def add_checkbutton(self, cnf={}, **kw):
1297 self.add('checkbutton', cnf or kw)
1298 def add_command(self, cnf={}, **kw):
1299 self.add('command', cnf or kw)
1300 def add_radiobutton(self, cnf={}, **kw):
1301 self.add('radiobutton', cnf or kw)
1302 def add_separator(self, cnf={}, **kw):
1303 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001304 def insert(self, index, itemType, cnf={}, **kw):
1305 apply(self.tk.call, (self._w, 'insert', index, itemType)
1306 + self._options(cnf, kw))
1307 def insert_cascade(self, index, cnf={}, **kw):
1308 self.insert(index, 'cascade', cnf or kw)
1309 def insert_checkbutton(self, index, cnf={}, **kw):
1310 self.insert(index, 'checkbutton', cnf or kw)
1311 def insert_command(self, index, cnf={}, **kw):
1312 self.insert(index, 'command', cnf or kw)
1313 def insert_radiobutton(self, index, cnf={}, **kw):
1314 self.insert(index, 'radiobutton', cnf or kw)
1315 def insert_separator(self, index, cnf={}, **kw):
1316 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001317 def delete(self, index1, index2=None):
1318 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001319 def entryconfig(self, index, cnf=None, **kw):
1320 if cnf is None and not kw:
1321 cnf = {}
1322 for x in self.tk.split(apply(self.tk.call,
1323 (self._w, 'entryconfigure', index))):
1324 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1325 return cnf
1326 if type(cnf) == StringType and not kw:
1327 x = self.tk.split(apply(self.tk.call,
1328 (self._w, 'entryconfigure', index, '-'+cnf)))
1329 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001330 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001331 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001332 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001333 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001334 i = self.tk.call(self._w, 'index', index)
1335 if i == 'none': return None
1336 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001337 def invoke(self, index):
1338 return self.tk.call(self._w, 'invoke', index)
1339 def post(self, x, y):
1340 self.tk.call(self._w, 'post', x, y)
1341 def unpost(self):
1342 self.tk.call(self._w, 'unpost')
1343 def yposition(self, index):
1344 return self.tk.getint(self.tk.call(
1345 self._w, 'yposition', index))
1346
1347class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001348 def __init__(self, master=None, cnf={}, **kw):
1349 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001350
1351class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001352 def __init__(self, master=None, cnf={}, **kw):
1353 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001354
1355class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001356 def __init__(self, master=None, cnf={}, **kw):
1357 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001358 def deselect(self):
1359 self.tk.call(self._w, 'deselect')
1360 def flash(self):
1361 self.tk.call(self._w, 'flash')
1362 def invoke(self):
1363 self.tk.call(self._w, 'invoke')
1364 def select(self):
1365 self.tk.call(self._w, 'select')
1366
1367class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001368 def __init__(self, master=None, cnf={}, **kw):
1369 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001370 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001371 value = self.tk.call(self._w, 'get')
1372 try:
1373 return self.tk.getint(value)
1374 except TclError:
1375 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001376 def set(self, value):
1377 self.tk.call(self._w, 'set', value)
1378
1379class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001380 def __init__(self, master=None, cnf={}, **kw):
1381 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001382 def activate(self, index):
1383 self.tk.call(self._w, 'activate', index)
1384 def delta(self, deltax, deltay):
1385 return self.getdouble(self.tk.call(
1386 self._w, 'delta', deltax, deltay))
1387 def fraction(self, x, y):
1388 return self.getdouble(self.tk.call(
1389 self._w, 'fraction', x, y))
1390 def identify(self, x, y):
1391 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001392 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001393 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001394 def set(self, *args):
1395 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001396
1397class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001398 def __init__(self, master=None, cnf={}, **kw):
1399 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001400 def bbox(self, *args):
1401 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001402 def tk_textSelectTo(self, index):
1403 self.tk.call('tk_textSelectTo', self._w, index)
1404 def tk_textBackspace(self):
1405 self.tk.call('tk_textBackspace', self._w)
1406 def tk_textIndexCloser(self, a, b, c):
1407 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1408 def tk_textResetAnchor(self, index):
1409 self.tk.call('tk_textResetAnchor', self._w, index)
1410 def compare(self, index1, op, index2):
1411 return self.tk.getboolean(self.tk.call(
1412 self._w, 'compare', index1, op, index2))
1413 def debug(self, boolean=None):
1414 return self.tk.getboolean(self.tk.call(
1415 self._w, 'debug', boolean))
1416 def delete(self, index1, index2=None):
1417 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001418 def dlineinfo(self, index):
1419 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001420 def get(self, index1, index2=None):
1421 return self.tk.call(self._w, 'get', index1, index2)
1422 def index(self, index):
1423 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001424 def insert(self, index, chars, *args):
1425 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001426 def mark_gravity(self, markName, direction=None):
1427 return apply(self.tk.call,
1428 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001429 def mark_names(self):
1430 return self.tk.splitlist(self.tk.call(
1431 self._w, 'mark', 'names'))
1432 def mark_set(self, markName, index):
1433 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001434 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001435 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001436 def scan_mark(self, x, y):
1437 self.tk.call(self._w, 'scan', 'mark', x, y)
1438 def scan_dragto(self, x, y):
1439 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001440 def search(self, pattern, index, stopindex=None,
1441 forwards=None, backwards=None, exact=None,
1442 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001443 args = [self._w, 'search']
1444 if forwards: args.append('-forwards')
1445 if backwards: args.append('-backwards')
1446 if exact: args.append('-exact')
1447 if regexp: args.append('-regexp')
1448 if nocase: args.append('-nocase')
1449 if count: args.append('-count'); args.append(count)
1450 if pattern[0] == '-': args.append('--')
1451 args.append(pattern)
1452 args.append(index)
1453 if stopindex: args.append(stopindex)
1454 return apply(self.tk.call, tuple(args))
1455 def see(self, index):
1456 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001457 def tag_add(self, tagName, index1, index2=None):
1458 self.tk.call(
1459 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001460 def tag_unbind(self, tagName, sequence):
1461 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001462 def tag_bind(self, tagName, sequence, func, add=None):
1463 return self._bind((self._w, 'tag', 'bind', tagName),
1464 sequence, func, add)
1465 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001466 if option[:1] != '-':
1467 option = '-' + option
1468 if option[-1:] == '_':
1469 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001470 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001471 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001472 if type(cnf) == StringType:
1473 x = self.tk.split(self.tk.call(
1474 self._w, 'tag', 'configure', tagName, '-'+cnf))
1475 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001476 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001477 (self._w, 'tag', 'configure', tagName)
1478 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001479 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001480 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001481 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001482 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001483 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001484 def tag_names(self, index=None):
1485 return self.tk.splitlist(
1486 self.tk.call(self._w, 'tag', 'names', index))
1487 def tag_nextrange(self, tagName, index1, index2=None):
1488 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001489 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001490 def tag_raise(self, tagName, aboveThis=None):
1491 self.tk.call(
1492 self._w, 'tag', 'raise', tagName, aboveThis)
1493 def tag_ranges(self, tagName):
1494 return self.tk.splitlist(self.tk.call(
1495 self._w, 'tag', 'ranges', tagName))
1496 def tag_remove(self, tagName, index1, index2=None):
1497 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001498 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001499 def window_cget(self, index, option):
1500 return self.tk.call(self._w, 'window', 'cget', index, option)
1501 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001502 if type(cnf) == StringType:
1503 x = self.tk.split(self.tk.call(
1504 self._w, 'window', 'configure',
1505 index, '-'+cnf))
1506 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001507 apply(self.tk.call,
1508 (self._w, 'window', 'configure', index)
1509 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001510 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001511 def window_create(self, index, cnf={}, **kw):
1512 apply(self.tk.call,
1513 (self._w, 'window', 'create', index)
1514 + self._options(cnf, kw))
1515 def window_names(self):
1516 return self.tk.splitlist(
1517 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001518 def xview(self, *what):
1519 if not what:
1520 return self._getdoubles(self.tk.call(self._w, 'xview'))
1521 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001522 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001523 if not what:
1524 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001525 apply(self.tk.call, (self._w, 'yview')+what)
1526 def yview_pickplace(self, *what):
1527 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001528
Guido van Rossum28574b51996-10-21 15:16:51 +00001529class _setit:
1530 def __init__(self, var, value):
1531 self.__value = value
1532 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001533 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001534 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001535
1536class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001537 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001538 kw = {"borderwidth": 2, "textvariable": variable,
1539 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1540 "highlightthickness": 2}
1541 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001542 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001543 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1544 self.menuname = menu._w
1545 menu.add_command(label=value, command=_setit(variable, value))
1546 for v in values:
1547 menu.add_command(label=v, command=_setit(variable, v))
1548 self["menu"] = menu
1549
1550 def __getitem__(self, name):
1551 if name == 'menu':
1552 return self.__menu
1553 return Widget.__getitem__(self, name)
1554
1555 def destroy(self):
1556 Menubutton.destroy(self)
1557 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001558
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001559class Image:
1560 def __init__(self, imgtype, name=None, cnf={}, **kw):
1561 self.name = None
1562 master = _default_root
1563 if not master: raise RuntimeError, 'Too early to create image'
1564 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001565 if not name:
1566 name = `id(self)`
1567 # The following is needed for systems where id(x)
1568 # can return a negative number, such as Linux/m68k:
1569 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001570 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1571 elif kw: cnf = kw
1572 options = ()
1573 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001574 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001575 v = self._register(v)
1576 options = options + ('-'+k, v)
1577 apply(self.tk.call,
1578 ('image', 'create', imgtype, name,) + options)
1579 self.name = name
1580 def __str__(self): return self.name
1581 def __del__(self):
1582 if self.name:
1583 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001584 def __setitem__(self, key, value):
1585 self.tk.call(self.name, 'configure', '-'+key, value)
1586 def __getitem__(self, key):
1587 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001588 def height(self):
1589 return self.tk.getint(
1590 self.tk.call('image', 'height', self.name))
1591 def type(self):
1592 return self.tk.call('image', 'type', self.name)
1593 def width(self):
1594 return self.tk.getint(
1595 self.tk.call('image', 'width', self.name))
1596
1597class PhotoImage(Image):
1598 def __init__(self, name=None, cnf={}, **kw):
1599 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001600 def blank(self):
1601 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001602 def cget(self, option):
1603 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001604 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001605 def __getitem__(self, key):
1606 return self.tk.call(self.name, 'cget', '-' + key)
1607 def copy(self):
1608 destImage = PhotoImage()
1609 self.tk.call(destImage, 'copy', self.name)
1610 return destImage
1611 def zoom(self,x,y=''):
1612 destImage = PhotoImage()
1613 if y=='': y=x
1614 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1615 return destImage
1616 def subsample(self,x,y=''):
1617 destImage = PhotoImage()
1618 if y=='': y=x
1619 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1620 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001621 def get(self, x, y):
1622 return self.tk.call(self.name, 'get', x, y)
1623 def put(self, data, to=None):
1624 args = (self.name, 'put', data)
1625 if to:
1626 args = args + to
1627 apply(self.tk.call, args)
1628 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001629 def write(self, filename, format=None, from_coords=None):
1630 args = (self.name, 'write', filename)
1631 if format:
1632 args = args + ('-format', format)
1633 if from_coords:
1634 args = args + ('-from',) + tuple(from_coords)
1635 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001636
1637class BitmapImage(Image):
1638 def __init__(self, name=None, cnf={}, **kw):
1639 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1640
1641def image_names(): return _default_root.tk.call('image', 'names')
1642def image_types(): return _default_root.tk.call('image', 'types')
1643
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001644######################################################################
1645# Extensions:
1646
1647class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001648 def __init__(self, master=None, cnf={}, **kw):
1649 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001650 self.bind('<Any-Enter>', self.tkButtonEnter)
1651 self.bind('<Any-Leave>', self.tkButtonLeave)
1652 self.bind('<1>', self.tkButtonDown)
1653 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001654
1655class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001656 def __init__(self, master=None, cnf={}, **kw):
1657 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001658 self.bind('<Any-Enter>', self.tkButtonEnter)
1659 self.bind('<Any-Leave>', self.tkButtonLeave)
1660 self.bind('<1>', self.tkButtonDown)
1661 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1662 self['fg'] = self['bg']
1663 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001664
Guido van Rossumc417ef81996-08-21 23:38:59 +00001665######################################################################
1666# Test:
1667
1668def _test():
1669 root = Tk()
1670 label = Label(root, text="Proof-of-existence test for Tk")
1671 label.pack()
1672 test = Button(root, text="Click me!",
1673 command=lambda root=root: root.test.config(
1674 text="[%s]" % root.test['text']))
1675 test.pack()
1676 root.test = test
1677 quit = Button(root, text="QUIT", command=root.destroy)
1678 quit.pack()
1679 root.mainloop()
1680
1681if __name__ == '__main__':
1682 _test()
1683
Guido van Rossum37dcab11996-05-16 16:00:19 +00001684
1685# Emacs cruft
1686# Local Variables:
1687# py-indent-offset: 8
1688# End: