blob: c492d8d57286dfec266e64acb183ac80bfa3e0a1 [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
Guido van Rossum95806091997-02-15 18:33:24 +00005import _tkinter # If this fails your Python is not configured for Tk
6tkinter = _tkinter # b/w compat for export
7TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00008from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +00009from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000010import string; _string = string; del string
Guido van Rossum18468821994-06-20 07:49:28 +000011
Guido van Rossum95806091997-02-15 18:33:24 +000012TkVersion = _string.atof(_tkinter.TK_VERSION)
13TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000014
Guido van Rossum36269991996-05-16 17:11:27 +000015######################################################################
16# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
17# they are defined here (and not in Tkconstants):
18######################################################################
19if TkVersion >= 4.1:
20 READABLE = 2
21 WRITABLE = 4
22 EXCEPTION = 8
23else:
24 READABLE = 1
25 WRITABLE = 2
26 EXCEPTION = 4
27
28
Guido van Rossum2dcf5291994-07-06 09:23:20 +000029def _flatten(tuple):
30 res = ()
31 for item in tuple:
32 if type(item) in (TupleType, ListType):
33 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000034 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000035 res = res + (item,)
36 return res
37
38def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000039 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000040 return cnfs
41 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000042 return cnfs
43 else:
44 cnf = {}
45 for c in _flatten(cnfs):
46 for k, v in c.items():
47 cnf[k] = v
48 return cnf
49
50class Event:
51 pass
52
Guido van Rossumaec5dc91994-06-27 07:55:12 +000053_default_root = None
54
Guido van Rossum45853db1994-06-20 12:19:19 +000055def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000056 pass
57
Guido van Rossum97aeca11994-07-07 13:12:12 +000058def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000059 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000060
Guido van Rossumaec5dc91994-06-27 07:55:12 +000061_varnum = 0
62class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000063 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000064 def __init__(self, master=None):
65 global _default_root
66 global _varnum
67 if master:
68 self._tk = master.tk
69 else:
70 self._tk = _default_root.tk
71 self._name = 'PY_VAR' + `_varnum`
72 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000073 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000074 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000075 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076 def __str__(self):
77 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000079 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000080
81class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000082 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000083 def __init__(self, master=None):
84 Variable.__init__(self, master)
85 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000086 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000087
88class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000089 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000090 def __init__(self, master=None):
91 Variable.__init__(self, master)
92 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000093 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000094
95class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000096 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000097 def __init__(self, master=None):
98 Variable.__init__(self, master)
99 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000100 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000101
102class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000103 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000104 def __init__(self, master=None):
105 Variable.__init__(self, master)
106 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000107 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000108
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000109def mainloop(n=0):
110 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000111
112def getint(s):
113 return _default_root.tk.getint(s)
114
115def getdouble(s):
116 return _default_root.tk.getdouble(s)
117
118def getboolean(s):
119 return _default_root.tk.getboolean(s)
120
Guido van Rossum18468821994-06-20 07:49:28 +0000121class Misc:
122 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000123 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000124 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000125 def tk_bisque(self):
126 self.tk.call('tk_bisque')
127 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000128 apply(self.tk.call, ('tk_setPalette',)
129 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000130 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000131 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000132 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000133 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000134 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000135 def wait_window(self, window=None):
136 if window == None:
137 window = self
138 self.tk.call('tkwait', 'window', window._w)
139 def wait_visibility(self, window=None):
140 if window == None:
141 window = self
142 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000143 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000144 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000145 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000146 return self.tk.getvar(name)
147 def getint(self, s):
148 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000149 def getdouble(self, s):
150 return self.tk.getdouble(s)
151 def getboolean(self, s):
152 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000153 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000154 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000155 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000156 def focus_force(self):
157 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000158 def focus_get(self):
159 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000160 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000161 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000162 def focus_displayof(self):
163 name = self.tk.call('focus', '-displayof', self._w)
164 if name == 'none' or not name: return None
165 return self._nametowidget(name)
166 def focus_lastfor(self):
167 name = self.tk.call('focus', '-lastfor', self._w)
168 if name == 'none' or not name: return None
169 return self._nametowidget(name)
170 def tk_focusFollowsMouse(self):
171 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000172 def tk_focusNext(self):
173 name = self.tk.call('tk_focusNext', self._w)
174 if not name: return None
175 return self._nametowidget(name)
176 def tk_focusPrev(self):
177 name = self.tk.call('tk_focusPrev', self._w)
178 if not name: return None
179 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000180 def after(self, ms, func=None, *args):
181 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000182 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000183 self.tk.call('after', ms)
184 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000185 # XXX Disgusting hack to clean up after calling func
186 tmp = []
187 def callit(func=func, args=args, tk=self.tk, tmp=tmp):
188 try:
189 apply(func, args)
190 finally:
191 tk.deletecommand(tmp[0])
192 name = self._register(callit)
193 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000194 return self.tk.call('after', ms, name)
195 def after_idle(self, func, *args):
196 return apply(self.after, ('idle', func) + args)
197 def after_cancel(self, id):
198 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000199 def bell(self, displayof=0):
200 apply(self.tk.call, ('bell',) + self._displayof(displayof))
201 # Clipboard handling:
202 def clipboard_clear(self, **kw):
203 if not kw.has_key('displayof'): kw['displayof'] = self._w
204 apply(self.tk.call,
205 ('clipboard', 'clear') + self._options(kw))
206 def clipboard_append(self, string, **kw):
207 if not kw.has_key('displayof'): kw['displayof'] = self._w
208 apply(self.tk.call,
209 ('clipboard', 'append') + self._options(kw)
210 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000211 # XXX grab current w/o window argument
212 def grab_current(self):
213 name = self.tk.call('grab', 'current', self._w)
214 if not name: return None
215 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000216 def grab_release(self):
217 self.tk.call('grab', 'release', self._w)
218 def grab_set(self):
219 self.tk.call('grab', 'set', self._w)
220 def grab_set_global(self):
221 self.tk.call('grab', 'set', '-global', self._w)
222 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000223 status = self.tk.call('grab', 'status', self._w)
224 if status == 'none': status = None
225 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000226 def lower(self, belowThis=None):
227 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000228 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000229 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000230 def option_clear(self):
231 self.tk.call('option', 'clear')
232 def option_get(self, name, className):
233 return self.tk.call('option', 'get', self._w, name, className)
234 def option_readfile(self, fileName, priority = None):
235 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000236 def selection_clear(self, **kw):
237 if not kw.has_key('displayof'): kw['displayof'] = self._w
238 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
239 def selection_get(self, **kw):
240 if not kw.has_key('displayof'): kw['displayof'] = self._w
241 return apply(self.tk.call,
242 ('selection', 'get') + self._options(kw))
243 def selection_handle(self, command, **kw):
244 name = self._register(command)
245 apply(self.tk.call,
246 ('selection', 'handle') + self._options(kw)
247 + (self._w, name))
248 def selection_own(self, **kw):
249 "Become owner of X selection."
250 apply(self.tk.call,
251 ('selection', 'own') + self._options(kw) + (self._w,))
252 def selection_own_get(self, **kw):
253 "Find owner of X selection."
254 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000255 name = apply(self.tk.call,
256 ('selection', 'own') + self._options(kw))
257 if not name: return None
258 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000259 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000260 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000261 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000262 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000263 def tkraise(self, aboveThis=None):
264 self.tk.call('raise', self._w, aboveThis)
265 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000266 def colormodel(self, value=None):
267 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000268 def winfo_atom(self, name, displayof=0):
269 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
270 return self.tk.getint(apply(self.tk.call, args))
271 def winfo_atomname(self, id, displayof=0):
272 args = ('winfo', 'atomname') \
273 + self._displayof(displayof) + (id,)
274 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000275 def winfo_cells(self):
276 return self.tk.getint(
277 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000278 def winfo_children(self):
279 return map(self._nametowidget,
280 self.tk.splitlist(self.tk.call(
281 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000282 def winfo_class(self):
283 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000284 def winfo_colormapfull(self):
285 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000286 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000287 def winfo_containing(self, rootX, rootY, displayof=0):
288 args = ('winfo', 'containing') \
289 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000290 name = apply(self.tk.call, args)
291 if not name: return None
292 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000293 def winfo_depth(self):
294 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
295 def winfo_exists(self):
296 return self.tk.getint(
297 self.tk.call('winfo', 'exists', self._w))
298 def winfo_fpixels(self, number):
299 return self.tk.getdouble(self.tk.call(
300 'winfo', 'fpixels', self._w, number))
301 def winfo_geometry(self):
302 return self.tk.call('winfo', 'geometry', self._w)
303 def winfo_height(self):
304 return self.tk.getint(
305 self.tk.call('winfo', 'height', self._w))
306 def winfo_id(self):
307 return self.tk.getint(
308 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000309 def winfo_interps(self, displayof=0):
310 args = ('winfo', 'interps') + self._displayof(displayof)
311 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000312 def winfo_ismapped(self):
313 return self.tk.getint(
314 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000315 def winfo_manager(self):
316 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000317 def winfo_name(self):
318 return self.tk.call('winfo', 'name', self._w)
319 def winfo_parent(self):
320 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000321 def winfo_pathname(self, id, displayof=0):
322 args = ('winfo', 'pathname') \
323 + self._displayof(displayof) + (id,)
324 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000325 def winfo_pixels(self, number):
326 return self.tk.getint(
327 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000328 def winfo_pointerx(self):
329 return self.tk.getint(
330 self.tk.call('winfo', 'pointerx', self._w))
331 def winfo_pointerxy(self):
332 return self._getints(
333 self.tk.call('winfo', 'pointerxy', self._w))
334 def winfo_pointery(self):
335 return self.tk.getint(
336 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000337 def winfo_reqheight(self):
338 return self.tk.getint(
339 self.tk.call('winfo', 'reqheight', self._w))
340 def winfo_reqwidth(self):
341 return self.tk.getint(
342 self.tk.call('winfo', 'reqwidth', self._w))
343 def winfo_rgb(self, color):
344 return self._getints(
345 self.tk.call('winfo', 'rgb', self._w, color))
346 def winfo_rootx(self):
347 return self.tk.getint(
348 self.tk.call('winfo', 'rootx', self._w))
349 def winfo_rooty(self):
350 return self.tk.getint(
351 self.tk.call('winfo', 'rooty', self._w))
352 def winfo_screen(self):
353 return self.tk.call('winfo', 'screen', self._w)
354 def winfo_screencells(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'screencells', self._w))
357 def winfo_screendepth(self):
358 return self.tk.getint(
359 self.tk.call('winfo', 'screendepth', self._w))
360 def winfo_screenheight(self):
361 return self.tk.getint(
362 self.tk.call('winfo', 'screenheight', self._w))
363 def winfo_screenmmheight(self):
364 return self.tk.getint(
365 self.tk.call('winfo', 'screenmmheight', self._w))
366 def winfo_screenmmwidth(self):
367 return self.tk.getint(
368 self.tk.call('winfo', 'screenmmwidth', self._w))
369 def winfo_screenvisual(self):
370 return self.tk.call('winfo', 'screenvisual', self._w)
371 def winfo_screenwidth(self):
372 return self.tk.getint(
373 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000374 def winfo_server(self):
375 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000376 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000377 return self._nametowidget(self.tk.call(
378 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000379 def winfo_viewable(self):
380 return self.tk.getint(
381 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000382 def winfo_visual(self):
383 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000384 def winfo_visualid(self):
385 return self.tk.call('winfo', 'visualid', self._w)
386 def winfo_visualsavailable(self, includeids=0):
387 data = self.tk.split(
388 self.tk.call('winfo', 'visualsavailable', self._w,
389 includeids and 'includeids' or None))
390 def parseitem(x, self=self):
391 return x[:1] + tuple(map(self.tk.getint, x[1:]))
392 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000393 def winfo_vrootheight(self):
394 return self.tk.getint(
395 self.tk.call('winfo', 'vrootheight', self._w))
396 def winfo_vrootwidth(self):
397 return self.tk.getint(
398 self.tk.call('winfo', 'vrootwidth', self._w))
399 def winfo_vrootx(self):
400 return self.tk.getint(
401 self.tk.call('winfo', 'vrootx', self._w))
402 def winfo_vrooty(self):
403 return self.tk.getint(
404 self.tk.call('winfo', 'vrooty', self._w))
405 def winfo_width(self):
406 return self.tk.getint(
407 self.tk.call('winfo', 'width', self._w))
408 def winfo_x(self):
409 return self.tk.getint(
410 self.tk.call('winfo', 'x', self._w))
411 def winfo_y(self):
412 return self.tk.getint(
413 self.tk.call('winfo', 'y', self._w))
414 def update(self):
415 self.tk.call('update')
416 def update_idletasks(self):
417 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000418 def bindtags(self, tagList=None):
419 if tagList is None:
420 return self.tk.splitlist(
421 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000422 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000423 self.tk.call('bindtags', self._w, tagList)
424 def _bind(self, what, sequence, func, add):
425 if func:
426 cmd = ("%sset _tkinter_break [%s %s]\n"
427 'if {"$_tkinter_break" == "break"} break\n') \
428 % (add and '+' or '',
429 self._register(func, self._substitute),
430 _string.join(self._subst_format))
431 apply(self.tk.call, what + (sequence, cmd))
432 elif func == '':
433 apply(self.tk.call, what + (sequence, func))
434 else:
435 return apply(self.tk.call, what + (sequence,))
436 def bind(self, sequence=None, func=None, add=None):
437 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000438 def unbind(self, sequence):
439 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000440 def bind_all(self, sequence=None, func=None, add=None):
441 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000442 def unbind_all(self, sequence):
443 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000444 def bind_class(self, className, sequence=None, func=None, add=None):
445 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000446 def unbind_class(self, className, sequence):
447 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000448 def mainloop(self, n=0):
449 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000450 def quit(self):
451 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000452 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000453 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000454 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
455 def _getdoubles(self, string):
456 if not string: return None
457 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000458 def _getboolean(self, string):
459 if string:
460 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000461 def _displayof(self, displayof):
462 if displayof:
463 return ('-displayof', displayof)
464 if displayof is None:
465 return ('-displayof', self._w)
466 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000467 def _options(self, cnf, kw = None):
468 if kw:
469 cnf = _cnfmerge((cnf, kw))
470 else:
471 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000472 res = ()
473 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000474 if v is not None:
475 if k[-1] == '_': k = k[:-1]
476 if callable(v):
477 v = self._register(v)
478 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000479 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000480 def _nametowidget(self, name):
481 w = self
482 if name[0] == '.':
483 w = w._root()
484 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000485 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000486 while name:
487 i = find(name, '.')
488 if i >= 0:
489 name, tail = name[:i], name[i+1:]
490 else:
491 tail = ''
492 w = w.children[name]
493 name = tail
494 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000495 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000496 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000497 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000498 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000499 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000500 except AttributeError:
501 pass
502 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000503 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000504 except AttributeError:
505 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000506 self.tk.createcommand(name, f)
507 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000508 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000509 def _root(self):
510 w = self
511 while w.master: w = w.master
512 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000513 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000514 '%s', '%t', '%w', '%x', '%y',
515 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
516 def _substitute(self, *args):
517 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000518 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000519 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
520 # Missing: (a, c, d, m, o, v, B, R)
521 e = Event()
522 e.serial = tk.getint(nsign)
523 e.num = tk.getint(b)
524 try: e.focus = tk.getboolean(f)
525 except TclError: pass
526 e.height = tk.getint(h)
527 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000528 # For Visibility events, event state is a string and
529 # not an integer:
530 try:
531 e.state = tk.getint(s)
532 except TclError:
533 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000534 e.time = tk.getint(t)
535 e.width = tk.getint(w)
536 e.x = tk.getint(x)
537 e.y = tk.getint(y)
538 e.char = A
539 try: e.send_event = tk.getboolean(E)
540 except TclError: pass
541 e.keysym = K
542 e.keysym_num = tk.getint(N)
543 e.type = T
544 e.widget = self._nametowidget(W)
545 e.x_root = tk.getint(X)
546 e.y_root = tk.getint(Y)
547 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000548 def _report_exception(self):
549 import sys
550 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
551 root = self._root()
552 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000553
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000554class CallWrapper:
555 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000556 self.func = func
557 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000558 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000559 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000560 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000561 if self.subst:
562 args = apply(self.subst, args)
563 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000564 except SystemExit, msg:
565 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000566 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000567 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000568
569class Wm:
570 def aspect(self,
571 minNumer=None, minDenom=None,
572 maxNumer=None, maxDenom=None):
573 return self._getints(
574 self.tk.call('wm', 'aspect', self._w,
575 minNumer, minDenom,
576 maxNumer, maxDenom))
577 def client(self, name=None):
578 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000579 def colormapwindows(self, *wlist):
580 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
581 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000582 def command(self, value=None):
583 return self.tk.call('wm', 'command', self._w, value)
584 def deiconify(self):
585 return self.tk.call('wm', 'deiconify', self._w)
586 def focusmodel(self, model=None):
587 return self.tk.call('wm', 'focusmodel', self._w, model)
588 def frame(self):
589 return self.tk.call('wm', 'frame', self._w)
590 def geometry(self, newGeometry=None):
591 return self.tk.call('wm', 'geometry', self._w, newGeometry)
592 def grid(self,
593 baseWidht=None, baseHeight=None,
594 widthInc=None, heightInc=None):
595 return self._getints(self.tk.call(
596 'wm', 'grid', self._w,
597 baseWidht, baseHeight, widthInc, heightInc))
598 def group(self, pathName=None):
599 return self.tk.call('wm', 'group', self._w, pathName)
600 def iconbitmap(self, bitmap=None):
601 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
602 def iconify(self):
603 return self.tk.call('wm', 'iconify', self._w)
604 def iconmask(self, bitmap=None):
605 return self.tk.call('wm', 'iconmask', self._w, bitmap)
606 def iconname(self, newName=None):
607 return self.tk.call('wm', 'iconname', self._w, newName)
608 def iconposition(self, x=None, y=None):
609 return self._getints(self.tk.call(
610 'wm', 'iconposition', self._w, x, y))
611 def iconwindow(self, pathName=None):
612 return self.tk.call('wm', 'iconwindow', self._w, pathName)
613 def maxsize(self, width=None, height=None):
614 return self._getints(self.tk.call(
615 'wm', 'maxsize', self._w, width, height))
616 def minsize(self, width=None, height=None):
617 return self._getints(self.tk.call(
618 'wm', 'minsize', self._w, width, height))
619 def overrideredirect(self, boolean=None):
620 return self._getboolean(self.tk.call(
621 'wm', 'overrideredirect', self._w, boolean))
622 def positionfrom(self, who=None):
623 return self.tk.call('wm', 'positionfrom', self._w, who)
624 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000625 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000626 command = self._register(func)
627 else:
628 command = func
629 return self.tk.call(
630 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000631 def resizable(self, width=None, height=None):
632 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000633 def sizefrom(self, who=None):
634 return self.tk.call('wm', 'sizefrom', self._w, who)
635 def state(self):
636 return self.tk.call('wm', 'state', self._w)
637 def title(self, string=None):
638 return self.tk.call('wm', 'title', self._w, string)
639 def transient(self, master=None):
640 return self.tk.call('wm', 'transient', self._w, master)
641 def withdraw(self):
642 return self.tk.call('wm', 'withdraw', self._w)
643
644class Tk(Misc, Wm):
645 _w = '.'
646 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000647 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000648 self.master = None
649 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000650 if baseName is None:
651 import sys, os
652 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000653 baseName, ext = os.path.splitext(baseName)
654 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000655 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000656 try:
657 # Disable event scanning except for Command-Period
658 import MacOS
659 MacOS.EnableAppswitch(0)
660 except ImportError:
661 pass
662 else:
663 # Work around nasty MacTk bug
664 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000665 # Version sanity checks
666 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000667 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000668 raise RuntimeError, \
669 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000670 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000671 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000672 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000673 raise RuntimeError, \
674 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000675 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000676 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000677 raise RuntimeError, \
678 "Tk 4.0 or higher is required; found Tk %s" \
679 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000680 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000681 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000682 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000683 if not _default_root:
684 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000685 def destroy(self):
686 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000687 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000688 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000689 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000690 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000691 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000692 if os.environ.has_key('HOME'): home = os.environ['HOME']
693 else: home = os.curdir
694 class_tcl = os.path.join(home, '.%s.tcl' % className)
695 class_py = os.path.join(home, '.%s.py' % className)
696 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
697 base_py = os.path.join(home, '.%s.py' % baseName)
698 dir = {'self': self}
699 exec 'from Tkinter import *' in dir
700 if os.path.isfile(class_tcl):
701 print 'source', `class_tcl`
702 self.tk.call('source', class_tcl)
703 if os.path.isfile(class_py):
704 print 'execfile', `class_py`
705 execfile(class_py, dir)
706 if os.path.isfile(base_tcl):
707 print 'source', `base_tcl`
708 self.tk.call('source', base_tcl)
709 if os.path.isfile(base_py):
710 print 'execfile', `base_py`
711 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000712 def report_callback_exception(self, exc, val, tb):
713 import traceback
714 print "Exception in Tkinter callback"
715 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000716
717class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000718 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000719 apply(self.tk.call,
720 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000721 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000722 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000723 pack = config
724 def __setitem__(self, key, value):
725 Pack.config({key: value})
726 def forget(self):
727 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000728 pack_forget = forget
729 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000730 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000731 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000732 dict = {}
733 for i in range(0, len(words), 2):
734 key = words[i][1:]
735 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000736 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000737 value = self._nametowidget(value)
738 dict[key] = value
739 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000740 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000741 _noarg_ = ['_noarg_']
742 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000743 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000744 return self._getboolean(self.tk.call(
745 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000746 else:
747 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000748 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000749 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000750 return map(self._nametowidget,
751 self.tk.splitlist(
752 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000753 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000754
755class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000756 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000757 for k in ['in_']:
758 if kw.has_key(k):
759 kw[k[:-1]] = kw[k]
760 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000761 apply(self.tk.call,
762 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000763 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000764 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000765 place = config
766 def __setitem__(self, key, value):
767 Place.config({key: value})
768 def forget(self):
769 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000770 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000771 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000772 words = self.tk.splitlist(
773 self.tk.call('place', 'info', self._w))
774 dict = {}
775 for i in range(0, len(words), 2):
776 key = words[i][1:]
777 value = words[i+1]
778 if value[:1] == '.':
779 value = self._nametowidget(value)
780 dict[key] = value
781 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000782 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000783 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000784 return map(self._nametowidget,
785 self.tk.splitlist(
786 self.tk.call(
787 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000788 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000789
Guido van Rossum37dcab11996-05-16 16:00:19 +0000790class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000791 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000792 def config(self, cnf={}, **kw):
793 apply(self.tk.call,
794 ('grid', 'configure', self._w)
795 + self._options(cnf, kw))
796 grid = config
797 def __setitem__(self, key, value):
798 Grid.config({key: value})
799 def bbox(self, column, row):
800 return self._getints(
801 self.tk.call(
802 'grid', 'bbox', self._w, column, row)) or None
803 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000804 def columnconfigure(self, index, cnf={}, **kw):
805 if type(cnf) is not DictionaryType and not kw:
806 options = self._options({cnf: None})
807 else:
808 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000809 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000810 ('grid', 'columnconfigure', self._w, index)
811 + options)
812 if options == ('-minsize', None):
813 return self.tk.getint(res) or None
814 elif options == ('-weight', None):
815 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000816 def forget(self):
817 self.tk.call('grid', 'forget', self._w)
818 grid_forget = forget
819 def info(self):
820 words = self.tk.splitlist(
821 self.tk.call('grid', 'info', self._w))
822 dict = {}
823 for i in range(0, len(words), 2):
824 key = words[i][1:]
825 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000826 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000827 value = self._nametowidget(value)
828 dict[key] = value
829 return dict
830 grid_info = info
831 def location(self, x, y):
832 return self._getints(
833 self.tk.call(
834 'grid', 'location', self._w, x, y)) or None
835 _noarg_ = ['_noarg_']
836 def propagate(self, flag=_noarg_):
837 if flag is Grid._noarg_:
838 return self._getboolean(self.tk.call(
839 'grid', 'propagate', self._w))
840 else:
841 self.tk.call('grid', 'propagate', self._w, flag)
842 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000843 def rowconfigure(self, index, cnf={}, **kw):
844 if type(cnf) is not DictionaryType and not kw:
845 options = self._options({cnf: None})
846 else:
847 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000848 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000849 ('grid', 'rowconfigure', self._w, index)
850 + options)
851 if options == ('-minsize', None):
852 return self.tk.getint(res) or None
853 elif options == ('-weight', None):
854 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000855 def size(self):
856 return self._getints(
857 self.tk.call('grid', 'size', self._w)) or None
858 def slaves(self, *args):
859 return map(self._nametowidget,
860 self.tk.splitlist(
861 apply(self.tk.call,
862 ('grid', 'slaves', self._w) + args)))
863 grid_slaves = slaves
864
865class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000866 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000867 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000868 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000869 if not _default_root:
870 _default_root = Tk()
871 master = _default_root
872 if not _default_root:
873 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000874 self.master = master
875 self.tk = master.tk
876 if cnf.has_key('name'):
877 name = cnf['name']
878 del cnf['name']
879 else:
880 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000881 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000882 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000883 self._w = '.' + name
884 else:
885 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000886 self.children = {}
887 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000888 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000889 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000890 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
891 if kw:
892 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000893 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000894 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000895 classes = []
896 for k in cnf.keys():
897 if type(k) is ClassType:
898 classes.append((k, cnf[k]))
899 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000900 apply(self.tk.call,
901 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000902 for k, v in classes:
903 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000904 def config(self, cnf=None, **kw):
905 # XXX ought to generalize this so tag_config etc. can use it
906 if kw:
907 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000908 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000909 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000910 if cnf is None:
911 cnf = {}
912 for x in self.tk.split(
913 self.tk.call(self._w, 'configure')):
914 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
915 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000916 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000917 x = self.tk.split(self.tk.call(
918 self._w, 'configure', '-'+cnf))
919 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000920 apply(self.tk.call, (self._w, 'configure')
921 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000922 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000923 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000924 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000925 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000926 def __setitem__(self, key, value):
927 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000928 def keys(self):
929 return map(lambda x: x[0][1:],
930 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000931 def __str__(self):
932 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000933 def destroy(self):
934 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000935 if self.master.children.has_key(self._name):
936 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000937 self.tk.call('destroy', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000938 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000939 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000940
941class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000942 def __init__(self, master=None, cnf={}, **kw):
943 if kw:
944 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000945 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000946 for wmkey in ['screen', 'class_', 'class', 'visual',
947 'colormap']:
948 if cnf.has_key(wmkey):
949 val = cnf[wmkey]
950 # TBD: a hack needed because some keys
951 # are not valid as keyword arguments
952 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
953 else: opt = '-'+wmkey
954 extra = extra + (opt, val)
955 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000956 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000957 root = self._root()
958 self.iconname(root.iconname())
959 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000960
961class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000962 def __init__(self, master=None, cnf={}, **kw):
963 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000964 def tkButtonEnter(self, *dummy):
965 self.tk.call('tkButtonEnter', self._w)
966 def tkButtonLeave(self, *dummy):
967 self.tk.call('tkButtonLeave', self._w)
968 def tkButtonDown(self, *dummy):
969 self.tk.call('tkButtonDown', self._w)
970 def tkButtonUp(self, *dummy):
971 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000972 def tkButtonInvoke(self, *dummy):
973 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000974 def flash(self):
975 self.tk.call(self._w, 'flash')
976 def invoke(self):
977 self.tk.call(self._w, 'invoke')
978
979# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000980# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000981def AtEnd():
982 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000983def AtInsert(*args):
984 s = 'insert'
985 for a in args:
986 if a: s = s + (' ' + a)
987 return s
Guido van Rossum18468821994-06-20 07:49:28 +0000988def AtSelFirst():
989 return 'sel.first'
990def AtSelLast():
991 return 'sel.last'
992def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000993 if y is None:
994 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +0000995 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +0000996 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +0000997
998class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000999 def __init__(self, master=None, cnf={}, **kw):
1000 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001001 def addtag(self, *args):
1002 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001003 def addtag_above(self, newtag, tagOrId):
1004 self.addtag(newtag, 'above', tagOrId)
1005 def addtag_all(self, newtag):
1006 self.addtag(newtag, 'all')
1007 def addtag_below(self, newtag, tagOrId):
1008 self.addtag(newtag, 'below', tagOrId)
1009 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1010 self.addtag(newtag, 'closest', x, y, halo, start)
1011 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1012 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1013 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1014 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1015 def addtag_withtag(self, newtag, tagOrId):
1016 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001017 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001018 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001019 def tag_unbind(self, tagOrId, sequence):
1020 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001021 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001022 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001023 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001024 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001025 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001026 self._w, 'canvasx', screenx, gridspacing))
1027 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001028 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001029 self._w, 'canvasy', screeny, gridspacing))
1030 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001031 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001032 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001033 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001034 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001035 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001036 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001037 args = args[:-1]
1038 else:
1039 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001040 return self.tk.getint(apply(
1041 self.tk.call,
1042 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001043 + args + self._options(cnf, kw)))
1044 def create_arc(self, *args, **kw):
1045 return self._create('arc', args, kw)
1046 def create_bitmap(self, *args, **kw):
1047 return self._create('bitmap', args, kw)
1048 def create_image(self, *args, **kw):
1049 return self._create('image', args, kw)
1050 def create_line(self, *args, **kw):
1051 return self._create('line', args, kw)
1052 def create_oval(self, *args, **kw):
1053 return self._create('oval', args, kw)
1054 def create_polygon(self, *args, **kw):
1055 return self._create('polygon', args, kw)
1056 def create_rectangle(self, *args, **kw):
1057 return self._create('rectangle', args, kw)
1058 def create_text(self, *args, **kw):
1059 return self._create('text', args, kw)
1060 def create_window(self, *args, **kw):
1061 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001062 def dchars(self, *args):
1063 self._do('dchars', args)
1064 def delete(self, *args):
1065 self._do('delete', args)
1066 def dtag(self, *args):
1067 self._do('dtag', args)
1068 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001069 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001070 def find_above(self, tagOrId):
1071 return self.find('above', tagOrId)
1072 def find_all(self):
1073 return self.find('all')
1074 def find_below(self, tagOrId):
1075 return self.find('below', tagOrId)
1076 def find_closest(self, x, y, halo=None, start=None):
1077 return self.find('closest', x, y, halo, start)
1078 def find_enclosed(self, x1, y1, x2, y2):
1079 return self.find('enclosed', x1, y1, x2, y2)
1080 def find_overlapping(self, x1, y1, x2, y2):
1081 return self.find('overlapping', x1, y1, x2, y2)
1082 def find_withtag(self, tagOrId):
1083 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001084 def focus(self, *args):
1085 return self._do('focus', args)
1086 def gettags(self, *args):
1087 return self.tk.splitlist(self._do('gettags', args))
1088 def icursor(self, *args):
1089 self._do('icursor', args)
1090 def index(self, *args):
1091 return self.tk.getint(self._do('index', args))
1092 def insert(self, *args):
1093 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001094 def itemcget(self, tagOrId, option):
1095 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001096 def itemconfig(self, tagOrId, cnf=None, **kw):
1097 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001098 cnf = {}
1099 for x in self.tk.split(
1100 self._do('itemconfigure', (tagOrId))):
1101 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1102 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001103 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001104 x = self.tk.split(self._do('itemconfigure',
1105 (tagOrId, '-'+cnf,)))
1106 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001107 self._do('itemconfigure', (tagOrId,)
1108 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001109 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001110 def lower(self, *args):
1111 self._do('lower', args)
1112 def move(self, *args):
1113 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001114 def postscript(self, cnf={}, **kw):
1115 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001116 def tkraise(self, *args):
1117 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001118 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001119 def scale(self, *args):
1120 self._do('scale', args)
1121 def scan_mark(self, x, y):
1122 self.tk.call(self._w, 'scan', 'mark', x, y)
1123 def scan_dragto(self, x, y):
1124 self.tk.call(self._w, 'scan', 'dragto', x, y)
1125 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001126 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001128 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001129 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001130 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001131 def select_item(self):
1132 self.tk.call(self._w, 'select', 'item')
1133 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001134 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001135 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001136 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001137 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001138 if not args:
1139 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001140 apply(self.tk.call, (self._w, 'xview')+args)
1141 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001142 if not args:
1143 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001144 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001145
1146class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001147 def __init__(self, master=None, cnf={}, **kw):
1148 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001149 def deselect(self):
1150 self.tk.call(self._w, 'deselect')
1151 def flash(self):
1152 self.tk.call(self._w, 'flash')
1153 def invoke(self):
1154 self.tk.call(self._w, 'invoke')
1155 def select(self):
1156 self.tk.call(self._w, 'select')
1157 def toggle(self):
1158 self.tk.call(self._w, 'toggle')
1159
1160class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001161 def __init__(self, master=None, cnf={}, **kw):
1162 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001163 def delete(self, first, last=None):
1164 self.tk.call(self._w, 'delete', first, last)
1165 def get(self):
1166 return self.tk.call(self._w, 'get')
1167 def icursor(self, index):
1168 self.tk.call(self._w, 'icursor', index)
1169 def index(self, index):
1170 return self.tk.getint(self.tk.call(
1171 self._w, 'index', index))
1172 def insert(self, index, string):
1173 self.tk.call(self._w, 'insert', index, string)
1174 def scan_mark(self, x):
1175 self.tk.call(self._w, 'scan', 'mark', x)
1176 def scan_dragto(self, x):
1177 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001178 def selection_adjust(self, index):
1179 self.tk.call(self._w, 'selection', 'adjust', index)
1180 select_adjust = selection_adjust
1181 def selection_clear(self):
1182 self.tk.call(self._w, 'selection', 'clear')
1183 select_clear = selection_clear
1184 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001185 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001186 select_from = selection_from
1187 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001188 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001189 self.tk.call(self._w, 'selection', 'present'))
1190 select_present = selection_present
1191 def selection_range(self, start, end):
1192 self.tk.call(self._w, 'selection', 'range', start, end)
1193 select_range = selection_range
1194 def selection_to(self, index):
1195 self.tk.call(self._w, 'selection', 'to', index)
1196 select_to = selection_to
1197 def xview(self, index):
1198 self.tk.call(self._w, 'xview', index)
1199 def xview_moveto(self, fraction):
1200 self.tk.call(self._w, 'xview', 'moveto', fraction)
1201 def xview_scroll(self, number, what):
1202 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001203
1204class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001205 def __init__(self, master=None, cnf={}, **kw):
1206 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001207 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001208 if cnf.has_key('class_'):
1209 extra = ('-class', cnf['class_'])
1210 del cnf['class_']
1211 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001212 extra = ('-class', cnf['class'])
1213 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001214 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001215
1216class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001217 def __init__(self, master=None, cnf={}, **kw):
1218 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001219
Guido van Rossum18468821994-06-20 07:49:28 +00001220class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001221 def __init__(self, master=None, cnf={}, **kw):
1222 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001223 def activate(self, index):
1224 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001225 def bbox(self, *args):
1226 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001227 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001228 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001229 return self.tk.splitlist(self.tk.call(
1230 self._w, 'curselection'))
1231 def delete(self, first, last=None):
1232 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001233 def get(self, first, last=None):
1234 if last:
1235 return self.tk.splitlist(self.tk.call(
1236 self._w, 'get', first, last))
1237 else:
1238 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001239 def insert(self, index, *elements):
1240 apply(self.tk.call,
1241 (self._w, 'insert', index) + elements)
1242 def nearest(self, y):
1243 return self.tk.getint(self.tk.call(
1244 self._w, 'nearest', y))
1245 def scan_mark(self, x, y):
1246 self.tk.call(self._w, 'scan', 'mark', x, y)
1247 def scan_dragto(self, x, y):
1248 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001249 def see(self, index):
1250 self.tk.call(self._w, 'see', index)
1251 def index(self, index):
1252 i = self.tk.call(self._w, 'index', index)
1253 if i == 'none': return None
1254 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001255 def select_anchor(self, index):
1256 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001257 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001258 def select_clear(self, first, last=None):
1259 self.tk.call(self._w,
1260 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001261 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001262 def select_includes(self, index):
1263 return self.tk.getboolean(self.tk.call(
1264 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001265 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001266 def select_set(self, first, last=None):
1267 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001268 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001269 def size(self):
1270 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001271 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001272 if not what:
1273 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001274 apply(self.tk.call, (self._w, 'xview')+what)
1275 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001276 if not what:
1277 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001278 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001279
1280class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001281 def __init__(self, master=None, cnf={}, **kw):
1282 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001283 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001284 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001285 def tk_mbPost(self):
1286 self.tk.call('tk_mbPost', self._w)
1287 def tk_mbUnpost(self):
1288 self.tk.call('tk_mbUnpost')
1289 def tk_traverseToMenu(self, char):
1290 self.tk.call('tk_traverseToMenu', self._w, char)
1291 def tk_traverseWithinMenu(self, char):
1292 self.tk.call('tk_traverseWithinMenu', self._w, char)
1293 def tk_getMenuButtons(self):
1294 return self.tk.call('tk_getMenuButtons', self._w)
1295 def tk_nextMenu(self, count):
1296 self.tk.call('tk_nextMenu', count)
1297 def tk_nextMenuEntry(self, count):
1298 self.tk.call('tk_nextMenuEntry', count)
1299 def tk_invokeMenu(self):
1300 self.tk.call('tk_invokeMenu', self._w)
1301 def tk_firstMenu(self):
1302 self.tk.call('tk_firstMenu', self._w)
1303 def tk_mbButtonDown(self):
1304 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001305 def tk_popup(self, x, y, entry=""):
1306 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001307 def activate(self, index):
1308 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001309 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001310 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001311 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001312 def add_cascade(self, cnf={}, **kw):
1313 self.add('cascade', cnf or kw)
1314 def add_checkbutton(self, cnf={}, **kw):
1315 self.add('checkbutton', cnf or kw)
1316 def add_command(self, cnf={}, **kw):
1317 self.add('command', cnf or kw)
1318 def add_radiobutton(self, cnf={}, **kw):
1319 self.add('radiobutton', cnf or kw)
1320 def add_separator(self, cnf={}, **kw):
1321 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001322 def insert(self, index, itemType, cnf={}, **kw):
1323 apply(self.tk.call, (self._w, 'insert', index, itemType)
1324 + self._options(cnf, kw))
1325 def insert_cascade(self, index, cnf={}, **kw):
1326 self.insert(index, 'cascade', cnf or kw)
1327 def insert_checkbutton(self, index, cnf={}, **kw):
1328 self.insert(index, 'checkbutton', cnf or kw)
1329 def insert_command(self, index, cnf={}, **kw):
1330 self.insert(index, 'command', cnf or kw)
1331 def insert_radiobutton(self, index, cnf={}, **kw):
1332 self.insert(index, 'radiobutton', cnf or kw)
1333 def insert_separator(self, index, cnf={}, **kw):
1334 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001335 def delete(self, index1, index2=None):
1336 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001337 def entryconfig(self, index, cnf=None, **kw):
1338 if cnf is None and not kw:
1339 cnf = {}
1340 for x in self.tk.split(apply(self.tk.call,
1341 (self._w, 'entryconfigure', index))):
1342 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1343 return cnf
1344 if type(cnf) == StringType and not kw:
1345 x = self.tk.split(apply(self.tk.call,
1346 (self._w, 'entryconfigure', index, '-'+cnf)))
1347 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001348 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001349 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001350 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001351 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001352 i = self.tk.call(self._w, 'index', index)
1353 if i == 'none': return None
1354 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001355 def invoke(self, index):
1356 return self.tk.call(self._w, 'invoke', index)
1357 def post(self, x, y):
1358 self.tk.call(self._w, 'post', x, y)
1359 def unpost(self):
1360 self.tk.call(self._w, 'unpost')
1361 def yposition(self, index):
1362 return self.tk.getint(self.tk.call(
1363 self._w, 'yposition', index))
1364
1365class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001366 def __init__(self, master=None, cnf={}, **kw):
1367 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001368
1369class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001370 def __init__(self, master=None, cnf={}, **kw):
1371 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001372
1373class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001374 def __init__(self, master=None, cnf={}, **kw):
1375 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001376 def deselect(self):
1377 self.tk.call(self._w, 'deselect')
1378 def flash(self):
1379 self.tk.call(self._w, 'flash')
1380 def invoke(self):
1381 self.tk.call(self._w, 'invoke')
1382 def select(self):
1383 self.tk.call(self._w, 'select')
1384
1385class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001386 def __init__(self, master=None, cnf={}, **kw):
1387 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001388 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001389 value = self.tk.call(self._w, 'get')
1390 try:
1391 return self.tk.getint(value)
1392 except TclError:
1393 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001394 def set(self, value):
1395 self.tk.call(self._w, 'set', value)
1396
1397class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001398 def __init__(self, master=None, cnf={}, **kw):
1399 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001400 def activate(self, index):
1401 self.tk.call(self._w, 'activate', index)
1402 def delta(self, deltax, deltay):
1403 return self.getdouble(self.tk.call(
1404 self._w, 'delta', deltax, deltay))
1405 def fraction(self, x, y):
1406 return self.getdouble(self.tk.call(
1407 self._w, 'fraction', x, y))
1408 def identify(self, x, y):
1409 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001410 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001411 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001412 def set(self, *args):
1413 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001414
1415class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001416 def __init__(self, master=None, cnf={}, **kw):
1417 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001418 def bbox(self, *args):
1419 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001420 def tk_textSelectTo(self, index):
1421 self.tk.call('tk_textSelectTo', self._w, index)
1422 def tk_textBackspace(self):
1423 self.tk.call('tk_textBackspace', self._w)
1424 def tk_textIndexCloser(self, a, b, c):
1425 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1426 def tk_textResetAnchor(self, index):
1427 self.tk.call('tk_textResetAnchor', self._w, index)
1428 def compare(self, index1, op, index2):
1429 return self.tk.getboolean(self.tk.call(
1430 self._w, 'compare', index1, op, index2))
1431 def debug(self, boolean=None):
1432 return self.tk.getboolean(self.tk.call(
1433 self._w, 'debug', boolean))
1434 def delete(self, index1, index2=None):
1435 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001436 def dlineinfo(self, index):
1437 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001438 def get(self, index1, index2=None):
1439 return self.tk.call(self._w, 'get', index1, index2)
1440 def index(self, index):
1441 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001442 def insert(self, index, chars, *args):
1443 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001444 def mark_gravity(self, markName, direction=None):
1445 return apply(self.tk.call,
1446 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001447 def mark_names(self):
1448 return self.tk.splitlist(self.tk.call(
1449 self._w, 'mark', 'names'))
1450 def mark_set(self, markName, index):
1451 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001452 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001453 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001454 def scan_mark(self, x, y):
1455 self.tk.call(self._w, 'scan', 'mark', x, y)
1456 def scan_dragto(self, x, y):
1457 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001458 def search(self, pattern, index, stopindex=None,
1459 forwards=None, backwards=None, exact=None,
1460 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001461 args = [self._w, 'search']
1462 if forwards: args.append('-forwards')
1463 if backwards: args.append('-backwards')
1464 if exact: args.append('-exact')
1465 if regexp: args.append('-regexp')
1466 if nocase: args.append('-nocase')
1467 if count: args.append('-count'); args.append(count)
1468 if pattern[0] == '-': args.append('--')
1469 args.append(pattern)
1470 args.append(index)
1471 if stopindex: args.append(stopindex)
1472 return apply(self.tk.call, tuple(args))
1473 def see(self, index):
1474 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001475 def tag_add(self, tagName, index1, index2=None):
1476 self.tk.call(
1477 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001478 def tag_unbind(self, tagName, sequence):
1479 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001480 def tag_bind(self, tagName, sequence, func, add=None):
1481 return self._bind((self._w, 'tag', 'bind', tagName),
1482 sequence, func, add)
1483 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001484 if option[:1] != '-':
1485 option = '-' + option
1486 if option[-1:] == '_':
1487 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001488 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001489 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001490 if type(cnf) == StringType:
1491 x = self.tk.split(self.tk.call(
1492 self._w, 'tag', 'configure', tagName, '-'+cnf))
1493 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001494 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001495 (self._w, 'tag', 'configure', tagName)
1496 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001497 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001498 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001499 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001500 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001501 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001502 def tag_names(self, index=None):
1503 return self.tk.splitlist(
1504 self.tk.call(self._w, 'tag', 'names', index))
1505 def tag_nextrange(self, tagName, index1, index2=None):
1506 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001507 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001508 def tag_raise(self, tagName, aboveThis=None):
1509 self.tk.call(
1510 self._w, 'tag', 'raise', tagName, aboveThis)
1511 def tag_ranges(self, tagName):
1512 return self.tk.splitlist(self.tk.call(
1513 self._w, 'tag', 'ranges', tagName))
1514 def tag_remove(self, tagName, index1, index2=None):
1515 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001516 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001517 def window_cget(self, index, option):
1518 return self.tk.call(self._w, 'window', 'cget', index, option)
1519 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001520 if type(cnf) == StringType:
1521 x = self.tk.split(self.tk.call(
1522 self._w, 'window', 'configure',
1523 index, '-'+cnf))
1524 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001525 apply(self.tk.call,
1526 (self._w, 'window', 'configure', index)
1527 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001528 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001529 def window_create(self, index, cnf={}, **kw):
1530 apply(self.tk.call,
1531 (self._w, 'window', 'create', index)
1532 + self._options(cnf, kw))
1533 def window_names(self):
1534 return self.tk.splitlist(
1535 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001536 def xview(self, *what):
1537 if not what:
1538 return self._getdoubles(self.tk.call(self._w, 'xview'))
1539 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001540 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001541 if not what:
1542 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001543 apply(self.tk.call, (self._w, 'yview')+what)
1544 def yview_pickplace(self, *what):
1545 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001546
Guido van Rossum28574b51996-10-21 15:16:51 +00001547class _setit:
1548 def __init__(self, var, value):
1549 self.__value = value
1550 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001551 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001552 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001553
1554class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001555 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001556 kw = {"borderwidth": 2, "textvariable": variable,
1557 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1558 "highlightthickness": 2}
1559 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001560 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001561 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1562 self.menuname = menu._w
1563 menu.add_command(label=value, command=_setit(variable, value))
1564 for v in values:
1565 menu.add_command(label=v, command=_setit(variable, v))
1566 self["menu"] = menu
1567
1568 def __getitem__(self, name):
1569 if name == 'menu':
1570 return self.__menu
1571 return Widget.__getitem__(self, name)
1572
1573 def destroy(self):
1574 Menubutton.destroy(self)
1575 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001576
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001577class Image:
1578 def __init__(self, imgtype, name=None, cnf={}, **kw):
1579 self.name = None
1580 master = _default_root
1581 if not master: raise RuntimeError, 'Too early to create image'
1582 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001583 if not name:
1584 name = `id(self)`
1585 # The following is needed for systems where id(x)
1586 # can return a negative number, such as Linux/m68k:
1587 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001588 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1589 elif kw: cnf = kw
1590 options = ()
1591 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001592 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001593 v = self._register(v)
1594 options = options + ('-'+k, v)
1595 apply(self.tk.call,
1596 ('image', 'create', imgtype, name,) + options)
1597 self.name = name
1598 def __str__(self): return self.name
1599 def __del__(self):
1600 if self.name:
1601 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001602 def __setitem__(self, key, value):
1603 self.tk.call(self.name, 'configure', '-'+key, value)
1604 def __getitem__(self, key):
1605 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001606 def config(self, **kw):
1607 res = ()
1608 for k, v in _cnfmerge(kw).items():
1609 if v is not None:
1610 if k[-1] == '_': k = k[:-1]
1611 if callable(v):
1612 v = self._register(v)
1613 res = res + ('-'+k, v)
1614 apply(self.tk.call, (self.name, 'config') + res)
1615 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001616 def height(self):
1617 return self.tk.getint(
1618 self.tk.call('image', 'height', self.name))
1619 def type(self):
1620 return self.tk.call('image', 'type', self.name)
1621 def width(self):
1622 return self.tk.getint(
1623 self.tk.call('image', 'width', self.name))
1624
1625class PhotoImage(Image):
1626 def __init__(self, name=None, cnf={}, **kw):
1627 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001628 def blank(self):
1629 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001630 def cget(self, option):
1631 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001632 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001633 def __getitem__(self, key):
1634 return self.tk.call(self.name, 'cget', '-' + key)
1635 def copy(self):
1636 destImage = PhotoImage()
1637 self.tk.call(destImage, 'copy', self.name)
1638 return destImage
1639 def zoom(self,x,y=''):
1640 destImage = PhotoImage()
1641 if y=='': y=x
1642 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1643 return destImage
1644 def subsample(self,x,y=''):
1645 destImage = PhotoImage()
1646 if y=='': y=x
1647 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1648 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001649 def get(self, x, y):
1650 return self.tk.call(self.name, 'get', x, y)
1651 def put(self, data, to=None):
1652 args = (self.name, 'put', data)
1653 if to:
1654 args = args + to
1655 apply(self.tk.call, args)
1656 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001657 def write(self, filename, format=None, from_coords=None):
1658 args = (self.name, 'write', filename)
1659 if format:
1660 args = args + ('-format', format)
1661 if from_coords:
1662 args = args + ('-from',) + tuple(from_coords)
1663 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001664
1665class BitmapImage(Image):
1666 def __init__(self, name=None, cnf={}, **kw):
1667 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1668
1669def image_names(): return _default_root.tk.call('image', 'names')
1670def image_types(): return _default_root.tk.call('image', 'types')
1671
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001672######################################################################
1673# Extensions:
1674
1675class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001676 def __init__(self, master=None, cnf={}, **kw):
1677 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001678 self.bind('<Any-Enter>', self.tkButtonEnter)
1679 self.bind('<Any-Leave>', self.tkButtonLeave)
1680 self.bind('<1>', self.tkButtonDown)
1681 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001682
1683class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001684 def __init__(self, master=None, cnf={}, **kw):
1685 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001686 self.bind('<Any-Enter>', self.tkButtonEnter)
1687 self.bind('<Any-Leave>', self.tkButtonLeave)
1688 self.bind('<1>', self.tkButtonDown)
1689 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1690 self['fg'] = self['bg']
1691 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001692
Guido van Rossumc417ef81996-08-21 23:38:59 +00001693######################################################################
1694# Test:
1695
1696def _test():
1697 root = Tk()
1698 label = Label(root, text="Proof-of-existence test for Tk")
1699 label.pack()
1700 test = Button(root, text="Click me!",
1701 command=lambda root=root: root.test.config(
1702 text="[%s]" % root.test['text']))
1703 test.pack()
1704 root.test = test
1705 quit = Button(root, text="QUIT", command=root.destroy)
1706 quit.pack()
1707 root.mainloop()
1708
1709if __name__ == '__main__':
1710 _test()
1711
Guido van Rossum37dcab11996-05-16 16:00:19 +00001712
1713# Emacs cruft
1714# Local Variables:
1715# py-indent-offset: 8
1716# End: