blob: 9fb1d025ebedaf1b8012f4ad0d05949c328621e6 [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum37dcab11996-05-16 16:00:19 +00003__version__ = "$Revision$"
4
5try:
6 # See if modern _tkinter is present
7 import _tkinter
8 tkinter = _tkinter # b/w compat
9except ImportError:
10 # No modern _tkinter -- try oldfashioned tkinter
11 import tkinter
12 if hasattr(tkinter, "__path__"):
13 import sys, os
14 # Append standard platform specific directory
15 p = tkinter.__path__
16 for dir in sys.path:
17 if (dir not in p and
18 os.path.basename(dir) == sys.platform):
19 p.append(dir)
20 del sys, os, p, dir
21 from tkinter import tkinter
22TclError = tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +000023from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +000024from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000025import string; _string = string; del string
Guido van Rossum18468821994-06-20 07:49:28 +000026
Guido van Rossum36269991996-05-16 17:11:27 +000027TkVersion = _string.atof(tkinter.TK_VERSION)
28TclVersion = _string.atof(tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000029
Guido van Rossum36269991996-05-16 17:11:27 +000030######################################################################
31# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
32# they are defined here (and not in Tkconstants):
33######################################################################
34if TkVersion >= 4.1:
35 READABLE = 2
36 WRITABLE = 4
37 EXCEPTION = 8
38else:
39 READABLE = 1
40 WRITABLE = 2
41 EXCEPTION = 4
42
43
Guido van Rossum2dcf5291994-07-06 09:23:20 +000044def _flatten(tuple):
45 res = ()
46 for item in tuple:
47 if type(item) in (TupleType, ListType):
48 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000049 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000050 res = res + (item,)
51 return res
52
53def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000054 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000055 return cnfs
56 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000057 return cnfs
58 else:
59 cnf = {}
60 for c in _flatten(cnfs):
61 for k, v in c.items():
62 cnf[k] = v
63 return cnf
64
65class Event:
66 pass
67
Guido van Rossumaec5dc91994-06-27 07:55:12 +000068_default_root = None
69
Guido van Rossum45853db1994-06-20 12:19:19 +000070def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000071 pass
72
Guido van Rossum97aeca11994-07-07 13:12:12 +000073def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000074 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000075
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076_varnum = 0
77class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000078 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000079 def __init__(self, master=None):
80 global _default_root
81 global _varnum
82 if master:
83 self._tk = master.tk
84 else:
85 self._tk = _default_root.tk
86 self._name = 'PY_VAR' + `_varnum`
87 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000088 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000089 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000090 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000091 def __str__(self):
92 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000093 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000094 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000095
96class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000097 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000098 def __init__(self, master=None):
99 Variable.__init__(self, master)
100 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000101 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000102
103class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000104 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000105 def __init__(self, master=None):
106 Variable.__init__(self, master)
107 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000108 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000109
110class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000111 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000112 def __init__(self, master=None):
113 Variable.__init__(self, master)
114 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000115 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000116
117class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000118 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000119 def __init__(self, master=None):
120 Variable.__init__(self, master)
121 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000122 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000123
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000124def mainloop(n=0):
125 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000126
127def getint(s):
128 return _default_root.tk.getint(s)
129
130def getdouble(s):
131 return _default_root.tk.getdouble(s)
132
133def getboolean(s):
134 return _default_root.tk.getboolean(s)
135
Guido van Rossum18468821994-06-20 07:49:28 +0000136class Misc:
137 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000138 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000139 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000140 def tk_bisque(self):
141 self.tk.call('tk_bisque')
142 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000143 apply(self.tk.call, ('tk_setPalette',)
144 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000145 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000146 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000147 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000148 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000149 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000150 def wait_window(self, window=None):
151 if window == None:
152 window = self
153 self.tk.call('tkwait', 'window', window._w)
154 def wait_visibility(self, window=None):
155 if window == None:
156 window = self
157 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000158 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000159 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000160 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000161 return self.tk.getvar(name)
162 def getint(self, s):
163 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000164 def getdouble(self, s):
165 return self.tk.getdouble(s)
166 def getboolean(self, s):
167 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000168 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000169 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000170 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000171 def focus_force(self):
172 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000173 def focus_get(self):
174 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000175 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000176 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000177 def focus_displayof(self):
178 name = self.tk.call('focus', '-displayof', self._w)
179 if name == 'none' or not name: return None
180 return self._nametowidget(name)
181 def focus_lastfor(self):
182 name = self.tk.call('focus', '-lastfor', self._w)
183 if name == 'none' or not name: return None
184 return self._nametowidget(name)
185 def tk_focusFollowsMouse(self):
186 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000187 def tk_focusNext(self):
188 name = self.tk.call('tk_focusNext', self._w)
189 if not name: return None
190 return self._nametowidget(name)
191 def tk_focusPrev(self):
192 name = self.tk.call('tk_focusPrev', self._w)
193 if not name: return None
194 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000195 def after(self, ms, func=None, *args):
196 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000197 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000198 self.tk.call('after', ms)
199 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000200 # XXX Disgusting hack to clean up after calling func
201 tmp = []
202 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
203 try:
204 apply(func, args)
205 finally:
206 tk.deletecommand(tmp[0])
207 name = self._register(callit)
208 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000209 return self.tk.call('after', ms, name)
210 def after_idle(self, func, *args):
211 return apply(self.after, ('idle', func) + args)
212 def after_cancel(self, id):
213 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000214 def bell(self, displayof=0):
215 apply(self.tk.call, ('bell',) + self._displayof(displayof))
216 # Clipboard handling:
217 def clipboard_clear(self, **kw):
218 if not kw.has_key('displayof'): kw['displayof'] = self._w
219 apply(self.tk.call,
220 ('clipboard', 'clear') + self._options(kw))
221 def clipboard_append(self, string, **kw):
222 if not kw.has_key('displayof'): kw['displayof'] = self._w
223 apply(self.tk.call,
224 ('clipboard', 'append') + self._options(kw)
225 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000226 # XXX grab current w/o window argument
227 def grab_current(self):
228 name = self.tk.call('grab', 'current', self._w)
229 if not name: return None
230 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000231 def grab_release(self):
232 self.tk.call('grab', 'release', self._w)
233 def grab_set(self):
234 self.tk.call('grab', 'set', self._w)
235 def grab_set_global(self):
236 self.tk.call('grab', 'set', '-global', self._w)
237 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000238 status = self.tk.call('grab', 'status', self._w)
239 if status == 'none': status = None
240 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000241 def lower(self, belowThis=None):
242 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000243 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000244 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000245 def option_clear(self):
246 self.tk.call('option', 'clear')
247 def option_get(self, name, className):
248 return self.tk.call('option', 'get', self._w, name, className)
249 def option_readfile(self, fileName, priority = None):
250 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000251 def selection_clear(self, **kw):
252 if not kw.has_key('displayof'): kw['displayof'] = self._w
253 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
254 def selection_get(self, **kw):
255 if not kw.has_key('displayof'): kw['displayof'] = self._w
256 return apply(self.tk.call,
257 ('selection', 'get') + self._options(kw))
258 def selection_handle(self, command, **kw):
259 name = self._register(command)
260 apply(self.tk.call,
261 ('selection', 'handle') + self._options(kw)
262 + (self._w, name))
263 def selection_own(self, **kw):
264 "Become owner of X selection."
265 apply(self.tk.call,
266 ('selection', 'own') + self._options(kw) + (self._w,))
267 def selection_own_get(self, **kw):
268 "Find owner of X selection."
269 if not kw.has_key('displayof'): kw['displayof'] = self._w
270 return self._nametowidget(
271 apply(self.tk.call,
272 ('selection', 'own') + self._options(kw)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000273 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000274 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000275 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000276 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000277 def tkraise(self, aboveThis=None):
278 self.tk.call('raise', self._w, aboveThis)
279 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000280 def colormodel(self, value=None):
281 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000282 def winfo_atom(self, name, displayof=0):
283 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
284 return self.tk.getint(apply(self.tk.call, args))
285 def winfo_atomname(self, id, displayof=0):
286 args = ('winfo', 'atomname') \
287 + self._displayof(displayof) + (id,)
288 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000289 def winfo_cells(self):
290 return self.tk.getint(
291 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000292 def winfo_children(self):
293 return map(self._nametowidget,
294 self.tk.splitlist(self.tk.call(
295 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000296 def winfo_class(self):
297 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000298 def winfo_colormapfull(self):
299 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000300 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000301 def winfo_containing(self, rootX, rootY, displayof=0):
302 args = ('winfo', 'containing') \
303 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000304 name = apply(self.tk.call, args)
305 if not name: return None
306 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000307 def winfo_depth(self):
308 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
309 def winfo_exists(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'exists', self._w))
312 def winfo_fpixels(self, number):
313 return self.tk.getdouble(self.tk.call(
314 'winfo', 'fpixels', self._w, number))
315 def winfo_geometry(self):
316 return self.tk.call('winfo', 'geometry', self._w)
317 def winfo_height(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'height', self._w))
320 def winfo_id(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000323 def winfo_interps(self, displayof=0):
324 args = ('winfo', 'interps') + self._displayof(displayof)
325 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000326 def winfo_ismapped(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000329 def winfo_manager(self):
330 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000331 def winfo_name(self):
332 return self.tk.call('winfo', 'name', self._w)
333 def winfo_parent(self):
334 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000335 def winfo_pathname(self, id, displayof=0):
336 args = ('winfo', 'pathname') \
337 + self._displayof(displayof) + (id,)
338 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000339 def winfo_pixels(self, number):
340 return self.tk.getint(
341 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000342 def winfo_pointerx(self):
343 return self.tk.getint(
344 self.tk.call('winfo', 'pointerx', self._w))
345 def winfo_pointerxy(self):
346 return self._getints(
347 self.tk.call('winfo', 'pointerxy', self._w))
348 def winfo_pointery(self):
349 return self.tk.getint(
350 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000351 def winfo_reqheight(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'reqheight', self._w))
354 def winfo_reqwidth(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'reqwidth', self._w))
357 def winfo_rgb(self, color):
358 return self._getints(
359 self.tk.call('winfo', 'rgb', self._w, color))
360 def winfo_rootx(self):
361 return self.tk.getint(
362 self.tk.call('winfo', 'rootx', self._w))
363 def winfo_rooty(self):
364 return self.tk.getint(
365 self.tk.call('winfo', 'rooty', self._w))
366 def winfo_screen(self):
367 return self.tk.call('winfo', 'screen', self._w)
368 def winfo_screencells(self):
369 return self.tk.getint(
370 self.tk.call('winfo', 'screencells', self._w))
371 def winfo_screendepth(self):
372 return self.tk.getint(
373 self.tk.call('winfo', 'screendepth', self._w))
374 def winfo_screenheight(self):
375 return self.tk.getint(
376 self.tk.call('winfo', 'screenheight', self._w))
377 def winfo_screenmmheight(self):
378 return self.tk.getint(
379 self.tk.call('winfo', 'screenmmheight', self._w))
380 def winfo_screenmmwidth(self):
381 return self.tk.getint(
382 self.tk.call('winfo', 'screenmmwidth', self._w))
383 def winfo_screenvisual(self):
384 return self.tk.call('winfo', 'screenvisual', self._w)
385 def winfo_screenwidth(self):
386 return self.tk.getint(
387 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000388 def winfo_server(self):
389 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000390 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000391 return self._nametowidget(self.tk.call(
392 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000393 def winfo_viewable(self):
394 return self.tk.getint(
395 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000396 def winfo_visual(self):
397 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000398 def winfo_visualid(self):
399 return self.tk.call('winfo', 'visualid', self._w)
400 def winfo_visualsavailable(self, includeids=0):
401 data = self.tk.split(
402 self.tk.call('winfo', 'visualsavailable', self._w,
403 includeids and 'includeids' or None))
404 def parseitem(x, self=self):
405 return x[:1] + tuple(map(self.tk.getint, x[1:]))
406 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000407 def winfo_vrootheight(self):
408 return self.tk.getint(
409 self.tk.call('winfo', 'vrootheight', self._w))
410 def winfo_vrootwidth(self):
411 return self.tk.getint(
412 self.tk.call('winfo', 'vrootwidth', self._w))
413 def winfo_vrootx(self):
414 return self.tk.getint(
415 self.tk.call('winfo', 'vrootx', self._w))
416 def winfo_vrooty(self):
417 return self.tk.getint(
418 self.tk.call('winfo', 'vrooty', self._w))
419 def winfo_width(self):
420 return self.tk.getint(
421 self.tk.call('winfo', 'width', self._w))
422 def winfo_x(self):
423 return self.tk.getint(
424 self.tk.call('winfo', 'x', self._w))
425 def winfo_y(self):
426 return self.tk.getint(
427 self.tk.call('winfo', 'y', self._w))
428 def update(self):
429 self.tk.call('update')
430 def update_idletasks(self):
431 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000432 def bindtags(self, tagList=None):
433 if tagList is None:
434 return self.tk.splitlist(
435 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000436 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000437 self.tk.call('bindtags', self._w, tagList)
438 def _bind(self, what, sequence, func, add):
439 if func:
440 cmd = ("%sset _tkinter_break [%s %s]\n"
441 'if {"$_tkinter_break" == "break"} break\n') \
442 % (add and '+' or '',
443 self._register(func, self._substitute),
444 _string.join(self._subst_format))
445 apply(self.tk.call, what + (sequence, cmd))
446 elif func == '':
447 apply(self.tk.call, what + (sequence, func))
448 else:
449 return apply(self.tk.call, what + (sequence,))
450 def bind(self, sequence=None, func=None, add=None):
451 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000452 def unbind(self, sequence):
453 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000454 def bind_all(self, sequence=None, func=None, add=None):
455 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000456 def unbind_all(self, sequence):
457 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000458 def bind_class(self, className, sequence=None, func=None, add=None):
459 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000460 def unbind_class(self, className, sequence):
461 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000462 def mainloop(self, n=0):
463 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000464 def quit(self):
465 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000466 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000467 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000468 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
469 def _getdoubles(self, string):
470 if not string: return None
471 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000472 def _getboolean(self, string):
473 if string:
474 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000475 def _displayof(self, displayof):
476 if displayof:
477 return ('-displayof', displayof)
478 if displayof is None:
479 return ('-displayof', self._w)
480 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000481 def _options(self, cnf, kw = None):
482 if kw:
483 cnf = _cnfmerge((cnf, kw))
484 else:
485 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000486 res = ()
487 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000488 if v is not None:
489 if k[-1] == '_': k = k[:-1]
490 if callable(v):
491 v = self._register(v)
492 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000493 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000494 def _nametowidget(self, name):
495 w = self
496 if name[0] == '.':
497 w = w._root()
498 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000499 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000500 while name:
501 i = find(name, '.')
502 if i >= 0:
503 name, tail = name[:i], name[i+1:]
504 else:
505 tail = ''
506 w = w.children[name]
507 name = tail
508 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000509 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000510 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000511 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000512 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000513 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000514 except AttributeError:
515 pass
516 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000517 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000518 except AttributeError:
519 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000520 self.tk.createcommand(name, f)
521 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000522 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000523 def _root(self):
524 w = self
525 while w.master: w = w.master
526 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000527 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000528 '%s', '%t', '%w', '%x', '%y',
529 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
530 def _substitute(self, *args):
531 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000532 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000533 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
534 # Missing: (a, c, d, m, o, v, B, R)
535 e = Event()
536 e.serial = tk.getint(nsign)
537 e.num = tk.getint(b)
538 try: e.focus = tk.getboolean(f)
539 except TclError: pass
540 e.height = tk.getint(h)
541 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000542 # For Visibility events, event state is a string and
543 # not an integer:
544 try:
545 e.state = tk.getint(s)
546 except TclError:
547 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000548 e.time = tk.getint(t)
549 e.width = tk.getint(w)
550 e.x = tk.getint(x)
551 e.y = tk.getint(y)
552 e.char = A
553 try: e.send_event = tk.getboolean(E)
554 except TclError: pass
555 e.keysym = K
556 e.keysym_num = tk.getint(N)
557 e.type = T
558 e.widget = self._nametowidget(W)
559 e.x_root = tk.getint(X)
560 e.y_root = tk.getint(Y)
561 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000562 def _report_exception(self):
563 import sys
564 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
565 root = self._root()
566 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000567
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000568class CallWrapper:
569 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000570 self.func = func
571 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000572 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000573 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000574 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000575 if self.subst:
576 args = apply(self.subst, args)
577 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000578 except SystemExit, msg:
579 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000580 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000581 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000582
583class Wm:
584 def aspect(self,
585 minNumer=None, minDenom=None,
586 maxNumer=None, maxDenom=None):
587 return self._getints(
588 self.tk.call('wm', 'aspect', self._w,
589 minNumer, minDenom,
590 maxNumer, maxDenom))
591 def client(self, name=None):
592 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000593 def colormapwindows(self, *wlist):
594 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
595 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000596 def command(self, value=None):
597 return self.tk.call('wm', 'command', self._w, value)
598 def deiconify(self):
599 return self.tk.call('wm', 'deiconify', self._w)
600 def focusmodel(self, model=None):
601 return self.tk.call('wm', 'focusmodel', self._w, model)
602 def frame(self):
603 return self.tk.call('wm', 'frame', self._w)
604 def geometry(self, newGeometry=None):
605 return self.tk.call('wm', 'geometry', self._w, newGeometry)
606 def grid(self,
607 baseWidht=None, baseHeight=None,
608 widthInc=None, heightInc=None):
609 return self._getints(self.tk.call(
610 'wm', 'grid', self._w,
611 baseWidht, baseHeight, widthInc, heightInc))
612 def group(self, pathName=None):
613 return self.tk.call('wm', 'group', self._w, pathName)
614 def iconbitmap(self, bitmap=None):
615 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
616 def iconify(self):
617 return self.tk.call('wm', 'iconify', self._w)
618 def iconmask(self, bitmap=None):
619 return self.tk.call('wm', 'iconmask', self._w, bitmap)
620 def iconname(self, newName=None):
621 return self.tk.call('wm', 'iconname', self._w, newName)
622 def iconposition(self, x=None, y=None):
623 return self._getints(self.tk.call(
624 'wm', 'iconposition', self._w, x, y))
625 def iconwindow(self, pathName=None):
626 return self.tk.call('wm', 'iconwindow', self._w, pathName)
627 def maxsize(self, width=None, height=None):
628 return self._getints(self.tk.call(
629 'wm', 'maxsize', self._w, width, height))
630 def minsize(self, width=None, height=None):
631 return self._getints(self.tk.call(
632 'wm', 'minsize', self._w, width, height))
633 def overrideredirect(self, boolean=None):
634 return self._getboolean(self.tk.call(
635 'wm', 'overrideredirect', self._w, boolean))
636 def positionfrom(self, who=None):
637 return self.tk.call('wm', 'positionfrom', self._w, who)
638 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000639 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000640 command = self._register(func)
641 else:
642 command = func
643 return self.tk.call(
644 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000645 def resizable(self, width=None, height=None):
646 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000647 def sizefrom(self, who=None):
648 return self.tk.call('wm', 'sizefrom', self._w, who)
649 def state(self):
650 return self.tk.call('wm', 'state', self._w)
651 def title(self, string=None):
652 return self.tk.call('wm', 'title', self._w, string)
653 def transient(self, master=None):
654 return self.tk.call('wm', 'transient', self._w, master)
655 def withdraw(self):
656 return self.tk.call('wm', 'withdraw', self._w)
657
658class Tk(Misc, Wm):
659 _w = '.'
660 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000661 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000662 self.master = None
663 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000664 if baseName is None:
665 import sys, os
666 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000667 baseName, ext = os.path.splitext(baseName)
668 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum37dcab11996-05-16 16:00:19 +0000669 self.tk = tkinter.create(screenName, baseName, className)
670 try:
671 # Disable event scanning except for Command-Period
672 import MacOS
673 MacOS.EnableAppswitch(0)
674 except ImportError:
675 pass
676 else:
677 # Work around nasty MacTk bug
678 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000679 # Version sanity checks
680 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000681 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000682 raise RuntimeError, \
683 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000684 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000685 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000686 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000687 raise RuntimeError, \
688 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000689 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000690 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000691 raise RuntimeError, \
692 "Tk 4.0 or higher is required; found Tk %s" \
693 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000694 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000695 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000696 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000697 if not _default_root:
698 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000699 def destroy(self):
700 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000701 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000702 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000703 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000704 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000705 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000706 if os.environ.has_key('HOME'): home = os.environ['HOME']
707 else: home = os.curdir
708 class_tcl = os.path.join(home, '.%s.tcl' % className)
709 class_py = os.path.join(home, '.%s.py' % className)
710 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
711 base_py = os.path.join(home, '.%s.py' % baseName)
712 dir = {'self': self}
713 exec 'from Tkinter import *' in dir
714 if os.path.isfile(class_tcl):
715 print 'source', `class_tcl`
716 self.tk.call('source', class_tcl)
717 if os.path.isfile(class_py):
718 print 'execfile', `class_py`
719 execfile(class_py, dir)
720 if os.path.isfile(base_tcl):
721 print 'source', `base_tcl`
722 self.tk.call('source', base_tcl)
723 if os.path.isfile(base_py):
724 print 'execfile', `base_py`
725 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000726 def report_callback_exception(self, exc, val, tb):
727 import traceback
728 print "Exception in Tkinter callback"
729 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000730
731class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000732 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000733 apply(self.tk.call,
734 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000735 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000736 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000737 pack = config
738 def __setitem__(self, key, value):
739 Pack.config({key: value})
740 def forget(self):
741 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000742 pack_forget = forget
743 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000744 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000745 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000746 dict = {}
747 for i in range(0, len(words), 2):
748 key = words[i][1:]
749 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000750 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000751 value = self._nametowidget(value)
752 dict[key] = value
753 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000754 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000755 _noarg_ = ['_noarg_']
756 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000757 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000758 return self._getboolean(self.tk.call(
759 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000760 else:
761 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000762 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000763 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000764 return map(self._nametowidget,
765 self.tk.splitlist(
766 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000767 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000768
769class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000770 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000771 for k in ['in_']:
772 if kw.has_key(k):
773 kw[k[:-1]] = kw[k]
774 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000775 apply(self.tk.call,
776 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000777 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000778 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000779 place = config
780 def __setitem__(self, key, value):
781 Place.config({key: value})
782 def forget(self):
783 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000784 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000785 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000786 words = self.tk.splitlist(
787 self.tk.call('place', 'info', self._w))
788 dict = {}
789 for i in range(0, len(words), 2):
790 key = words[i][1:]
791 value = words[i+1]
792 if value[:1] == '.':
793 value = self._nametowidget(value)
794 dict[key] = value
795 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000796 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000797 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000798 return map(self._nametowidget,
799 self.tk.splitlist(
800 self.tk.call(
801 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000802 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000803
Guido van Rossum37dcab11996-05-16 16:00:19 +0000804class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000805 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000806 def config(self, cnf={}, **kw):
807 apply(self.tk.call,
808 ('grid', 'configure', self._w)
809 + self._options(cnf, kw))
810 grid = config
811 def __setitem__(self, key, value):
812 Grid.config({key: value})
813 def bbox(self, column, row):
814 return self._getints(
815 self.tk.call(
816 'grid', 'bbox', self._w, column, row)) or None
817 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000818 def columnconfigure(self, index, cnf={}, **kw):
819 if type(cnf) is not DictionaryType and not kw:
820 options = self._options({cnf: None})
821 else:
822 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000823 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000824 ('grid', 'columnconfigure', self._w, index)
825 + options)
826 if options == ('-minsize', None):
827 return self.tk.getint(res) or None
828 elif options == ('-weight', None):
829 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000830 def forget(self):
831 self.tk.call('grid', 'forget', self._w)
832 grid_forget = forget
833 def info(self):
834 words = self.tk.splitlist(
835 self.tk.call('grid', 'info', self._w))
836 dict = {}
837 for i in range(0, len(words), 2):
838 key = words[i][1:]
839 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000840 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000841 value = self._nametowidget(value)
842 dict[key] = value
843 return dict
844 grid_info = info
845 def location(self, x, y):
846 return self._getints(
847 self.tk.call(
848 'grid', 'location', self._w, x, y)) or None
849 _noarg_ = ['_noarg_']
850 def propagate(self, flag=_noarg_):
851 if flag is Grid._noarg_:
852 return self._getboolean(self.tk.call(
853 'grid', 'propagate', self._w))
854 else:
855 self.tk.call('grid', 'propagate', self._w, flag)
856 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000857 def rowconfigure(self, index, cnf={}, **kw):
858 if type(cnf) is not DictionaryType and not kw:
859 options = self._options({cnf: None})
860 else:
861 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000862 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000863 ('grid', 'rowconfigure', self._w, index)
864 + options)
865 if options == ('-minsize', None):
866 return self.tk.getint(res) or None
867 elif options == ('-weight', None):
868 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000869 def size(self):
870 return self._getints(
871 self.tk.call('grid', 'size', self._w)) or None
872 def slaves(self, *args):
873 return map(self._nametowidget,
874 self.tk.splitlist(
875 apply(self.tk.call,
876 ('grid', 'slaves', self._w) + args)))
877 grid_slaves = slaves
878
879class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000880 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000881 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000882 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000883 if not _default_root:
884 _default_root = Tk()
885 master = _default_root
886 if not _default_root:
887 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000888 self.master = master
889 self.tk = master.tk
890 if cnf.has_key('name'):
891 name = cnf['name']
892 del cnf['name']
893 else:
894 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000895 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000896 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000897 self._w = '.' + name
898 else:
899 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000900 self.children = {}
901 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000902 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000903 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000904 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
905 if kw:
906 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000907 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000908 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000909 classes = []
910 for k in cnf.keys():
911 if type(k) is ClassType:
912 classes.append((k, cnf[k]))
913 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000914 apply(self.tk.call,
915 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000916 for k, v in classes:
917 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000918 def config(self, cnf=None, **kw):
919 # XXX ought to generalize this so tag_config etc. can use it
920 if kw:
921 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000922 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000923 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000924 if cnf is None:
925 cnf = {}
926 for x in self.tk.split(
927 self.tk.call(self._w, 'configure')):
928 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
929 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000930 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000931 x = self.tk.split(self.tk.call(
932 self._w, 'configure', '-'+cnf))
933 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000934 apply(self.tk.call, (self._w, 'configure')
935 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000936 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000937 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000938 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000939 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000940 def __setitem__(self, key, value):
941 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000942 def keys(self):
943 return map(lambda x: x[0][1:],
944 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000945 def __str__(self):
946 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000947 def destroy(self):
948 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000949 if self.master.children.has_key(self._name):
950 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000951 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000952 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000953 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000954
955class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000956 def __init__(self, master=None, cnf={}, **kw):
957 if kw:
958 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000959 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000960 for wmkey in ['screen', 'class_', 'class', 'visual',
961 'colormap']:
962 if cnf.has_key(wmkey):
963 val = cnf[wmkey]
964 # TBD: a hack needed because some keys
965 # are not valid as keyword arguments
966 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
967 else: opt = '-'+wmkey
968 extra = extra + (opt, val)
969 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000970 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000971 root = self._root()
972 self.iconname(root.iconname())
973 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000974
975class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000976 def __init__(self, master=None, cnf={}, **kw):
977 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000978 def tkButtonEnter(self, *dummy):
979 self.tk.call('tkButtonEnter', self._w)
980 def tkButtonLeave(self, *dummy):
981 self.tk.call('tkButtonLeave', self._w)
982 def tkButtonDown(self, *dummy):
983 self.tk.call('tkButtonDown', self._w)
984 def tkButtonUp(self, *dummy):
985 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000986 def tkButtonInvoke(self, *dummy):
987 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000988 def flash(self):
989 self.tk.call(self._w, 'flash')
990 def invoke(self):
991 self.tk.call(self._w, 'invoke')
992
993# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000994# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000995def AtEnd():
996 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000997def AtInsert(*args):
998 s = 'insert'
999 for a in args:
1000 if a: s = s + (' ' + a)
1001 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001002def AtSelFirst():
1003 return 'sel.first'
1004def AtSelLast():
1005 return 'sel.last'
1006def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001007 if y is None:
1008 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001009 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001010 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001011
1012class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001013 def __init__(self, master=None, cnf={}, **kw):
1014 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001015 def addtag(self, *args):
1016 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001017 def addtag_above(self, newtag, tagOrId):
1018 self.addtag(newtag, 'above', tagOrId)
1019 def addtag_all(self, newtag):
1020 self.addtag(newtag, 'all')
1021 def addtag_below(self, newtag, tagOrId):
1022 self.addtag(newtag, 'below', tagOrId)
1023 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1024 self.addtag(newtag, 'closest', x, y, halo, start)
1025 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1026 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1027 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1028 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1029 def addtag_withtag(self, newtag, tagOrId):
1030 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001031 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001032 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001033 def tag_unbind(self, tagOrId, sequence):
1034 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001035 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001036 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001037 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001038 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001039 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001040 self._w, 'canvasx', screenx, gridspacing))
1041 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001042 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001043 self._w, 'canvasy', screeny, gridspacing))
1044 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001045 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001046 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001047 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001048 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001049 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001050 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001051 args = args[:-1]
1052 else:
1053 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001054 return self.tk.getint(apply(
1055 self.tk.call,
1056 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001057 + args + self._options(cnf, kw)))
1058 def create_arc(self, *args, **kw):
1059 return self._create('arc', args, kw)
1060 def create_bitmap(self, *args, **kw):
1061 return self._create('bitmap', args, kw)
1062 def create_image(self, *args, **kw):
1063 return self._create('image', args, kw)
1064 def create_line(self, *args, **kw):
1065 return self._create('line', args, kw)
1066 def create_oval(self, *args, **kw):
1067 return self._create('oval', args, kw)
1068 def create_polygon(self, *args, **kw):
1069 return self._create('polygon', args, kw)
1070 def create_rectangle(self, *args, **kw):
1071 return self._create('rectangle', args, kw)
1072 def create_text(self, *args, **kw):
1073 return self._create('text', args, kw)
1074 def create_window(self, *args, **kw):
1075 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001076 def dchars(self, *args):
1077 self._do('dchars', args)
1078 def delete(self, *args):
1079 self._do('delete', args)
1080 def dtag(self, *args):
1081 self._do('dtag', args)
1082 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001083 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001084 def find_above(self, tagOrId):
1085 return self.find('above', tagOrId)
1086 def find_all(self):
1087 return self.find('all')
1088 def find_below(self, tagOrId):
1089 return self.find('below', tagOrId)
1090 def find_closest(self, x, y, halo=None, start=None):
1091 return self.find('closest', x, y, halo, start)
1092 def find_enclosed(self, x1, y1, x2, y2):
1093 return self.find('enclosed', x1, y1, x2, y2)
1094 def find_overlapping(self, x1, y1, x2, y2):
1095 return self.find('overlapping', x1, y1, x2, y2)
1096 def find_withtag(self, tagOrId):
1097 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001098 def focus(self, *args):
1099 return self._do('focus', args)
1100 def gettags(self, *args):
1101 return self.tk.splitlist(self._do('gettags', args))
1102 def icursor(self, *args):
1103 self._do('icursor', args)
1104 def index(self, *args):
1105 return self.tk.getint(self._do('index', args))
1106 def insert(self, *args):
1107 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001108 def itemcget(self, tagOrId, option):
1109 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001110 def itemconfig(self, tagOrId, cnf=None, **kw):
1111 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001112 cnf = {}
1113 for x in self.tk.split(
1114 self._do('itemconfigure', (tagOrId))):
1115 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1116 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001117 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001118 x = self.tk.split(self._do('itemconfigure',
1119 (tagOrId, '-'+cnf,)))
1120 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001121 self._do('itemconfigure', (tagOrId,)
1122 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001123 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001124 def lower(self, *args):
1125 self._do('lower', args)
1126 def move(self, *args):
1127 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001128 def postscript(self, cnf={}, **kw):
1129 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001130 def tkraise(self, *args):
1131 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001132 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001133 def scale(self, *args):
1134 self._do('scale', args)
1135 def scan_mark(self, x, y):
1136 self.tk.call(self._w, 'scan', 'mark', x, y)
1137 def scan_dragto(self, x, y):
1138 self.tk.call(self._w, 'scan', 'dragto', x, y)
1139 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001140 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001141 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001142 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001143 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001144 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001145 def select_item(self):
1146 self.tk.call(self._w, 'select', 'item')
1147 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001148 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001149 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001150 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001151 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001152 if not args:
1153 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001154 apply(self.tk.call, (self._w, 'xview')+args)
1155 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001156 if not args:
1157 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001158 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001159
1160class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001161 def __init__(self, master=None, cnf={}, **kw):
1162 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001163 def deselect(self):
1164 self.tk.call(self._w, 'deselect')
1165 def flash(self):
1166 self.tk.call(self._w, 'flash')
1167 def invoke(self):
1168 self.tk.call(self._w, 'invoke')
1169 def select(self):
1170 self.tk.call(self._w, 'select')
1171 def toggle(self):
1172 self.tk.call(self._w, 'toggle')
1173
1174class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001175 def __init__(self, master=None, cnf={}, **kw):
1176 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001177 def delete(self, first, last=None):
1178 self.tk.call(self._w, 'delete', first, last)
1179 def get(self):
1180 return self.tk.call(self._w, 'get')
1181 def icursor(self, index):
1182 self.tk.call(self._w, 'icursor', index)
1183 def index(self, index):
1184 return self.tk.getint(self.tk.call(
1185 self._w, 'index', index))
1186 def insert(self, index, string):
1187 self.tk.call(self._w, 'insert', index, string)
1188 def scan_mark(self, x):
1189 self.tk.call(self._w, 'scan', 'mark', x)
1190 def scan_dragto(self, x):
1191 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001192 def selection_adjust(self, index):
1193 self.tk.call(self._w, 'selection', 'adjust', index)
1194 select_adjust = selection_adjust
1195 def selection_clear(self):
1196 self.tk.call(self._w, 'selection', 'clear')
1197 select_clear = selection_clear
1198 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001199 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001200 select_from = selection_from
1201 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001202 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001203 self.tk.call(self._w, 'selection', 'present'))
1204 select_present = selection_present
1205 def selection_range(self, start, end):
1206 self.tk.call(self._w, 'selection', 'range', start, end)
1207 select_range = selection_range
1208 def selection_to(self, index):
1209 self.tk.call(self._w, 'selection', 'to', index)
1210 select_to = selection_to
1211 def xview(self, index):
1212 self.tk.call(self._w, 'xview', index)
1213 def xview_moveto(self, fraction):
1214 self.tk.call(self._w, 'xview', 'moveto', fraction)
1215 def xview_scroll(self, number, what):
1216 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001217
1218class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001219 def __init__(self, master=None, cnf={}, **kw):
1220 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001221 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001222 if cnf.has_key('class_'):
1223 extra = ('-class', cnf['class_'])
1224 del cnf['class_']
1225 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001226 extra = ('-class', cnf['class'])
1227 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001228 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001229
1230class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001231 def __init__(self, master=None, cnf={}, **kw):
1232 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001233
Guido van Rossum18468821994-06-20 07:49:28 +00001234class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001235 def __init__(self, master=None, cnf={}, **kw):
1236 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001237 def activate(self, index):
1238 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001239 def bbox(self, *args):
1240 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001241 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001242 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001243 return self.tk.splitlist(self.tk.call(
1244 self._w, 'curselection'))
1245 def delete(self, first, last=None):
1246 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001247 def get(self, first, last=None):
1248 if last:
1249 return self.tk.splitlist(self.tk.call(
1250 self._w, 'get', first, last))
1251 else:
1252 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001253 def insert(self, index, *elements):
1254 apply(self.tk.call,
1255 (self._w, 'insert', index) + elements)
1256 def nearest(self, y):
1257 return self.tk.getint(self.tk.call(
1258 self._w, 'nearest', y))
1259 def scan_mark(self, x, y):
1260 self.tk.call(self._w, 'scan', 'mark', x, y)
1261 def scan_dragto(self, x, y):
1262 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001263 def see(self, index):
1264 self.tk.call(self._w, 'see', index)
1265 def index(self, index):
1266 i = self.tk.call(self._w, 'index', index)
1267 if i == 'none': return None
1268 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001269 def select_adjust(self, index):
1270 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001271 def select_anchor(self, index):
1272 self.tk.call(self._w, 'selection', 'anchor', index)
1273 def select_clear(self, first, last=None):
1274 self.tk.call(self._w,
1275 'selection', 'clear', first, last)
1276 def select_includes(self, index):
1277 return self.tk.getboolean(self.tk.call(
1278 self._w, 'selection', 'includes', index))
1279 def select_set(self, first, last=None):
1280 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001281 def size(self):
1282 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001283 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001284 if not what:
1285 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001286 apply(self.tk.call, (self._w, 'xview')+what)
1287 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001288 if not what:
1289 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001290 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001291
1292class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001293 def __init__(self, master=None, cnf={}, **kw):
1294 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001295 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001296 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001297 def tk_mbPost(self):
1298 self.tk.call('tk_mbPost', self._w)
1299 def tk_mbUnpost(self):
1300 self.tk.call('tk_mbUnpost')
1301 def tk_traverseToMenu(self, char):
1302 self.tk.call('tk_traverseToMenu', self._w, char)
1303 def tk_traverseWithinMenu(self, char):
1304 self.tk.call('tk_traverseWithinMenu', self._w, char)
1305 def tk_getMenuButtons(self):
1306 return self.tk.call('tk_getMenuButtons', self._w)
1307 def tk_nextMenu(self, count):
1308 self.tk.call('tk_nextMenu', count)
1309 def tk_nextMenuEntry(self, count):
1310 self.tk.call('tk_nextMenuEntry', count)
1311 def tk_invokeMenu(self):
1312 self.tk.call('tk_invokeMenu', self._w)
1313 def tk_firstMenu(self):
1314 self.tk.call('tk_firstMenu', self._w)
1315 def tk_mbButtonDown(self):
1316 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001317 def tk_popup(self, x, y, entry=""):
1318 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001319 def activate(self, index):
1320 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001321 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001322 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001323 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001324 def add_cascade(self, cnf={}, **kw):
1325 self.add('cascade', cnf or kw)
1326 def add_checkbutton(self, cnf={}, **kw):
1327 self.add('checkbutton', cnf or kw)
1328 def add_command(self, cnf={}, **kw):
1329 self.add('command', cnf or kw)
1330 def add_radiobutton(self, cnf={}, **kw):
1331 self.add('radiobutton', cnf or kw)
1332 def add_separator(self, cnf={}, **kw):
1333 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001334 def insert(self, index, itemType, cnf={}, **kw):
1335 apply(self.tk.call, (self._w, 'insert', index, itemType)
1336 + self._options(cnf, kw))
1337 def insert_cascade(self, index, cnf={}, **kw):
1338 self.insert(index, 'cascade', cnf or kw)
1339 def insert_checkbutton(self, index, cnf={}, **kw):
1340 self.insert(index, 'checkbutton', cnf or kw)
1341 def insert_command(self, index, cnf={}, **kw):
1342 self.insert(index, 'command', cnf or kw)
1343 def insert_radiobutton(self, index, cnf={}, **kw):
1344 self.insert(index, 'radiobutton', cnf or kw)
1345 def insert_separator(self, index, cnf={}, **kw):
1346 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001347 def delete(self, index1, index2=None):
1348 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001349 def entryconfig(self, index, cnf=None, **kw):
1350 if cnf is None and not kw:
1351 cnf = {}
1352 for x in self.tk.split(apply(self.tk.call,
1353 (self._w, 'entryconfigure', index))):
1354 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1355 return cnf
1356 if type(cnf) == StringType and not kw:
1357 x = self.tk.split(apply(self.tk.call,
1358 (self._w, 'entryconfigure', index, '-'+cnf)))
1359 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001360 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001361 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001362 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001363 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001364 i = self.tk.call(self._w, 'index', index)
1365 if i == 'none': return None
1366 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001367 def invoke(self, index):
1368 return self.tk.call(self._w, 'invoke', index)
1369 def post(self, x, y):
1370 self.tk.call(self._w, 'post', x, y)
1371 def unpost(self):
1372 self.tk.call(self._w, 'unpost')
1373 def yposition(self, index):
1374 return self.tk.getint(self.tk.call(
1375 self._w, 'yposition', index))
1376
1377class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001378 def __init__(self, master=None, cnf={}, **kw):
1379 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001380
1381class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001382 def __init__(self, master=None, cnf={}, **kw):
1383 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001384
1385class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001386 def __init__(self, master=None, cnf={}, **kw):
1387 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001388 def deselect(self):
1389 self.tk.call(self._w, 'deselect')
1390 def flash(self):
1391 self.tk.call(self._w, 'flash')
1392 def invoke(self):
1393 self.tk.call(self._w, 'invoke')
1394 def select(self):
1395 self.tk.call(self._w, 'select')
1396
1397class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001398 def __init__(self, master=None, cnf={}, **kw):
1399 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001400 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001401 value = self.tk.call(self._w, 'get')
1402 try:
1403 return self.tk.getint(value)
1404 except TclError:
1405 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001406 def set(self, value):
1407 self.tk.call(self._w, 'set', value)
1408
1409class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001410 def __init__(self, master=None, cnf={}, **kw):
1411 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001412 def activate(self, index):
1413 self.tk.call(self._w, 'activate', index)
1414 def delta(self, deltax, deltay):
1415 return self.getdouble(self.tk.call(
1416 self._w, 'delta', deltax, deltay))
1417 def fraction(self, x, y):
1418 return self.getdouble(self.tk.call(
1419 self._w, 'fraction', x, y))
1420 def identify(self, x, y):
1421 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001422 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001423 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001424 def set(self, *args):
1425 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001426
1427class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001428 def __init__(self, master=None, cnf={}, **kw):
1429 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001430 def bbox(self, *args):
1431 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001432 def tk_textSelectTo(self, index):
1433 self.tk.call('tk_textSelectTo', self._w, index)
1434 def tk_textBackspace(self):
1435 self.tk.call('tk_textBackspace', self._w)
1436 def tk_textIndexCloser(self, a, b, c):
1437 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1438 def tk_textResetAnchor(self, index):
1439 self.tk.call('tk_textResetAnchor', self._w, index)
1440 def compare(self, index1, op, index2):
1441 return self.tk.getboolean(self.tk.call(
1442 self._w, 'compare', index1, op, index2))
1443 def debug(self, boolean=None):
1444 return self.tk.getboolean(self.tk.call(
1445 self._w, 'debug', boolean))
1446 def delete(self, index1, index2=None):
1447 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001448 def dlineinfo(self, index):
1449 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001450 def get(self, index1, index2=None):
1451 return self.tk.call(self._w, 'get', index1, index2)
1452 def index(self, index):
1453 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001454 def insert(self, index, chars, *args):
1455 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001456 def mark_gravity(self, markName, direction=None):
1457 return apply(self.tk.call,
1458 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001459 def mark_names(self):
1460 return self.tk.splitlist(self.tk.call(
1461 self._w, 'mark', 'names'))
1462 def mark_set(self, markName, index):
1463 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001464 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001465 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001466 def scan_mark(self, x, y):
1467 self.tk.call(self._w, 'scan', 'mark', x, y)
1468 def scan_dragto(self, x, y):
1469 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001470 def search(self, pattern, index, stopindex=None,
1471 forwards=None, backwards=None, exact=None,
1472 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001473 args = [self._w, 'search']
1474 if forwards: args.append('-forwards')
1475 if backwards: args.append('-backwards')
1476 if exact: args.append('-exact')
1477 if regexp: args.append('-regexp')
1478 if nocase: args.append('-nocase')
1479 if count: args.append('-count'); args.append(count)
1480 if pattern[0] == '-': args.append('--')
1481 args.append(pattern)
1482 args.append(index)
1483 if stopindex: args.append(stopindex)
1484 return apply(self.tk.call, tuple(args))
1485 def see(self, index):
1486 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001487 def tag_add(self, tagName, index1, index2=None):
1488 self.tk.call(
1489 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001490 def tag_unbind(self, tagName, sequence):
1491 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001492 def tag_bind(self, tagName, sequence, func, add=None):
1493 return self._bind((self._w, 'tag', 'bind', tagName),
1494 sequence, func, add)
1495 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001496 if option[:1] != '-':
1497 option = '-' + option
1498 if option[-1:] == '_':
1499 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001500 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001501 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001502 if type(cnf) == StringType:
1503 x = self.tk.split(self.tk.call(
1504 self._w, 'tag', 'configure', tagName, '-'+cnf))
1505 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001506 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001507 (self._w, 'tag', 'configure', tagName)
1508 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001509 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001510 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001511 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001512 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001513 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001514 def tag_names(self, index=None):
1515 return self.tk.splitlist(
1516 self.tk.call(self._w, 'tag', 'names', index))
1517 def tag_nextrange(self, tagName, index1, index2=None):
1518 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001519 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001520 def tag_raise(self, tagName, aboveThis=None):
1521 self.tk.call(
1522 self._w, 'tag', 'raise', tagName, aboveThis)
1523 def tag_ranges(self, tagName):
1524 return self.tk.splitlist(self.tk.call(
1525 self._w, 'tag', 'ranges', tagName))
1526 def tag_remove(self, tagName, index1, index2=None):
1527 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001528 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001529 def window_cget(self, index, option):
1530 return self.tk.call(self._w, 'window', 'cget', index, option)
1531 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001532 if type(cnf) == StringType:
1533 x = self.tk.split(self.tk.call(
1534 self._w, 'window', 'configure',
1535 index, '-'+cnf))
1536 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001537 apply(self.tk.call,
1538 (self._w, 'window', 'configure', index)
1539 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001540 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001541 def window_create(self, index, cnf={}, **kw):
1542 apply(self.tk.call,
1543 (self._w, 'window', 'create', index)
1544 + self._options(cnf, kw))
1545 def window_names(self):
1546 return self.tk.splitlist(
1547 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001548 def xview(self, *what):
1549 if not what:
1550 return self._getdoubles(self.tk.call(self._w, 'xview'))
1551 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001552 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001553 if not what:
1554 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001555 apply(self.tk.call, (self._w, 'yview')+what)
1556 def yview_pickplace(self, *what):
1557 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001558
Guido van Rossum28574b51996-10-21 15:16:51 +00001559class _setit:
1560 def __init__(self, var, value):
1561 self.__value = value
1562 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001563 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001564 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001565
1566class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001567 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001568 kw = {"borderwidth": 2, "textvariable": variable,
1569 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1570 "highlightthickness": 2}
1571 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001572 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001573 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1574 self.menuname = menu._w
1575 menu.add_command(label=value, command=_setit(variable, value))
1576 for v in values:
1577 menu.add_command(label=v, command=_setit(variable, v))
1578 self["menu"] = menu
1579
1580 def __getitem__(self, name):
1581 if name == 'menu':
1582 return self.__menu
1583 return Widget.__getitem__(self, name)
1584
1585 def destroy(self):
1586 Menubutton.destroy(self)
1587 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001588
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001589class Image:
1590 def __init__(self, imgtype, name=None, cnf={}, **kw):
1591 self.name = None
1592 master = _default_root
1593 if not master: raise RuntimeError, 'Too early to create image'
1594 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001595 if not name:
1596 name = `id(self)`
1597 # The following is needed for systems where id(x)
1598 # can return a negative number, such as Linux/m68k:
1599 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001600 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1601 elif kw: cnf = kw
1602 options = ()
1603 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001604 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001605 v = self._register(v)
1606 options = options + ('-'+k, v)
1607 apply(self.tk.call,
1608 ('image', 'create', imgtype, name,) + options)
1609 self.name = name
1610 def __str__(self): return self.name
1611 def __del__(self):
1612 if self.name:
1613 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001614 def __setitem__(self, key, value):
1615 self.tk.call(self.name, 'configure', '-'+key, value)
1616 def __getitem__(self, key):
1617 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001618 def config(self, **kw):
1619 res = ()
1620 for k, v in _cnfmerge(kw).items():
1621 if v is not None:
1622 if k[-1] == '_': k = k[:-1]
1623 if callable(v):
1624 v = self._register(v)
1625 res = res + ('-'+k, v)
1626 apply(self.tk.call, (self.name, 'config') + res)
1627 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001628 def height(self):
1629 return self.tk.getint(
1630 self.tk.call('image', 'height', self.name))
1631 def type(self):
1632 return self.tk.call('image', 'type', self.name)
1633 def width(self):
1634 return self.tk.getint(
1635 self.tk.call('image', 'width', self.name))
1636
1637class PhotoImage(Image):
1638 def __init__(self, name=None, cnf={}, **kw):
1639 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001640 def blank(self):
1641 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001642 def cget(self, option):
1643 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001644 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001645 def __getitem__(self, key):
1646 return self.tk.call(self.name, 'cget', '-' + key)
1647 def copy(self):
1648 destImage = PhotoImage()
1649 self.tk.call(destImage, 'copy', self.name)
1650 return destImage
1651 def zoom(self,x,y=''):
1652 destImage = PhotoImage()
1653 if y=='': y=x
1654 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1655 return destImage
1656 def subsample(self,x,y=''):
1657 destImage = PhotoImage()
1658 if y=='': y=x
1659 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1660 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001661 def get(self, x, y):
1662 return self.tk.call(self.name, 'get', x, y)
1663 def put(self, data, to=None):
1664 args = (self.name, 'put', data)
1665 if to:
1666 args = args + to
1667 apply(self.tk.call, args)
1668 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001669 def write(self, filename, format=None, from_coords=None):
1670 args = (self.name, 'write', filename)
1671 if format:
1672 args = args + ('-format', format)
1673 if from_coords:
1674 args = args + ('-from',) + tuple(from_coords)
1675 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001676
1677class BitmapImage(Image):
1678 def __init__(self, name=None, cnf={}, **kw):
1679 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1680
1681def image_names(): return _default_root.tk.call('image', 'names')
1682def image_types(): return _default_root.tk.call('image', 'types')
1683
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001684######################################################################
1685# Extensions:
1686
1687class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001688 def __init__(self, master=None, cnf={}, **kw):
1689 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001690 self.bind('<Any-Enter>', self.tkButtonEnter)
1691 self.bind('<Any-Leave>', self.tkButtonLeave)
1692 self.bind('<1>', self.tkButtonDown)
1693 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001694
1695class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001696 def __init__(self, master=None, cnf={}, **kw):
1697 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001698 self.bind('<Any-Enter>', self.tkButtonEnter)
1699 self.bind('<Any-Leave>', self.tkButtonLeave)
1700 self.bind('<1>', self.tkButtonDown)
1701 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1702 self['fg'] = self['bg']
1703 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001704
Guido van Rossumc417ef81996-08-21 23:38:59 +00001705######################################################################
1706# Test:
1707
1708def _test():
1709 root = Tk()
1710 label = Label(root, text="Proof-of-existence test for Tk")
1711 label.pack()
1712 test = Button(root, text="Click me!",
1713 command=lambda root=root: root.test.config(
1714 text="[%s]" % root.test['text']))
1715 test.pack()
1716 root.test = test
1717 quit = Button(root, text="QUIT", command=root.destroy)
1718 quit.pack()
1719 root.mainloop()
1720
1721if __name__ == '__main__':
1722 _test()
1723
Guido van Rossum37dcab11996-05-16 16:00:19 +00001724
1725# Emacs cruft
1726# Local Variables:
1727# py-indent-offset: 8
1728# End: