blob: dbf59b1900bdab2f5fd3f96996c5fbd7bf8369d6 [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 Rossumd6615ab1997-08-05 02:35:01 +000015READABLE = _tkinter.READABLE
16WRITABLE = _tkinter.WRITABLE
17EXCEPTION = _tkinter.EXCEPTION
Guido van Rossum36269991996-05-16 17:11:27 +000018
19
Guido van Rossum2dcf5291994-07-06 09:23:20 +000020def _flatten(tuple):
21 res = ()
22 for item in tuple:
23 if type(item) in (TupleType, ListType):
24 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000025 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000026 res = res + (item,)
27 return res
28
29def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000030 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000031 return cnfs
32 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000033 return cnfs
34 else:
35 cnf = {}
36 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000037 try:
38 cnf.update(c)
39 except (AttributeError, TypeError), msg:
40 print "_cnfmerge: fallback due to:", msg
41 for k, v in c.items():
42 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000043 return cnf
44
45class Event:
46 pass
47
Guido van Rossumaec5dc91994-06-27 07:55:12 +000048_default_root = None
49
Guido van Rossum45853db1994-06-20 12:19:19 +000050def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000051 pass
52
Guido van Rossum97aeca11994-07-07 13:12:12 +000053def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000054 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000055
Guido van Rossumaec5dc91994-06-27 07:55:12 +000056_varnum = 0
57class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000058 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000059 def __init__(self, master=None):
60 global _default_root
61 global _varnum
62 if master:
63 self._tk = master.tk
64 else:
65 self._tk = _default_root.tk
66 self._name = 'PY_VAR' + `_varnum`
67 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000068 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000069 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000070 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000071 def __str__(self):
72 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000073 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000074 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000075
76class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000077 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 def __init__(self, master=None):
79 Variable.__init__(self, master)
80 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000081 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000082
83class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000084 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000085 def __init__(self, master=None):
86 Variable.__init__(self, master)
87 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000088 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000089
90class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000091 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000092 def __init__(self, master=None):
93 Variable.__init__(self, master)
94 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000095 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000096
97class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000098 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +000099 def __init__(self, master=None):
100 Variable.__init__(self, master)
101 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000102 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000103
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000104def mainloop(n=0):
105 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000106
107def getint(s):
108 return _default_root.tk.getint(s)
109
110def getdouble(s):
111 return _default_root.tk.getdouble(s)
112
113def getboolean(s):
114 return _default_root.tk.getboolean(s)
115
Guido van Rossum18468821994-06-20 07:49:28 +0000116class Misc:
Fred Drake526749b1997-05-03 04:16:23 +0000117 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000118 def destroy(self):
119 if self._tclCommands is not None:
120 for name in self._tclCommands:
121 #print '- Tkinter: deleted command', name
122 self.tk.deletecommand(name)
123 self._tclCommands = None
124 def deletecommand(self, name):
125 #print '- Tkinter: deleted command', name
126 self.tk.deletecommand(name)
127 index = self._tclCommands.index(name)
128 del self._tclCommands[index]
Guido van Rossum18468821994-06-20 07:49:28 +0000129 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000130 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000131 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000132 def tk_bisque(self):
133 self.tk.call('tk_bisque')
134 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000135 apply(self.tk.call, ('tk_setPalette',)
136 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000137 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000138 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000139 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000140 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000141 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000142 def wait_window(self, window=None):
143 if window == None:
144 window = self
145 self.tk.call('tkwait', 'window', window._w)
146 def wait_visibility(self, window=None):
147 if window == None:
148 window = self
149 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000150 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000151 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000152 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000153 return self.tk.getvar(name)
154 def getint(self, s):
155 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000156 def getdouble(self, s):
157 return self.tk.getdouble(s)
158 def getboolean(self, s):
159 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000160 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000161 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000162 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000163 def focus_force(self):
164 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000165 def focus_get(self):
166 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000167 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000168 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000169 def focus_displayof(self):
170 name = self.tk.call('focus', '-displayof', self._w)
171 if name == 'none' or not name: return None
172 return self._nametowidget(name)
173 def focus_lastfor(self):
174 name = self.tk.call('focus', '-lastfor', self._w)
175 if name == 'none' or not name: return None
176 return self._nametowidget(name)
177 def tk_focusFollowsMouse(self):
178 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000179 def tk_focusNext(self):
180 name = self.tk.call('tk_focusNext', self._w)
181 if not name: return None
182 return self._nametowidget(name)
183 def tk_focusPrev(self):
184 name = self.tk.call('tk_focusPrev', self._w)
185 if not name: return None
186 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000187 def after(self, ms, func=None, *args):
188 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000189 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000190 self.tk.call('after', ms)
191 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000192 # XXX Disgusting hack to clean up after calling func
193 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000194 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000195 try:
196 apply(func, args)
197 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000198 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000199 name = self._register(callit)
200 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000201 return self.tk.call('after', ms, name)
202 def after_idle(self, func, *args):
203 return apply(self.after, ('idle', func) + args)
204 def after_cancel(self, id):
205 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000206 def bell(self, displayof=0):
207 apply(self.tk.call, ('bell',) + self._displayof(displayof))
208 # Clipboard handling:
209 def clipboard_clear(self, **kw):
210 if not kw.has_key('displayof'): kw['displayof'] = self._w
211 apply(self.tk.call,
212 ('clipboard', 'clear') + self._options(kw))
213 def clipboard_append(self, string, **kw):
214 if not kw.has_key('displayof'): kw['displayof'] = self._w
215 apply(self.tk.call,
216 ('clipboard', 'append') + self._options(kw)
217 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000218 # XXX grab current w/o window argument
219 def grab_current(self):
220 name = self.tk.call('grab', 'current', self._w)
221 if not name: return None
222 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000223 def grab_release(self):
224 self.tk.call('grab', 'release', self._w)
225 def grab_set(self):
226 self.tk.call('grab', 'set', self._w)
227 def grab_set_global(self):
228 self.tk.call('grab', 'set', '-global', self._w)
229 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000230 status = self.tk.call('grab', 'status', self._w)
231 if status == 'none': status = None
232 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000233 def lower(self, belowThis=None):
234 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000235 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000236 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000237 def option_clear(self):
238 self.tk.call('option', 'clear')
239 def option_get(self, name, className):
240 return self.tk.call('option', 'get', self._w, name, className)
241 def option_readfile(self, fileName, priority = None):
242 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000243 def selection_clear(self, **kw):
244 if not kw.has_key('displayof'): kw['displayof'] = self._w
245 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
246 def selection_get(self, **kw):
247 if not kw.has_key('displayof'): kw['displayof'] = self._w
248 return apply(self.tk.call,
249 ('selection', 'get') + self._options(kw))
250 def selection_handle(self, command, **kw):
251 name = self._register(command)
252 apply(self.tk.call,
253 ('selection', 'handle') + self._options(kw)
254 + (self._w, name))
255 def selection_own(self, **kw):
256 "Become owner of X selection."
257 apply(self.tk.call,
258 ('selection', 'own') + self._options(kw) + (self._w,))
259 def selection_own_get(self, **kw):
260 "Find owner of X selection."
261 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000262 name = apply(self.tk.call,
263 ('selection', 'own') + self._options(kw))
264 if not name: return None
265 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000266 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000267 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000268 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000269 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000270 def tkraise(self, aboveThis=None):
271 self.tk.call('raise', self._w, aboveThis)
272 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000273 def colormodel(self, value=None):
274 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000275 def winfo_atom(self, name, displayof=0):
276 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
277 return self.tk.getint(apply(self.tk.call, args))
278 def winfo_atomname(self, id, displayof=0):
279 args = ('winfo', 'atomname') \
280 + self._displayof(displayof) + (id,)
281 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000282 def winfo_cells(self):
283 return self.tk.getint(
284 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000285 def winfo_children(self):
286 return map(self._nametowidget,
287 self.tk.splitlist(self.tk.call(
288 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000289 def winfo_class(self):
290 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000291 def winfo_colormapfull(self):
292 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000293 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000294 def winfo_containing(self, rootX, rootY, displayof=0):
295 args = ('winfo', 'containing') \
296 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000297 name = apply(self.tk.call, args)
298 if not name: return None
299 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000300 def winfo_depth(self):
301 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
302 def winfo_exists(self):
303 return self.tk.getint(
304 self.tk.call('winfo', 'exists', self._w))
305 def winfo_fpixels(self, number):
306 return self.tk.getdouble(self.tk.call(
307 'winfo', 'fpixels', self._w, number))
308 def winfo_geometry(self):
309 return self.tk.call('winfo', 'geometry', self._w)
310 def winfo_height(self):
311 return self.tk.getint(
312 self.tk.call('winfo', 'height', self._w))
313 def winfo_id(self):
314 return self.tk.getint(
315 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000316 def winfo_interps(self, displayof=0):
317 args = ('winfo', 'interps') + self._displayof(displayof)
318 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000319 def winfo_ismapped(self):
320 return self.tk.getint(
321 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000322 def winfo_manager(self):
323 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def winfo_name(self):
325 return self.tk.call('winfo', 'name', self._w)
326 def winfo_parent(self):
327 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000328 def winfo_pathname(self, id, displayof=0):
329 args = ('winfo', 'pathname') \
330 + self._displayof(displayof) + (id,)
331 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000332 def winfo_pixels(self, number):
333 return self.tk.getint(
334 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000335 def winfo_pointerx(self):
336 return self.tk.getint(
337 self.tk.call('winfo', 'pointerx', self._w))
338 def winfo_pointerxy(self):
339 return self._getints(
340 self.tk.call('winfo', 'pointerxy', self._w))
341 def winfo_pointery(self):
342 return self.tk.getint(
343 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000344 def winfo_reqheight(self):
345 return self.tk.getint(
346 self.tk.call('winfo', 'reqheight', self._w))
347 def winfo_reqwidth(self):
348 return self.tk.getint(
349 self.tk.call('winfo', 'reqwidth', self._w))
350 def winfo_rgb(self, color):
351 return self._getints(
352 self.tk.call('winfo', 'rgb', self._w, color))
353 def winfo_rootx(self):
354 return self.tk.getint(
355 self.tk.call('winfo', 'rootx', self._w))
356 def winfo_rooty(self):
357 return self.tk.getint(
358 self.tk.call('winfo', 'rooty', self._w))
359 def winfo_screen(self):
360 return self.tk.call('winfo', 'screen', self._w)
361 def winfo_screencells(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'screencells', self._w))
364 def winfo_screendepth(self):
365 return self.tk.getint(
366 self.tk.call('winfo', 'screendepth', self._w))
367 def winfo_screenheight(self):
368 return self.tk.getint(
369 self.tk.call('winfo', 'screenheight', self._w))
370 def winfo_screenmmheight(self):
371 return self.tk.getint(
372 self.tk.call('winfo', 'screenmmheight', self._w))
373 def winfo_screenmmwidth(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'screenmmwidth', self._w))
376 def winfo_screenvisual(self):
377 return self.tk.call('winfo', 'screenvisual', self._w)
378 def winfo_screenwidth(self):
379 return self.tk.getint(
380 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000381 def winfo_server(self):
382 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000383 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000384 return self._nametowidget(self.tk.call(
385 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000386 def winfo_viewable(self):
387 return self.tk.getint(
388 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000389 def winfo_visual(self):
390 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000391 def winfo_visualid(self):
392 return self.tk.call('winfo', 'visualid', self._w)
393 def winfo_visualsavailable(self, includeids=0):
394 data = self.tk.split(
395 self.tk.call('winfo', 'visualsavailable', self._w,
396 includeids and 'includeids' or None))
397 def parseitem(x, self=self):
398 return x[:1] + tuple(map(self.tk.getint, x[1:]))
399 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000400 def winfo_vrootheight(self):
401 return self.tk.getint(
402 self.tk.call('winfo', 'vrootheight', self._w))
403 def winfo_vrootwidth(self):
404 return self.tk.getint(
405 self.tk.call('winfo', 'vrootwidth', self._w))
406 def winfo_vrootx(self):
407 return self.tk.getint(
408 self.tk.call('winfo', 'vrootx', self._w))
409 def winfo_vrooty(self):
410 return self.tk.getint(
411 self.tk.call('winfo', 'vrooty', self._w))
412 def winfo_width(self):
413 return self.tk.getint(
414 self.tk.call('winfo', 'width', self._w))
415 def winfo_x(self):
416 return self.tk.getint(
417 self.tk.call('winfo', 'x', self._w))
418 def winfo_y(self):
419 return self.tk.getint(
420 self.tk.call('winfo', 'y', self._w))
421 def update(self):
422 self.tk.call('update')
423 def update_idletasks(self):
424 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000425 def bindtags(self, tagList=None):
426 if tagList is None:
427 return self.tk.splitlist(
428 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000429 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000430 self.tk.call('bindtags', self._w, tagList)
431 def _bind(self, what, sequence, func, add):
432 if func:
433 cmd = ("%sset _tkinter_break [%s %s]\n"
434 'if {"$_tkinter_break" == "break"} break\n') \
435 % (add and '+' or '',
436 self._register(func, self._substitute),
437 _string.join(self._subst_format))
438 apply(self.tk.call, what + (sequence, cmd))
439 elif func == '':
440 apply(self.tk.call, what + (sequence, func))
441 else:
442 return apply(self.tk.call, what + (sequence,))
443 def bind(self, sequence=None, func=None, add=None):
444 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000445 def unbind(self, sequence):
446 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000447 def bind_all(self, sequence=None, func=None, add=None):
448 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000449 def unbind_all(self, sequence):
450 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000451 def bind_class(self, className, sequence=None, func=None, add=None):
452 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000453 def unbind_class(self, className, sequence):
454 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000455 def mainloop(self, n=0):
456 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000457 def quit(self):
458 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000459 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000460 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000461 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
462 def _getdoubles(self, string):
463 if not string: return None
464 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000465 def _getboolean(self, string):
466 if string:
467 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000468 def _displayof(self, displayof):
469 if displayof:
470 return ('-displayof', displayof)
471 if displayof is None:
472 return ('-displayof', self._w)
473 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000474 def _options(self, cnf, kw = None):
475 if kw:
476 cnf = _cnfmerge((cnf, kw))
477 else:
478 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000479 res = ()
480 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000481 if v is not None:
482 if k[-1] == '_': k = k[:-1]
483 if callable(v):
484 v = self._register(v)
485 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000486 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000487 def _nametowidget(self, name):
488 w = self
489 if name[0] == '.':
490 w = w._root()
491 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000492 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000493 while name:
494 i = find(name, '.')
495 if i >= 0:
496 name, tail = name[:i], name[i+1:]
497 else:
498 tail = ''
499 w = w.children[name]
500 name = tail
501 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000502 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000503 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000504 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000505 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000506 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000507 except AttributeError:
508 pass
509 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000510 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000511 except AttributeError:
512 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000513 self.tk.createcommand(name, f)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000514 if self._tclCommands is None:
515 self._tclCommands = []
516 self._tclCommands.append(name)
517 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000518 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000519 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000520 def _root(self):
521 w = self
522 while w.master: w = w.master
523 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000524 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000525 '%s', '%t', '%w', '%x', '%y',
526 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
527 def _substitute(self, *args):
528 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000529 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000530 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
531 # Missing: (a, c, d, m, o, v, B, R)
532 e = Event()
533 e.serial = tk.getint(nsign)
534 e.num = tk.getint(b)
535 try: e.focus = tk.getboolean(f)
536 except TclError: pass
537 e.height = tk.getint(h)
538 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000539 # For Visibility events, event state is a string and
540 # not an integer:
541 try:
542 e.state = tk.getint(s)
543 except TclError:
544 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000545 e.time = tk.getint(t)
546 e.width = tk.getint(w)
547 e.x = tk.getint(x)
548 e.y = tk.getint(y)
549 e.char = A
550 try: e.send_event = tk.getboolean(E)
551 except TclError: pass
552 e.keysym = K
553 e.keysym_num = tk.getint(N)
554 e.type = T
555 e.widget = self._nametowidget(W)
556 e.x_root = tk.getint(X)
557 e.y_root = tk.getint(Y)
558 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000559 def _report_exception(self):
560 import sys
561 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
562 root = self._root()
563 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000564
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000565class CallWrapper:
566 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000567 self.func = func
568 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000569 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000570 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000571 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000572 if self.subst:
573 args = apply(self.subst, args)
574 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000575 except SystemExit, msg:
576 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000577 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000578 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000579
580class Wm:
581 def aspect(self,
582 minNumer=None, minDenom=None,
583 maxNumer=None, maxDenom=None):
584 return self._getints(
585 self.tk.call('wm', 'aspect', self._w,
586 minNumer, minDenom,
587 maxNumer, maxDenom))
588 def client(self, name=None):
589 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000590 def colormapwindows(self, *wlist):
591 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
592 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000593 def command(self, value=None):
594 return self.tk.call('wm', 'command', self._w, value)
595 def deiconify(self):
596 return self.tk.call('wm', 'deiconify', self._w)
597 def focusmodel(self, model=None):
598 return self.tk.call('wm', 'focusmodel', self._w, model)
599 def frame(self):
600 return self.tk.call('wm', 'frame', self._w)
601 def geometry(self, newGeometry=None):
602 return self.tk.call('wm', 'geometry', self._w, newGeometry)
603 def grid(self,
604 baseWidht=None, baseHeight=None,
605 widthInc=None, heightInc=None):
606 return self._getints(self.tk.call(
607 'wm', 'grid', self._w,
608 baseWidht, baseHeight, widthInc, heightInc))
609 def group(self, pathName=None):
610 return self.tk.call('wm', 'group', self._w, pathName)
611 def iconbitmap(self, bitmap=None):
612 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
613 def iconify(self):
614 return self.tk.call('wm', 'iconify', self._w)
615 def iconmask(self, bitmap=None):
616 return self.tk.call('wm', 'iconmask', self._w, bitmap)
617 def iconname(self, newName=None):
618 return self.tk.call('wm', 'iconname', self._w, newName)
619 def iconposition(self, x=None, y=None):
620 return self._getints(self.tk.call(
621 'wm', 'iconposition', self._w, x, y))
622 def iconwindow(self, pathName=None):
623 return self.tk.call('wm', 'iconwindow', self._w, pathName)
624 def maxsize(self, width=None, height=None):
625 return self._getints(self.tk.call(
626 'wm', 'maxsize', self._w, width, height))
627 def minsize(self, width=None, height=None):
628 return self._getints(self.tk.call(
629 'wm', 'minsize', self._w, width, height))
630 def overrideredirect(self, boolean=None):
631 return self._getboolean(self.tk.call(
632 'wm', 'overrideredirect', self._w, boolean))
633 def positionfrom(self, who=None):
634 return self.tk.call('wm', 'positionfrom', self._w, who)
635 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000636 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000637 command = self._register(func)
638 else:
639 command = func
640 return self.tk.call(
641 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000642 def resizable(self, width=None, height=None):
643 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000644 def sizefrom(self, who=None):
645 return self.tk.call('wm', 'sizefrom', self._w, who)
646 def state(self):
647 return self.tk.call('wm', 'state', self._w)
648 def title(self, string=None):
649 return self.tk.call('wm', 'title', self._w, string)
650 def transient(self, master=None):
651 return self.tk.call('wm', 'transient', self._w, master)
652 def withdraw(self):
653 return self.tk.call('wm', 'withdraw', self._w)
654
655class Tk(Misc, Wm):
656 _w = '.'
657 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000658 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000659 self.master = None
660 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000661 if baseName is None:
662 import sys, os
663 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000664 baseName, ext = os.path.splitext(baseName)
665 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000666 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000667 try:
668 # Disable event scanning except for Command-Period
669 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000670 try:
671 MacOS.SchedParams(1, 0)
672 except AttributeError:
673 # pre-1.5, use old routine
674 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000675 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 Rossum95806091997-02-15 18:33:24 +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 Rossum95806091997-02-15 18:33:24 +0000685 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000686 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +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 Rossum95806091997-02-15 18:33:24 +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 Rossum103cc6d1997-04-14 13:30:24 +0000703 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000704 global _default_root
705 if _default_root is self:
706 _default_root = None
Guido van Rossum18468821994-06-20 07:49:28 +0000707 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000708 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000709 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000710 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000711 if os.environ.has_key('HOME'): home = os.environ['HOME']
712 else: home = os.curdir
713 class_tcl = os.path.join(home, '.%s.tcl' % className)
714 class_py = os.path.join(home, '.%s.py' % className)
715 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
716 base_py = os.path.join(home, '.%s.py' % baseName)
717 dir = {'self': self}
718 exec 'from Tkinter import *' in dir
719 if os.path.isfile(class_tcl):
720 print 'source', `class_tcl`
721 self.tk.call('source', class_tcl)
722 if os.path.isfile(class_py):
723 print 'execfile', `class_py`
724 execfile(class_py, dir)
725 if os.path.isfile(base_tcl):
726 print 'source', `base_tcl`
727 self.tk.call('source', base_tcl)
728 if os.path.isfile(base_py):
729 print 'execfile', `base_py`
730 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000731 def report_callback_exception(self, exc, val, tb):
732 import traceback
733 print "Exception in Tkinter callback"
734 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000735
736class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000737 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000738 apply(self.tk.call,
739 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000740 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000741 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000742 pack = config
743 def __setitem__(self, key, value):
744 Pack.config({key: value})
745 def forget(self):
746 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000747 pack_forget = forget
748 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000749 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000750 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000751 dict = {}
752 for i in range(0, len(words), 2):
753 key = words[i][1:]
754 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000755 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000756 value = self._nametowidget(value)
757 dict[key] = value
758 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000759 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000760 _noarg_ = ['_noarg_']
761 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000762 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000763 return self._getboolean(self.tk.call(
764 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000765 else:
766 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000767 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000768 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000769 return map(self._nametowidget,
770 self.tk.splitlist(
771 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000772 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000773
774class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000775 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000776 for k in ['in_']:
777 if kw.has_key(k):
778 kw[k[:-1]] = kw[k]
779 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000780 apply(self.tk.call,
781 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000782 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000783 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000784 place = config
785 def __setitem__(self, key, value):
786 Place.config({key: value})
787 def forget(self):
788 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000789 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000790 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000791 words = self.tk.splitlist(
792 self.tk.call('place', 'info', self._w))
793 dict = {}
794 for i in range(0, len(words), 2):
795 key = words[i][1:]
796 value = words[i+1]
797 if value[:1] == '.':
798 value = self._nametowidget(value)
799 dict[key] = value
800 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000801 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000802 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000803 return map(self._nametowidget,
804 self.tk.splitlist(
805 self.tk.call(
806 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000807 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000808
Guido van Rossum37dcab11996-05-16 16:00:19 +0000809class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000810 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000811 def config(self, cnf={}, **kw):
812 apply(self.tk.call,
813 ('grid', 'configure', self._w)
814 + self._options(cnf, kw))
815 grid = config
816 def __setitem__(self, key, value):
817 Grid.config({key: value})
818 def bbox(self, column, row):
819 return self._getints(
820 self.tk.call(
821 'grid', 'bbox', self._w, column, row)) or None
822 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000823 def columnconfigure(self, index, cnf={}, **kw):
824 if type(cnf) is not DictionaryType and not kw:
825 options = self._options({cnf: None})
826 else:
827 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000828 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000829 ('grid', 'columnconfigure', self._w, index)
830 + options)
831 if options == ('-minsize', None):
832 return self.tk.getint(res) or None
833 elif options == ('-weight', None):
834 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000835 def forget(self):
836 self.tk.call('grid', 'forget', self._w)
837 grid_forget = forget
838 def info(self):
839 words = self.tk.splitlist(
840 self.tk.call('grid', 'info', self._w))
841 dict = {}
842 for i in range(0, len(words), 2):
843 key = words[i][1:]
844 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000845 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000846 value = self._nametowidget(value)
847 dict[key] = value
848 return dict
849 grid_info = info
850 def location(self, x, y):
851 return self._getints(
852 self.tk.call(
853 'grid', 'location', self._w, x, y)) or None
854 _noarg_ = ['_noarg_']
855 def propagate(self, flag=_noarg_):
856 if flag is Grid._noarg_:
857 return self._getboolean(self.tk.call(
858 'grid', 'propagate', self._w))
859 else:
860 self.tk.call('grid', 'propagate', self._w, flag)
861 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000862 def rowconfigure(self, index, cnf={}, **kw):
863 if type(cnf) is not DictionaryType and not kw:
864 options = self._options({cnf: None})
865 else:
866 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000867 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000868 ('grid', 'rowconfigure', self._w, index)
869 + options)
870 if options == ('-minsize', None):
871 return self.tk.getint(res) or None
872 elif options == ('-weight', None):
873 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000874 def size(self):
875 return self._getints(
876 self.tk.call('grid', 'size', self._w)) or None
877 def slaves(self, *args):
878 return map(self._nametowidget,
879 self.tk.splitlist(
880 apply(self.tk.call,
881 ('grid', 'slaves', self._w) + args)))
882 grid_slaves = slaves
883
884class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000885 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000886 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000887 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000888 if not _default_root:
889 _default_root = Tk()
890 master = _default_root
891 if not _default_root:
892 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000893 self.master = master
894 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000895 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000896 if cnf.has_key('name'):
897 name = cnf['name']
898 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000899 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000900 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000901 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000902 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000903 self._w = '.' + name
904 else:
905 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000906 self.children = {}
907 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000908 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000909 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000910 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
911 if kw:
912 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000913 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000914 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000915 classes = []
916 for k in cnf.keys():
917 if type(k) is ClassType:
918 classes.append((k, cnf[k]))
919 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000920 apply(self.tk.call,
921 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000922 for k, v in classes:
923 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000924 def config(self, cnf=None, **kw):
925 # XXX ought to generalize this so tag_config etc. can use it
926 if kw:
927 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000928 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000929 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000930 if cnf is None:
931 cnf = {}
932 for x in self.tk.split(
933 self.tk.call(self._w, 'configure')):
934 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
935 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000936 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000937 x = self.tk.split(self.tk.call(
938 self._w, 'configure', '-'+cnf))
939 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000940 apply(self.tk.call, (self._w, 'configure')
941 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000942 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000943 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000944 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000945 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000946 def __setitem__(self, key, value):
947 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000948 def keys(self):
949 return map(lambda x: x[0][1:],
950 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000951 def __str__(self):
952 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000953 def destroy(self):
954 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000955 if self.master.children.has_key(self._name):
956 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000957 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000958 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000959 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000960 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000961
962class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000963 def __init__(self, master=None, cnf={}, **kw):
964 if kw:
965 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000966 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000967 for wmkey in ['screen', 'class_', 'class', 'visual',
968 'colormap']:
969 if cnf.has_key(wmkey):
970 val = cnf[wmkey]
971 # TBD: a hack needed because some keys
972 # are not valid as keyword arguments
973 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
974 else: opt = '-'+wmkey
975 extra = extra + (opt, val)
976 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000977 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000978 root = self._root()
979 self.iconname(root.iconname())
980 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000981
982class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000983 def __init__(self, master=None, cnf={}, **kw):
984 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000985 def tkButtonEnter(self, *dummy):
986 self.tk.call('tkButtonEnter', self._w)
987 def tkButtonLeave(self, *dummy):
988 self.tk.call('tkButtonLeave', self._w)
989 def tkButtonDown(self, *dummy):
990 self.tk.call('tkButtonDown', self._w)
991 def tkButtonUp(self, *dummy):
992 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000993 def tkButtonInvoke(self, *dummy):
994 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000995 def flash(self):
996 self.tk.call(self._w, 'flash')
997 def invoke(self):
998 self.tk.call(self._w, 'invoke')
999
1000# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001001# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001002def AtEnd():
1003 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001004def AtInsert(*args):
1005 s = 'insert'
1006 for a in args:
1007 if a: s = s + (' ' + a)
1008 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001009def AtSelFirst():
1010 return 'sel.first'
1011def AtSelLast():
1012 return 'sel.last'
1013def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001014 if y is None:
1015 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001016 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001017 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001018
1019class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001020 def __init__(self, master=None, cnf={}, **kw):
1021 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001022 def addtag(self, *args):
1023 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001024 def addtag_above(self, newtag, tagOrId):
1025 self.addtag(newtag, 'above', tagOrId)
1026 def addtag_all(self, newtag):
1027 self.addtag(newtag, 'all')
1028 def addtag_below(self, newtag, tagOrId):
1029 self.addtag(newtag, 'below', tagOrId)
1030 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1031 self.addtag(newtag, 'closest', x, y, halo, start)
1032 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1033 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1034 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1035 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1036 def addtag_withtag(self, newtag, tagOrId):
1037 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001038 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001039 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001040 def tag_unbind(self, tagOrId, sequence):
1041 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001042 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001043 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001044 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001045 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001046 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001047 self._w, 'canvasx', screenx, gridspacing))
1048 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001049 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001050 self._w, 'canvasy', screeny, gridspacing))
1051 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001052 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001053 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001054 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001055 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001056 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001057 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001058 args = args[:-1]
1059 else:
1060 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001061 return self.tk.getint(apply(
1062 self.tk.call,
1063 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001064 + args + self._options(cnf, kw)))
1065 def create_arc(self, *args, **kw):
1066 return self._create('arc', args, kw)
1067 def create_bitmap(self, *args, **kw):
1068 return self._create('bitmap', args, kw)
1069 def create_image(self, *args, **kw):
1070 return self._create('image', args, kw)
1071 def create_line(self, *args, **kw):
1072 return self._create('line', args, kw)
1073 def create_oval(self, *args, **kw):
1074 return self._create('oval', args, kw)
1075 def create_polygon(self, *args, **kw):
1076 return self._create('polygon', args, kw)
1077 def create_rectangle(self, *args, **kw):
1078 return self._create('rectangle', args, kw)
1079 def create_text(self, *args, **kw):
1080 return self._create('text', args, kw)
1081 def create_window(self, *args, **kw):
1082 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001083 def dchars(self, *args):
1084 self._do('dchars', args)
1085 def delete(self, *args):
1086 self._do('delete', args)
1087 def dtag(self, *args):
1088 self._do('dtag', args)
1089 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001090 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001091 def find_above(self, tagOrId):
1092 return self.find('above', tagOrId)
1093 def find_all(self):
1094 return self.find('all')
1095 def find_below(self, tagOrId):
1096 return self.find('below', tagOrId)
1097 def find_closest(self, x, y, halo=None, start=None):
1098 return self.find('closest', x, y, halo, start)
1099 def find_enclosed(self, x1, y1, x2, y2):
1100 return self.find('enclosed', x1, y1, x2, y2)
1101 def find_overlapping(self, x1, y1, x2, y2):
1102 return self.find('overlapping', x1, y1, x2, y2)
1103 def find_withtag(self, tagOrId):
1104 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001105 def focus(self, *args):
1106 return self._do('focus', args)
1107 def gettags(self, *args):
1108 return self.tk.splitlist(self._do('gettags', args))
1109 def icursor(self, *args):
1110 self._do('icursor', args)
1111 def index(self, *args):
1112 return self.tk.getint(self._do('index', args))
1113 def insert(self, *args):
1114 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001115 def itemcget(self, tagOrId, option):
1116 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001117 def itemconfig(self, tagOrId, cnf=None, **kw):
1118 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001119 cnf = {}
1120 for x in self.tk.split(
1121 self._do('itemconfigure', (tagOrId))):
1122 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1123 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001124 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001125 x = self.tk.split(self._do('itemconfigure',
1126 (tagOrId, '-'+cnf,)))
1127 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001128 self._do('itemconfigure', (tagOrId,)
1129 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001130 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001131 def lower(self, *args):
1132 self._do('lower', args)
1133 def move(self, *args):
1134 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001135 def postscript(self, cnf={}, **kw):
1136 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001137 def tkraise(self, *args):
1138 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001139 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001140 def scale(self, *args):
1141 self._do('scale', args)
1142 def scan_mark(self, x, y):
1143 self.tk.call(self._w, 'scan', 'mark', x, y)
1144 def scan_dragto(self, x, y):
1145 self.tk.call(self._w, 'scan', 'dragto', x, y)
1146 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001147 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001148 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001149 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001151 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001152 def select_item(self):
1153 self.tk.call(self._w, 'select', 'item')
1154 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001155 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001156 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001157 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001158 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001159 if not args:
1160 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001161 apply(self.tk.call, (self._w, 'xview')+args)
1162 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001163 if not args:
1164 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001165 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001166
1167class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001168 def __init__(self, master=None, cnf={}, **kw):
1169 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001170 def deselect(self):
1171 self.tk.call(self._w, 'deselect')
1172 def flash(self):
1173 self.tk.call(self._w, 'flash')
1174 def invoke(self):
1175 self.tk.call(self._w, 'invoke')
1176 def select(self):
1177 self.tk.call(self._w, 'select')
1178 def toggle(self):
1179 self.tk.call(self._w, 'toggle')
1180
1181class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001182 def __init__(self, master=None, cnf={}, **kw):
1183 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001184 def delete(self, first, last=None):
1185 self.tk.call(self._w, 'delete', first, last)
1186 def get(self):
1187 return self.tk.call(self._w, 'get')
1188 def icursor(self, index):
1189 self.tk.call(self._w, 'icursor', index)
1190 def index(self, index):
1191 return self.tk.getint(self.tk.call(
1192 self._w, 'index', index))
1193 def insert(self, index, string):
1194 self.tk.call(self._w, 'insert', index, string)
1195 def scan_mark(self, x):
1196 self.tk.call(self._w, 'scan', 'mark', x)
1197 def scan_dragto(self, x):
1198 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001199 def selection_adjust(self, index):
1200 self.tk.call(self._w, 'selection', 'adjust', index)
1201 select_adjust = selection_adjust
1202 def selection_clear(self):
1203 self.tk.call(self._w, 'selection', 'clear')
1204 select_clear = selection_clear
1205 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001206 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001207 select_from = selection_from
1208 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001209 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001210 self.tk.call(self._w, 'selection', 'present'))
1211 select_present = selection_present
1212 def selection_range(self, start, end):
1213 self.tk.call(self._w, 'selection', 'range', start, end)
1214 select_range = selection_range
1215 def selection_to(self, index):
1216 self.tk.call(self._w, 'selection', 'to', index)
1217 select_to = selection_to
1218 def xview(self, index):
1219 self.tk.call(self._w, 'xview', index)
1220 def xview_moveto(self, fraction):
1221 self.tk.call(self._w, 'xview', 'moveto', fraction)
1222 def xview_scroll(self, number, what):
1223 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001224
1225class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001226 def __init__(self, master=None, cnf={}, **kw):
1227 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001228 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001229 if cnf.has_key('class_'):
1230 extra = ('-class', cnf['class_'])
1231 del cnf['class_']
1232 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001233 extra = ('-class', cnf['class'])
1234 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001235 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001236
1237class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001238 def __init__(self, master=None, cnf={}, **kw):
1239 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001240
Guido van Rossum18468821994-06-20 07:49:28 +00001241class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001242 def __init__(self, master=None, cnf={}, **kw):
1243 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001244 def activate(self, index):
1245 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001246 def bbox(self, *args):
1247 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001248 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001249 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001250 return self.tk.splitlist(self.tk.call(
1251 self._w, 'curselection'))
1252 def delete(self, first, last=None):
1253 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001254 def get(self, first, last=None):
1255 if last:
1256 return self.tk.splitlist(self.tk.call(
1257 self._w, 'get', first, last))
1258 else:
1259 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001260 def insert(self, index, *elements):
1261 apply(self.tk.call,
1262 (self._w, 'insert', index) + elements)
1263 def nearest(self, y):
1264 return self.tk.getint(self.tk.call(
1265 self._w, 'nearest', y))
1266 def scan_mark(self, x, y):
1267 self.tk.call(self._w, 'scan', 'mark', x, y)
1268 def scan_dragto(self, x, y):
1269 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001270 def see(self, index):
1271 self.tk.call(self._w, 'see', index)
1272 def index(self, index):
1273 i = self.tk.call(self._w, 'index', index)
1274 if i == 'none': return None
1275 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001276 def select_anchor(self, index):
1277 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001278 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001279 def select_clear(self, first, last=None):
1280 self.tk.call(self._w,
1281 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001282 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001283 def select_includes(self, index):
1284 return self.tk.getboolean(self.tk.call(
1285 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001286 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001287 def select_set(self, first, last=None):
1288 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001289 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001290 def size(self):
1291 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001292 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001293 if not what:
1294 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001295 apply(self.tk.call, (self._w, 'xview')+what)
1296 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001297 if not what:
1298 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001299 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001300
1301class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001302 def __init__(self, master=None, cnf={}, **kw):
1303 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001304 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001305 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001306 def tk_mbPost(self):
1307 self.tk.call('tk_mbPost', self._w)
1308 def tk_mbUnpost(self):
1309 self.tk.call('tk_mbUnpost')
1310 def tk_traverseToMenu(self, char):
1311 self.tk.call('tk_traverseToMenu', self._w, char)
1312 def tk_traverseWithinMenu(self, char):
1313 self.tk.call('tk_traverseWithinMenu', self._w, char)
1314 def tk_getMenuButtons(self):
1315 return self.tk.call('tk_getMenuButtons', self._w)
1316 def tk_nextMenu(self, count):
1317 self.tk.call('tk_nextMenu', count)
1318 def tk_nextMenuEntry(self, count):
1319 self.tk.call('tk_nextMenuEntry', count)
1320 def tk_invokeMenu(self):
1321 self.tk.call('tk_invokeMenu', self._w)
1322 def tk_firstMenu(self):
1323 self.tk.call('tk_firstMenu', self._w)
1324 def tk_mbButtonDown(self):
1325 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001326 def tk_popup(self, x, y, entry=""):
1327 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001328 def activate(self, index):
1329 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001330 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001331 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001332 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001333 def add_cascade(self, cnf={}, **kw):
1334 self.add('cascade', cnf or kw)
1335 def add_checkbutton(self, cnf={}, **kw):
1336 self.add('checkbutton', cnf or kw)
1337 def add_command(self, cnf={}, **kw):
1338 self.add('command', cnf or kw)
1339 def add_radiobutton(self, cnf={}, **kw):
1340 self.add('radiobutton', cnf or kw)
1341 def add_separator(self, cnf={}, **kw):
1342 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001343 def insert(self, index, itemType, cnf={}, **kw):
1344 apply(self.tk.call, (self._w, 'insert', index, itemType)
1345 + self._options(cnf, kw))
1346 def insert_cascade(self, index, cnf={}, **kw):
1347 self.insert(index, 'cascade', cnf or kw)
1348 def insert_checkbutton(self, index, cnf={}, **kw):
1349 self.insert(index, 'checkbutton', cnf or kw)
1350 def insert_command(self, index, cnf={}, **kw):
1351 self.insert(index, 'command', cnf or kw)
1352 def insert_radiobutton(self, index, cnf={}, **kw):
1353 self.insert(index, 'radiobutton', cnf or kw)
1354 def insert_separator(self, index, cnf={}, **kw):
1355 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001356 def delete(self, index1, index2=None):
1357 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001358 def entryconfig(self, index, cnf=None, **kw):
1359 if cnf is None and not kw:
1360 cnf = {}
1361 for x in self.tk.split(apply(self.tk.call,
1362 (self._w, 'entryconfigure', index))):
1363 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1364 return cnf
1365 if type(cnf) == StringType and not kw:
1366 x = self.tk.split(apply(self.tk.call,
1367 (self._w, 'entryconfigure', index, '-'+cnf)))
1368 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001369 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001370 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001371 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001372 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001373 i = self.tk.call(self._w, 'index', index)
1374 if i == 'none': return None
1375 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001376 def invoke(self, index):
1377 return self.tk.call(self._w, 'invoke', index)
1378 def post(self, x, y):
1379 self.tk.call(self._w, 'post', x, y)
1380 def unpost(self):
1381 self.tk.call(self._w, 'unpost')
1382 def yposition(self, index):
1383 return self.tk.getint(self.tk.call(
1384 self._w, 'yposition', index))
1385
1386class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001387 def __init__(self, master=None, cnf={}, **kw):
1388 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001389
1390class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001391 def __init__(self, master=None, cnf={}, **kw):
1392 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001393
1394class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001395 def __init__(self, master=None, cnf={}, **kw):
1396 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001397 def deselect(self):
1398 self.tk.call(self._w, 'deselect')
1399 def flash(self):
1400 self.tk.call(self._w, 'flash')
1401 def invoke(self):
1402 self.tk.call(self._w, 'invoke')
1403 def select(self):
1404 self.tk.call(self._w, 'select')
1405
1406class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001407 def __init__(self, master=None, cnf={}, **kw):
1408 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001409 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001410 value = self.tk.call(self._w, 'get')
1411 try:
1412 return self.tk.getint(value)
1413 except TclError:
1414 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001415 def set(self, value):
1416 self.tk.call(self._w, 'set', value)
1417
1418class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001419 def __init__(self, master=None, cnf={}, **kw):
1420 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001421 def activate(self, index):
1422 self.tk.call(self._w, 'activate', index)
1423 def delta(self, deltax, deltay):
1424 return self.getdouble(self.tk.call(
1425 self._w, 'delta', deltax, deltay))
1426 def fraction(self, x, y):
1427 return self.getdouble(self.tk.call(
1428 self._w, 'fraction', x, y))
1429 def identify(self, x, y):
1430 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001431 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001432 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001433 def set(self, *args):
1434 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001435
1436class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001437 def __init__(self, master=None, cnf={}, **kw):
1438 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001439 def bbox(self, *args):
1440 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001441 def tk_textSelectTo(self, index):
1442 self.tk.call('tk_textSelectTo', self._w, index)
1443 def tk_textBackspace(self):
1444 self.tk.call('tk_textBackspace', self._w)
1445 def tk_textIndexCloser(self, a, b, c):
1446 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1447 def tk_textResetAnchor(self, index):
1448 self.tk.call('tk_textResetAnchor', self._w, index)
1449 def compare(self, index1, op, index2):
1450 return self.tk.getboolean(self.tk.call(
1451 self._w, 'compare', index1, op, index2))
1452 def debug(self, boolean=None):
1453 return self.tk.getboolean(self.tk.call(
1454 self._w, 'debug', boolean))
1455 def delete(self, index1, index2=None):
1456 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001457 def dlineinfo(self, index):
1458 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001459 def get(self, index1, index2=None):
1460 return self.tk.call(self._w, 'get', index1, index2)
1461 def index(self, index):
1462 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001463 def insert(self, index, chars, *args):
1464 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001465 def mark_gravity(self, markName, direction=None):
1466 return apply(self.tk.call,
1467 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001468 def mark_names(self):
1469 return self.tk.splitlist(self.tk.call(
1470 self._w, 'mark', 'names'))
1471 def mark_set(self, markName, index):
1472 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001473 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001474 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001475 def scan_mark(self, x, y):
1476 self.tk.call(self._w, 'scan', 'mark', x, y)
1477 def scan_dragto(self, x, y):
1478 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001479 def search(self, pattern, index, stopindex=None,
1480 forwards=None, backwards=None, exact=None,
1481 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001482 args = [self._w, 'search']
1483 if forwards: args.append('-forwards')
1484 if backwards: args.append('-backwards')
1485 if exact: args.append('-exact')
1486 if regexp: args.append('-regexp')
1487 if nocase: args.append('-nocase')
1488 if count: args.append('-count'); args.append(count)
1489 if pattern[0] == '-': args.append('--')
1490 args.append(pattern)
1491 args.append(index)
1492 if stopindex: args.append(stopindex)
1493 return apply(self.tk.call, tuple(args))
1494 def see(self, index):
1495 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001496 def tag_add(self, tagName, index1, index2=None):
1497 self.tk.call(
1498 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001499 def tag_unbind(self, tagName, sequence):
1500 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001501 def tag_bind(self, tagName, sequence, func, add=None):
1502 return self._bind((self._w, 'tag', 'bind', tagName),
1503 sequence, func, add)
1504 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001505 if option[:1] != '-':
1506 option = '-' + option
1507 if option[-1:] == '_':
1508 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001509 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001510 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001511 if type(cnf) == StringType:
1512 x = self.tk.split(self.tk.call(
1513 self._w, 'tag', 'configure', tagName, '-'+cnf))
1514 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001515 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001516 (self._w, 'tag', 'configure', tagName)
1517 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001518 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001519 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001520 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001521 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001522 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001523 def tag_names(self, index=None):
1524 return self.tk.splitlist(
1525 self.tk.call(self._w, 'tag', 'names', index))
1526 def tag_nextrange(self, tagName, index1, index2=None):
1527 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001528 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001529 def tag_raise(self, tagName, aboveThis=None):
1530 self.tk.call(
1531 self._w, 'tag', 'raise', tagName, aboveThis)
1532 def tag_ranges(self, tagName):
1533 return self.tk.splitlist(self.tk.call(
1534 self._w, 'tag', 'ranges', tagName))
1535 def tag_remove(self, tagName, index1, index2=None):
1536 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001537 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001538 def window_cget(self, index, option):
1539 return self.tk.call(self._w, 'window', 'cget', index, option)
1540 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001541 if type(cnf) == StringType:
1542 x = self.tk.split(self.tk.call(
1543 self._w, 'window', 'configure',
1544 index, '-'+cnf))
1545 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001546 apply(self.tk.call,
1547 (self._w, 'window', 'configure', index)
1548 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001549 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001550 def window_create(self, index, cnf={}, **kw):
1551 apply(self.tk.call,
1552 (self._w, 'window', 'create', index)
1553 + self._options(cnf, kw))
1554 def window_names(self):
1555 return self.tk.splitlist(
1556 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001557 def xview(self, *what):
1558 if not what:
1559 return self._getdoubles(self.tk.call(self._w, 'xview'))
1560 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001561 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001562 if not what:
1563 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001564 apply(self.tk.call, (self._w, 'yview')+what)
1565 def yview_pickplace(self, *what):
1566 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001567
Guido van Rossum28574b51996-10-21 15:16:51 +00001568class _setit:
1569 def __init__(self, var, value):
1570 self.__value = value
1571 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001572 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001573 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001574
1575class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001576 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001577 kw = {"borderwidth": 2, "textvariable": variable,
1578 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1579 "highlightthickness": 2}
1580 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001581 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001582 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1583 self.menuname = menu._w
1584 menu.add_command(label=value, command=_setit(variable, value))
1585 for v in values:
1586 menu.add_command(label=v, command=_setit(variable, v))
1587 self["menu"] = menu
1588
1589 def __getitem__(self, name):
1590 if name == 'menu':
1591 return self.__menu
1592 return Widget.__getitem__(self, name)
1593
1594 def destroy(self):
1595 Menubutton.destroy(self)
1596 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001597
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001598class Image:
1599 def __init__(self, imgtype, name=None, cnf={}, **kw):
1600 self.name = None
1601 master = _default_root
1602 if not master: raise RuntimeError, 'Too early to create image'
1603 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001604 if not name:
1605 name = `id(self)`
1606 # The following is needed for systems where id(x)
1607 # can return a negative number, such as Linux/m68k:
1608 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001609 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1610 elif kw: cnf = kw
1611 options = ()
1612 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001613 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001614 v = self._register(v)
1615 options = options + ('-'+k, v)
1616 apply(self.tk.call,
1617 ('image', 'create', imgtype, name,) + options)
1618 self.name = name
1619 def __str__(self): return self.name
1620 def __del__(self):
1621 if self.name:
1622 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001623 def __setitem__(self, key, value):
1624 self.tk.call(self.name, 'configure', '-'+key, value)
1625 def __getitem__(self, key):
1626 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001627 def config(self, **kw):
1628 res = ()
1629 for k, v in _cnfmerge(kw).items():
1630 if v is not None:
1631 if k[-1] == '_': k = k[:-1]
1632 if callable(v):
1633 v = self._register(v)
1634 res = res + ('-'+k, v)
1635 apply(self.tk.call, (self.name, 'config') + res)
1636 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001637 def height(self):
1638 return self.tk.getint(
1639 self.tk.call('image', 'height', self.name))
1640 def type(self):
1641 return self.tk.call('image', 'type', self.name)
1642 def width(self):
1643 return self.tk.getint(
1644 self.tk.call('image', 'width', self.name))
1645
1646class PhotoImage(Image):
1647 def __init__(self, name=None, cnf={}, **kw):
1648 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001649 def blank(self):
1650 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001651 def cget(self, option):
1652 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001653 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001654 def __getitem__(self, key):
1655 return self.tk.call(self.name, 'cget', '-' + key)
1656 def copy(self):
1657 destImage = PhotoImage()
1658 self.tk.call(destImage, 'copy', self.name)
1659 return destImage
1660 def zoom(self,x,y=''):
1661 destImage = PhotoImage()
1662 if y=='': y=x
1663 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1664 return destImage
1665 def subsample(self,x,y=''):
1666 destImage = PhotoImage()
1667 if y=='': y=x
1668 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1669 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001670 def get(self, x, y):
1671 return self.tk.call(self.name, 'get', x, y)
1672 def put(self, data, to=None):
1673 args = (self.name, 'put', data)
1674 if to:
1675 args = args + to
1676 apply(self.tk.call, args)
1677 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001678 def write(self, filename, format=None, from_coords=None):
1679 args = (self.name, 'write', filename)
1680 if format:
1681 args = args + ('-format', format)
1682 if from_coords:
1683 args = args + ('-from',) + tuple(from_coords)
1684 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001685
1686class BitmapImage(Image):
1687 def __init__(self, name=None, cnf={}, **kw):
1688 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1689
1690def image_names(): return _default_root.tk.call('image', 'names')
1691def image_types(): return _default_root.tk.call('image', 'types')
1692
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001693######################################################################
1694# Extensions:
1695
1696class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001697 def __init__(self, master=None, cnf={}, **kw):
1698 Widget.__init__(self, master, 'studbutton', 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)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001703
1704class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001705 def __init__(self, master=None, cnf={}, **kw):
1706 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001707 self.bind('<Any-Enter>', self.tkButtonEnter)
1708 self.bind('<Any-Leave>', self.tkButtonLeave)
1709 self.bind('<1>', self.tkButtonDown)
1710 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1711 self['fg'] = self['bg']
1712 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001713
Guido van Rossumc417ef81996-08-21 23:38:59 +00001714######################################################################
1715# Test:
1716
1717def _test():
1718 root = Tk()
1719 label = Label(root, text="Proof-of-existence test for Tk")
1720 label.pack()
1721 test = Button(root, text="Click me!",
1722 command=lambda root=root: root.test.config(
1723 text="[%s]" % root.test['text']))
1724 test.pack()
1725 root.test = test
1726 quit = Button(root, text="QUIT", command=root.destroy)
1727 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001728 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001729 root.mainloop()
1730
1731if __name__ == '__main__':
1732 _test()
1733
Guido van Rossum37dcab11996-05-16 16:00:19 +00001734
1735# Emacs cruft
1736# Local Variables:
1737# py-indent-offset: 8
1738# End: