blob: a19cf5054b40a0ca65922890543932f2d62409d6 [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
Guido van Rossum76f587b1997-01-21 23:22:03 +0000270 name = apply(self.tk.call,
271 ('selection', 'own') + self._options(kw))
272 if not name: return None
273 return self._nametowidget(name)
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(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000301 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000302 def winfo_containing(self, rootX, rootY, displayof=0):
303 args = ('winfo', 'containing') \
304 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000305 name = apply(self.tk.call, args)
306 if not name: return None
307 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000308 def winfo_depth(self):
309 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
310 def winfo_exists(self):
311 return self.tk.getint(
312 self.tk.call('winfo', 'exists', self._w))
313 def winfo_fpixels(self, number):
314 return self.tk.getdouble(self.tk.call(
315 'winfo', 'fpixels', self._w, number))
316 def winfo_geometry(self):
317 return self.tk.call('winfo', 'geometry', self._w)
318 def winfo_height(self):
319 return self.tk.getint(
320 self.tk.call('winfo', 'height', self._w))
321 def winfo_id(self):
322 return self.tk.getint(
323 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000324 def winfo_interps(self, displayof=0):
325 args = ('winfo', 'interps') + self._displayof(displayof)
326 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000327 def winfo_ismapped(self):
328 return self.tk.getint(
329 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000330 def winfo_manager(self):
331 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000332 def winfo_name(self):
333 return self.tk.call('winfo', 'name', self._w)
334 def winfo_parent(self):
335 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000336 def winfo_pathname(self, id, displayof=0):
337 args = ('winfo', 'pathname') \
338 + self._displayof(displayof) + (id,)
339 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000340 def winfo_pixels(self, number):
341 return self.tk.getint(
342 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000343 def winfo_pointerx(self):
344 return self.tk.getint(
345 self.tk.call('winfo', 'pointerx', self._w))
346 def winfo_pointerxy(self):
347 return self._getints(
348 self.tk.call('winfo', 'pointerxy', self._w))
349 def winfo_pointery(self):
350 return self.tk.getint(
351 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000352 def winfo_reqheight(self):
353 return self.tk.getint(
354 self.tk.call('winfo', 'reqheight', self._w))
355 def winfo_reqwidth(self):
356 return self.tk.getint(
357 self.tk.call('winfo', 'reqwidth', self._w))
358 def winfo_rgb(self, color):
359 return self._getints(
360 self.tk.call('winfo', 'rgb', self._w, color))
361 def winfo_rootx(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'rootx', self._w))
364 def winfo_rooty(self):
365 return self.tk.getint(
366 self.tk.call('winfo', 'rooty', self._w))
367 def winfo_screen(self):
368 return self.tk.call('winfo', 'screen', self._w)
369 def winfo_screencells(self):
370 return self.tk.getint(
371 self.tk.call('winfo', 'screencells', self._w))
372 def winfo_screendepth(self):
373 return self.tk.getint(
374 self.tk.call('winfo', 'screendepth', self._w))
375 def winfo_screenheight(self):
376 return self.tk.getint(
377 self.tk.call('winfo', 'screenheight', self._w))
378 def winfo_screenmmheight(self):
379 return self.tk.getint(
380 self.tk.call('winfo', 'screenmmheight', self._w))
381 def winfo_screenmmwidth(self):
382 return self.tk.getint(
383 self.tk.call('winfo', 'screenmmwidth', self._w))
384 def winfo_screenvisual(self):
385 return self.tk.call('winfo', 'screenvisual', self._w)
386 def winfo_screenwidth(self):
387 return self.tk.getint(
388 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000389 def winfo_server(self):
390 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000391 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000392 return self._nametowidget(self.tk.call(
393 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000394 def winfo_viewable(self):
395 return self.tk.getint(
396 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000397 def winfo_visual(self):
398 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000399 def winfo_visualid(self):
400 return self.tk.call('winfo', 'visualid', self._w)
401 def winfo_visualsavailable(self, includeids=0):
402 data = self.tk.split(
403 self.tk.call('winfo', 'visualsavailable', self._w,
404 includeids and 'includeids' or None))
405 def parseitem(x, self=self):
406 return x[:1] + tuple(map(self.tk.getint, x[1:]))
407 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000408 def winfo_vrootheight(self):
409 return self.tk.getint(
410 self.tk.call('winfo', 'vrootheight', self._w))
411 def winfo_vrootwidth(self):
412 return self.tk.getint(
413 self.tk.call('winfo', 'vrootwidth', self._w))
414 def winfo_vrootx(self):
415 return self.tk.getint(
416 self.tk.call('winfo', 'vrootx', self._w))
417 def winfo_vrooty(self):
418 return self.tk.getint(
419 self.tk.call('winfo', 'vrooty', self._w))
420 def winfo_width(self):
421 return self.tk.getint(
422 self.tk.call('winfo', 'width', self._w))
423 def winfo_x(self):
424 return self.tk.getint(
425 self.tk.call('winfo', 'x', self._w))
426 def winfo_y(self):
427 return self.tk.getint(
428 self.tk.call('winfo', 'y', self._w))
429 def update(self):
430 self.tk.call('update')
431 def update_idletasks(self):
432 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000433 def bindtags(self, tagList=None):
434 if tagList is None:
435 return self.tk.splitlist(
436 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000437 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000438 self.tk.call('bindtags', self._w, tagList)
439 def _bind(self, what, sequence, func, add):
440 if func:
441 cmd = ("%sset _tkinter_break [%s %s]\n"
442 'if {"$_tkinter_break" == "break"} break\n') \
443 % (add and '+' or '',
444 self._register(func, self._substitute),
445 _string.join(self._subst_format))
446 apply(self.tk.call, what + (sequence, cmd))
447 elif func == '':
448 apply(self.tk.call, what + (sequence, func))
449 else:
450 return apply(self.tk.call, what + (sequence,))
451 def bind(self, sequence=None, func=None, add=None):
452 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000453 def unbind(self, sequence):
454 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000455 def bind_all(self, sequence=None, func=None, add=None):
456 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000457 def unbind_all(self, sequence):
458 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000459 def bind_class(self, className, sequence=None, func=None, add=None):
460 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000461 def unbind_class(self, className, sequence):
462 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000463 def mainloop(self, n=0):
464 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000465 def quit(self):
466 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000467 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000468 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000469 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
470 def _getdoubles(self, string):
471 if not string: return None
472 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000473 def _getboolean(self, string):
474 if string:
475 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000476 def _displayof(self, displayof):
477 if displayof:
478 return ('-displayof', displayof)
479 if displayof is None:
480 return ('-displayof', self._w)
481 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000482 def _options(self, cnf, kw = None):
483 if kw:
484 cnf = _cnfmerge((cnf, kw))
485 else:
486 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000487 res = ()
488 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000489 if v is not None:
490 if k[-1] == '_': k = k[:-1]
491 if callable(v):
492 v = self._register(v)
493 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000494 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000495 def _nametowidget(self, name):
496 w = self
497 if name[0] == '.':
498 w = w._root()
499 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000500 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000501 while name:
502 i = find(name, '.')
503 if i >= 0:
504 name, tail = name[:i], name[i+1:]
505 else:
506 tail = ''
507 w = w.children[name]
508 name = tail
509 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000510 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000511 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000512 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000513 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000514 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000515 except AttributeError:
516 pass
517 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000518 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000519 except AttributeError:
520 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000521 self.tk.createcommand(name, f)
522 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000523 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000524 def _root(self):
525 w = self
526 while w.master: w = w.master
527 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000528 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000529 '%s', '%t', '%w', '%x', '%y',
530 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
531 def _substitute(self, *args):
532 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000533 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000534 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
535 # Missing: (a, c, d, m, o, v, B, R)
536 e = Event()
537 e.serial = tk.getint(nsign)
538 e.num = tk.getint(b)
539 try: e.focus = tk.getboolean(f)
540 except TclError: pass
541 e.height = tk.getint(h)
542 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000543 # For Visibility events, event state is a string and
544 # not an integer:
545 try:
546 e.state = tk.getint(s)
547 except TclError:
548 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000549 e.time = tk.getint(t)
550 e.width = tk.getint(w)
551 e.x = tk.getint(x)
552 e.y = tk.getint(y)
553 e.char = A
554 try: e.send_event = tk.getboolean(E)
555 except TclError: pass
556 e.keysym = K
557 e.keysym_num = tk.getint(N)
558 e.type = T
559 e.widget = self._nametowidget(W)
560 e.x_root = tk.getint(X)
561 e.y_root = tk.getint(Y)
562 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000563 def _report_exception(self):
564 import sys
565 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
566 root = self._root()
567 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000568
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000569class CallWrapper:
570 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000571 self.func = func
572 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000573 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000574 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000575 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000576 if self.subst:
577 args = apply(self.subst, args)
578 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000579 except SystemExit, msg:
580 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000581 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000582 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000583
584class Wm:
585 def aspect(self,
586 minNumer=None, minDenom=None,
587 maxNumer=None, maxDenom=None):
588 return self._getints(
589 self.tk.call('wm', 'aspect', self._w,
590 minNumer, minDenom,
591 maxNumer, maxDenom))
592 def client(self, name=None):
593 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000594 def colormapwindows(self, *wlist):
595 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
596 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000597 def command(self, value=None):
598 return self.tk.call('wm', 'command', self._w, value)
599 def deiconify(self):
600 return self.tk.call('wm', 'deiconify', self._w)
601 def focusmodel(self, model=None):
602 return self.tk.call('wm', 'focusmodel', self._w, model)
603 def frame(self):
604 return self.tk.call('wm', 'frame', self._w)
605 def geometry(self, newGeometry=None):
606 return self.tk.call('wm', 'geometry', self._w, newGeometry)
607 def grid(self,
608 baseWidht=None, baseHeight=None,
609 widthInc=None, heightInc=None):
610 return self._getints(self.tk.call(
611 'wm', 'grid', self._w,
612 baseWidht, baseHeight, widthInc, heightInc))
613 def group(self, pathName=None):
614 return self.tk.call('wm', 'group', self._w, pathName)
615 def iconbitmap(self, bitmap=None):
616 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
617 def iconify(self):
618 return self.tk.call('wm', 'iconify', self._w)
619 def iconmask(self, bitmap=None):
620 return self.tk.call('wm', 'iconmask', self._w, bitmap)
621 def iconname(self, newName=None):
622 return self.tk.call('wm', 'iconname', self._w, newName)
623 def iconposition(self, x=None, y=None):
624 return self._getints(self.tk.call(
625 'wm', 'iconposition', self._w, x, y))
626 def iconwindow(self, pathName=None):
627 return self.tk.call('wm', 'iconwindow', self._w, pathName)
628 def maxsize(self, width=None, height=None):
629 return self._getints(self.tk.call(
630 'wm', 'maxsize', self._w, width, height))
631 def minsize(self, width=None, height=None):
632 return self._getints(self.tk.call(
633 'wm', 'minsize', self._w, width, height))
634 def overrideredirect(self, boolean=None):
635 return self._getboolean(self.tk.call(
636 'wm', 'overrideredirect', self._w, boolean))
637 def positionfrom(self, who=None):
638 return self.tk.call('wm', 'positionfrom', self._w, who)
639 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000640 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000641 command = self._register(func)
642 else:
643 command = func
644 return self.tk.call(
645 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000646 def resizable(self, width=None, height=None):
647 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000648 def sizefrom(self, who=None):
649 return self.tk.call('wm', 'sizefrom', self._w, who)
650 def state(self):
651 return self.tk.call('wm', 'state', self._w)
652 def title(self, string=None):
653 return self.tk.call('wm', 'title', self._w, string)
654 def transient(self, master=None):
655 return self.tk.call('wm', 'transient', self._w, master)
656 def withdraw(self):
657 return self.tk.call('wm', 'withdraw', self._w)
658
659class Tk(Misc, Wm):
660 _w = '.'
661 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000662 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000663 self.master = None
664 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000665 if baseName is None:
666 import sys, os
667 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000668 baseName, ext = os.path.splitext(baseName)
669 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum37dcab11996-05-16 16:00:19 +0000670 self.tk = tkinter.create(screenName, baseName, className)
671 try:
672 # Disable event scanning except for Command-Period
673 import MacOS
674 MacOS.EnableAppswitch(0)
675 except ImportError:
676 pass
677 else:
678 # Work around nasty MacTk bug
679 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000680 # Version sanity checks
681 tk_version = self.tk.getvar('tk_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000682 if tk_version != tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000683 raise RuntimeError, \
684 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000685 % (tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000686 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000687 if tcl_version != tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000688 raise RuntimeError, \
689 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum37dcab11996-05-16 16:00:19 +0000690 % (tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000691 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000692 raise RuntimeError, \
693 "Tk 4.0 or higher is required; found Tk %s" \
694 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000695 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000696 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000697 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000698 if not _default_root:
699 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000700 def destroy(self):
701 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000702 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000703 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000704 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000705 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000706 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000707 if os.environ.has_key('HOME'): home = os.environ['HOME']
708 else: home = os.curdir
709 class_tcl = os.path.join(home, '.%s.tcl' % className)
710 class_py = os.path.join(home, '.%s.py' % className)
711 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
712 base_py = os.path.join(home, '.%s.py' % baseName)
713 dir = {'self': self}
714 exec 'from Tkinter import *' in dir
715 if os.path.isfile(class_tcl):
716 print 'source', `class_tcl`
717 self.tk.call('source', class_tcl)
718 if os.path.isfile(class_py):
719 print 'execfile', `class_py`
720 execfile(class_py, dir)
721 if os.path.isfile(base_tcl):
722 print 'source', `base_tcl`
723 self.tk.call('source', base_tcl)
724 if os.path.isfile(base_py):
725 print 'execfile', `base_py`
726 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000727 def report_callback_exception(self, exc, val, tb):
728 import traceback
729 print "Exception in Tkinter callback"
730 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000731
732class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000733 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000734 apply(self.tk.call,
735 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000736 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000737 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000738 pack = config
739 def __setitem__(self, key, value):
740 Pack.config({key: value})
741 def forget(self):
742 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000743 pack_forget = forget
744 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000745 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000746 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000747 dict = {}
748 for i in range(0, len(words), 2):
749 key = words[i][1:]
750 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000751 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000752 value = self._nametowidget(value)
753 dict[key] = value
754 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000755 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000756 _noarg_ = ['_noarg_']
757 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000758 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000759 return self._getboolean(self.tk.call(
760 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000761 else:
762 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000763 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000764 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000765 return map(self._nametowidget,
766 self.tk.splitlist(
767 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000768 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000769
770class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000771 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000772 for k in ['in_']:
773 if kw.has_key(k):
774 kw[k[:-1]] = kw[k]
775 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000776 apply(self.tk.call,
777 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000778 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000779 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000780 place = config
781 def __setitem__(self, key, value):
782 Place.config({key: value})
783 def forget(self):
784 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000785 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000786 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000787 words = self.tk.splitlist(
788 self.tk.call('place', 'info', self._w))
789 dict = {}
790 for i in range(0, len(words), 2):
791 key = words[i][1:]
792 value = words[i+1]
793 if value[:1] == '.':
794 value = self._nametowidget(value)
795 dict[key] = value
796 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000797 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000798 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000799 return map(self._nametowidget,
800 self.tk.splitlist(
801 self.tk.call(
802 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000804
Guido van Rossum37dcab11996-05-16 16:00:19 +0000805class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000806 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000807 def config(self, cnf={}, **kw):
808 apply(self.tk.call,
809 ('grid', 'configure', self._w)
810 + self._options(cnf, kw))
811 grid = config
812 def __setitem__(self, key, value):
813 Grid.config({key: value})
814 def bbox(self, column, row):
815 return self._getints(
816 self.tk.call(
817 'grid', 'bbox', self._w, column, row)) or None
818 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000819 def columnconfigure(self, index, cnf={}, **kw):
820 if type(cnf) is not DictionaryType and not kw:
821 options = self._options({cnf: None})
822 else:
823 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000824 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000825 ('grid', 'columnconfigure', self._w, index)
826 + options)
827 if options == ('-minsize', None):
828 return self.tk.getint(res) or None
829 elif options == ('-weight', None):
830 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000831 def forget(self):
832 self.tk.call('grid', 'forget', self._w)
833 grid_forget = forget
834 def info(self):
835 words = self.tk.splitlist(
836 self.tk.call('grid', 'info', self._w))
837 dict = {}
838 for i in range(0, len(words), 2):
839 key = words[i][1:]
840 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000841 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 value = self._nametowidget(value)
843 dict[key] = value
844 return dict
845 grid_info = info
846 def location(self, x, y):
847 return self._getints(
848 self.tk.call(
849 'grid', 'location', self._w, x, y)) or None
850 _noarg_ = ['_noarg_']
851 def propagate(self, flag=_noarg_):
852 if flag is Grid._noarg_:
853 return self._getboolean(self.tk.call(
854 'grid', 'propagate', self._w))
855 else:
856 self.tk.call('grid', 'propagate', self._w, flag)
857 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000858 def rowconfigure(self, index, cnf={}, **kw):
859 if type(cnf) is not DictionaryType and not kw:
860 options = self._options({cnf: None})
861 else:
862 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000863 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000864 ('grid', 'rowconfigure', self._w, index)
865 + options)
866 if options == ('-minsize', None):
867 return self.tk.getint(res) or None
868 elif options == ('-weight', None):
869 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000870 def size(self):
871 return self._getints(
872 self.tk.call('grid', 'size', self._w)) or None
873 def slaves(self, *args):
874 return map(self._nametowidget,
875 self.tk.splitlist(
876 apply(self.tk.call,
877 ('grid', 'slaves', self._w) + args)))
878 grid_slaves = slaves
879
880class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000881 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000882 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000883 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000884 if not _default_root:
885 _default_root = Tk()
886 master = _default_root
887 if not _default_root:
888 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000889 self.master = master
890 self.tk = master.tk
891 if cnf.has_key('name'):
892 name = cnf['name']
893 del cnf['name']
894 else:
895 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000896 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000897 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000898 self._w = '.' + name
899 else:
900 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000901 self.children = {}
902 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000903 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000904 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000905 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
906 if kw:
907 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000908 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000909 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000910 classes = []
911 for k in cnf.keys():
912 if type(k) is ClassType:
913 classes.append((k, cnf[k]))
914 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000915 apply(self.tk.call,
916 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000917 for k, v in classes:
918 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000919 def config(self, cnf=None, **kw):
920 # XXX ought to generalize this so tag_config etc. can use it
921 if kw:
922 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000923 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000924 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000925 if cnf is None:
926 cnf = {}
927 for x in self.tk.split(
928 self.tk.call(self._w, 'configure')):
929 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
930 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000931 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000932 x = self.tk.split(self.tk.call(
933 self._w, 'configure', '-'+cnf))
934 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000935 apply(self.tk.call, (self._w, 'configure')
936 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000937 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000938 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000939 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000940 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000941 def __setitem__(self, key, value):
942 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000943 def keys(self):
944 return map(lambda x: x[0][1:],
945 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000946 def __str__(self):
947 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000948 def destroy(self):
949 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000950 if self.master.children.has_key(self._name):
951 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000952 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000953 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000954 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000955
956class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000957 def __init__(self, master=None, cnf={}, **kw):
958 if kw:
959 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000960 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000961 for wmkey in ['screen', 'class_', 'class', 'visual',
962 'colormap']:
963 if cnf.has_key(wmkey):
964 val = cnf[wmkey]
965 # TBD: a hack needed because some keys
966 # are not valid as keyword arguments
967 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
968 else: opt = '-'+wmkey
969 extra = extra + (opt, val)
970 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000971 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000972 root = self._root()
973 self.iconname(root.iconname())
974 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000975
976class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000977 def __init__(self, master=None, cnf={}, **kw):
978 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000979 def tkButtonEnter(self, *dummy):
980 self.tk.call('tkButtonEnter', self._w)
981 def tkButtonLeave(self, *dummy):
982 self.tk.call('tkButtonLeave', self._w)
983 def tkButtonDown(self, *dummy):
984 self.tk.call('tkButtonDown', self._w)
985 def tkButtonUp(self, *dummy):
986 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000987 def tkButtonInvoke(self, *dummy):
988 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000989 def flash(self):
990 self.tk.call(self._w, 'flash')
991 def invoke(self):
992 self.tk.call(self._w, 'invoke')
993
994# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000995# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000996def AtEnd():
997 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000998def AtInsert(*args):
999 s = 'insert'
1000 for a in args:
1001 if a: s = s + (' ' + a)
1002 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001003def AtSelFirst():
1004 return 'sel.first'
1005def AtSelLast():
1006 return 'sel.last'
1007def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001008 if y is None:
1009 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001010 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001011 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001012
1013class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001014 def __init__(self, master=None, cnf={}, **kw):
1015 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001016 def addtag(self, *args):
1017 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001018 def addtag_above(self, newtag, tagOrId):
1019 self.addtag(newtag, 'above', tagOrId)
1020 def addtag_all(self, newtag):
1021 self.addtag(newtag, 'all')
1022 def addtag_below(self, newtag, tagOrId):
1023 self.addtag(newtag, 'below', tagOrId)
1024 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1025 self.addtag(newtag, 'closest', x, y, halo, start)
1026 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1027 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1028 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1029 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1030 def addtag_withtag(self, newtag, tagOrId):
1031 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001032 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001033 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001034 def tag_unbind(self, tagOrId, sequence):
1035 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001036 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001037 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001038 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001039 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001040 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001041 self._w, 'canvasx', screenx, gridspacing))
1042 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001043 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001044 self._w, 'canvasy', screeny, gridspacing))
1045 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001046 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001047 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001048 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001049 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001050 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001051 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001052 args = args[:-1]
1053 else:
1054 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001055 return self.tk.getint(apply(
1056 self.tk.call,
1057 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001058 + args + self._options(cnf, kw)))
1059 def create_arc(self, *args, **kw):
1060 return self._create('arc', args, kw)
1061 def create_bitmap(self, *args, **kw):
1062 return self._create('bitmap', args, kw)
1063 def create_image(self, *args, **kw):
1064 return self._create('image', args, kw)
1065 def create_line(self, *args, **kw):
1066 return self._create('line', args, kw)
1067 def create_oval(self, *args, **kw):
1068 return self._create('oval', args, kw)
1069 def create_polygon(self, *args, **kw):
1070 return self._create('polygon', args, kw)
1071 def create_rectangle(self, *args, **kw):
1072 return self._create('rectangle', args, kw)
1073 def create_text(self, *args, **kw):
1074 return self._create('text', args, kw)
1075 def create_window(self, *args, **kw):
1076 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001077 def dchars(self, *args):
1078 self._do('dchars', args)
1079 def delete(self, *args):
1080 self._do('delete', args)
1081 def dtag(self, *args):
1082 self._do('dtag', args)
1083 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001084 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001085 def find_above(self, tagOrId):
1086 return self.find('above', tagOrId)
1087 def find_all(self):
1088 return self.find('all')
1089 def find_below(self, tagOrId):
1090 return self.find('below', tagOrId)
1091 def find_closest(self, x, y, halo=None, start=None):
1092 return self.find('closest', x, y, halo, start)
1093 def find_enclosed(self, x1, y1, x2, y2):
1094 return self.find('enclosed', x1, y1, x2, y2)
1095 def find_overlapping(self, x1, y1, x2, y2):
1096 return self.find('overlapping', x1, y1, x2, y2)
1097 def find_withtag(self, tagOrId):
1098 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001099 def focus(self, *args):
1100 return self._do('focus', args)
1101 def gettags(self, *args):
1102 return self.tk.splitlist(self._do('gettags', args))
1103 def icursor(self, *args):
1104 self._do('icursor', args)
1105 def index(self, *args):
1106 return self.tk.getint(self._do('index', args))
1107 def insert(self, *args):
1108 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001109 def itemcget(self, tagOrId, option):
1110 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001111 def itemconfig(self, tagOrId, cnf=None, **kw):
1112 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001113 cnf = {}
1114 for x in self.tk.split(
1115 self._do('itemconfigure', (tagOrId))):
1116 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1117 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001118 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001119 x = self.tk.split(self._do('itemconfigure',
1120 (tagOrId, '-'+cnf,)))
1121 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001122 self._do('itemconfigure', (tagOrId,)
1123 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001124 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001125 def lower(self, *args):
1126 self._do('lower', args)
1127 def move(self, *args):
1128 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001129 def postscript(self, cnf={}, **kw):
1130 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001131 def tkraise(self, *args):
1132 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001133 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001134 def scale(self, *args):
1135 self._do('scale', args)
1136 def scan_mark(self, x, y):
1137 self.tk.call(self._w, 'scan', 'mark', x, y)
1138 def scan_dragto(self, x, y):
1139 self.tk.call(self._w, 'scan', 'dragto', x, y)
1140 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001141 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001142 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001143 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001144 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001145 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001146 def select_item(self):
1147 self.tk.call(self._w, 'select', 'item')
1148 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001149 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001151 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001152 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001153 if not args:
1154 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001155 apply(self.tk.call, (self._w, 'xview')+args)
1156 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001157 if not args:
1158 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001159 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001160
1161class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001162 def __init__(self, master=None, cnf={}, **kw):
1163 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001164 def deselect(self):
1165 self.tk.call(self._w, 'deselect')
1166 def flash(self):
1167 self.tk.call(self._w, 'flash')
1168 def invoke(self):
1169 self.tk.call(self._w, 'invoke')
1170 def select(self):
1171 self.tk.call(self._w, 'select')
1172 def toggle(self):
1173 self.tk.call(self._w, 'toggle')
1174
1175class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001176 def __init__(self, master=None, cnf={}, **kw):
1177 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001178 def delete(self, first, last=None):
1179 self.tk.call(self._w, 'delete', first, last)
1180 def get(self):
1181 return self.tk.call(self._w, 'get')
1182 def icursor(self, index):
1183 self.tk.call(self._w, 'icursor', index)
1184 def index(self, index):
1185 return self.tk.getint(self.tk.call(
1186 self._w, 'index', index))
1187 def insert(self, index, string):
1188 self.tk.call(self._w, 'insert', index, string)
1189 def scan_mark(self, x):
1190 self.tk.call(self._w, 'scan', 'mark', x)
1191 def scan_dragto(self, x):
1192 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001193 def selection_adjust(self, index):
1194 self.tk.call(self._w, 'selection', 'adjust', index)
1195 select_adjust = selection_adjust
1196 def selection_clear(self):
1197 self.tk.call(self._w, 'selection', 'clear')
1198 select_clear = selection_clear
1199 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001200 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001201 select_from = selection_from
1202 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001203 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001204 self.tk.call(self._w, 'selection', 'present'))
1205 select_present = selection_present
1206 def selection_range(self, start, end):
1207 self.tk.call(self._w, 'selection', 'range', start, end)
1208 select_range = selection_range
1209 def selection_to(self, index):
1210 self.tk.call(self._w, 'selection', 'to', index)
1211 select_to = selection_to
1212 def xview(self, index):
1213 self.tk.call(self._w, 'xview', index)
1214 def xview_moveto(self, fraction):
1215 self.tk.call(self._w, 'xview', 'moveto', fraction)
1216 def xview_scroll(self, number, what):
1217 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001218
1219class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001220 def __init__(self, master=None, cnf={}, **kw):
1221 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001222 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001223 if cnf.has_key('class_'):
1224 extra = ('-class', cnf['class_'])
1225 del cnf['class_']
1226 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001227 extra = ('-class', cnf['class'])
1228 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001229 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001230
1231class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001232 def __init__(self, master=None, cnf={}, **kw):
1233 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001234
Guido van Rossum18468821994-06-20 07:49:28 +00001235class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001236 def __init__(self, master=None, cnf={}, **kw):
1237 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001238 def activate(self, index):
1239 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001240 def bbox(self, *args):
1241 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001242 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001243 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001244 return self.tk.splitlist(self.tk.call(
1245 self._w, 'curselection'))
1246 def delete(self, first, last=None):
1247 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001248 def get(self, first, last=None):
1249 if last:
1250 return self.tk.splitlist(self.tk.call(
1251 self._w, 'get', first, last))
1252 else:
1253 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001254 def insert(self, index, *elements):
1255 apply(self.tk.call,
1256 (self._w, 'insert', index) + elements)
1257 def nearest(self, y):
1258 return self.tk.getint(self.tk.call(
1259 self._w, 'nearest', y))
1260 def scan_mark(self, x, y):
1261 self.tk.call(self._w, 'scan', 'mark', x, y)
1262 def scan_dragto(self, x, y):
1263 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001264 def see(self, index):
1265 self.tk.call(self._w, 'see', index)
1266 def index(self, index):
1267 i = self.tk.call(self._w, 'index', index)
1268 if i == 'none': return None
1269 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001270 def select_adjust(self, index):
1271 self.tk.call(self._w, 'select', 'adjust', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001272 def select_anchor(self, index):
1273 self.tk.call(self._w, 'selection', 'anchor', index)
1274 def select_clear(self, first, last=None):
1275 self.tk.call(self._w,
1276 'selection', 'clear', first, last)
1277 def select_includes(self, index):
1278 return self.tk.getboolean(self.tk.call(
1279 self._w, 'selection', 'includes', index))
1280 def select_set(self, first, last=None):
1281 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum18468821994-06-20 07:49:28 +00001282 def size(self):
1283 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001284 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001285 if not what:
1286 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001287 apply(self.tk.call, (self._w, 'xview')+what)
1288 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001289 if not what:
1290 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001291 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001292
1293class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001294 def __init__(self, master=None, cnf={}, **kw):
1295 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001296 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001297 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001298 def tk_mbPost(self):
1299 self.tk.call('tk_mbPost', self._w)
1300 def tk_mbUnpost(self):
1301 self.tk.call('tk_mbUnpost')
1302 def tk_traverseToMenu(self, char):
1303 self.tk.call('tk_traverseToMenu', self._w, char)
1304 def tk_traverseWithinMenu(self, char):
1305 self.tk.call('tk_traverseWithinMenu', self._w, char)
1306 def tk_getMenuButtons(self):
1307 return self.tk.call('tk_getMenuButtons', self._w)
1308 def tk_nextMenu(self, count):
1309 self.tk.call('tk_nextMenu', count)
1310 def tk_nextMenuEntry(self, count):
1311 self.tk.call('tk_nextMenuEntry', count)
1312 def tk_invokeMenu(self):
1313 self.tk.call('tk_invokeMenu', self._w)
1314 def tk_firstMenu(self):
1315 self.tk.call('tk_firstMenu', self._w)
1316 def tk_mbButtonDown(self):
1317 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001318 def tk_popup(self, x, y, entry=""):
1319 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001320 def activate(self, index):
1321 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001322 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001323 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001324 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001325 def add_cascade(self, cnf={}, **kw):
1326 self.add('cascade', cnf or kw)
1327 def add_checkbutton(self, cnf={}, **kw):
1328 self.add('checkbutton', cnf or kw)
1329 def add_command(self, cnf={}, **kw):
1330 self.add('command', cnf or kw)
1331 def add_radiobutton(self, cnf={}, **kw):
1332 self.add('radiobutton', cnf or kw)
1333 def add_separator(self, cnf={}, **kw):
1334 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001335 def insert(self, index, itemType, cnf={}, **kw):
1336 apply(self.tk.call, (self._w, 'insert', index, itemType)
1337 + self._options(cnf, kw))
1338 def insert_cascade(self, index, cnf={}, **kw):
1339 self.insert(index, 'cascade', cnf or kw)
1340 def insert_checkbutton(self, index, cnf={}, **kw):
1341 self.insert(index, 'checkbutton', cnf or kw)
1342 def insert_command(self, index, cnf={}, **kw):
1343 self.insert(index, 'command', cnf or kw)
1344 def insert_radiobutton(self, index, cnf={}, **kw):
1345 self.insert(index, 'radiobutton', cnf or kw)
1346 def insert_separator(self, index, cnf={}, **kw):
1347 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001348 def delete(self, index1, index2=None):
1349 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001350 def entryconfig(self, index, cnf=None, **kw):
1351 if cnf is None and not kw:
1352 cnf = {}
1353 for x in self.tk.split(apply(self.tk.call,
1354 (self._w, 'entryconfigure', index))):
1355 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1356 return cnf
1357 if type(cnf) == StringType and not kw:
1358 x = self.tk.split(apply(self.tk.call,
1359 (self._w, 'entryconfigure', index, '-'+cnf)))
1360 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001361 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001362 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001363 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001364 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001365 i = self.tk.call(self._w, 'index', index)
1366 if i == 'none': return None
1367 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001368 def invoke(self, index):
1369 return self.tk.call(self._w, 'invoke', index)
1370 def post(self, x, y):
1371 self.tk.call(self._w, 'post', x, y)
1372 def unpost(self):
1373 self.tk.call(self._w, 'unpost')
1374 def yposition(self, index):
1375 return self.tk.getint(self.tk.call(
1376 self._w, 'yposition', index))
1377
1378class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001379 def __init__(self, master=None, cnf={}, **kw):
1380 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001381
1382class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001383 def __init__(self, master=None, cnf={}, **kw):
1384 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001385
1386class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001387 def __init__(self, master=None, cnf={}, **kw):
1388 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001389 def deselect(self):
1390 self.tk.call(self._w, 'deselect')
1391 def flash(self):
1392 self.tk.call(self._w, 'flash')
1393 def invoke(self):
1394 self.tk.call(self._w, 'invoke')
1395 def select(self):
1396 self.tk.call(self._w, 'select')
1397
1398class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001399 def __init__(self, master=None, cnf={}, **kw):
1400 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001401 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001402 value = self.tk.call(self._w, 'get')
1403 try:
1404 return self.tk.getint(value)
1405 except TclError:
1406 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001407 def set(self, value):
1408 self.tk.call(self._w, 'set', value)
1409
1410class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001411 def __init__(self, master=None, cnf={}, **kw):
1412 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001413 def activate(self, index):
1414 self.tk.call(self._w, 'activate', index)
1415 def delta(self, deltax, deltay):
1416 return self.getdouble(self.tk.call(
1417 self._w, 'delta', deltax, deltay))
1418 def fraction(self, x, y):
1419 return self.getdouble(self.tk.call(
1420 self._w, 'fraction', x, y))
1421 def identify(self, x, y):
1422 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001423 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001424 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001425 def set(self, *args):
1426 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001427
1428class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001429 def __init__(self, master=None, cnf={}, **kw):
1430 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001431 def bbox(self, *args):
1432 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001433 def tk_textSelectTo(self, index):
1434 self.tk.call('tk_textSelectTo', self._w, index)
1435 def tk_textBackspace(self):
1436 self.tk.call('tk_textBackspace', self._w)
1437 def tk_textIndexCloser(self, a, b, c):
1438 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1439 def tk_textResetAnchor(self, index):
1440 self.tk.call('tk_textResetAnchor', self._w, index)
1441 def compare(self, index1, op, index2):
1442 return self.tk.getboolean(self.tk.call(
1443 self._w, 'compare', index1, op, index2))
1444 def debug(self, boolean=None):
1445 return self.tk.getboolean(self.tk.call(
1446 self._w, 'debug', boolean))
1447 def delete(self, index1, index2=None):
1448 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001449 def dlineinfo(self, index):
1450 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001451 def get(self, index1, index2=None):
1452 return self.tk.call(self._w, 'get', index1, index2)
1453 def index(self, index):
1454 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001455 def insert(self, index, chars, *args):
1456 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001457 def mark_gravity(self, markName, direction=None):
1458 return apply(self.tk.call,
1459 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001460 def mark_names(self):
1461 return self.tk.splitlist(self.tk.call(
1462 self._w, 'mark', 'names'))
1463 def mark_set(self, markName, index):
1464 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001465 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001466 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001467 def scan_mark(self, x, y):
1468 self.tk.call(self._w, 'scan', 'mark', x, y)
1469 def scan_dragto(self, x, y):
1470 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001471 def search(self, pattern, index, stopindex=None,
1472 forwards=None, backwards=None, exact=None,
1473 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001474 args = [self._w, 'search']
1475 if forwards: args.append('-forwards')
1476 if backwards: args.append('-backwards')
1477 if exact: args.append('-exact')
1478 if regexp: args.append('-regexp')
1479 if nocase: args.append('-nocase')
1480 if count: args.append('-count'); args.append(count)
1481 if pattern[0] == '-': args.append('--')
1482 args.append(pattern)
1483 args.append(index)
1484 if stopindex: args.append(stopindex)
1485 return apply(self.tk.call, tuple(args))
1486 def see(self, index):
1487 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001488 def tag_add(self, tagName, index1, index2=None):
1489 self.tk.call(
1490 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001491 def tag_unbind(self, tagName, sequence):
1492 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001493 def tag_bind(self, tagName, sequence, func, add=None):
1494 return self._bind((self._w, 'tag', 'bind', tagName),
1495 sequence, func, add)
1496 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001497 if option[:1] != '-':
1498 option = '-' + option
1499 if option[-1:] == '_':
1500 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001501 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001502 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001503 if type(cnf) == StringType:
1504 x = self.tk.split(self.tk.call(
1505 self._w, 'tag', 'configure', tagName, '-'+cnf))
1506 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001507 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001508 (self._w, 'tag', 'configure', tagName)
1509 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001510 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001511 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001512 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001513 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001514 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001515 def tag_names(self, index=None):
1516 return self.tk.splitlist(
1517 self.tk.call(self._w, 'tag', 'names', index))
1518 def tag_nextrange(self, tagName, index1, index2=None):
1519 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001520 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001521 def tag_raise(self, tagName, aboveThis=None):
1522 self.tk.call(
1523 self._w, 'tag', 'raise', tagName, aboveThis)
1524 def tag_ranges(self, tagName):
1525 return self.tk.splitlist(self.tk.call(
1526 self._w, 'tag', 'ranges', tagName))
1527 def tag_remove(self, tagName, index1, index2=None):
1528 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001529 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001530 def window_cget(self, index, option):
1531 return self.tk.call(self._w, 'window', 'cget', index, option)
1532 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001533 if type(cnf) == StringType:
1534 x = self.tk.split(self.tk.call(
1535 self._w, 'window', 'configure',
1536 index, '-'+cnf))
1537 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001538 apply(self.tk.call,
1539 (self._w, 'window', 'configure', index)
1540 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001541 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001542 def window_create(self, index, cnf={}, **kw):
1543 apply(self.tk.call,
1544 (self._w, 'window', 'create', index)
1545 + self._options(cnf, kw))
1546 def window_names(self):
1547 return self.tk.splitlist(
1548 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001549 def xview(self, *what):
1550 if not what:
1551 return self._getdoubles(self.tk.call(self._w, 'xview'))
1552 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001553 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001554 if not what:
1555 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001556 apply(self.tk.call, (self._w, 'yview')+what)
1557 def yview_pickplace(self, *what):
1558 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001559
Guido van Rossum28574b51996-10-21 15:16:51 +00001560class _setit:
1561 def __init__(self, var, value):
1562 self.__value = value
1563 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001564 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001565 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001566
1567class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001568 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001569 kw = {"borderwidth": 2, "textvariable": variable,
1570 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1571 "highlightthickness": 2}
1572 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001573 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001574 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1575 self.menuname = menu._w
1576 menu.add_command(label=value, command=_setit(variable, value))
1577 for v in values:
1578 menu.add_command(label=v, command=_setit(variable, v))
1579 self["menu"] = menu
1580
1581 def __getitem__(self, name):
1582 if name == 'menu':
1583 return self.__menu
1584 return Widget.__getitem__(self, name)
1585
1586 def destroy(self):
1587 Menubutton.destroy(self)
1588 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001589
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001590class Image:
1591 def __init__(self, imgtype, name=None, cnf={}, **kw):
1592 self.name = None
1593 master = _default_root
1594 if not master: raise RuntimeError, 'Too early to create image'
1595 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001596 if not name:
1597 name = `id(self)`
1598 # The following is needed for systems where id(x)
1599 # can return a negative number, such as Linux/m68k:
1600 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001601 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1602 elif kw: cnf = kw
1603 options = ()
1604 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001605 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001606 v = self._register(v)
1607 options = options + ('-'+k, v)
1608 apply(self.tk.call,
1609 ('image', 'create', imgtype, name,) + options)
1610 self.name = name
1611 def __str__(self): return self.name
1612 def __del__(self):
1613 if self.name:
1614 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001615 def __setitem__(self, key, value):
1616 self.tk.call(self.name, 'configure', '-'+key, value)
1617 def __getitem__(self, key):
1618 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001619 def config(self, **kw):
1620 res = ()
1621 for k, v in _cnfmerge(kw).items():
1622 if v is not None:
1623 if k[-1] == '_': k = k[:-1]
1624 if callable(v):
1625 v = self._register(v)
1626 res = res + ('-'+k, v)
1627 apply(self.tk.call, (self.name, 'config') + res)
1628 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001629 def height(self):
1630 return self.tk.getint(
1631 self.tk.call('image', 'height', self.name))
1632 def type(self):
1633 return self.tk.call('image', 'type', self.name)
1634 def width(self):
1635 return self.tk.getint(
1636 self.tk.call('image', 'width', self.name))
1637
1638class PhotoImage(Image):
1639 def __init__(self, name=None, cnf={}, **kw):
1640 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001641 def blank(self):
1642 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001643 def cget(self, option):
1644 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001645 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001646 def __getitem__(self, key):
1647 return self.tk.call(self.name, 'cget', '-' + key)
1648 def copy(self):
1649 destImage = PhotoImage()
1650 self.tk.call(destImage, 'copy', self.name)
1651 return destImage
1652 def zoom(self,x,y=''):
1653 destImage = PhotoImage()
1654 if y=='': y=x
1655 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1656 return destImage
1657 def subsample(self,x,y=''):
1658 destImage = PhotoImage()
1659 if y=='': y=x
1660 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1661 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001662 def get(self, x, y):
1663 return self.tk.call(self.name, 'get', x, y)
1664 def put(self, data, to=None):
1665 args = (self.name, 'put', data)
1666 if to:
1667 args = args + to
1668 apply(self.tk.call, args)
1669 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001670 def write(self, filename, format=None, from_coords=None):
1671 args = (self.name, 'write', filename)
1672 if format:
1673 args = args + ('-format', format)
1674 if from_coords:
1675 args = args + ('-from',) + tuple(from_coords)
1676 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001677
1678class BitmapImage(Image):
1679 def __init__(self, name=None, cnf={}, **kw):
1680 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1681
1682def image_names(): return _default_root.tk.call('image', 'names')
1683def image_types(): return _default_root.tk.call('image', 'types')
1684
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001685######################################################################
1686# Extensions:
1687
1688class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001689 def __init__(self, master=None, cnf={}, **kw):
1690 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001691 self.bind('<Any-Enter>', self.tkButtonEnter)
1692 self.bind('<Any-Leave>', self.tkButtonLeave)
1693 self.bind('<1>', self.tkButtonDown)
1694 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001695
1696class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001697 def __init__(self, master=None, cnf={}, **kw):
1698 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001699 self.bind('<Any-Enter>', self.tkButtonEnter)
1700 self.bind('<Any-Leave>', self.tkButtonLeave)
1701 self.bind('<1>', self.tkButtonDown)
1702 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1703 self['fg'] = self['bg']
1704 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001705
Guido van Rossumc417ef81996-08-21 23:38:59 +00001706######################################################################
1707# Test:
1708
1709def _test():
1710 root = Tk()
1711 label = Label(root, text="Proof-of-existence test for Tk")
1712 label.pack()
1713 test = Button(root, text="Click me!",
1714 command=lambda root=root: root.test.config(
1715 text="[%s]" % root.test['text']))
1716 test.pack()
1717 root.test = test
1718 quit = Button(root, text="QUIT", command=root.destroy)
1719 quit.pack()
1720 root.mainloop()
1721
1722if __name__ == '__main__':
1723 _test()
1724
Guido van Rossum37dcab11996-05-16 16:00:19 +00001725
1726# Emacs cruft
1727# Local Variables:
1728# py-indent-offset: 8
1729# End: