blob: f7aafb05b4273bb4965656c61e88a1d2cc36d182 [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))
Fred Drake3c602d71996-09-27 14:06:54 +0000141 def tk_bisque(self):
142 self.tk.call('tk_bisque')
143 def tk_setPalette(self, *args, **kw):
144 apply(self.tk.call, 'tk_setPalette',
145 _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000146 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000147 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000148 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000149 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000150 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000151 def wait_window(self, window=None):
152 if window == None:
153 window = self
154 self.tk.call('tkwait', 'window', window._w)
155 def wait_visibility(self, window=None):
156 if window == None:
157 window = self
158 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000159 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000160 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000161 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000162 return self.tk.getvar(name)
163 def getint(self, s):
164 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000165 def getdouble(self, s):
166 return self.tk.getdouble(s)
167 def getboolean(self, s):
168 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000169 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000170 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000171 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000172 def focus_force(self):
173 self.tk.call('focus', '-force', self._w)
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)
Fred Drake3c602d71996-09-27 14:06:54 +0000178 def focus_displayof(self):
179 name = self.tk.call('focus', '-displayof', self._w)
180 if name == 'none' or not name: return None
181 return self._nametowidget(name)
182 def focus_lastfor(self):
183 name = self.tk.call('focus', '-lastfor', self._w)
184 if name == 'none' or not name: return None
185 return self._nametowidget(name)
186 def tk_focusFollowsMouse(self):
187 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000188 def tk_focusNext(self):
189 name = self.tk.call('tk_focusNext', self._w)
190 if not name: return None
191 return self._nametowidget(name)
192 def tk_focusPrev(self):
193 name = self.tk.call('tk_focusPrev', self._w)
194 if not name: return None
195 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000196 def after(self, ms, func=None, *args):
197 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000198 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000199 self.tk.call('after', ms)
200 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000201 # XXX Disgusting hack to clean up after calling func
202 tmp = []
203 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
204 try:
205 apply(func, args)
206 finally:
207 tk.deletecommand(tmp[0])
208 name = self._register(callit)
209 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000210 return self.tk.call('after', ms, name)
211 def after_idle(self, func, *args):
212 return apply(self.after, ('idle', func) + args)
213 def after_cancel(self, id):
214 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000215 def bell(self, displayof=0):
216 apply(self.tk.call, ('bell',) + self._displayof(displayof))
217 # Clipboard handling:
218 def clipboard_clear(self, **kw):
219 if not kw.has_key('displayof'): kw['displayof'] = self._w
220 apply(self.tk.call,
221 ('clipboard', 'clear') + self._options(kw))
222 def clipboard_append(self, string, **kw):
223 if not kw.has_key('displayof'): kw['displayof'] = self._w
224 apply(self.tk.call,
225 ('clipboard', 'append') + self._options(kw)
226 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000227 # XXX grab current w/o window argument
228 def grab_current(self):
229 name = self.tk.call('grab', 'current', self._w)
230 if not name: return None
231 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000232 def grab_release(self):
233 self.tk.call('grab', 'release', self._w)
234 def grab_set(self):
235 self.tk.call('grab', 'set', self._w)
236 def grab_set_global(self):
237 self.tk.call('grab', 'set', '-global', self._w)
238 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000239 status = self.tk.call('grab', 'status', self._w)
240 if status == 'none': status = None
241 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000242 def lower(self, belowThis=None):
243 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000244 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000245 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000246 def option_clear(self):
247 self.tk.call('option', 'clear')
248 def option_get(self, name, className):
249 return self.tk.call('option', 'get', self._w, name, className)
250 def option_readfile(self, fileName, priority = None):
251 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000252 def selection_clear(self, **kw):
253 if not kw.has_key('displayof'): kw['displayof'] = self._w
254 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
255 def selection_get(self, **kw):
256 if not kw.has_key('displayof'): kw['displayof'] = self._w
257 return apply(self.tk.call,
258 ('selection', 'get') + self._options(kw))
259 def selection_handle(self, command, **kw):
260 name = self._register(command)
261 apply(self.tk.call,
262 ('selection', 'handle') + self._options(kw)
263 + (self._w, name))
264 def selection_own(self, **kw):
265 "Become owner of X selection."
266 apply(self.tk.call,
267 ('selection', 'own') + self._options(kw) + (self._w,))
268 def selection_own_get(self, **kw):
269 "Find owner of X selection."
270 if not kw.has_key('displayof'): kw['displayof'] = self._w
271 return self._nametowidget(
272 apply(self.tk.call,
273 ('selection', 'own') + self._options(kw)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000274 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000275 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000276 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000277 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000278 def tkraise(self, aboveThis=None):
279 self.tk.call('raise', self._w, aboveThis)
280 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000281 def colormodel(self, value=None):
282 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000283 def winfo_atom(self, name, displayof=0):
284 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
285 return self.tk.getint(apply(self.tk.call, args))
286 def winfo_atomname(self, id, displayof=0):
287 args = ('winfo', 'atomname') \
288 + self._displayof(displayof) + (id,)
289 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000290 def winfo_cells(self):
291 return self.tk.getint(
292 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000293 def winfo_children(self):
294 return map(self._nametowidget,
295 self.tk.splitlist(self.tk.call(
296 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000297 def winfo_class(self):
298 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000299 def winfo_colormapfull(self):
300 return self.tk.getboolean(
301 self.tk.call('winfo', 'colormapfull'))
302 def winfo_containing(self, rootX, rootY, displayof=0):
303 args = ('winfo', 'containing') \
304 + self._displayof(displayof) + (rootX, rootY)
305 return self._nametowidget(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000306 def winfo_depth(self):
307 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
308 def winfo_exists(self):
309 return self.tk.getint(
310 self.tk.call('winfo', 'exists', self._w))
311 def winfo_fpixels(self, number):
312 return self.tk.getdouble(self.tk.call(
313 'winfo', 'fpixels', self._w, number))
314 def winfo_geometry(self):
315 return self.tk.call('winfo', 'geometry', self._w)
316 def winfo_height(self):
317 return self.tk.getint(
318 self.tk.call('winfo', 'height', self._w))
319 def winfo_id(self):
320 return self.tk.getint(
321 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000322 def winfo_interps(self, displayof=0):
323 args = ('winfo', 'interps') + self._displayof(displayof)
324 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000325 def winfo_ismapped(self):
326 return self.tk.getint(
327 self.tk.call('winfo', 'ismapped', self._w))
328 def winfo_name(self):
329 return self.tk.call('winfo', 'name', self._w)
330 def winfo_parent(self):
331 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000332 def winfo_pathname(self, id, displayof=0):
333 args = ('winfo', 'pathname') \
334 + self._displayof(displayof) + (id,)
335 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000336 def winfo_pixels(self, number):
337 return self.tk.getint(
338 self.tk.call('winfo', 'pixels', self._w, number))
339 def winfo_reqheight(self):
340 return self.tk.getint(
341 self.tk.call('winfo', 'reqheight', self._w))
342 def winfo_reqwidth(self):
343 return self.tk.getint(
344 self.tk.call('winfo', 'reqwidth', self._w))
345 def winfo_rgb(self, color):
346 return self._getints(
347 self.tk.call('winfo', 'rgb', self._w, color))
348 def winfo_rootx(self):
349 return self.tk.getint(
350 self.tk.call('winfo', 'rootx', self._w))
351 def winfo_rooty(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'rooty', self._w))
354 def winfo_screen(self):
355 return self.tk.call('winfo', 'screen', self._w)
356 def winfo_screencells(self):
357 return self.tk.getint(
358 self.tk.call('winfo', 'screencells', self._w))
359 def winfo_screendepth(self):
360 return self.tk.getint(
361 self.tk.call('winfo', 'screendepth', self._w))
362 def winfo_screenheight(self):
363 return self.tk.getint(
364 self.tk.call('winfo', 'screenheight', self._w))
365 def winfo_screenmmheight(self):
366 return self.tk.getint(
367 self.tk.call('winfo', 'screenmmheight', self._w))
368 def winfo_screenmmwidth(self):
369 return self.tk.getint(
370 self.tk.call('winfo', 'screenmmwidth', self._w))
371 def winfo_screenvisual(self):
372 return self.tk.call('winfo', 'screenvisual', self._w)
373 def winfo_screenwidth(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'screenwidth', self._w))
376 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000377 return self._nametowidget(self.tk.call(
378 'winfo', 'toplevel', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000379 def winfo_visual(self):
380 return self.tk.call('winfo', 'visual', self._w)
381 def winfo_vrootheight(self):
382 return self.tk.getint(
383 self.tk.call('winfo', 'vrootheight', self._w))
384 def winfo_vrootwidth(self):
385 return self.tk.getint(
386 self.tk.call('winfo', 'vrootwidth', self._w))
387 def winfo_vrootx(self):
388 return self.tk.getint(
389 self.tk.call('winfo', 'vrootx', self._w))
390 def winfo_vrooty(self):
391 return self.tk.getint(
392 self.tk.call('winfo', 'vrooty', self._w))
393 def winfo_width(self):
394 return self.tk.getint(
395 self.tk.call('winfo', 'width', self._w))
396 def winfo_x(self):
397 return self.tk.getint(
398 self.tk.call('winfo', 'x', self._w))
399 def winfo_y(self):
400 return self.tk.getint(
401 self.tk.call('winfo', 'y', self._w))
402 def update(self):
403 self.tk.call('update')
404 def update_idletasks(self):
405 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000406 def bindtags(self, tagList=None):
407 if tagList is None:
408 return self.tk.splitlist(
409 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000410 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000411 self.tk.call('bindtags', self._w, tagList)
412 def _bind(self, what, sequence, func, add):
413 if func:
414 cmd = ("%sset _tkinter_break [%s %s]\n"
415 'if {"$_tkinter_break" == "break"} break\n') \
416 % (add and '+' or '',
417 self._register(func, self._substitute),
418 _string.join(self._subst_format))
419 apply(self.tk.call, what + (sequence, cmd))
420 elif func == '':
421 apply(self.tk.call, what + (sequence, func))
422 else:
423 return apply(self.tk.call, what + (sequence,))
424 def bind(self, sequence=None, func=None, add=None):
425 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000426 def unbind(self, sequence):
427 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000428 def bind_all(self, sequence=None, func=None, add=None):
429 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000430 def unbind_all(self, sequence):
431 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000432 def bind_class(self, className, sequence=None, func=None, add=None):
433 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000434 def unbind_class(self, className, sequence):
435 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000436 def mainloop(self, n=0):
437 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000438 def quit(self):
439 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000440 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000441 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000442 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
443 def _getdoubles(self, string):
444 if not string: return None
445 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000446 def _getboolean(self, string):
447 if string:
448 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000449 def _displayof(self, displayof):
450 if displayof:
451 return ('-displayof', displayof)
452 if displayof is None:
453 return ('-displayof', self._w)
454 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000455 def _options(self, cnf, kw = None):
456 if kw:
457 cnf = _cnfmerge((cnf, kw))
458 else:
459 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000460 res = ()
461 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000462 if v is not None:
463 if k[-1] == '_': k = k[:-1]
464 if callable(v):
465 v = self._register(v)
466 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000467 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000468 def _nametowidget(self, name):
469 w = self
470 if name[0] == '.':
471 w = w._root()
472 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000473 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000474 while name:
475 i = find(name, '.')
476 if i >= 0:
477 name, tail = name[:i], name[i+1:]
478 else:
479 tail = ''
480 w = w.children[name]
481 name = tail
482 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000483 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000484 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000485 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000486 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000487 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000488 except AttributeError:
489 pass
490 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000491 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000492 except AttributeError:
493 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000494 self.tk.createcommand(name, f)
495 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000496 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000497 def _root(self):
498 w = self
499 while w.master: w = w.master
500 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000501 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000502 '%s', '%t', '%w', '%x', '%y',
503 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
504 def _substitute(self, *args):
505 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000506 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000507 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
508 # Missing: (a, c, d, m, o, v, B, R)
509 e = Event()
510 e.serial = tk.getint(nsign)
511 e.num = tk.getint(b)
512 try: e.focus = tk.getboolean(f)
513 except TclError: pass
514 e.height = tk.getint(h)
515 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000516 # For Visibility events, event state is a string and
517 # not an integer:
518 try:
519 e.state = tk.getint(s)
520 except TclError:
521 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000522 e.time = tk.getint(t)
523 e.width = tk.getint(w)
524 e.x = tk.getint(x)
525 e.y = tk.getint(y)
526 e.char = A
527 try: e.send_event = tk.getboolean(E)
528 except TclError: pass
529 e.keysym = K
530 e.keysym_num = tk.getint(N)
531 e.type = T
532 e.widget = self._nametowidget(W)
533 e.x_root = tk.getint(X)
534 e.y_root = tk.getint(Y)
535 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000536 def _report_exception(self):
537 import sys
538 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
539 root = self._root()
540 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000541
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000542class CallWrapper:
543 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000544 self.func = func
545 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000546 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000547 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000548 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000549 if self.subst:
550 args = apply(self.subst, args)
551 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000552 except SystemExit, msg:
553 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000554 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000555 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000556
557class Wm:
558 def aspect(self,
559 minNumer=None, minDenom=None,
560 maxNumer=None, maxDenom=None):
561 return self._getints(
562 self.tk.call('wm', 'aspect', self._w,
563 minNumer, minDenom,
564 maxNumer, maxDenom))
565 def client(self, name=None):
566 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000567 def colormapwindows(self, *wlist):
568 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
569 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000570 def command(self, value=None):
571 return self.tk.call('wm', 'command', self._w, value)
572 def deiconify(self):
573 return self.tk.call('wm', 'deiconify', self._w)
574 def focusmodel(self, model=None):
575 return self.tk.call('wm', 'focusmodel', self._w, model)
576 def frame(self):
577 return self.tk.call('wm', 'frame', self._w)
578 def geometry(self, newGeometry=None):
579 return self.tk.call('wm', 'geometry', self._w, newGeometry)
580 def grid(self,
581 baseWidht=None, baseHeight=None,
582 widthInc=None, heightInc=None):
583 return self._getints(self.tk.call(
584 'wm', 'grid', self._w,
585 baseWidht, baseHeight, widthInc, heightInc))
586 def group(self, pathName=None):
587 return self.tk.call('wm', 'group', self._w, pathName)
588 def iconbitmap(self, bitmap=None):
589 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
590 def iconify(self):
591 return self.tk.call('wm', 'iconify', self._w)
592 def iconmask(self, bitmap=None):
593 return self.tk.call('wm', 'iconmask', self._w, bitmap)
594 def iconname(self, newName=None):
595 return self.tk.call('wm', 'iconname', self._w, newName)
596 def iconposition(self, x=None, y=None):
597 return self._getints(self.tk.call(
598 'wm', 'iconposition', self._w, x, y))
599 def iconwindow(self, pathName=None):
600 return self.tk.call('wm', 'iconwindow', self._w, pathName)
601 def maxsize(self, width=None, height=None):
602 return self._getints(self.tk.call(
603 'wm', 'maxsize', self._w, width, height))
604 def minsize(self, width=None, height=None):
605 return self._getints(self.tk.call(
606 'wm', 'minsize', self._w, width, height))
607 def overrideredirect(self, boolean=None):
608 return self._getboolean(self.tk.call(
609 'wm', 'overrideredirect', self._w, boolean))
610 def positionfrom(self, who=None):
611 return self.tk.call('wm', 'positionfrom', self._w, who)
612 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000613 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000614 command = self._register(func)
615 else:
616 command = func
617 return self.tk.call(
618 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000619 def resizable(self, width=None, height=None):
620 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000621 def sizefrom(self, who=None):
622 return self.tk.call('wm', 'sizefrom', self._w, who)
623 def state(self):
624 return self.tk.call('wm', 'state', self._w)
625 def title(self, string=None):
626 return self.tk.call('wm', 'title', self._w, string)
627 def transient(self, master=None):
628 return self.tk.call('wm', 'transient', self._w, master)
629 def withdraw(self):
630 return self.tk.call('wm', 'withdraw', self._w)
631
632class Tk(Misc, Wm):
633 _w = '.'
634 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000635 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000636 self.master = None
637 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000638 if baseName is None:
639 import sys, os
640 baseName = os.path.basename(sys.argv[0])
641 if baseName[-3:] == '.py': baseName = baseName[:-3]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000642 self.tk = tkinter.create(screenName, baseName, className)
643 try:
644 # Disable event scanning except for Command-Period
645 import MacOS
646 MacOS.EnableAppswitch(0)
647 except ImportError:
648 pass
649 else:
650 # Work around nasty MacTk bug
651 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000652 # Version sanity checks
653 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000654 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000655 raise RuntimeError, \
656 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000657 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000658 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000659 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000660 raise RuntimeError, \
661 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000662 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000663 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000664 raise RuntimeError, \
665 "Tk 4.0 or higher is required; found Tk %s" \
666 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000667 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000668 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000669 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000670 if not _default_root:
671 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000672 def destroy(self):
673 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000674 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000675 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000676 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000677 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000678 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000679 if os.environ.has_key('HOME'): home = os.environ['HOME']
680 else: home = os.curdir
681 class_tcl = os.path.join(home, '.%s.tcl' % className)
682 class_py = os.path.join(home, '.%s.py' % className)
683 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
684 base_py = os.path.join(home, '.%s.py' % baseName)
685 dir = {'self': self}
686 exec 'from Tkinter import *' in dir
687 if os.path.isfile(class_tcl):
688 print 'source', `class_tcl`
689 self.tk.call('source', class_tcl)
690 if os.path.isfile(class_py):
691 print 'execfile', `class_py`
692 execfile(class_py, dir)
693 if os.path.isfile(base_tcl):
694 print 'source', `base_tcl`
695 self.tk.call('source', base_tcl)
696 if os.path.isfile(base_py):
697 print 'execfile', `base_py`
698 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000699 def report_callback_exception(self, exc, val, tb):
700 import traceback
701 print "Exception in Tkinter callback"
702 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000703
704class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000705 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000706 apply(self.tk.call,
707 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000708 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000709 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000710 pack = config
711 def __setitem__(self, key, value):
712 Pack.config({key: value})
713 def forget(self):
714 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000715 pack_forget = forget
716 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000717 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000718 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000719 dict = {}
720 for i in range(0, len(words), 2):
721 key = words[i][1:]
722 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000723 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000724 value = self._nametowidget(value)
725 dict[key] = value
726 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000727 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000728 _noarg_ = ['_noarg_']
729 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000730 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000731 return self._getboolean(self.tk.call(
732 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000733 else:
734 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000735 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000736 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000737 return map(self._nametowidget,
738 self.tk.splitlist(
739 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000740 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000741
742class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000743 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000744 for k in ['in_']:
745 if kw.has_key(k):
746 kw[k[:-1]] = kw[k]
747 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000748 apply(self.tk.call,
749 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000750 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000751 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000752 place = config
753 def __setitem__(self, key, value):
754 Place.config({key: value})
755 def forget(self):
756 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000757 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000758 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000759 words = self.tk.splitlist(
760 self.tk.call('place', 'info', self._w))
761 dict = {}
762 for i in range(0, len(words), 2):
763 key = words[i][1:]
764 value = words[i+1]
765 if value[:1] == '.':
766 value = self._nametowidget(value)
767 dict[key] = value
768 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000769 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000770 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000771 return map(self._nametowidget,
772 self.tk.splitlist(
773 self.tk.call(
774 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000775 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000776
Guido van Rossum37dcab11996-05-16 16:00:19 +0000777class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000778 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000779 def config(self, cnf={}, **kw):
780 apply(self.tk.call,
781 ('grid', 'configure', self._w)
782 + self._options(cnf, kw))
783 grid = config
784 def __setitem__(self, key, value):
785 Grid.config({key: value})
786 def bbox(self, column, row):
787 return self._getints(
788 self.tk.call(
789 'grid', 'bbox', self._w, column, row)) or None
790 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000791 def columnconfigure(self, index, cnf={}, **kw):
792 if type(cnf) is not DictionaryType and not kw:
793 options = self._options({cnf: None})
794 else:
795 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000796 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000797 ('grid', 'columnconfigure', self._w, index)
798 + options)
799 if options == ('-minsize', None):
800 return self.tk.getint(res) or None
801 elif options == ('-weight', None):
802 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 def forget(self):
804 self.tk.call('grid', 'forget', self._w)
805 grid_forget = forget
806 def info(self):
807 words = self.tk.splitlist(
808 self.tk.call('grid', 'info', self._w))
809 dict = {}
810 for i in range(0, len(words), 2):
811 key = words[i][1:]
812 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000813 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000814 value = self._nametowidget(value)
815 dict[key] = value
816 return dict
817 grid_info = info
818 def location(self, x, y):
819 return self._getints(
820 self.tk.call(
821 'grid', 'location', self._w, x, y)) or None
822 _noarg_ = ['_noarg_']
823 def propagate(self, flag=_noarg_):
824 if flag is Grid._noarg_:
825 return self._getboolean(self.tk.call(
826 'grid', 'propagate', self._w))
827 else:
828 self.tk.call('grid', 'propagate', self._w, flag)
829 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000830 def rowconfigure(self, index, cnf={}, **kw):
831 if type(cnf) is not DictionaryType and not kw:
832 options = self._options({cnf: None})
833 else:
834 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000835 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000836 ('grid', 'rowconfigure', self._w, index)
837 + options)
838 if options == ('-minsize', None):
839 return self.tk.getint(res) or None
840 elif options == ('-weight', None):
841 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 def size(self):
843 return self._getints(
844 self.tk.call('grid', 'size', self._w)) or None
845 def slaves(self, *args):
846 return map(self._nametowidget,
847 self.tk.splitlist(
848 apply(self.tk.call,
849 ('grid', 'slaves', self._w) + args)))
850 grid_slaves = slaves
851
852class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000853 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000854 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000855 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000856 if not _default_root:
857 _default_root = Tk()
858 master = _default_root
859 if not _default_root:
860 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000861 self.master = master
862 self.tk = master.tk
863 if cnf.has_key('name'):
864 name = cnf['name']
865 del cnf['name']
866 else:
867 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000868 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000869 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000870 self._w = '.' + name
871 else:
872 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000873 self.children = {}
874 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000875 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000876 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000877 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
878 if kw:
879 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000880 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000881 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000882 classes = []
883 for k in cnf.keys():
884 if type(k) is ClassType:
885 classes.append((k, cnf[k]))
886 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000887 apply(self.tk.call,
888 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000889 for k, v in classes:
890 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000891 def config(self, cnf=None, **kw):
892 # XXX ought to generalize this so tag_config etc. can use it
893 if kw:
894 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000895 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000896 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000897 if cnf is None:
898 cnf = {}
899 for x in self.tk.split(
900 self.tk.call(self._w, 'configure')):
901 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
902 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000903 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000904 x = self.tk.split(self.tk.call(
905 self._w, 'configure', '-'+cnf))
906 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000907 apply(self.tk.call, (self._w, 'configure')
908 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000909 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000910 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000911 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000912 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000913 def __setitem__(self, key, value):
914 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000915 def keys(self):
916 return map(lambda x: x[0][1:],
917 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000918 def __str__(self):
919 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000920 def destroy(self):
921 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000922 if self.master.children.has_key(self._name):
923 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000924 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000925 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000926 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000927
928class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000929 def __init__(self, master=None, cnf={}, **kw):
930 if kw:
931 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000932 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000933 for wmkey in ['screen', 'class_', 'class', 'visual',
934 'colormap']:
935 if cnf.has_key(wmkey):
936 val = cnf[wmkey]
937 # TBD: a hack needed because some keys
938 # are not valid as keyword arguments
939 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
940 else: opt = '-'+wmkey
941 extra = extra + (opt, val)
942 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000943 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000944 root = self._root()
945 self.iconname(root.iconname())
946 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000947
948class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000949 def __init__(self, master=None, cnf={}, **kw):
950 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000951 def tkButtonEnter(self, *dummy):
952 self.tk.call('tkButtonEnter', self._w)
953 def tkButtonLeave(self, *dummy):
954 self.tk.call('tkButtonLeave', self._w)
955 def tkButtonDown(self, *dummy):
956 self.tk.call('tkButtonDown', self._w)
957 def tkButtonUp(self, *dummy):
958 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000959 def tkButtonInvoke(self, *dummy):
960 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000961 def flash(self):
962 self.tk.call(self._w, 'flash')
963 def invoke(self):
964 self.tk.call(self._w, 'invoke')
965
966# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000967# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000968def AtEnd():
969 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000970def AtInsert(*args):
971 s = 'insert'
972 for a in args:
973 if a: s = s + (' ' + a)
974 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000975def AtSelFirst():
976 return 'sel.first'
977def AtSelLast():
978 return 'sel.last'
979def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000980 if y is None:
981 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000982 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000983 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000984
985class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000986 def __init__(self, master=None, cnf={}, **kw):
987 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000988 def addtag(self, *args):
989 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +0000990 def addtag_above(self, newtag, tagOrId):
991 self.addtag(newtag, 'above', tagOrId)
992 def addtag_all(self, newtag):
993 self.addtag(newtag, 'all')
994 def addtag_below(self, newtag, tagOrId):
995 self.addtag(newtag, 'below', tagOrId)
996 def addtag_closest(self, newtag, x, y, halo=None, start=None):
997 self.addtag(newtag, 'closest', x, y, halo, start)
998 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
999 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1000 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1001 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1002 def addtag_withtag(self, newtag, tagOrId):
1003 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001004 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001005 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001006 def tag_unbind(self, tagOrId, sequence):
1007 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001008 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001009 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001010 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001011 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001012 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001013 self._w, 'canvasx', screenx, gridspacing))
1014 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001015 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001016 self._w, 'canvasy', screeny, gridspacing))
1017 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001018 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001019 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001020 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001021 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001022 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001023 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001024 args = args[:-1]
1025 else:
1026 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001027 return self.tk.getint(apply(
1028 self.tk.call,
1029 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001030 + args + self._options(cnf, kw)))
1031 def create_arc(self, *args, **kw):
1032 return self._create('arc', args, kw)
1033 def create_bitmap(self, *args, **kw):
1034 return self._create('bitmap', args, kw)
1035 def create_image(self, *args, **kw):
1036 return self._create('image', args, kw)
1037 def create_line(self, *args, **kw):
1038 return self._create('line', args, kw)
1039 def create_oval(self, *args, **kw):
1040 return self._create('oval', args, kw)
1041 def create_polygon(self, *args, **kw):
1042 return self._create('polygon', args, kw)
1043 def create_rectangle(self, *args, **kw):
1044 return self._create('rectangle', args, kw)
1045 def create_text(self, *args, **kw):
1046 return self._create('text', args, kw)
1047 def create_window(self, *args, **kw):
1048 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001049 def dchars(self, *args):
1050 self._do('dchars', args)
1051 def delete(self, *args):
1052 self._do('delete', args)
1053 def dtag(self, *args):
1054 self._do('dtag', args)
1055 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001056 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001057 def find_above(self, tagOrId):
1058 return self.find('above', tagOrId)
1059 def find_all(self):
1060 return self.find('all')
1061 def find_below(self, tagOrId):
1062 return self.find('below', tagOrId)
1063 def find_closest(self, x, y, halo=None, start=None):
1064 return self.find('closest', x, y, halo, start)
1065 def find_enclosed(self, x1, y1, x2, y2):
1066 return self.find('enclosed', x1, y1, x2, y2)
1067 def find_overlapping(self, x1, y1, x2, y2):
1068 return self.find('overlapping', x1, y1, x2, y2)
1069 def find_withtag(self, tagOrId):
1070 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001071 def focus(self, *args):
1072 return self._do('focus', args)
1073 def gettags(self, *args):
1074 return self.tk.splitlist(self._do('gettags', args))
1075 def icursor(self, *args):
1076 self._do('icursor', args)
1077 def index(self, *args):
1078 return self.tk.getint(self._do('index', args))
1079 def insert(self, *args):
1080 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001081 def itemcget(self, tagOrId, option):
1082 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001083 def itemconfig(self, tagOrId, cnf=None, **kw):
1084 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001085 cnf = {}
1086 for x in self.tk.split(
1087 self._do('itemconfigure', (tagOrId))):
1088 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1089 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001090 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001091 x = self.tk.split(self._do('itemconfigure',
1092 (tagOrId, '-'+cnf,)))
1093 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001094 self._do('itemconfigure', (tagOrId,)
1095 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001096 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001097 def lower(self, *args):
1098 self._do('lower', args)
1099 def move(self, *args):
1100 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001101 def postscript(self, cnf={}, **kw):
1102 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001103 def tkraise(self, *args):
1104 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001105 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001106 def scale(self, *args):
1107 self._do('scale', args)
1108 def scan_mark(self, x, y):
1109 self.tk.call(self._w, 'scan', 'mark', x, y)
1110 def scan_dragto(self, x, y):
1111 self.tk.call(self._w, 'scan', 'dragto', x, y)
1112 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001113 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001114 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001115 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001116 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001117 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001118 def select_item(self):
1119 self.tk.call(self._w, 'select', 'item')
1120 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001121 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001122 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001123 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001124 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001125 if not args:
1126 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001127 apply(self.tk.call, (self._w, 'xview')+args)
1128 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001129 if not args:
1130 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001131 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001132
1133class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001134 def __init__(self, master=None, cnf={}, **kw):
1135 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001136 def deselect(self):
1137 self.tk.call(self._w, 'deselect')
1138 def flash(self):
1139 self.tk.call(self._w, 'flash')
1140 def invoke(self):
1141 self.tk.call(self._w, 'invoke')
1142 def select(self):
1143 self.tk.call(self._w, 'select')
1144 def toggle(self):
1145 self.tk.call(self._w, 'toggle')
1146
1147class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001148 def __init__(self, master=None, cnf={}, **kw):
1149 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def delete(self, first, last=None):
1151 self.tk.call(self._w, 'delete', first, last)
1152 def get(self):
1153 return self.tk.call(self._w, 'get')
1154 def icursor(self, index):
1155 self.tk.call(self._w, 'icursor', index)
1156 def index(self, index):
1157 return self.tk.getint(self.tk.call(
1158 self._w, 'index', index))
1159 def insert(self, index, string):
1160 self.tk.call(self._w, 'insert', index, string)
1161 def scan_mark(self, x):
1162 self.tk.call(self._w, 'scan', 'mark', x)
1163 def scan_dragto(self, x):
1164 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001165 def selection_adjust(self, index):
1166 self.tk.call(self._w, 'selection', 'adjust', index)
1167 select_adjust = selection_adjust
1168 def selection_clear(self):
1169 self.tk.call(self._w, 'selection', 'clear')
1170 select_clear = selection_clear
1171 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001172 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001173 select_from = selection_from
1174 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001175 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001176 self.tk.call(self._w, 'selection', 'present'))
1177 select_present = selection_present
1178 def selection_range(self, start, end):
1179 self.tk.call(self._w, 'selection', 'range', start, end)
1180 select_range = selection_range
1181 def selection_to(self, index):
1182 self.tk.call(self._w, 'selection', 'to', index)
1183 select_to = selection_to
1184 def xview(self, index):
1185 self.tk.call(self._w, 'xview', index)
1186 def xview_moveto(self, fraction):
1187 self.tk.call(self._w, 'xview', 'moveto', fraction)
1188 def xview_scroll(self, number, what):
1189 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001190
1191class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001192 def __init__(self, master=None, cnf={}, **kw):
1193 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001194 extra = ()
1195 if cnf.has_key('class'):
1196 extra = ('-class', cnf['class'])
1197 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001198 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001199
1200class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001201 def __init__(self, master=None, cnf={}, **kw):
1202 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001203
Guido van Rossum18468821994-06-20 07:49:28 +00001204class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001205 def __init__(self, master=None, cnf={}, **kw):
1206 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001207 def activate(self, index):
1208 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001209 def bbox(self, *args):
1210 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001211 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001212 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001213 return self.tk.splitlist(self.tk.call(
1214 self._w, 'curselection'))
1215 def delete(self, first, last=None):
1216 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001217 def get(self, first, last=None):
1218 if last:
1219 return self.tk.splitlist(self.tk.call(
1220 self._w, 'get', first, last))
1221 else:
1222 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001223 def insert(self, index, *elements):
1224 apply(self.tk.call,
1225 (self._w, 'insert', index) + elements)
1226 def nearest(self, y):
1227 return self.tk.getint(self.tk.call(
1228 self._w, 'nearest', y))
1229 def scan_mark(self, x, y):
1230 self.tk.call(self._w, 'scan', 'mark', x, y)
1231 def scan_dragto(self, x, y):
1232 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001233 def see(self, index):
1234 self.tk.call(self._w, 'see', index)
1235 def index(self, index):
1236 i = self.tk.call(self._w, 'index', index)
1237 if i == 'none': return None
1238 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001239 def select_adjust(self, index):
1240 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001241 def select_anchor(self, index):
1242 self.tk.call(self._w, 'selection', 'anchor', index)
1243 def select_clear(self, first, last=None):
1244 self.tk.call(self._w,
1245 'selection', 'clear', first, last)
1246 def select_includes(self, index):
1247 return self.tk.getboolean(self.tk.call(
1248 self._w, 'selection', 'includes', index))
1249 def select_set(self, first, last=None):
1250 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001251 def size(self):
1252 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001253 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001254 if not what:
1255 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001256 apply(self.tk.call, (self._w, 'xview')+what)
1257 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001258 if not what:
1259 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001260 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001261
1262class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001263 def __init__(self, master=None, cnf={}, **kw):
1264 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001265 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001266 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001267 def tk_mbPost(self):
1268 self.tk.call('tk_mbPost', self._w)
1269 def tk_mbUnpost(self):
1270 self.tk.call('tk_mbUnpost')
1271 def tk_traverseToMenu(self, char):
1272 self.tk.call('tk_traverseToMenu', self._w, char)
1273 def tk_traverseWithinMenu(self, char):
1274 self.tk.call('tk_traverseWithinMenu', self._w, char)
1275 def tk_getMenuButtons(self):
1276 return self.tk.call('tk_getMenuButtons', self._w)
1277 def tk_nextMenu(self, count):
1278 self.tk.call('tk_nextMenu', count)
1279 def tk_nextMenuEntry(self, count):
1280 self.tk.call('tk_nextMenuEntry', count)
1281 def tk_invokeMenu(self):
1282 self.tk.call('tk_invokeMenu', self._w)
1283 def tk_firstMenu(self):
1284 self.tk.call('tk_firstMenu', self._w)
1285 def tk_mbButtonDown(self):
1286 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001287 def tk_popup(self, x, y, entry=""):
1288 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001289 def activate(self, index):
1290 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001291 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001292 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001293 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001294 def add_cascade(self, cnf={}, **kw):
1295 self.add('cascade', cnf or kw)
1296 def add_checkbutton(self, cnf={}, **kw):
1297 self.add('checkbutton', cnf or kw)
1298 def add_command(self, cnf={}, **kw):
1299 self.add('command', cnf or kw)
1300 def add_radiobutton(self, cnf={}, **kw):
1301 self.add('radiobutton', cnf or kw)
1302 def add_separator(self, cnf={}, **kw):
1303 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001304 def insert(self, index, itemType, cnf={}, **kw):
1305 apply(self.tk.call, (self._w, 'insert', index, itemType)
1306 + self._options(cnf, kw))
1307 def insert_cascade(self, index, cnf={}, **kw):
1308 self.insert(index, 'cascade', cnf or kw)
1309 def insert_checkbutton(self, index, cnf={}, **kw):
1310 self.insert(index, 'checkbutton', cnf or kw)
1311 def insert_command(self, index, cnf={}, **kw):
1312 self.insert(index, 'command', cnf or kw)
1313 def insert_radiobutton(self, index, cnf={}, **kw):
1314 self.insert(index, 'radiobutton', cnf or kw)
1315 def insert_separator(self, index, cnf={}, **kw):
1316 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001317 def delete(self, index1, index2=None):
1318 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001319 def entryconfig(self, index, cnf=None, **kw):
1320 if cnf is None and not kw:
1321 cnf = {}
1322 for x in self.tk.split(apply(self.tk.call,
1323 (self._w, 'entryconfigure', index))):
1324 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1325 return cnf
1326 if type(cnf) == StringType and not kw:
1327 x = self.tk.split(apply(self.tk.call,
1328 (self._w, 'entryconfigure', index, '-'+cnf)))
1329 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001330 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001331 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001332 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001333 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001334 i = self.tk.call(self._w, 'index', index)
1335 if i == 'none': return None
1336 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001337 def invoke(self, index):
1338 return self.tk.call(self._w, 'invoke', index)
1339 def post(self, x, y):
1340 self.tk.call(self._w, 'post', x, y)
1341 def unpost(self):
1342 self.tk.call(self._w, 'unpost')
1343 def yposition(self, index):
1344 return self.tk.getint(self.tk.call(
1345 self._w, 'yposition', index))
1346
1347class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001348 def __init__(self, master=None, cnf={}, **kw):
1349 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001350
1351class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001352 def __init__(self, master=None, cnf={}, **kw):
1353 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001354
1355class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001356 def __init__(self, master=None, cnf={}, **kw):
1357 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001358 def deselect(self):
1359 self.tk.call(self._w, 'deselect')
1360 def flash(self):
1361 self.tk.call(self._w, 'flash')
1362 def invoke(self):
1363 self.tk.call(self._w, 'invoke')
1364 def select(self):
1365 self.tk.call(self._w, 'select')
1366
1367class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001368 def __init__(self, master=None, cnf={}, **kw):
1369 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001370 def get(self):
1371 return self.tk.getint(self.tk.call(self._w, 'get'))
1372 def set(self, value):
1373 self.tk.call(self._w, 'set', value)
1374
1375class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001376 def __init__(self, master=None, cnf={}, **kw):
1377 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001378 def activate(self, index):
1379 self.tk.call(self._w, 'activate', index)
1380 def delta(self, deltax, deltay):
1381 return self.getdouble(self.tk.call(
1382 self._w, 'delta', deltax, deltay))
1383 def fraction(self, x, y):
1384 return self.getdouble(self.tk.call(
1385 self._w, 'fraction', x, y))
1386 def identify(self, x, y):
1387 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001388 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001389 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001390 def set(self, *args):
1391 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001392
1393class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001394 def __init__(self, master=None, cnf={}, **kw):
1395 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001396 self.bind('<Delete>', self.bspace)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001397 def bbox(self, *args):
1398 return self._getints(self._do('bbox', args)) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001399 def bspace(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001400 self.delete('insert')
Guido van Rossum18468821994-06-20 07:49:28 +00001401 def tk_textSelectTo(self, index):
1402 self.tk.call('tk_textSelectTo', self._w, index)
1403 def tk_textBackspace(self):
1404 self.tk.call('tk_textBackspace', self._w)
1405 def tk_textIndexCloser(self, a, b, c):
1406 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1407 def tk_textResetAnchor(self, index):
1408 self.tk.call('tk_textResetAnchor', self._w, index)
1409 def compare(self, index1, op, index2):
1410 return self.tk.getboolean(self.tk.call(
1411 self._w, 'compare', index1, op, index2))
1412 def debug(self, boolean=None):
1413 return self.tk.getboolean(self.tk.call(
1414 self._w, 'debug', boolean))
1415 def delete(self, index1, index2=None):
1416 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001417 def dlineinfo(self, index):
1418 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001419 def get(self, index1, index2=None):
1420 return self.tk.call(self._w, 'get', index1, index2)
1421 def index(self, index):
1422 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001423 def insert(self, index, chars, *args):
1424 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001425 def mark_gravity(self, markName, direction=None):
1426 return apply(self.tk.call,
1427 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001428 def mark_names(self):
1429 return self.tk.splitlist(self.tk.call(
1430 self._w, 'mark', 'names'))
1431 def mark_set(self, markName, index):
1432 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001433 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001434 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001435 def scan_mark(self, x, y):
1436 self.tk.call(self._w, 'scan', 'mark', x, y)
1437 def scan_dragto(self, x, y):
1438 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001439 def search(self, pattern, index, stopindex=None,
1440 forwards=None, backwards=None, exact=None,
1441 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001442 args = [self._w, 'search']
1443 if forwards: args.append('-forwards')
1444 if backwards: args.append('-backwards')
1445 if exact: args.append('-exact')
1446 if regexp: args.append('-regexp')
1447 if nocase: args.append('-nocase')
1448 if count: args.append('-count'); args.append(count)
1449 if pattern[0] == '-': args.append('--')
1450 args.append(pattern)
1451 args.append(index)
1452 if stopindex: args.append(stopindex)
1453 return apply(self.tk.call, tuple(args))
1454 def see(self, index):
1455 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001456 def tag_add(self, tagName, index1, index2=None):
1457 self.tk.call(
1458 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001459 def tag_unbind(self, tagName, sequence):
1460 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001461 def tag_bind(self, tagName, sequence, func, add=None):
1462 return self._bind((self._w, 'tag', 'bind', tagName),
1463 sequence, func, add)
1464 def tag_cget(self, tagName, option):
1465 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001466 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001467 if type(cnf) == StringType:
1468 x = self.tk.split(self.tk.call(
1469 self._w, 'tag', 'configure', tagName, '-'+cnf))
1470 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001471 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001472 (self._w, 'tag', 'configure', tagName)
1473 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001474 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001475 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001476 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001477 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001478 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001479 def tag_names(self, index=None):
1480 return self.tk.splitlist(
1481 self.tk.call(self._w, 'tag', 'names', index))
1482 def tag_nextrange(self, tagName, index1, index2=None):
1483 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001484 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001485 def tag_raise(self, tagName, aboveThis=None):
1486 self.tk.call(
1487 self._w, 'tag', 'raise', tagName, aboveThis)
1488 def tag_ranges(self, tagName):
1489 return self.tk.splitlist(self.tk.call(
1490 self._w, 'tag', 'ranges', tagName))
1491 def tag_remove(self, tagName, index1, index2=None):
1492 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001493 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001494 def window_cget(self, index, option):
1495 return self.tk.call(self._w, 'window', 'cget', index, option)
1496 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001497 if type(cnf) == StringType:
1498 x = self.tk.split(self.tk.call(
1499 self._w, 'window', 'configure',
1500 index, '-'+cnf))
1501 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001502 apply(self.tk.call,
1503 (self._w, 'window', 'configure', index)
1504 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001505 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001506 def window_create(self, index, cnf={}, **kw):
1507 apply(self.tk.call,
1508 (self._w, 'window', 'create', index)
1509 + self._options(cnf, kw))
1510 def window_names(self):
1511 return self.tk.splitlist(
1512 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001513 def xview(self, *what):
1514 if not what:
1515 return self._getdoubles(self.tk.call(self._w, 'xview'))
1516 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001517 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001518 if not what:
1519 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001520 apply(self.tk.call, (self._w, 'yview')+what)
1521 def yview_pickplace(self, *what):
1522 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001523
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001524class OptionMenu(Widget):
1525 def __init__(self, master, variable, value, *values):
1526 self.widgetName = 'tk_optionMenu'
1527 Widget._setup(self, master, {})
1528 self.menuname = apply(
1529 self.tk.call,
1530 (self.widgetName, self._w, variable, value) + values)
1531
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001532class Image:
1533 def __init__(self, imgtype, name=None, cnf={}, **kw):
1534 self.name = None
1535 master = _default_root
1536 if not master: raise RuntimeError, 'Too early to create image'
1537 self.tk = master.tk
1538 if not name: name = `id(self)`
1539 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1540 elif kw: cnf = kw
1541 options = ()
1542 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001543 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001544 v = self._register(v)
1545 options = options + ('-'+k, v)
1546 apply(self.tk.call,
1547 ('image', 'create', imgtype, name,) + options)
1548 self.name = name
1549 def __str__(self): return self.name
1550 def __del__(self):
1551 if self.name:
1552 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001553 def __setitem__(self, key, value):
1554 self.tk.call(self.name, 'configure', '-'+key, value)
1555 def __getitem__(self, key):
1556 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001557 def height(self):
1558 return self.tk.getint(
1559 self.tk.call('image', 'height', self.name))
1560 def type(self):
1561 return self.tk.call('image', 'type', self.name)
1562 def width(self):
1563 return self.tk.getint(
1564 self.tk.call('image', 'width', self.name))
1565
1566class PhotoImage(Image):
1567 def __init__(self, name=None, cnf={}, **kw):
1568 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001569 def blank(self):
1570 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001571 def cget(self, option):
1572 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001573 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001574 def __getitem__(self, key):
1575 return self.tk.call(self.name, 'cget', '-' + key)
1576 def copy(self):
1577 destImage = PhotoImage()
1578 self.tk.call(destImage, 'copy', self.name)
1579 return destImage
1580 def zoom(self,x,y=''):
1581 destImage = PhotoImage()
1582 if y=='': y=x
1583 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1584 return destImage
1585 def subsample(self,x,y=''):
1586 destImage = PhotoImage()
1587 if y=='': y=x
1588 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1589 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001590 def get(self, x, y):
1591 return self.tk.call(self.name, 'get', x, y)
1592 def put(self, data, to=None):
1593 args = (self.name, 'put', data)
1594 if to:
1595 args = args + to
1596 apply(self.tk.call, args)
1597 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001598 def write(self, filename, format=None, from_coords=None):
1599 args = (self.name, 'write', filename)
1600 if format:
1601 args = args + ('-format', format)
1602 if from_coords:
1603 args = args + ('-from',) + tuple(from_coords)
1604 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001605
1606class BitmapImage(Image):
1607 def __init__(self, name=None, cnf={}, **kw):
1608 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1609
1610def image_names(): return _default_root.tk.call('image', 'names')
1611def image_types(): return _default_root.tk.call('image', 'types')
1612
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001613######################################################################
1614# Extensions:
1615
1616class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001617 def __init__(self, master=None, cnf={}, **kw):
1618 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001619 self.bind('<Any-Enter>', self.tkButtonEnter)
1620 self.bind('<Any-Leave>', self.tkButtonLeave)
1621 self.bind('<1>', self.tkButtonDown)
1622 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001623
1624class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001625 def __init__(self, master=None, cnf={}, **kw):
1626 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001627 self.bind('<Any-Enter>', self.tkButtonEnter)
1628 self.bind('<Any-Leave>', self.tkButtonLeave)
1629 self.bind('<1>', self.tkButtonDown)
1630 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1631 self['fg'] = self['bg']
1632 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001633
Guido van Rossumc417ef81996-08-21 23:38:59 +00001634######################################################################
1635# Test:
1636
1637def _test():
1638 root = Tk()
1639 label = Label(root, text="Proof-of-existence test for Tk")
1640 label.pack()
1641 test = Button(root, text="Click me!",
1642 command=lambda root=root: root.test.config(
1643 text="[%s]" % root.test['text']))
1644 test.pack()
1645 root.test = test
1646 quit = Button(root, text="QUIT", command=root.destroy)
1647 quit.pack()
1648 root.mainloop()
1649
1650if __name__ == '__main__':
1651 _test()
1652
Guido van Rossum37dcab11996-05-16 16:00:19 +00001653
1654# Emacs cruft
1655# Local Variables:
1656# py-indent-offset: 8
1657# End: