blob: 0892a697c399c7eada149f992782d3947ab6ed48 [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 Rossum8535b291996-09-10 12:36:34 +0000142 if TkVersion < 4.1:
143 apply(self.tk.call, ('tk_menuBar', self._w) + args)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000144 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000145 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000146 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000147 def wait_window(self, window=None):
148 if window == None:
149 window = self
150 self.tk.call('tkwait', 'window', window._w)
151 def wait_visibility(self, window=None):
152 if window == None:
153 window = self
154 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000155 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000156 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000157 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000158 return self.tk.getvar(name)
159 def getint(self, s):
160 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000161 def getdouble(self, s):
162 return self.tk.getdouble(s)
163 def getboolean(self, s):
164 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000165 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000166 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000167 focus = focus_set # XXX b/w compat?
168 def focus_default_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000169 self.tk.call('focus', 'default', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000170 def focus_default_none(self):
171 self.tk.call('focus', 'default', 'none')
172 focus_default = focus_default_set
Guido van Rossum18468821994-06-20 07:49:28 +0000173 def focus_none(self):
174 self.tk.call('focus', 'none')
Guido van Rossum45853db1994-06-20 12:19:19 +0000175 def focus_get(self):
176 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000177 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000178 return self._nametowidget(name)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000179 def tk_focusNext(self):
180 name = self.tk.call('tk_focusNext', self._w)
181 if not name: return None
182 return self._nametowidget(name)
183 def tk_focusPrev(self):
184 name = self.tk.call('tk_focusPrev', self._w)
185 if not name: return None
186 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000187 def after(self, ms, func=None, *args):
188 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000189 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000190 self.tk.call('after', ms)
191 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000192 # XXX Disgusting hack to clean up after calling func
193 tmp = []
194 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
195 try:
196 apply(func, args)
197 finally:
198 tk.deletecommand(tmp[0])
199 name = self._register(callit)
200 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000201 return self.tk.call('after', ms, name)
202 def after_idle(self, func, *args):
203 return apply(self.after, ('idle', func) + args)
204 def after_cancel(self, id):
205 self.tk.call('after', 'cancel', id)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000206 def bell(self, displayof=None):
207 if displayof:
208 self.tk.call('bell', '-displayof', displayof)
209 else:
210 self.tk.call('bell', '-displayof', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000211 # XXX grab current w/o window argument
212 def grab_current(self):
213 name = self.tk.call('grab', 'current', self._w)
214 if not name: return None
215 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000216 def grab_release(self):
217 self.tk.call('grab', 'release', self._w)
218 def grab_set(self):
219 self.tk.call('grab', 'set', self._w)
220 def grab_set_global(self):
221 self.tk.call('grab', 'set', '-global', self._w)
222 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000223 status = self.tk.call('grab', 'status', self._w)
224 if status == 'none': status = None
225 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000226 def lower(self, belowThis=None):
227 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000228 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000229 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000230 def option_clear(self):
231 self.tk.call('option', 'clear')
232 def option_get(self, name, className):
233 return self.tk.call('option', 'get', self._w, name, className)
234 def option_readfile(self, fileName, priority = None):
235 self.tk.call('option', 'readfile', fileName, priority)
Guido van Rossum18468821994-06-20 07:49:28 +0000236 def selection_clear(self):
237 self.tk.call('selection', 'clear', self._w)
238 def selection_get(self, type=None):
Guido van Rossumbd84b041994-07-04 10:48:25 +0000239 return self.tk.call('selection', 'get', type)
Guido van Rossum18468821994-06-20 07:49:28 +0000240 def selection_handle(self, func, type=None, format=None):
241 name = self._register(func)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000242 self.tk.call('selection', 'handle', self._w,
243 name, type, format)
244 def selection_own(self, func=None):
245 name = self._register(func)
246 self.tk.call('selection', 'own', self._w, name)
247 def selection_own_get(self):
248 return self._nametowidget(self.tk.call('selection', 'own'))
249 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000250 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000251 def lower(self, belowThis=None):
252 self.tk.call('lift', self._w, belowThis)
253 def tkraise(self, aboveThis=None):
254 self.tk.call('raise', self._w, aboveThis)
255 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000256 def colormodel(self, value=None):
257 return self.tk.call('tk', 'colormodel', self._w, value)
258 def winfo_atom(self, name):
259 return self.tk.getint(self.tk.call('winfo', 'atom', name))
260 def winfo_atomname(self, id):
261 return self.tk.call('winfo', 'atomname', id)
262 def winfo_cells(self):
263 return self.tk.getint(
264 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000265 def winfo_children(self):
266 return map(self._nametowidget,
267 self.tk.splitlist(self.tk.call(
268 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000269 def winfo_class(self):
270 return self.tk.call('winfo', 'class', self._w)
271 def winfo_containing(self, rootX, rootY):
Guido van Rossum36269991996-05-16 17:11:27 +0000272 return self.tk.call('winfo', 'containing', rootX, rootY)
Guido van Rossum18468821994-06-20 07:49:28 +0000273 def winfo_depth(self):
274 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
275 def winfo_exists(self):
276 return self.tk.getint(
277 self.tk.call('winfo', 'exists', self._w))
278 def winfo_fpixels(self, number):
279 return self.tk.getdouble(self.tk.call(
280 'winfo', 'fpixels', self._w, number))
281 def winfo_geometry(self):
282 return self.tk.call('winfo', 'geometry', self._w)
283 def winfo_height(self):
284 return self.tk.getint(
285 self.tk.call('winfo', 'height', self._w))
286 def winfo_id(self):
287 return self.tk.getint(
288 self.tk.call('winfo', 'id', self._w))
289 def winfo_interps(self):
290 return self.tk.splitlist(
291 self.tk.call('winfo', 'interps'))
292 def winfo_ismapped(self):
293 return self.tk.getint(
294 self.tk.call('winfo', 'ismapped', self._w))
295 def winfo_name(self):
296 return self.tk.call('winfo', 'name', self._w)
297 def winfo_parent(self):
298 return self.tk.call('winfo', 'parent', self._w)
299 def winfo_pathname(self, id):
300 return self.tk.call('winfo', 'pathname', id)
301 def winfo_pixels(self, number):
302 return self.tk.getint(
303 self.tk.call('winfo', 'pixels', self._w, number))
304 def winfo_reqheight(self):
305 return self.tk.getint(
306 self.tk.call('winfo', 'reqheight', self._w))
307 def winfo_reqwidth(self):
308 return self.tk.getint(
309 self.tk.call('winfo', 'reqwidth', self._w))
310 def winfo_rgb(self, color):
311 return self._getints(
312 self.tk.call('winfo', 'rgb', self._w, color))
313 def winfo_rootx(self):
314 return self.tk.getint(
315 self.tk.call('winfo', 'rootx', self._w))
316 def winfo_rooty(self):
317 return self.tk.getint(
318 self.tk.call('winfo', 'rooty', self._w))
319 def winfo_screen(self):
320 return self.tk.call('winfo', 'screen', self._w)
321 def winfo_screencells(self):
322 return self.tk.getint(
323 self.tk.call('winfo', 'screencells', self._w))
324 def winfo_screendepth(self):
325 return self.tk.getint(
326 self.tk.call('winfo', 'screendepth', self._w))
327 def winfo_screenheight(self):
328 return self.tk.getint(
329 self.tk.call('winfo', 'screenheight', self._w))
330 def winfo_screenmmheight(self):
331 return self.tk.getint(
332 self.tk.call('winfo', 'screenmmheight', self._w))
333 def winfo_screenmmwidth(self):
334 return self.tk.getint(
335 self.tk.call('winfo', 'screenmmwidth', self._w))
336 def winfo_screenvisual(self):
337 return self.tk.call('winfo', 'screenvisual', self._w)
338 def winfo_screenwidth(self):
339 return self.tk.getint(
340 self.tk.call('winfo', 'screenwidth', self._w))
341 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000342 return self._nametowidget(self.tk.call(
343 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000344 def winfo_visual(self):
345 return self.tk.call('winfo', 'visual', self._w)
346 def winfo_vrootheight(self):
347 return self.tk.getint(
348 self.tk.call('winfo', 'vrootheight', self._w))
349 def winfo_vrootwidth(self):
350 return self.tk.getint(
351 self.tk.call('winfo', 'vrootwidth', self._w))
352 def winfo_vrootx(self):
353 return self.tk.getint(
354 self.tk.call('winfo', 'vrootx', self._w))
355 def winfo_vrooty(self):
356 return self.tk.getint(
357 self.tk.call('winfo', 'vrooty', self._w))
358 def winfo_width(self):
359 return self.tk.getint(
360 self.tk.call('winfo', 'width', self._w))
361 def winfo_x(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'x', self._w))
364 def winfo_y(self):
365 return self.tk.getint(
366 self.tk.call('winfo', 'y', self._w))
367 def update(self):
368 self.tk.call('update')
369 def update_idletasks(self):
370 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000371 def bindtags(self, tagList=None):
372 if tagList is None:
373 return self.tk.splitlist(
374 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000375 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000376 self.tk.call('bindtags', self._w, tagList)
377 def _bind(self, what, sequence, func, add):
378 if func:
379 cmd = ("%sset _tkinter_break [%s %s]\n"
380 'if {"$_tkinter_break" == "break"} break\n') \
381 % (add and '+' or '',
382 self._register(func, self._substitute),
383 _string.join(self._subst_format))
384 apply(self.tk.call, what + (sequence, cmd))
385 elif func == '':
386 apply(self.tk.call, what + (sequence, func))
387 else:
388 return apply(self.tk.call, what + (sequence,))
389 def bind(self, sequence=None, func=None, add=None):
390 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000391 def unbind(self, sequence):
392 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000393 def bind_all(self, sequence=None, func=None, add=None):
394 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000395 def unbind_all(self, sequence):
396 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000397 def bind_class(self, className, sequence=None, func=None, add=None):
398 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000399 def unbind_class(self, className, sequence):
400 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000401 def mainloop(self, n=0):
402 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000403 def quit(self):
404 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000405 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000406 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000407 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
408 def _getdoubles(self, string):
409 if not string: return None
410 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000411 def _getboolean(self, string):
412 if string:
413 return self.tk.getboolean(string)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000414 def _options(self, cnf, kw = None):
415 if kw:
416 cnf = _cnfmerge((cnf, kw))
417 else:
418 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000419 res = ()
420 for k, v in cnf.items():
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000421 if k[-1] == '_': k = k[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000422 if callable(v):
Guido van Rossum18468821994-06-20 07:49:28 +0000423 v = self._register(v)
424 res = res + ('-'+k, v)
425 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000426 def _nametowidget(self, name):
427 w = self
428 if name[0] == '.':
429 w = w._root()
430 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000431 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000432 while name:
433 i = find(name, '.')
434 if i >= 0:
435 name, tail = name[:i], name[i+1:]
436 else:
437 tail = ''
438 w = w.children[name]
439 name = tail
440 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000441 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000442 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000443 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000444 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000445 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000446 except AttributeError:
447 pass
448 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000449 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000450 except AttributeError:
451 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000452 self.tk.createcommand(name, f)
453 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000454 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000455 def _root(self):
456 w = self
457 while w.master: w = w.master
458 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000459 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000460 '%s', '%t', '%w', '%x', '%y',
461 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
462 def _substitute(self, *args):
463 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000464 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000465 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
466 # Missing: (a, c, d, m, o, v, B, R)
467 e = Event()
468 e.serial = tk.getint(nsign)
469 e.num = tk.getint(b)
470 try: e.focus = tk.getboolean(f)
471 except TclError: pass
472 e.height = tk.getint(h)
473 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000474 # For Visibility events, event state is a string and
475 # not an integer:
476 try:
477 e.state = tk.getint(s)
478 except TclError:
479 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000480 e.time = tk.getint(t)
481 e.width = tk.getint(w)
482 e.x = tk.getint(x)
483 e.y = tk.getint(y)
484 e.char = A
485 try: e.send_event = tk.getboolean(E)
486 except TclError: pass
487 e.keysym = K
488 e.keysym_num = tk.getint(N)
489 e.type = T
490 e.widget = self._nametowidget(W)
491 e.x_root = tk.getint(X)
492 e.y_root = tk.getint(Y)
493 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000494 def _report_exception(self):
495 import sys
496 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
497 root = self._root()
498 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000499
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000500class CallWrapper:
501 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000502 self.func = func
503 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000504 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000505 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000506 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000507 if self.subst:
508 args = apply(self.subst, args)
509 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000510 except SystemExit, msg:
511 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000512 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000513 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000514
515class Wm:
516 def aspect(self,
517 minNumer=None, minDenom=None,
518 maxNumer=None, maxDenom=None):
519 return self._getints(
520 self.tk.call('wm', 'aspect', self._w,
521 minNumer, minDenom,
522 maxNumer, maxDenom))
523 def client(self, name=None):
524 return self.tk.call('wm', 'client', self._w, name)
525 def command(self, value=None):
526 return self.tk.call('wm', 'command', self._w, value)
527 def deiconify(self):
528 return self.tk.call('wm', 'deiconify', self._w)
529 def focusmodel(self, model=None):
530 return self.tk.call('wm', 'focusmodel', self._w, model)
531 def frame(self):
532 return self.tk.call('wm', 'frame', self._w)
533 def geometry(self, newGeometry=None):
534 return self.tk.call('wm', 'geometry', self._w, newGeometry)
535 def grid(self,
536 baseWidht=None, baseHeight=None,
537 widthInc=None, heightInc=None):
538 return self._getints(self.tk.call(
539 'wm', 'grid', self._w,
540 baseWidht, baseHeight, widthInc, heightInc))
541 def group(self, pathName=None):
542 return self.tk.call('wm', 'group', self._w, pathName)
543 def iconbitmap(self, bitmap=None):
544 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
545 def iconify(self):
546 return self.tk.call('wm', 'iconify', self._w)
547 def iconmask(self, bitmap=None):
548 return self.tk.call('wm', 'iconmask', self._w, bitmap)
549 def iconname(self, newName=None):
550 return self.tk.call('wm', 'iconname', self._w, newName)
551 def iconposition(self, x=None, y=None):
552 return self._getints(self.tk.call(
553 'wm', 'iconposition', self._w, x, y))
554 def iconwindow(self, pathName=None):
555 return self.tk.call('wm', 'iconwindow', self._w, pathName)
556 def maxsize(self, width=None, height=None):
557 return self._getints(self.tk.call(
558 'wm', 'maxsize', self._w, width, height))
559 def minsize(self, width=None, height=None):
560 return self._getints(self.tk.call(
561 'wm', 'minsize', self._w, width, height))
562 def overrideredirect(self, boolean=None):
563 return self._getboolean(self.tk.call(
564 'wm', 'overrideredirect', self._w, boolean))
565 def positionfrom(self, who=None):
566 return self.tk.call('wm', 'positionfrom', self._w, who)
567 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000568 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000569 command = self._register(func)
570 else:
571 command = func
572 return self.tk.call(
573 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000574 def resizable(self, width=None, height=None):
575 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000576 def sizefrom(self, who=None):
577 return self.tk.call('wm', 'sizefrom', self._w, who)
578 def state(self):
579 return self.tk.call('wm', 'state', self._w)
580 def title(self, string=None):
581 return self.tk.call('wm', 'title', self._w, string)
582 def transient(self, master=None):
583 return self.tk.call('wm', 'transient', self._w, master)
584 def withdraw(self):
585 return self.tk.call('wm', 'withdraw', self._w)
586
587class Tk(Misc, Wm):
588 _w = '.'
589 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000590 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000591 self.master = None
592 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000593 if baseName is None:
594 import sys, os
595 baseName = os.path.basename(sys.argv[0])
596 if baseName[-3:] == '.py': baseName = baseName[:-3]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000597 self.tk = tkinter.create(screenName, baseName, className)
598 try:
599 # Disable event scanning except for Command-Period
600 import MacOS
601 MacOS.EnableAppswitch(0)
602 except ImportError:
603 pass
604 else:
605 # Work around nasty MacTk bug
606 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000607 # Version sanity checks
608 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000609 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000610 raise RuntimeError, \
611 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000612 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000613 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000614 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000615 raise RuntimeError, \
616 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000617 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000618 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000619 raise RuntimeError, \
620 "Tk 4.0 or higher is required; found Tk %s" \
621 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000622 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000623 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000624 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000625 if not _default_root:
626 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000627 def destroy(self):
628 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000629 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000630 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000631 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000632 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000633 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000634 if os.environ.has_key('HOME'): home = os.environ['HOME']
635 else: home = os.curdir
636 class_tcl = os.path.join(home, '.%s.tcl' % className)
637 class_py = os.path.join(home, '.%s.py' % className)
638 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
639 base_py = os.path.join(home, '.%s.py' % baseName)
640 dir = {'self': self}
641 exec 'from Tkinter import *' in dir
642 if os.path.isfile(class_tcl):
643 print 'source', `class_tcl`
644 self.tk.call('source', class_tcl)
645 if os.path.isfile(class_py):
646 print 'execfile', `class_py`
647 execfile(class_py, dir)
648 if os.path.isfile(base_tcl):
649 print 'source', `base_tcl`
650 self.tk.call('source', base_tcl)
651 if os.path.isfile(base_py):
652 print 'execfile', `base_py`
653 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000654 def report_callback_exception(self, exc, val, tb):
655 import traceback
656 print "Exception in Tkinter callback"
657 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000658
659class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000660 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000661 apply(self.tk.call,
662 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000663 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000664 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000665 pack = config
666 def __setitem__(self, key, value):
667 Pack.config({key: value})
668 def forget(self):
669 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000670 pack_forget = forget
671 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000672 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000673 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000674 dict = {}
675 for i in range(0, len(words), 2):
676 key = words[i][1:]
677 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000678 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000679 value = self._nametowidget(value)
680 dict[key] = value
681 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000682 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000683 _noarg_ = ['_noarg_']
684 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000685 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000686 return self._getboolean(self.tk.call(
687 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000688 else:
689 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000690 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000691 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000692 return map(self._nametowidget,
693 self.tk.splitlist(
694 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000695 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000696
697class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000698 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000699 for k in ['in_']:
700 if kw.has_key(k):
701 kw[k[:-1]] = kw[k]
702 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000703 apply(self.tk.call,
704 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000705 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000706 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000707 place = config
708 def __setitem__(self, key, value):
709 Place.config({key: value})
710 def forget(self):
711 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000712 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000713 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000714 words = self.tk.splitlist(
715 self.tk.call('place', 'info', self._w))
716 dict = {}
717 for i in range(0, len(words), 2):
718 key = words[i][1:]
719 value = words[i+1]
720 if value[:1] == '.':
721 value = self._nametowidget(value)
722 dict[key] = value
723 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000724 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000725 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000726 return map(self._nametowidget,
727 self.tk.splitlist(
728 self.tk.call(
729 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000730 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000731
Guido van Rossum37dcab11996-05-16 16:00:19 +0000732class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000733 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000734 def config(self, cnf={}, **kw):
735 apply(self.tk.call,
736 ('grid', 'configure', self._w)
737 + self._options(cnf, kw))
738 grid = config
739 def __setitem__(self, key, value):
740 Grid.config({key: value})
741 def bbox(self, column, row):
742 return self._getints(
743 self.tk.call(
744 'grid', 'bbox', self._w, column, row)) or None
745 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000746 def columnconfigure(self, index, cnf={}, **kw):
747 if type(cnf) is not DictionaryType and not kw:
748 options = self._options({cnf: None})
749 else:
750 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000751 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000752 ('grid', 'columnconfigure', self._w, index)
753 + options)
754 if options == ('-minsize', None):
755 return self.tk.getint(res) or None
756 elif options == ('-weight', None):
757 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000758 def forget(self):
759 self.tk.call('grid', 'forget', self._w)
760 grid_forget = forget
761 def info(self):
762 words = self.tk.splitlist(
763 self.tk.call('grid', 'info', self._w))
764 dict = {}
765 for i in range(0, len(words), 2):
766 key = words[i][1:]
767 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000768 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000769 value = self._nametowidget(value)
770 dict[key] = value
771 return dict
772 grid_info = info
773 def location(self, x, y):
774 return self._getints(
775 self.tk.call(
776 'grid', 'location', self._w, x, y)) or None
777 _noarg_ = ['_noarg_']
778 def propagate(self, flag=_noarg_):
779 if flag is Grid._noarg_:
780 return self._getboolean(self.tk.call(
781 'grid', 'propagate', self._w))
782 else:
783 self.tk.call('grid', 'propagate', self._w, flag)
784 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000785 def rowconfigure(self, index, cnf={}, **kw):
786 if type(cnf) is not DictionaryType and not kw:
787 options = self._options({cnf: None})
788 else:
789 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000790 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000791 ('grid', 'rowconfigure', self._w, index)
792 + options)
793 if options == ('-minsize', None):
794 return self.tk.getint(res) or None
795 elif options == ('-weight', None):
796 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000797 def size(self):
798 return self._getints(
799 self.tk.call('grid', 'size', self._w)) or None
800 def slaves(self, *args):
801 return map(self._nametowidget,
802 self.tk.splitlist(
803 apply(self.tk.call,
804 ('grid', 'slaves', self._w) + args)))
805 grid_slaves = slaves
806
807class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000808 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000809 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000810 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000811 if not _default_root:
812 _default_root = Tk()
813 master = _default_root
814 if not _default_root:
815 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000816 self.master = master
817 self.tk = master.tk
818 if cnf.has_key('name'):
819 name = cnf['name']
820 del cnf['name']
821 else:
822 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000823 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000824 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000825 self._w = '.' + name
826 else:
827 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000828 self.children = {}
829 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000830 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000831 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000832 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
833 if kw:
834 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000835 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000836 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000837 classes = []
838 for k in cnf.keys():
839 if type(k) is ClassType:
840 classes.append((k, cnf[k]))
841 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 apply(self.tk.call,
843 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000844 for k, v in classes:
845 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000846 def config(self, cnf=None, **kw):
847 # XXX ought to generalize this so tag_config etc. can use it
848 if kw:
849 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000850 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000851 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000852 if cnf is None:
853 cnf = {}
854 for x in self.tk.split(
855 self.tk.call(self._w, 'configure')):
856 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
857 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000858 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000859 x = self.tk.split(self.tk.call(
860 self._w, 'configure', '-'+cnf))
861 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000862 apply(self.tk.call, (self._w, 'configure')
863 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000864 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000865 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000866 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000867 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000868 def __setitem__(self, key, value):
869 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000870 def keys(self):
871 return map(lambda x: x[0][1:],
872 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000873 def __str__(self):
874 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000875 def destroy(self):
876 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000877 if self.master.children.has_key(self._name):
878 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000879 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000880 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000881 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000882
883class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000884 def __init__(self, master=None, cnf={}, **kw):
885 if kw:
886 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000887 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000888 for wmkey in ['screen', 'class_', 'class', 'visual',
889 'colormap']:
890 if cnf.has_key(wmkey):
891 val = cnf[wmkey]
892 # TBD: a hack needed because some keys
893 # are not valid as keyword arguments
894 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
895 else: opt = '-'+wmkey
896 extra = extra + (opt, val)
897 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000898 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000899 root = self._root()
900 self.iconname(root.iconname())
901 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000902
903class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000904 def __init__(self, master=None, cnf={}, **kw):
905 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000906 def tkButtonEnter(self, *dummy):
907 self.tk.call('tkButtonEnter', self._w)
908 def tkButtonLeave(self, *dummy):
909 self.tk.call('tkButtonLeave', self._w)
910 def tkButtonDown(self, *dummy):
911 self.tk.call('tkButtonDown', self._w)
912 def tkButtonUp(self, *dummy):
913 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000914 def tkButtonInvoke(self, *dummy):
915 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000916 def flash(self):
917 self.tk.call(self._w, 'flash')
918 def invoke(self):
919 self.tk.call(self._w, 'invoke')
920
921# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000922# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000923def AtEnd():
924 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000925def AtInsert(*args):
926 s = 'insert'
927 for a in args:
928 if a: s = s + (' ' + a)
929 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000930def AtSelFirst():
931 return 'sel.first'
932def AtSelLast():
933 return 'sel.last'
934def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000935 if y is None:
936 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000937 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000938 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000939
940class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000941 def __init__(self, master=None, cnf={}, **kw):
942 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000943 def addtag(self, *args):
944 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +0000945 def addtag_above(self, newtag, tagOrId):
946 self.addtag(newtag, 'above', tagOrId)
947 def addtag_all(self, newtag):
948 self.addtag(newtag, 'all')
949 def addtag_below(self, newtag, tagOrId):
950 self.addtag(newtag, 'below', tagOrId)
951 def addtag_closest(self, newtag, x, y, halo=None, start=None):
952 self.addtag(newtag, 'closest', x, y, halo, start)
953 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
954 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
955 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
956 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
957 def addtag_withtag(self, newtag, tagOrId):
958 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +0000959 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000960 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +0000961 def tag_unbind(self, tagOrId, sequence):
962 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000963 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +0000964 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +0000965 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +0000966 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000967 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000968 self._w, 'canvasx', screenx, gridspacing))
969 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000970 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000971 self._w, 'canvasy', screeny, gridspacing))
972 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +0000973 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +0000974 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000975 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +0000976 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000977 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000978 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +0000979 args = args[:-1]
980 else:
981 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000982 return self.tk.getint(apply(
983 self.tk.call,
984 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000985 + args + self._options(cnf, kw)))
986 def create_arc(self, *args, **kw):
987 return self._create('arc', args, kw)
988 def create_bitmap(self, *args, **kw):
989 return self._create('bitmap', args, kw)
990 def create_image(self, *args, **kw):
991 return self._create('image', args, kw)
992 def create_line(self, *args, **kw):
993 return self._create('line', args, kw)
994 def create_oval(self, *args, **kw):
995 return self._create('oval', args, kw)
996 def create_polygon(self, *args, **kw):
997 return self._create('polygon', args, kw)
998 def create_rectangle(self, *args, **kw):
999 return self._create('rectangle', args, kw)
1000 def create_text(self, *args, **kw):
1001 return self._create('text', args, kw)
1002 def create_window(self, *args, **kw):
1003 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001004 def dchars(self, *args):
1005 self._do('dchars', args)
1006 def delete(self, *args):
1007 self._do('delete', args)
1008 def dtag(self, *args):
1009 self._do('dtag', args)
1010 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001011 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001012 def find_above(self, tagOrId):
1013 return self.find('above', tagOrId)
1014 def find_all(self):
1015 return self.find('all')
1016 def find_below(self, tagOrId):
1017 return self.find('below', tagOrId)
1018 def find_closest(self, x, y, halo=None, start=None):
1019 return self.find('closest', x, y, halo, start)
1020 def find_enclosed(self, x1, y1, x2, y2):
1021 return self.find('enclosed', x1, y1, x2, y2)
1022 def find_overlapping(self, x1, y1, x2, y2):
1023 return self.find('overlapping', x1, y1, x2, y2)
1024 def find_withtag(self, tagOrId):
1025 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001026 def focus(self, *args):
1027 return self._do('focus', args)
1028 def gettags(self, *args):
1029 return self.tk.splitlist(self._do('gettags', args))
1030 def icursor(self, *args):
1031 self._do('icursor', args)
1032 def index(self, *args):
1033 return self.tk.getint(self._do('index', args))
1034 def insert(self, *args):
1035 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001036 def itemcget(self, tagOrId, option):
1037 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001038 def itemconfig(self, tagOrId, cnf=None, **kw):
1039 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001040 cnf = {}
1041 for x in self.tk.split(
1042 self._do('itemconfigure', (tagOrId))):
1043 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1044 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001045 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001046 x = self.tk.split(self._do('itemconfigure',
1047 (tagOrId, '-'+cnf,)))
1048 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001049 self._do('itemconfigure', (tagOrId,)
1050 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001051 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001052 def lower(self, *args):
1053 self._do('lower', args)
1054 def move(self, *args):
1055 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001056 def postscript(self, cnf={}, **kw):
1057 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001058 def tkraise(self, *args):
1059 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001060 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001061 def scale(self, *args):
1062 self._do('scale', args)
1063 def scan_mark(self, x, y):
1064 self.tk.call(self._w, 'scan', 'mark', x, y)
1065 def scan_dragto(self, x, y):
1066 self.tk.call(self._w, 'scan', 'dragto', x, y)
1067 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001068 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001069 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001070 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001071 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001072 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001073 def select_item(self):
1074 self.tk.call(self._w, 'select', 'item')
1075 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001076 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001077 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001078 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001079 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001080 if not args:
1081 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001082 apply(self.tk.call, (self._w, 'xview')+args)
1083 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001084 if not args:
1085 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001086 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001087
1088class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001089 def __init__(self, master=None, cnf={}, **kw):
1090 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001091 def deselect(self):
1092 self.tk.call(self._w, 'deselect')
1093 def flash(self):
1094 self.tk.call(self._w, 'flash')
1095 def invoke(self):
1096 self.tk.call(self._w, 'invoke')
1097 def select(self):
1098 self.tk.call(self._w, 'select')
1099 def toggle(self):
1100 self.tk.call(self._w, 'toggle')
1101
1102class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001103 def __init__(self, master=None, cnf={}, **kw):
1104 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001105 def delete(self, first, last=None):
1106 self.tk.call(self._w, 'delete', first, last)
1107 def get(self):
1108 return self.tk.call(self._w, 'get')
1109 def icursor(self, index):
1110 self.tk.call(self._w, 'icursor', index)
1111 def index(self, index):
1112 return self.tk.getint(self.tk.call(
1113 self._w, 'index', index))
1114 def insert(self, index, string):
1115 self.tk.call(self._w, 'insert', index, string)
1116 def scan_mark(self, x):
1117 self.tk.call(self._w, 'scan', 'mark', x)
1118 def scan_dragto(self, x):
1119 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001120 def selection_adjust(self, index):
1121 self.tk.call(self._w, 'selection', 'adjust', index)
1122 select_adjust = selection_adjust
1123 def selection_clear(self):
1124 self.tk.call(self._w, 'selection', 'clear')
1125 select_clear = selection_clear
1126 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001127 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001128 select_from = selection_from
1129 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001130 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001131 self.tk.call(self._w, 'selection', 'present'))
1132 select_present = selection_present
1133 def selection_range(self, start, end):
1134 self.tk.call(self._w, 'selection', 'range', start, end)
1135 select_range = selection_range
1136 def selection_to(self, index):
1137 self.tk.call(self._w, 'selection', 'to', index)
1138 select_to = selection_to
1139 def xview(self, index):
1140 self.tk.call(self._w, 'xview', index)
1141 def xview_moveto(self, fraction):
1142 self.tk.call(self._w, 'xview', 'moveto', fraction)
1143 def xview_scroll(self, number, what):
1144 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001145
1146class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001147 def __init__(self, master=None, cnf={}, **kw):
1148 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001149 extra = ()
1150 if cnf.has_key('class'):
1151 extra = ('-class', cnf['class'])
1152 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001153 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001154
1155class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001156 def __init__(self, master=None, cnf={}, **kw):
1157 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001158
Guido van Rossum18468821994-06-20 07:49:28 +00001159class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001160 def __init__(self, master=None, cnf={}, **kw):
1161 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001162 def activate(self, index):
1163 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001164 def bbox(self, *args):
1165 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001166 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001167 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001168 return self.tk.splitlist(self.tk.call(
1169 self._w, 'curselection'))
1170 def delete(self, first, last=None):
1171 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001172 def get(self, first, last=None):
1173 if last:
1174 return self.tk.splitlist(self.tk.call(
1175 self._w, 'get', first, last))
1176 else:
1177 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001178 def insert(self, index, *elements):
1179 apply(self.tk.call,
1180 (self._w, 'insert', index) + elements)
1181 def nearest(self, y):
1182 return self.tk.getint(self.tk.call(
1183 self._w, 'nearest', y))
1184 def scan_mark(self, x, y):
1185 self.tk.call(self._w, 'scan', 'mark', x, y)
1186 def scan_dragto(self, x, y):
1187 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001188 def see(self, index):
1189 self.tk.call(self._w, 'see', index)
1190 def index(self, index):
1191 i = self.tk.call(self._w, 'index', index)
1192 if i == 'none': return None
1193 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001194 def select_adjust(self, index):
1195 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001196 def select_anchor(self, index):
1197 self.tk.call(self._w, 'selection', 'anchor', index)
1198 def select_clear(self, first, last=None):
1199 self.tk.call(self._w,
1200 'selection', 'clear', first, last)
1201 def select_includes(self, index):
1202 return self.tk.getboolean(self.tk.call(
1203 self._w, 'selection', 'includes', index))
1204 def select_set(self, first, last=None):
1205 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001206 def size(self):
1207 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001208 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001209 if not what:
1210 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001211 apply(self.tk.call, (self._w, 'xview')+what)
1212 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001213 if not what:
1214 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001215 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001216
1217class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001218 def __init__(self, master=None, cnf={}, **kw):
1219 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001220 def tk_bindForTraversal(self):
Guido van Rossum8535b291996-09-10 12:36:34 +00001221 if TkVersion < 4.1:
1222 self.tk.call('tk_bindForTraversal', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001223 def tk_mbPost(self):
1224 self.tk.call('tk_mbPost', self._w)
1225 def tk_mbUnpost(self):
1226 self.tk.call('tk_mbUnpost')
1227 def tk_traverseToMenu(self, char):
1228 self.tk.call('tk_traverseToMenu', self._w, char)
1229 def tk_traverseWithinMenu(self, char):
1230 self.tk.call('tk_traverseWithinMenu', self._w, char)
1231 def tk_getMenuButtons(self):
1232 return self.tk.call('tk_getMenuButtons', self._w)
1233 def tk_nextMenu(self, count):
1234 self.tk.call('tk_nextMenu', count)
1235 def tk_nextMenuEntry(self, count):
1236 self.tk.call('tk_nextMenuEntry', count)
1237 def tk_invokeMenu(self):
1238 self.tk.call('tk_invokeMenu', self._w)
1239 def tk_firstMenu(self):
1240 self.tk.call('tk_firstMenu', self._w)
1241 def tk_mbButtonDown(self):
1242 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001243 def tk_popup(self, x, y, entry=""):
1244 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001245 def activate(self, index):
1246 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001247 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001248 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001249 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001250 def add_cascade(self, cnf={}, **kw):
1251 self.add('cascade', cnf or kw)
1252 def add_checkbutton(self, cnf={}, **kw):
1253 self.add('checkbutton', cnf or kw)
1254 def add_command(self, cnf={}, **kw):
1255 self.add('command', cnf or kw)
1256 def add_radiobutton(self, cnf={}, **kw):
1257 self.add('radiobutton', cnf or kw)
1258 def add_separator(self, cnf={}, **kw):
1259 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001260 def insert(self, index, itemType, cnf={}, **kw):
1261 apply(self.tk.call, (self._w, 'insert', index, itemType)
1262 + self._options(cnf, kw))
1263 def insert_cascade(self, index, cnf={}, **kw):
1264 self.insert(index, 'cascade', cnf or kw)
1265 def insert_checkbutton(self, index, cnf={}, **kw):
1266 self.insert(index, 'checkbutton', cnf or kw)
1267 def insert_command(self, index, cnf={}, **kw):
1268 self.insert(index, 'command', cnf or kw)
1269 def insert_radiobutton(self, index, cnf={}, **kw):
1270 self.insert(index, 'radiobutton', cnf or kw)
1271 def insert_separator(self, index, cnf={}, **kw):
1272 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001273 def delete(self, index1, index2=None):
1274 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001275 def entryconfig(self, index, cnf=None, **kw):
1276 if cnf is None and not kw:
1277 cnf = {}
1278 for x in self.tk.split(apply(self.tk.call,
1279 (self._w, 'entryconfigure', index))):
1280 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1281 return cnf
1282 if type(cnf) == StringType and not kw:
1283 x = self.tk.split(apply(self.tk.call,
1284 (self._w, 'entryconfigure', index, '-'+cnf)))
1285 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001286 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001287 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001288 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001289 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001290 i = self.tk.call(self._w, 'index', index)
1291 if i == 'none': return None
1292 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001293 def invoke(self, index):
1294 return self.tk.call(self._w, 'invoke', index)
1295 def post(self, x, y):
1296 self.tk.call(self._w, 'post', x, y)
1297 def unpost(self):
1298 self.tk.call(self._w, 'unpost')
1299 def yposition(self, index):
1300 return self.tk.getint(self.tk.call(
1301 self._w, 'yposition', index))
1302
1303class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001304 def __init__(self, master=None, cnf={}, **kw):
1305 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001306
1307class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001308 def __init__(self, master=None, cnf={}, **kw):
1309 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001310
1311class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001312 def __init__(self, master=None, cnf={}, **kw):
1313 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001314 def deselect(self):
1315 self.tk.call(self._w, 'deselect')
1316 def flash(self):
1317 self.tk.call(self._w, 'flash')
1318 def invoke(self):
1319 self.tk.call(self._w, 'invoke')
1320 def select(self):
1321 self.tk.call(self._w, 'select')
1322
1323class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001324 def __init__(self, master=None, cnf={}, **kw):
1325 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001326 def get(self):
1327 return self.tk.getint(self.tk.call(self._w, 'get'))
1328 def set(self, value):
1329 self.tk.call(self._w, 'set', value)
1330
1331class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001332 def __init__(self, master=None, cnf={}, **kw):
1333 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001334 def activate(self, index):
1335 self.tk.call(self._w, 'activate', index)
1336 def delta(self, deltax, deltay):
1337 return self.getdouble(self.tk.call(
1338 self._w, 'delta', deltax, deltay))
1339 def fraction(self, x, y):
1340 return self.getdouble(self.tk.call(
1341 self._w, 'fraction', x, y))
1342 def identify(self, x, y):
1343 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001344 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001345 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001346 def set(self, *args):
1347 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001348
1349class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001350 def __init__(self, master=None, cnf={}, **kw):
1351 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001352 self.bind('<Delete>', self.bspace)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001353 def bbox(self, *args):
1354 return self._getints(self._do('bbox', args)) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001355 def bspace(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001356 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001357 def tk_textSelectTo(self, index):
1358 self.tk.call('tk_textSelectTo', self._w, index)
1359 def tk_textBackspace(self):
1360 self.tk.call('tk_textBackspace', self._w)
1361 def tk_textIndexCloser(self, a, b, c):
1362 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1363 def tk_textResetAnchor(self, index):
1364 self.tk.call('tk_textResetAnchor', self._w, index)
1365 def compare(self, index1, op, index2):
1366 return self.tk.getboolean(self.tk.call(
1367 self._w, 'compare', index1, op, index2))
1368 def debug(self, boolean=None):
1369 return self.tk.getboolean(self.tk.call(
1370 self._w, 'debug', boolean))
1371 def delete(self, index1, index2=None):
1372 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001373 def dlineinfo(self, index):
1374 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001375 def get(self, index1, index2=None):
1376 return self.tk.call(self._w, 'get', index1, index2)
1377 def index(self, index):
1378 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001379 def insert(self, index, chars, *args):
1380 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001381 def mark_gravity(self, markName, direction=None):
1382 return apply(self.tk.call,
1383 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001384 def mark_names(self):
1385 return self.tk.splitlist(self.tk.call(
1386 self._w, 'mark', 'names'))
1387 def mark_set(self, markName, index):
1388 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001389 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001390 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001391 def scan_mark(self, x, y):
1392 self.tk.call(self._w, 'scan', 'mark', x, y)
1393 def scan_dragto(self, x, y):
1394 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001395 def search(self, pattern, index, stopindex=None,
1396 forwards=None, backwards=None, exact=None,
1397 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001398 args = [self._w, 'search']
1399 if forwards: args.append('-forwards')
1400 if backwards: args.append('-backwards')
1401 if exact: args.append('-exact')
1402 if regexp: args.append('-regexp')
1403 if nocase: args.append('-nocase')
1404 if count: args.append('-count'); args.append(count)
1405 if pattern[0] == '-': args.append('--')
1406 args.append(pattern)
1407 args.append(index)
1408 if stopindex: args.append(stopindex)
1409 return apply(self.tk.call, tuple(args))
1410 def see(self, index):
1411 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001412 def tag_add(self, tagName, index1, index2=None):
1413 self.tk.call(
1414 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001415 def tag_unbind(self, tagName, sequence):
1416 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001417 def tag_bind(self, tagName, sequence, func, add=None):
1418 return self._bind((self._w, 'tag', 'bind', tagName),
1419 sequence, func, add)
1420 def tag_cget(self, tagName, option):
1421 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001422 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001423 if type(cnf) == StringType:
1424 x = self.tk.split(self.tk.call(
1425 self._w, 'tag', 'configure', tagName, '-'+cnf))
1426 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001427 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001428 (self._w, 'tag', 'configure', tagName)
1429 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001430 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001431 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001432 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001433 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001434 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001435 def tag_names(self, index=None):
1436 return self.tk.splitlist(
1437 self.tk.call(self._w, 'tag', 'names', index))
1438 def tag_nextrange(self, tagName, index1, index2=None):
1439 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001440 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001441 def tag_raise(self, tagName, aboveThis=None):
1442 self.tk.call(
1443 self._w, 'tag', 'raise', tagName, aboveThis)
1444 def tag_ranges(self, tagName):
1445 return self.tk.splitlist(self.tk.call(
1446 self._w, 'tag', 'ranges', tagName))
1447 def tag_remove(self, tagName, index1, index2=None):
1448 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001449 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001450 def window_cget(self, index, option):
1451 return self.tk.call(self._w, 'window', 'cget', index, option)
1452 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001453 if type(cnf) == StringType:
1454 x = self.tk.split(self.tk.call(
1455 self._w, 'window', 'configure',
1456 index, '-'+cnf))
1457 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001458 apply(self.tk.call,
1459 (self._w, 'window', 'configure', index)
1460 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001461 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001462 def window_create(self, index, cnf={}, **kw):
1463 apply(self.tk.call,
1464 (self._w, 'window', 'create', index)
1465 + self._options(cnf, kw))
1466 def window_names(self):
1467 return self.tk.splitlist(
1468 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001469 def xview(self, *what):
1470 if not what:
1471 return self._getdoubles(self.tk.call(self._w, 'xview'))
1472 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001473 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001474 if not what:
1475 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001476 apply(self.tk.call, (self._w, 'yview')+what)
1477 def yview_pickplace(self, *what):
1478 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001479
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001480class OptionMenu(Widget):
1481 def __init__(self, master, variable, value, *values):
1482 self.widgetName = 'tk_optionMenu'
1483 Widget._setup(self, master, {})
1484 self.menuname = apply(
1485 self.tk.call,
1486 (self.widgetName, self._w, variable, value) + values)
1487
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001488class Image:
1489 def __init__(self, imgtype, name=None, cnf={}, **kw):
1490 self.name = None
1491 master = _default_root
1492 if not master: raise RuntimeError, 'Too early to create image'
1493 self.tk = master.tk
1494 if not name: name = `id(self)`
1495 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1496 elif kw: cnf = kw
1497 options = ()
1498 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001499 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001500 v = self._register(v)
1501 options = options + ('-'+k, v)
1502 apply(self.tk.call,
1503 ('image', 'create', imgtype, name,) + options)
1504 self.name = name
1505 def __str__(self): return self.name
1506 def __del__(self):
1507 if self.name:
1508 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001509 def __setitem__(self, key, value):
1510 self.tk.call(self.name, 'configure', '-'+key, value)
1511 def __getitem__(self, key):
1512 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001513 def height(self):
1514 return self.tk.getint(
1515 self.tk.call('image', 'height', self.name))
1516 def type(self):
1517 return self.tk.call('image', 'type', self.name)
1518 def width(self):
1519 return self.tk.getint(
1520 self.tk.call('image', 'width', self.name))
1521
1522class PhotoImage(Image):
1523 def __init__(self, name=None, cnf={}, **kw):
1524 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001525 def blank(self):
1526 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001527 def cget(self, option):
1528 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001529 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001530 def __getitem__(self, key):
1531 return self.tk.call(self.name, 'cget', '-' + key)
1532 def copy(self):
1533 destImage = PhotoImage()
1534 self.tk.call(destImage, 'copy', self.name)
1535 return destImage
1536 def zoom(self,x,y=''):
1537 destImage = PhotoImage()
1538 if y=='': y=x
1539 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1540 return destImage
1541 def subsample(self,x,y=''):
1542 destImage = PhotoImage()
1543 if y=='': y=x
1544 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1545 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001546 def get(self, x, y):
1547 return self.tk.call(self.name, 'get', x, y)
1548 def put(self, data, to=None):
1549 args = (self.name, 'put', data)
1550 if to:
1551 args = args + to
1552 apply(self.tk.call, args)
1553 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001554 def write(self, filename, format=None, from_coords=None):
1555 args = (self.name, 'write', filename)
1556 if format:
1557 args = args + ('-format', format)
1558 if from_coords:
1559 args = args + ('-from',) + tuple(from_coords)
1560 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001561
1562class BitmapImage(Image):
1563 def __init__(self, name=None, cnf={}, **kw):
1564 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1565
1566def image_names(): return _default_root.tk.call('image', 'names')
1567def image_types(): return _default_root.tk.call('image', 'types')
1568
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001569######################################################################
1570# Extensions:
1571
1572class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001573 def __init__(self, master=None, cnf={}, **kw):
1574 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001575 self.bind('<Any-Enter>', self.tkButtonEnter)
1576 self.bind('<Any-Leave>', self.tkButtonLeave)
1577 self.bind('<1>', self.tkButtonDown)
1578 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001579
1580class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001581 def __init__(self, master=None, cnf={}, **kw):
1582 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001583 self.bind('<Any-Enter>', self.tkButtonEnter)
1584 self.bind('<Any-Leave>', self.tkButtonLeave)
1585 self.bind('<1>', self.tkButtonDown)
1586 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1587 self['fg'] = self['bg']
1588 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001589
Guido van Rossumc417ef81996-08-21 23:38:59 +00001590######################################################################
1591# Test:
1592
1593def _test():
1594 root = Tk()
1595 label = Label(root, text="Proof-of-existence test for Tk")
1596 label.pack()
1597 test = Button(root, text="Click me!",
1598 command=lambda root=root: root.test.config(
1599 text="[%s]" % root.test['text']))
1600 test.pack()
1601 root.test = test
1602 quit = Button(root, text="QUIT", command=root.destroy)
1603 quit.pack()
1604 root.mainloop()
1605
1606if __name__ == '__main__':
1607 _test()
1608
Guido van Rossum37dcab11996-05-16 16:00:19 +00001609
1610# Emacs cruft
1611# Local Variables:
1612# py-indent-offset: 8
1613# End: