blob: edda34a82ca83027d516516cd3e17c9d189fa979 [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:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000079 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000080 def __init__(self, master=None):
81 global _default_root
82 global _varnum
83 if master:
84 self._tk = master.tk
85 else:
86 self._tk = _default_root.tk
87 self._name = 'PY_VAR' + `_varnum`
88 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000089 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000090 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000091 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000092 def __str__(self):
93 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000094 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000095 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000096
97class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000098 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000099 def __init__(self, master=None):
100 Variable.__init__(self, master)
101 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000102 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000103
104class IntVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000105 _default = "0"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000106 def __init__(self, master=None):
107 Variable.__init__(self, master)
108 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000109 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000110
111class DoubleVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000112 _default = "0.0"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000113 def __init__(self, master=None):
114 Variable.__init__(self, master)
115 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000116 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000117
118class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000119 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000120 def __init__(self, master=None):
121 Variable.__init__(self, master)
122 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000123 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000124
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000125def mainloop(n=0):
126 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000127
128def getint(s):
129 return _default_root.tk.getint(s)
130
131def getdouble(s):
132 return _default_root.tk.getdouble(s)
133
134def getboolean(s):
135 return _default_root.tk.getboolean(s)
136
Guido van Rossum18468821994-06-20 07:49:28 +0000137class Misc:
138 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000139 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000140 'set', 'tk_strictMotif', boolean))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000141 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000142 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000143 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000144 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000145 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000146 def wait_window(self, window=None):
147 if window == None:
148 window = self
149 self.tk.call('tkwait', 'window', window._w)
150 def wait_visibility(self, window=None):
151 if window == None:
152 window = self
153 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000154 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000155 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000156 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000157 return self.tk.getvar(name)
158 def getint(self, s):
159 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000160 def getdouble(self, s):
161 return self.tk.getdouble(s)
162 def getboolean(self, s):
163 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000164 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000165 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000166 focus = focus_set # XXX b/w compat?
167 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000168 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000169 def focus_default_none(self):
170 self.tk.call('focus', 'default', 'none')
171 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000172 def focus_none(self):
173 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000174 def focus_get(self):
175 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000176 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000177 return self._nametowidget(name)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000178 def tk_focusNext(self):
179 name = self.tk.call('tk_focusNext', self._w)
180 if not name: return None
181 return self._nametowidget(name)
182 def tk_focusPrev(self):
183 name = self.tk.call('tk_focusPrev', self._w)
184 if not name: return None
185 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000186 def after(self, ms, func=None, *args):
187 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000188 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000189 self.tk.call('after', ms)
190 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000191 # XXX Disgusting hack to clean up after calling func
192 tmp = []
193 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
194 try:
195 apply(func, args)
196 finally:
197 tk.deletecommand(tmp[0])
198 name = self._register(callit)
199 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000200 return self.tk.call('after', ms, name)
201 def after_idle(self, func, *args):
202 return apply(self.after, ('idle', func) + args)
203 def after_cancel(self, id):
204 self.tk.call('after', 'cancel', id)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000205 def bell(self, displayof=None):
206 if displayof:
207 self.tk.call('bell', '-displayof', displayof)
208 else:
209 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000210 # XXX grab current w/o window argument
211 def grab_current(self):
212 name = self.tk.call('grab', 'current', self._w)
213 if not name: return None
214 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000215 def grab_release(self):
216 self.tk.call('grab', 'release', self._w)
217 def grab_set(self):
218 self.tk.call('grab', 'set', self._w)
219 def grab_set_global(self):
220 self.tk.call('grab', 'set', '-global', self._w)
221 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000222 status = self.tk.call('grab', 'status', self._w)
223 if status == 'none': status = None
224 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000225 def lower(self, belowThis=None):
226 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000227 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000228 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000229 def option_clear(self):
230 self.tk.call('option', 'clear')
231 def option_get(self, name, className):
232 return self.tk.call('option', 'get', self._w, name, className)
233 def option_readfile(self, fileName, priority = None):
234 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000235 def selection_clear(self):
236 self.tk.call('selection', 'clear', self._w)
237 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000238 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000239 def selection_handle(self, func, type=None, format=None):
240 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000241 self.tk.call('selection', 'handle', self._w,
242 name, type, format)
243 def selection_own(self, func=None):
244 name = self._register(func)
245 self.tk.call('selection', 'own', self._w, name)
246 def selection_own_get(self):
247 return self._nametowidget(self.tk.call('selection', 'own'))
248 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000249 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000250 def lower(self, belowThis=None):
251 self.tk.call('lift', self._w, belowThis)
252 def tkraise(self, aboveThis=None):
253 self.tk.call('raise', self._w, aboveThis)
254 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000255 def colormodel(self, value=None):
256 return self.tk.call('tk', 'colormodel', self._w, value)
257 def winfo_atom(self, name):
258 return self.tk.getint(self.tk.call('winfo', 'atom', name))
259 def winfo_atomname(self, id):
260 return self.tk.call('winfo', 'atomname', id)
261 def winfo_cells(self):
262 return self.tk.getint(
263 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000264 def winfo_children(self):
265 return map(self._nametowidget,
266 self.tk.splitlist(self.tk.call(
267 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000268 def winfo_class(self):
269 return self.tk.call('winfo', 'class', self._w)
270 def winfo_containing(self, rootX, rootY):
Guido van Rossum36269991996-05-16 17:11:27 +0000271 return self.tk.call('winfo', 'containing', rootX, rootY)
Guido van Rossum18468821994-06-20 07:49:28 +0000272 def winfo_depth(self):
273 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
274 def winfo_exists(self):
275 return self.tk.getint(
276 self.tk.call('winfo', 'exists', self._w))
277 def winfo_fpixels(self, number):
278 return self.tk.getdouble(self.tk.call(
279 'winfo', 'fpixels', self._w, number))
280 def winfo_geometry(self):
281 return self.tk.call('winfo', 'geometry', self._w)
282 def winfo_height(self):
283 return self.tk.getint(
284 self.tk.call('winfo', 'height', self._w))
285 def winfo_id(self):
286 return self.tk.getint(
287 self.tk.call('winfo', 'id', self._w))
288 def winfo_interps(self):
289 return self.tk.splitlist(
290 self.tk.call('winfo', 'interps'))
291 def winfo_ismapped(self):
292 return self.tk.getint(
293 self.tk.call('winfo', 'ismapped', self._w))
294 def winfo_name(self):
295 return self.tk.call('winfo', 'name', self._w)
296 def winfo_parent(self):
297 return self.tk.call('winfo', 'parent', self._w)
298 def winfo_pathname(self, id):
299 return self.tk.call('winfo', 'pathname', id)
300 def winfo_pixels(self, number):
301 return self.tk.getint(
302 self.tk.call('winfo', 'pixels', self._w, number))
303 def winfo_reqheight(self):
304 return self.tk.getint(
305 self.tk.call('winfo', 'reqheight', self._w))
306 def winfo_reqwidth(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'reqwidth', self._w))
309 def winfo_rgb(self, color):
310 return self._getints(
311 self.tk.call('winfo', 'rgb', self._w, color))
312 def winfo_rootx(self):
313 return self.tk.getint(
314 self.tk.call('winfo', 'rootx', self._w))
315 def winfo_rooty(self):
316 return self.tk.getint(
317 self.tk.call('winfo', 'rooty', self._w))
318 def winfo_screen(self):
319 return self.tk.call('winfo', 'screen', self._w)
320 def winfo_screencells(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'screencells', self._w))
323 def winfo_screendepth(self):
324 return self.tk.getint(
325 self.tk.call('winfo', 'screendepth', self._w))
326 def winfo_screenheight(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'screenheight', self._w))
329 def winfo_screenmmheight(self):
330 return self.tk.getint(
331 self.tk.call('winfo', 'screenmmheight', self._w))
332 def winfo_screenmmwidth(self):
333 return self.tk.getint(
334 self.tk.call('winfo', 'screenmmwidth', self._w))
335 def winfo_screenvisual(self):
336 return self.tk.call('winfo', 'screenvisual', self._w)
337 def winfo_screenwidth(self):
338 return self.tk.getint(
339 self.tk.call('winfo', 'screenwidth', self._w))
340 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000341 return self._nametowidget(self.tk.call(
342 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000343 def winfo_visual(self):
344 return self.tk.call('winfo', 'visual', self._w)
345 def winfo_vrootheight(self):
346 return self.tk.getint(
347 self.tk.call('winfo', 'vrootheight', self._w))
348 def winfo_vrootwidth(self):
349 return self.tk.getint(
350 self.tk.call('winfo', 'vrootwidth', self._w))
351 def winfo_vrootx(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'vrootx', self._w))
354 def winfo_vrooty(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'vrooty', self._w))
357 def winfo_width(self):
358 return self.tk.getint(
359 self.tk.call('winfo', 'width', self._w))
360 def winfo_x(self):
361 return self.tk.getint(
362 self.tk.call('winfo', 'x', self._w))
363 def winfo_y(self):
364 return self.tk.getint(
365 self.tk.call('winfo', 'y', self._w))
366 def update(self):
367 self.tk.call('update')
368 def update_idletasks(self):
369 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000370 def bindtags(self, tagList=None):
371 if tagList is None:
372 return self.tk.splitlist(
373 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000374 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000375 self.tk.call('bindtags', self._w, tagList)
376 def _bind(self, what, sequence, func, add):
377 if func:
378 cmd = ("%sset _tkinter_break [%s %s]\n"
379 'if {"$_tkinter_break" == "break"} break\n') \
380 % (add and '+' or '',
381 self._register(func, self._substitute),
382 _string.join(self._subst_format))
383 apply(self.tk.call, what + (sequence, cmd))
384 elif func == '':
385 apply(self.tk.call, what + (sequence, func))
386 else:
387 return apply(self.tk.call, what + (sequence,))
388 def bind(self, sequence=None, func=None, add=None):
389 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000390 def unbind(self, sequence):
391 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000392 def bind_all(self, sequence=None, func=None, add=None):
393 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000394 def unbind_all(self, sequence):
395 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000396 def bind_class(self, className, sequence=None, func=None, add=None):
397 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000398 def unbind_class(self, className, sequence):
399 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000400 def mainloop(self, n=0):
401 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000402 def quit(self):
403 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000404 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000405 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000406 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
407 def _getdoubles(self, string):
408 if not string: return None
409 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000410 def _getboolean(self, string):
411 if string:
412 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000413 def _options(self, cnf, kw = None):
414 if kw:
415 cnf = _cnfmerge((cnf, kw))
416 else:
417 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000418 res = ()
419 for k, v in cnf.items():
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000420 if k[-1] == '_': k = k[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000421 if callable(v):
Guido van Rossum18468821994-06-20 07:49:28 +0000422 v = self._register(v)
423 res = res + ('-'+k, v)
424 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000425 def _nametowidget(self, name):
426 w = self
427 if name[0] == '.':
428 w = w._root()
429 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000430 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000431 while name:
432 i = find(name, '.')
433 if i >= 0:
434 name, tail = name[:i], name[i+1:]
435 else:
436 tail = ''
437 w = w.children[name]
438 name = tail
439 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000440 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000441 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000442 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000443 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000444 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000445 except AttributeError:
446 pass
447 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000448 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000449 except AttributeError:
450 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000451 self.tk.createcommand(name, f)
452 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000453 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000454 def _root(self):
455 w = self
456 while w.master: w = w.master
457 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000458 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000459 '%s', '%t', '%w', '%x', '%y',
460 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
461 def _substitute(self, *args):
462 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000463 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000464 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
465 # Missing: (a, c, d, m, o, v, B, R)
466 e = Event()
467 e.serial = tk.getint(nsign)
468 e.num = tk.getint(b)
469 try: e.focus = tk.getboolean(f)
470 except TclError: pass
471 e.height = tk.getint(h)
472 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000473 # For Visibility events, event state is a string and
474 # not an integer:
475 try:
476 e.state = tk.getint(s)
477 except TclError:
478 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000479 e.time = tk.getint(t)
480 e.width = tk.getint(w)
481 e.x = tk.getint(x)
482 e.y = tk.getint(y)
483 e.char = A
484 try: e.send_event = tk.getboolean(E)
485 except TclError: pass
486 e.keysym = K
487 e.keysym_num = tk.getint(N)
488 e.type = T
489 e.widget = self._nametowidget(W)
490 e.x_root = tk.getint(X)
491 e.y_root = tk.getint(Y)
492 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000493 def _report_exception(self):
494 import sys
495 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
496 root = self._root()
497 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000498
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000499class CallWrapper:
500 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000501 self.func = func
502 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000503 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000504 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000505 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000506 if self.subst:
507 args = apply(self.subst, args)
508 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000509 except SystemExit, msg:
510 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000511 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000512 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000513
514class Wm:
515 def aspect(self,
516 minNumer=None, minDenom=None,
517 maxNumer=None, maxDenom=None):
518 return self._getints(
519 self.tk.call('wm', 'aspect', self._w,
520 minNumer, minDenom,
521 maxNumer, maxDenom))
522 def client(self, name=None):
523 return self.tk.call('wm', 'client', self._w, name)
524 def command(self, value=None):
525 return self.tk.call('wm', 'command', self._w, value)
526 def deiconify(self):
527 return self.tk.call('wm', 'deiconify', self._w)
528 def focusmodel(self, model=None):
529 return self.tk.call('wm', 'focusmodel', self._w, model)
530 def frame(self):
531 return self.tk.call('wm', 'frame', self._w)
532 def geometry(self, newGeometry=None):
533 return self.tk.call('wm', 'geometry', self._w, newGeometry)
534 def grid(self,
535 baseWidht=None, baseHeight=None,
536 widthInc=None, heightInc=None):
537 return self._getints(self.tk.call(
538 'wm', 'grid', self._w,
539 baseWidht, baseHeight, widthInc, heightInc))
540 def group(self, pathName=None):
541 return self.tk.call('wm', 'group', self._w, pathName)
542 def iconbitmap(self, bitmap=None):
543 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
544 def iconify(self):
545 return self.tk.call('wm', 'iconify', self._w)
546 def iconmask(self, bitmap=None):
547 return self.tk.call('wm', 'iconmask', self._w, bitmap)
548 def iconname(self, newName=None):
549 return self.tk.call('wm', 'iconname', self._w, newName)
550 def iconposition(self, x=None, y=None):
551 return self._getints(self.tk.call(
552 'wm', 'iconposition', self._w, x, y))
553 def iconwindow(self, pathName=None):
554 return self.tk.call('wm', 'iconwindow', self._w, pathName)
555 def maxsize(self, width=None, height=None):
556 return self._getints(self.tk.call(
557 'wm', 'maxsize', self._w, width, height))
558 def minsize(self, width=None, height=None):
559 return self._getints(self.tk.call(
560 'wm', 'minsize', self._w, width, height))
561 def overrideredirect(self, boolean=None):
562 return self._getboolean(self.tk.call(
563 'wm', 'overrideredirect', self._w, boolean))
564 def positionfrom(self, who=None):
565 return self.tk.call('wm', 'positionfrom', self._w, who)
566 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000567 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000568 command = self._register(func)
569 else:
570 command = func
571 return self.tk.call(
572 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000573 def resizable(self, width=None, height=None):
574 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000575 def sizefrom(self, who=None):
576 return self.tk.call('wm', 'sizefrom', self._w, who)
577 def state(self):
578 return self.tk.call('wm', 'state', self._w)
579 def title(self, string=None):
580 return self.tk.call('wm', 'title', self._w, string)
581 def transient(self, master=None):
582 return self.tk.call('wm', 'transient', self._w, master)
583 def withdraw(self):
584 return self.tk.call('wm', 'withdraw', self._w)
585
586class Tk(Misc, Wm):
587 _w = '.'
588 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000589 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000590 self.master = None
591 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000592 if baseName is None:
593 import sys, os
594 baseName = os.path.basename(sys.argv[0])
595 if baseName[-3:] == '.py': baseName = baseName[:-3]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000596 self.tk = tkinter.create(screenName, baseName, className)
597 try:
598 # Disable event scanning except for Command-Period
599 import MacOS
600 MacOS.EnableAppswitch(0)
601 except ImportError:
602 pass
603 else:
604 # Work around nasty MacTk bug
605 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000606 # Version sanity checks
607 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000608 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000609 raise RuntimeError, \
610 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000611 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000612 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000613 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000614 raise RuntimeError, \
615 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000616 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000617 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000618 raise RuntimeError, \
619 "Tk 4.0 or higher is required; found Tk %s" \
620 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000621 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000622 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000623 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000624 if not _default_root:
625 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000626 def destroy(self):
627 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000628 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000629 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000630 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000631 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000632 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000633 if os.environ.has_key('HOME'): home = os.environ['HOME']
634 else: home = os.curdir
635 class_tcl = os.path.join(home, '.%s.tcl' % className)
636 class_py = os.path.join(home, '.%s.py' % className)
637 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
638 base_py = os.path.join(home, '.%s.py' % baseName)
639 dir = {'self': self}
640 exec 'from Tkinter import *' in dir
641 if os.path.isfile(class_tcl):
642 print 'source', `class_tcl`
643 self.tk.call('source', class_tcl)
644 if os.path.isfile(class_py):
645 print 'execfile', `class_py`
646 execfile(class_py, dir)
647 if os.path.isfile(base_tcl):
648 print 'source', `base_tcl`
649 self.tk.call('source', base_tcl)
650 if os.path.isfile(base_py):
651 print 'execfile', `base_py`
652 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000653 def report_callback_exception(self, exc, val, tb):
654 import traceback
655 print "Exception in Tkinter callback"
656 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000657
658class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000659 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000660 apply(self.tk.call,
661 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000662 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000663 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000664 pack = config
665 def __setitem__(self, key, value):
666 Pack.config({key: value})
667 def forget(self):
668 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000669 pack_forget = forget
670 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000671 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000672 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000673 dict = {}
674 for i in range(0, len(words), 2):
675 key = words[i][1:]
676 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000677 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000678 value = self._nametowidget(value)
679 dict[key] = value
680 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000681 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000682 _noarg_ = ['_noarg_']
683 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000684 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000685 return self._getboolean(self.tk.call(
686 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000687 else:
688 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000689 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000690 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000691 return map(self._nametowidget,
692 self.tk.splitlist(
693 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000694 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000695
696class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000697 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000698 for k in ['in_']:
699 if kw.has_key(k):
700 kw[k[:-1]] = kw[k]
701 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000702 apply(self.tk.call,
703 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000704 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000705 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000706 place = config
707 def __setitem__(self, key, value):
708 Place.config({key: value})
709 def forget(self):
710 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000711 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000712 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000713 words = self.tk.splitlist(
714 self.tk.call('place', 'info', self._w))
715 dict = {}
716 for i in range(0, len(words), 2):
717 key = words[i][1:]
718 value = words[i+1]
719 if value[:1] == '.':
720 value = self._nametowidget(value)
721 dict[key] = value
722 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000723 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000724 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000725 return map(self._nametowidget,
726 self.tk.splitlist(
727 self.tk.call(
728 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000729 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000730
Guido van Rossum37dcab11996-05-16 16:00:19 +0000731class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000732 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000733 def config(self, cnf={}, **kw):
734 apply(self.tk.call,
735 ('grid', 'configure', self._w)
736 + self._options(cnf, kw))
737 grid = config
738 def __setitem__(self, key, value):
739 Grid.config({key: value})
740 def bbox(self, column, row):
741 return self._getints(
742 self.tk.call(
743 'grid', 'bbox', self._w, column, row)) or None
744 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000745 def columnconfigure(self, index, cnf={}, **kw):
746 if type(cnf) is not DictionaryType and not kw:
747 options = self._options({cnf: None})
748 else:
749 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000750 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000751 ('grid', 'columnconfigure', self._w, index)
752 + options)
753 if options == ('-minsize', None):
754 return self.tk.getint(res) or None
755 elif options == ('-weight', None):
756 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000757 def forget(self):
758 self.tk.call('grid', 'forget', self._w)
759 grid_forget = forget
760 def info(self):
761 words = self.tk.splitlist(
762 self.tk.call('grid', 'info', self._w))
763 dict = {}
764 for i in range(0, len(words), 2):
765 key = words[i][1:]
766 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000767 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000768 value = self._nametowidget(value)
769 dict[key] = value
770 return dict
771 grid_info = info
772 def location(self, x, y):
773 return self._getints(
774 self.tk.call(
775 'grid', 'location', self._w, x, y)) or None
776 _noarg_ = ['_noarg_']
777 def propagate(self, flag=_noarg_):
778 if flag is Grid._noarg_:
779 return self._getboolean(self.tk.call(
780 'grid', 'propagate', self._w))
781 else:
782 self.tk.call('grid', 'propagate', self._w, flag)
783 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000784 def rowconfigure(self, index, cnf={}, **kw):
785 if type(cnf) is not DictionaryType and not kw:
786 options = self._options({cnf: None})
787 else:
788 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000789 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000790 ('grid', 'rowconfigure', self._w, index)
791 + options)
792 if options == ('-minsize', None):
793 return self.tk.getint(res) or None
794 elif options == ('-weight', None):
795 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000796 def size(self):
797 return self._getints(
798 self.tk.call('grid', 'size', self._w)) or None
799 def slaves(self, *args):
800 return map(self._nametowidget,
801 self.tk.splitlist(
802 apply(self.tk.call,
803 ('grid', 'slaves', self._w) + args)))
804 grid_slaves = slaves
805
806class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000807 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000808 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000809 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000810 if not _default_root:
811 _default_root = Tk()
812 master = _default_root
813 if not _default_root:
814 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000815 self.master = master
816 self.tk = master.tk
817 if cnf.has_key('name'):
818 name = cnf['name']
819 del cnf['name']
820 else:
821 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000822 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000823 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000824 self._w = '.' + name
825 else:
826 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000827 self.children = {}
828 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000829 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000830 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000831 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
832 if kw:
833 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000834 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000835 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000836 classes = []
837 for k in cnf.keys():
838 if type(k) is ClassType:
839 classes.append((k, cnf[k]))
840 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000841 apply(self.tk.call,
842 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000843 for k, v in classes:
844 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000845 def config(self, cnf=None, **kw):
846 # XXX ought to generalize this so tag_config etc. can use it
847 if kw:
848 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000849 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000850 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000851 if cnf is None:
852 cnf = {}
853 for x in self.tk.split(
854 self.tk.call(self._w, 'configure')):
855 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
856 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000857 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000858 x = self.tk.split(self.tk.call(
859 self._w, 'configure', '-'+cnf))
860 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000861 apply(self.tk.call, (self._w, 'configure')
862 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000863 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000864 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000865 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000866 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000867 def __setitem__(self, key, value):
868 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000869 def keys(self):
870 return map(lambda x: x[0][1:],
871 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000872 def __str__(self):
873 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000874 def destroy(self):
875 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000876 if self.master.children.has_key(self._name):
877 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000878 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000879 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000880 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000881
882class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000883 def __init__(self, master=None, cnf={}, **kw):
884 if kw:
885 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000886 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000887 for wmkey in ['screen', 'class_', 'class', 'visual',
888 'colormap']:
889 if cnf.has_key(wmkey):
890 val = cnf[wmkey]
891 # TBD: a hack needed because some keys
892 # are not valid as keyword arguments
893 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
894 else: opt = '-'+wmkey
895 extra = extra + (opt, val)
896 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000897 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000898 root = self._root()
899 self.iconname(root.iconname())
900 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000901
902class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000903 def __init__(self, master=None, cnf={}, **kw):
904 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000905 def tkButtonEnter(self, *dummy):
906 self.tk.call('tkButtonEnter', self._w)
907 def tkButtonLeave(self, *dummy):
908 self.tk.call('tkButtonLeave', self._w)
909 def tkButtonDown(self, *dummy):
910 self.tk.call('tkButtonDown', self._w)
911 def tkButtonUp(self, *dummy):
912 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000913 def tkButtonInvoke(self, *dummy):
914 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000915 def flash(self):
916 self.tk.call(self._w, 'flash')
917 def invoke(self):
918 self.tk.call(self._w, 'invoke')
919
920# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000921# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000922def AtEnd():
923 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000924def AtInsert(*args):
925 s = 'insert'
926 for a in args:
927 if a: s = s + (' ' + a)
928 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000929def AtSelFirst():
930 return 'sel.first'
931def AtSelLast():
932 return 'sel.last'
933def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000934 if y is None:
935 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000936 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000937 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000938
939class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000940 def __init__(self, master=None, cnf={}, **kw):
941 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000942 def addtag(self, *args):
943 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +0000944 def addtag_above(self, newtag, tagOrId):
945 self.addtag(newtag, 'above', tagOrId)
946 def addtag_all(self, newtag):
947 self.addtag(newtag, 'all')
948 def addtag_below(self, newtag, tagOrId):
949 self.addtag(newtag, 'below', tagOrId)
950 def addtag_closest(self, newtag, x, y, halo=None, start=None):
951 self.addtag(newtag, 'closest', x, y, halo, start)
952 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
953 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
954 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
955 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
956 def addtag_withtag(self, newtag, tagOrId):
957 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000958 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000959 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000960 def tag_unbind(self, tagOrId, sequence):
961 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000962 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +0000963 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +0000964 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +0000965 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000966 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000967 self._w, 'canvasx', screenx, gridspacing))
968 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000969 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000970 self._w, 'canvasy', screeny, gridspacing))
971 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +0000972 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +0000973 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000974 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000975 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000976 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000977 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000978 args = args[:-1]
979 else:
980 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000981 return self.tk.getint(apply(
982 self.tk.call,
983 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000984 + args + self._options(cnf, kw)))
985 def create_arc(self, *args, **kw):
986 return self._create('arc', args, kw)
987 def create_bitmap(self, *args, **kw):
988 return self._create('bitmap', args, kw)
989 def create_image(self, *args, **kw):
990 return self._create('image', args, kw)
991 def create_line(self, *args, **kw):
992 return self._create('line', args, kw)
993 def create_oval(self, *args, **kw):
994 return self._create('oval', args, kw)
995 def create_polygon(self, *args, **kw):
996 return self._create('polygon', args, kw)
997 def create_rectangle(self, *args, **kw):
998 return self._create('rectangle', args, kw)
999 def create_text(self, *args, **kw):
1000 return self._create('text', args, kw)
1001 def create_window(self, *args, **kw):
1002 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001003 def dchars(self, *args):
1004 self._do('dchars', args)
1005 def delete(self, *args):
1006 self._do('delete', args)
1007 def dtag(self, *args):
1008 self._do('dtag', args)
1009 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001010 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001011 def find_above(self, tagOrId):
1012 return self.find('above', tagOrId)
1013 def find_all(self):
1014 return self.find('all')
1015 def find_below(self, tagOrId):
1016 return self.find('below', tagOrId)
1017 def find_closest(self, x, y, halo=None, start=None):
1018 return self.find('closest', x, y, halo, start)
1019 def find_enclosed(self, x1, y1, x2, y2):
1020 return self.find('enclosed', x1, y1, x2, y2)
1021 def find_overlapping(self, x1, y1, x2, y2):
1022 return self.find('overlapping', x1, y1, x2, y2)
1023 def find_withtag(self, tagOrId):
1024 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001025 def focus(self, *args):
1026 return self._do('focus', args)
1027 def gettags(self, *args):
1028 return self.tk.splitlist(self._do('gettags', args))
1029 def icursor(self, *args):
1030 self._do('icursor', args)
1031 def index(self, *args):
1032 return self.tk.getint(self._do('index', args))
1033 def insert(self, *args):
1034 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001035 def itemcget(self, tagOrId, option):
1036 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001037 def itemconfig(self, tagOrId, cnf=None, **kw):
1038 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001039 cnf = {}
1040 for x in self.tk.split(
1041 self._do('itemconfigure', (tagOrId))):
1042 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1043 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001044 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001045 x = self.tk.split(self._do('itemconfigure',
1046 (tagOrId, '-'+cnf,)))
1047 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001048 self._do('itemconfigure', (tagOrId,)
1049 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001050 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001051 def lower(self, *args):
1052 self._do('lower', args)
1053 def move(self, *args):
1054 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001055 def postscript(self, cnf={}, **kw):
1056 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001057 def tkraise(self, *args):
1058 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001059 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001060 def scale(self, *args):
1061 self._do('scale', args)
1062 def scan_mark(self, x, y):
1063 self.tk.call(self._w, 'scan', 'mark', x, y)
1064 def scan_dragto(self, x, y):
1065 self.tk.call(self._w, 'scan', 'dragto', x, y)
1066 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001067 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001068 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001069 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001070 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001071 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001072 def select_item(self):
1073 self.tk.call(self._w, 'select', 'item')
1074 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001075 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001076 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001077 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001078 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001079 if not args:
1080 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001081 apply(self.tk.call, (self._w, 'xview')+args)
1082 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001083 if not args:
1084 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001085 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001086
1087class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001088 def __init__(self, master=None, cnf={}, **kw):
1089 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001090 def deselect(self):
1091 self.tk.call(self._w, 'deselect')
1092 def flash(self):
1093 self.tk.call(self._w, 'flash')
1094 def invoke(self):
1095 self.tk.call(self._w, 'invoke')
1096 def select(self):
1097 self.tk.call(self._w, 'select')
1098 def toggle(self):
1099 self.tk.call(self._w, 'toggle')
1100
1101class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001102 def __init__(self, master=None, cnf={}, **kw):
1103 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001104 def delete(self, first, last=None):
1105 self.tk.call(self._w, 'delete', first, last)
1106 def get(self):
1107 return self.tk.call(self._w, 'get')
1108 def icursor(self, index):
1109 self.tk.call(self._w, 'icursor', index)
1110 def index(self, index):
1111 return self.tk.getint(self.tk.call(
1112 self._w, 'index', index))
1113 def insert(self, index, string):
1114 self.tk.call(self._w, 'insert', index, string)
1115 def scan_mark(self, x):
1116 self.tk.call(self._w, 'scan', 'mark', x)
1117 def scan_dragto(self, x):
1118 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001119 def selection_adjust(self, index):
1120 self.tk.call(self._w, 'selection', 'adjust', index)
1121 select_adjust = selection_adjust
1122 def selection_clear(self):
1123 self.tk.call(self._w, 'selection', 'clear')
1124 select_clear = selection_clear
1125 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001126 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001127 select_from = selection_from
1128 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001129 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001130 self.tk.call(self._w, 'selection', 'present'))
1131 select_present = selection_present
1132 def selection_range(self, start, end):
1133 self.tk.call(self._w, 'selection', 'range', start, end)
1134 select_range = selection_range
1135 def selection_to(self, index):
1136 self.tk.call(self._w, 'selection', 'to', index)
1137 select_to = selection_to
1138 def xview(self, index):
1139 self.tk.call(self._w, 'xview', index)
1140 def xview_moveto(self, fraction):
1141 self.tk.call(self._w, 'xview', 'moveto', fraction)
1142 def xview_scroll(self, number, what):
1143 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001144
1145class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001146 def __init__(self, master=None, cnf={}, **kw):
1147 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001148 extra = ()
1149 if cnf.has_key('class'):
1150 extra = ('-class', cnf['class'])
1151 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001152 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001153
1154class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001155 def __init__(self, master=None, cnf={}, **kw):
1156 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001157
Guido van Rossum18468821994-06-20 07:49:28 +00001158class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001159 def __init__(self, master=None, cnf={}, **kw):
1160 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001161 def activate(self, index):
1162 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001163 def bbox(self, *args):
1164 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001165 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001166 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001167 return self.tk.splitlist(self.tk.call(
1168 self._w, 'curselection'))
1169 def delete(self, first, last=None):
1170 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001171 def get(self, first, last=None):
1172 if last:
1173 return self.tk.splitlist(self.tk.call(
1174 self._w, 'get', first, last))
1175 else:
1176 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001177 def insert(self, index, *elements):
1178 apply(self.tk.call,
1179 (self._w, 'insert', index) + elements)
1180 def nearest(self, y):
1181 return self.tk.getint(self.tk.call(
1182 self._w, 'nearest', y))
1183 def scan_mark(self, x, y):
1184 self.tk.call(self._w, 'scan', 'mark', x, y)
1185 def scan_dragto(self, x, y):
1186 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001187 def see(self, index):
1188 self.tk.call(self._w, 'see', index)
1189 def index(self, index):
1190 i = self.tk.call(self._w, 'index', index)
1191 if i == 'none': return None
1192 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001193 def select_adjust(self, index):
1194 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001195 def select_anchor(self, index):
1196 self.tk.call(self._w, 'selection', 'anchor', index)
1197 def select_clear(self, first, last=None):
1198 self.tk.call(self._w,
1199 'selection', 'clear', first, last)
1200 def select_includes(self, index):
1201 return self.tk.getboolean(self.tk.call(
1202 self._w, 'selection', 'includes', index))
1203 def select_set(self, first, last=None):
1204 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001205 def size(self):
1206 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001207 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001208 if not what:
1209 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001210 apply(self.tk.call, (self._w, 'xview')+what)
1211 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001212 if not what:
1213 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001214 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001215
1216class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001217 def __init__(self, master=None, cnf={}, **kw):
1218 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001219 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001220 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001221 def tk_mbPost(self):
1222 self.tk.call('tk_mbPost', self._w)
1223 def tk_mbUnpost(self):
1224 self.tk.call('tk_mbUnpost')
1225 def tk_traverseToMenu(self, char):
1226 self.tk.call('tk_traverseToMenu', self._w, char)
1227 def tk_traverseWithinMenu(self, char):
1228 self.tk.call('tk_traverseWithinMenu', self._w, char)
1229 def tk_getMenuButtons(self):
1230 return self.tk.call('tk_getMenuButtons', self._w)
1231 def tk_nextMenu(self, count):
1232 self.tk.call('tk_nextMenu', count)
1233 def tk_nextMenuEntry(self, count):
1234 self.tk.call('tk_nextMenuEntry', count)
1235 def tk_invokeMenu(self):
1236 self.tk.call('tk_invokeMenu', self._w)
1237 def tk_firstMenu(self):
1238 self.tk.call('tk_firstMenu', self._w)
1239 def tk_mbButtonDown(self):
1240 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001241 def tk_popup(self, x, y, entry=""):
1242 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001243 def activate(self, index):
1244 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001245 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001246 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001247 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001248 def add_cascade(self, cnf={}, **kw):
1249 self.add('cascade', cnf or kw)
1250 def add_checkbutton(self, cnf={}, **kw):
1251 self.add('checkbutton', cnf or kw)
1252 def add_command(self, cnf={}, **kw):
1253 self.add('command', cnf or kw)
1254 def add_radiobutton(self, cnf={}, **kw):
1255 self.add('radiobutton', cnf or kw)
1256 def add_separator(self, cnf={}, **kw):
1257 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001258 def insert(self, index, itemType, cnf={}, **kw):
1259 apply(self.tk.call, (self._w, 'insert', index, itemType)
1260 + self._options(cnf, kw))
1261 def insert_cascade(self, index, cnf={}, **kw):
1262 self.insert(index, 'cascade', cnf or kw)
1263 def insert_checkbutton(self, index, cnf={}, **kw):
1264 self.insert(index, 'checkbutton', cnf or kw)
1265 def insert_command(self, index, cnf={}, **kw):
1266 self.insert(index, 'command', cnf or kw)
1267 def insert_radiobutton(self, index, cnf={}, **kw):
1268 self.insert(index, 'radiobutton', cnf or kw)
1269 def insert_separator(self, index, cnf={}, **kw):
1270 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001271 def delete(self, index1, index2=None):
1272 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001273 def entryconfig(self, index, cnf=None, **kw):
1274 if cnf is None and not kw:
1275 cnf = {}
1276 for x in self.tk.split(apply(self.tk.call,
1277 (self._w, 'entryconfigure', index))):
1278 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1279 return cnf
1280 if type(cnf) == StringType and not kw:
1281 x = self.tk.split(apply(self.tk.call,
1282 (self._w, 'entryconfigure', index, '-'+cnf)))
1283 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001284 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001285 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001286 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001287 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001288 i = self.tk.call(self._w, 'index', index)
1289 if i == 'none': return None
1290 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001291 def invoke(self, index):
1292 return self.tk.call(self._w, 'invoke', index)
1293 def post(self, x, y):
1294 self.tk.call(self._w, 'post', x, y)
1295 def unpost(self):
1296 self.tk.call(self._w, 'unpost')
1297 def yposition(self, index):
1298 return self.tk.getint(self.tk.call(
1299 self._w, 'yposition', index))
1300
1301class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001302 def __init__(self, master=None, cnf={}, **kw):
1303 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001304
1305class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001306 def __init__(self, master=None, cnf={}, **kw):
1307 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001308
1309class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001310 def __init__(self, master=None, cnf={}, **kw):
1311 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001312 def deselect(self):
1313 self.tk.call(self._w, 'deselect')
1314 def flash(self):
1315 self.tk.call(self._w, 'flash')
1316 def invoke(self):
1317 self.tk.call(self._w, 'invoke')
1318 def select(self):
1319 self.tk.call(self._w, 'select')
1320
1321class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001322 def __init__(self, master=None, cnf={}, **kw):
1323 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001324 def get(self):
1325 return self.tk.getint(self.tk.call(self._w, 'get'))
1326 def set(self, value):
1327 self.tk.call(self._w, 'set', value)
1328
1329class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001330 def __init__(self, master=None, cnf={}, **kw):
1331 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001332 def activate(self, index):
1333 self.tk.call(self._w, 'activate', index)
1334 def delta(self, deltax, deltay):
1335 return self.getdouble(self.tk.call(
1336 self._w, 'delta', deltax, deltay))
1337 def fraction(self, x, y):
1338 return self.getdouble(self.tk.call(
1339 self._w, 'fraction', x, y))
1340 def identify(self, x, y):
1341 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001342 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001343 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001344 def set(self, *args):
1345 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001346
1347class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001348 def __init__(self, master=None, cnf={}, **kw):
1349 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001350 self.bind('<Delete>', self.bspace)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001351 def bbox(self, *args):
1352 return self._getints(self._do('bbox', args)) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001353 def bspace(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001354 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001355 def tk_textSelectTo(self, index):
1356 self.tk.call('tk_textSelectTo', self._w, index)
1357 def tk_textBackspace(self):
1358 self.tk.call('tk_textBackspace', self._w)
1359 def tk_textIndexCloser(self, a, b, c):
1360 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1361 def tk_textResetAnchor(self, index):
1362 self.tk.call('tk_textResetAnchor', self._w, index)
1363 def compare(self, index1, op, index2):
1364 return self.tk.getboolean(self.tk.call(
1365 self._w, 'compare', index1, op, index2))
1366 def debug(self, boolean=None):
1367 return self.tk.getboolean(self.tk.call(
1368 self._w, 'debug', boolean))
1369 def delete(self, index1, index2=None):
1370 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001371 def dlineinfo(self, index):
1372 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001373 def get(self, index1, index2=None):
1374 return self.tk.call(self._w, 'get', index1, index2)
1375 def index(self, index):
1376 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001377 def insert(self, index, chars, *args):
1378 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001379 def mark_gravity(self, markName, direction=None):
1380 return apply(self.tk.call,
1381 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001382 def mark_names(self):
1383 return self.tk.splitlist(self.tk.call(
1384 self._w, 'mark', 'names'))
1385 def mark_set(self, markName, index):
1386 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001387 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001388 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001389 def scan_mark(self, x, y):
1390 self.tk.call(self._w, 'scan', 'mark', x, y)
1391 def scan_dragto(self, x, y):
1392 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001393 def search(self, pattern, index, stopindex=None,
1394 forwards=None, backwards=None, exact=None,
1395 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001396 args = [self._w, 'search']
1397 if forwards: args.append('-forwards')
1398 if backwards: args.append('-backwards')
1399 if exact: args.append('-exact')
1400 if regexp: args.append('-regexp')
1401 if nocase: args.append('-nocase')
1402 if count: args.append('-count'); args.append(count)
1403 if pattern[0] == '-': args.append('--')
1404 args.append(pattern)
1405 args.append(index)
1406 if stopindex: args.append(stopindex)
1407 return apply(self.tk.call, tuple(args))
1408 def see(self, index):
1409 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001410 def tag_add(self, tagName, index1, index2=None):
1411 self.tk.call(
1412 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001413 def tag_unbind(self, tagName, sequence):
1414 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001415 def tag_bind(self, tagName, sequence, func, add=None):
1416 return self._bind((self._w, 'tag', 'bind', tagName),
1417 sequence, func, add)
1418 def tag_cget(self, tagName, option):
1419 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001420 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001421 if type(cnf) == StringType:
1422 x = self.tk.split(self.tk.call(
1423 self._w, 'tag', 'configure', tagName, '-'+cnf))
1424 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001425 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001426 (self._w, 'tag', 'configure', tagName)
1427 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001428 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001429 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001430 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001431 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001432 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001433 def tag_names(self, index=None):
1434 return self.tk.splitlist(
1435 self.tk.call(self._w, 'tag', 'names', index))
1436 def tag_nextrange(self, tagName, index1, index2=None):
1437 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001438 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001439 def tag_raise(self, tagName, aboveThis=None):
1440 self.tk.call(
1441 self._w, 'tag', 'raise', tagName, aboveThis)
1442 def tag_ranges(self, tagName):
1443 return self.tk.splitlist(self.tk.call(
1444 self._w, 'tag', 'ranges', tagName))
1445 def tag_remove(self, tagName, index1, index2=None):
1446 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001447 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001448 def window_cget(self, index, option):
1449 return self.tk.call(self._w, 'window', 'cget', index, option)
1450 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001451 if type(cnf) == StringType:
1452 x = self.tk.split(self.tk.call(
1453 self._w, 'window', 'configure',
1454 index, '-'+cnf))
1455 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001456 apply(self.tk.call,
1457 (self._w, 'window', 'configure', index)
1458 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001459 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001460 def window_create(self, index, cnf={}, **kw):
1461 apply(self.tk.call,
1462 (self._w, 'window', 'create', index)
1463 + self._options(cnf, kw))
1464 def window_names(self):
1465 return self.tk.splitlist(
1466 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001467 def xview(self, *what):
1468 if not what:
1469 return self._getdoubles(self.tk.call(self._w, 'xview'))
1470 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001471 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001472 if not what:
1473 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001474 apply(self.tk.call, (self._w, 'yview')+what)
1475 def yview_pickplace(self, *what):
1476 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001477
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001478class OptionMenu(Widget):
1479 def __init__(self, master, variable, value, *values):
1480 self.widgetName = 'tk_optionMenu'
1481 Widget._setup(self, master, {})
1482 self.menuname = apply(
1483 self.tk.call,
1484 (self.widgetName, self._w, variable, value) + values)
1485
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001486class Image:
1487 def __init__(self, imgtype, name=None, cnf={}, **kw):
1488 self.name = None
1489 master = _default_root
1490 if not master: raise RuntimeError, 'Too early to create image'
1491 self.tk = master.tk
1492 if not name: name = `id(self)`
1493 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1494 elif kw: cnf = kw
1495 options = ()
1496 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001497 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001498 v = self._register(v)
1499 options = options + ('-'+k, v)
1500 apply(self.tk.call,
1501 ('image', 'create', imgtype, name,) + options)
1502 self.name = name
1503 def __str__(self): return self.name
1504 def __del__(self):
1505 if self.name:
1506 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001507 def __setitem__(self, key, value):
1508 self.tk.call(self.name, 'configure', '-'+key, value)
1509 def __getitem__(self, key):
1510 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001511 def height(self):
1512 return self.tk.getint(
1513 self.tk.call('image', 'height', self.name))
1514 def type(self):
1515 return self.tk.call('image', 'type', self.name)
1516 def width(self):
1517 return self.tk.getint(
1518 self.tk.call('image', 'width', self.name))
1519
1520class PhotoImage(Image):
1521 def __init__(self, name=None, cnf={}, **kw):
1522 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001523 def blank(self):
1524 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001525 def cget(self, option):
1526 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001527 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001528 def __getitem__(self, key):
1529 return self.tk.call(self.name, 'cget', '-' + key)
1530 def copy(self):
1531 destImage = PhotoImage()
1532 self.tk.call(destImage, 'copy', self.name)
1533 return destImage
1534 def zoom(self,x,y=''):
1535 destImage = PhotoImage()
1536 if y=='': y=x
1537 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1538 return destImage
1539 def subsample(self,x,y=''):
1540 destImage = PhotoImage()
1541 if y=='': y=x
1542 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1543 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001544 def get(self, x, y):
1545 return self.tk.call(self.name, 'get', x, y)
1546 def put(self, data, to=None):
1547 args = (self.name, 'put', data)
1548 if to:
1549 args = args + to
1550 apply(self.tk.call, args)
1551 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001552 def write(self, filename, format=None, from_coords=None):
1553 args = (self.name, 'write', filename)
1554 if format:
1555 args = args + ('-format', format)
1556 if from_coords:
1557 args = args + ('-from',) + tuple(from_coords)
1558 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001559
1560class BitmapImage(Image):
1561 def __init__(self, name=None, cnf={}, **kw):
1562 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1563
1564def image_names(): return _default_root.tk.call('image', 'names')
1565def image_types(): return _default_root.tk.call('image', 'types')
1566
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001567######################################################################
1568# Extensions:
1569
1570class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001571 def __init__(self, master=None, cnf={}, **kw):
1572 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001573 self.bind('<Any-Enter>', self.tkButtonEnter)
1574 self.bind('<Any-Leave>', self.tkButtonLeave)
1575 self.bind('<1>', self.tkButtonDown)
1576 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001577
1578class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001579 def __init__(self, master=None, cnf={}, **kw):
1580 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001581 self.bind('<Any-Enter>', self.tkButtonEnter)
1582 self.bind('<Any-Leave>', self.tkButtonLeave)
1583 self.bind('<1>', self.tkButtonDown)
1584 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1585 self['fg'] = self['bg']
1586 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001587
Guido van Rossumc417ef81996-08-21 23:38:59 +00001588######################################################################
1589# Test:
1590
1591def _test():
1592 root = Tk()
1593 label = Label(root, text="Proof-of-existence test for Tk")
1594 label.pack()
1595 test = Button(root, text="Click me!",
1596 command=lambda root=root: root.test.config(
1597 text="[%s]" % root.test['text']))
1598 test.pack()
1599 root.test = test
1600 quit = Button(root, text="QUIT", command=root.destroy)
1601 quit.pack()
1602 root.mainloop()
1603
1604if __name__ == '__main__':
1605 _test()
1606
Guido van Rossum37dcab11996-05-16 16:00:19 +00001607
1608# Emacs cruft
1609# Local Variables:
1610# py-indent-offset: 8
1611# End: