blob: 287c06399c8713b7686fb6dfce8c30bff94b5276 [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
670 MacOS.EnableAppswitch(0)
671 except ImportError:
672 pass
673 else:
674 # Work around nasty MacTk bug
675 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000676 # Version sanity checks
677 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000678 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000679 raise RuntimeError, \
680 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000681 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000682 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000683 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000684 raise RuntimeError, \
685 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000686 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000687 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000688 raise RuntimeError, \
689 "Tk 4.0 or higher is required; found Tk %s" \
690 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000691 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000692 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000693 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000694 if not _default_root:
695 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000696 def destroy(self):
697 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000698 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000699 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000700 global _default_root
701 if _default_root is self:
702 _default_root = None
Guido van Rossum18468821994-06-20 07:49:28 +0000703 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000704 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000705 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000706 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000707 if os.environ.has_key('HOME'): home = os.environ['HOME']
708 else: home = os.curdir
709 class_tcl = os.path.join(home, '.%s.tcl' % className)
710 class_py = os.path.join(home, '.%s.py' % className)
711 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
712 base_py = os.path.join(home, '.%s.py' % baseName)
713 dir = {'self': self}
714 exec 'from Tkinter import *' in dir
715 if os.path.isfile(class_tcl):
716 print 'source', `class_tcl`
717 self.tk.call('source', class_tcl)
718 if os.path.isfile(class_py):
719 print 'execfile', `class_py`
720 execfile(class_py, dir)
721 if os.path.isfile(base_tcl):
722 print 'source', `base_tcl`
723 self.tk.call('source', base_tcl)
724 if os.path.isfile(base_py):
725 print 'execfile', `base_py`
726 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000727 def report_callback_exception(self, exc, val, tb):
728 import traceback
729 print "Exception in Tkinter callback"
730 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000731
732class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000733 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000734 apply(self.tk.call,
735 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000736 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000737 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000738 pack = config
739 def __setitem__(self, key, value):
740 Pack.config({key: value})
741 def forget(self):
742 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000743 pack_forget = forget
744 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000745 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000746 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000747 dict = {}
748 for i in range(0, len(words), 2):
749 key = words[i][1:]
750 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000751 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000752 value = self._nametowidget(value)
753 dict[key] = value
754 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000755 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000756 _noarg_ = ['_noarg_']
757 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000758 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000759 return self._getboolean(self.tk.call(
760 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000761 else:
762 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000763 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000764 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000765 return map(self._nametowidget,
766 self.tk.splitlist(
767 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000768 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000769
770class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000771 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000772 for k in ['in_']:
773 if kw.has_key(k):
774 kw[k[:-1]] = kw[k]
775 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000776 apply(self.tk.call,
777 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000778 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000779 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000780 place = config
781 def __setitem__(self, key, value):
782 Place.config({key: value})
783 def forget(self):
784 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000785 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000786 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000787 words = self.tk.splitlist(
788 self.tk.call('place', 'info', self._w))
789 dict = {}
790 for i in range(0, len(words), 2):
791 key = words[i][1:]
792 value = words[i+1]
793 if value[:1] == '.':
794 value = self._nametowidget(value)
795 dict[key] = value
796 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000797 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000798 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000799 return map(self._nametowidget,
800 self.tk.splitlist(
801 self.tk.call(
802 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000804
Guido van Rossum37dcab11996-05-16 16:00:19 +0000805class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000806 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000807 def config(self, cnf={}, **kw):
808 apply(self.tk.call,
809 ('grid', 'configure', self._w)
810 + self._options(cnf, kw))
811 grid = config
812 def __setitem__(self, key, value):
813 Grid.config({key: value})
814 def bbox(self, column, row):
815 return self._getints(
816 self.tk.call(
817 'grid', 'bbox', self._w, column, row)) or None
818 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000819 def columnconfigure(self, index, cnf={}, **kw):
820 if type(cnf) is not DictionaryType and not kw:
821 options = self._options({cnf: None})
822 else:
823 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000824 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000825 ('grid', 'columnconfigure', self._w, index)
826 + options)
827 if options == ('-minsize', None):
828 return self.tk.getint(res) or None
829 elif options == ('-weight', None):
830 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000831 def forget(self):
832 self.tk.call('grid', 'forget', self._w)
833 grid_forget = forget
834 def info(self):
835 words = self.tk.splitlist(
836 self.tk.call('grid', 'info', self._w))
837 dict = {}
838 for i in range(0, len(words), 2):
839 key = words[i][1:]
840 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000841 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 value = self._nametowidget(value)
843 dict[key] = value
844 return dict
845 grid_info = info
846 def location(self, x, y):
847 return self._getints(
848 self.tk.call(
849 'grid', 'location', self._w, x, y)) or None
850 _noarg_ = ['_noarg_']
851 def propagate(self, flag=_noarg_):
852 if flag is Grid._noarg_:
853 return self._getboolean(self.tk.call(
854 'grid', 'propagate', self._w))
855 else:
856 self.tk.call('grid', 'propagate', self._w, flag)
857 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000858 def rowconfigure(self, index, cnf={}, **kw):
859 if type(cnf) is not DictionaryType and not kw:
860 options = self._options({cnf: None})
861 else:
862 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000863 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000864 ('grid', 'rowconfigure', self._w, index)
865 + options)
866 if options == ('-minsize', None):
867 return self.tk.getint(res) or None
868 elif options == ('-weight', None):
869 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000870 def size(self):
871 return self._getints(
872 self.tk.call('grid', 'size', self._w)) or None
873 def slaves(self, *args):
874 return map(self._nametowidget,
875 self.tk.splitlist(
876 apply(self.tk.call,
877 ('grid', 'slaves', self._w) + args)))
878 grid_slaves = slaves
879
880class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000881 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000882 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000883 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000884 if not _default_root:
885 _default_root = Tk()
886 master = _default_root
887 if not _default_root:
888 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000889 self.master = master
890 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000891 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000892 if cnf.has_key('name'):
893 name = cnf['name']
894 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000895 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000896 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000897 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000898 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000899 self._w = '.' + name
900 else:
901 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000902 self.children = {}
903 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000904 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000905 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000906 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
907 if kw:
908 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000909 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000910 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000911 classes = []
912 for k in cnf.keys():
913 if type(k) is ClassType:
914 classes.append((k, cnf[k]))
915 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000916 apply(self.tk.call,
917 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000918 for k, v in classes:
919 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000920 def config(self, cnf=None, **kw):
921 # XXX ought to generalize this so tag_config etc. can use it
922 if kw:
923 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000924 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000925 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000926 if cnf is None:
927 cnf = {}
928 for x in self.tk.split(
929 self.tk.call(self._w, 'configure')):
930 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
931 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000932 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000933 x = self.tk.split(self.tk.call(
934 self._w, 'configure', '-'+cnf))
935 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000936 apply(self.tk.call, (self._w, 'configure')
937 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000938 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000939 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000940 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000941 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000942 def __setitem__(self, key, value):
943 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000944 def keys(self):
945 return map(lambda x: x[0][1:],
946 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000947 def __str__(self):
948 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000949 def destroy(self):
950 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000951 if self.master.children.has_key(self._name):
952 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000953 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000954 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000955 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000956 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000957
958class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000959 def __init__(self, master=None, cnf={}, **kw):
960 if kw:
961 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000962 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000963 for wmkey in ['screen', 'class_', 'class', 'visual',
964 'colormap']:
965 if cnf.has_key(wmkey):
966 val = cnf[wmkey]
967 # TBD: a hack needed because some keys
968 # are not valid as keyword arguments
969 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
970 else: opt = '-'+wmkey
971 extra = extra + (opt, val)
972 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000973 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000974 root = self._root()
975 self.iconname(root.iconname())
976 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000977
978class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000979 def __init__(self, master=None, cnf={}, **kw):
980 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000981 def tkButtonEnter(self, *dummy):
982 self.tk.call('tkButtonEnter', self._w)
983 def tkButtonLeave(self, *dummy):
984 self.tk.call('tkButtonLeave', self._w)
985 def tkButtonDown(self, *dummy):
986 self.tk.call('tkButtonDown', self._w)
987 def tkButtonUp(self, *dummy):
988 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000989 def tkButtonInvoke(self, *dummy):
990 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000991 def flash(self):
992 self.tk.call(self._w, 'flash')
993 def invoke(self):
994 self.tk.call(self._w, 'invoke')
995
996# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000997# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000998def AtEnd():
999 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001000def AtInsert(*args):
1001 s = 'insert'
1002 for a in args:
1003 if a: s = s + (' ' + a)
1004 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001005def AtSelFirst():
1006 return 'sel.first'
1007def AtSelLast():
1008 return 'sel.last'
1009def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001010 if y is None:
1011 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001012 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001013 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001014
1015class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001016 def __init__(self, master=None, cnf={}, **kw):
1017 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001018 def addtag(self, *args):
1019 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001020 def addtag_above(self, newtag, tagOrId):
1021 self.addtag(newtag, 'above', tagOrId)
1022 def addtag_all(self, newtag):
1023 self.addtag(newtag, 'all')
1024 def addtag_below(self, newtag, tagOrId):
1025 self.addtag(newtag, 'below', tagOrId)
1026 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1027 self.addtag(newtag, 'closest', x, y, halo, start)
1028 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1029 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1030 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1031 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1032 def addtag_withtag(self, newtag, tagOrId):
1033 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001034 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001035 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001036 def tag_unbind(self, tagOrId, sequence):
1037 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001038 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001039 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001040 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001041 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001042 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001043 self._w, 'canvasx', screenx, gridspacing))
1044 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001045 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001046 self._w, 'canvasy', screeny, gridspacing))
1047 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001048 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001049 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001050 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001051 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001052 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001053 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001054 args = args[:-1]
1055 else:
1056 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001057 return self.tk.getint(apply(
1058 self.tk.call,
1059 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001060 + args + self._options(cnf, kw)))
1061 def create_arc(self, *args, **kw):
1062 return self._create('arc', args, kw)
1063 def create_bitmap(self, *args, **kw):
1064 return self._create('bitmap', args, kw)
1065 def create_image(self, *args, **kw):
1066 return self._create('image', args, kw)
1067 def create_line(self, *args, **kw):
1068 return self._create('line', args, kw)
1069 def create_oval(self, *args, **kw):
1070 return self._create('oval', args, kw)
1071 def create_polygon(self, *args, **kw):
1072 return self._create('polygon', args, kw)
1073 def create_rectangle(self, *args, **kw):
1074 return self._create('rectangle', args, kw)
1075 def create_text(self, *args, **kw):
1076 return self._create('text', args, kw)
1077 def create_window(self, *args, **kw):
1078 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001079 def dchars(self, *args):
1080 self._do('dchars', args)
1081 def delete(self, *args):
1082 self._do('delete', args)
1083 def dtag(self, *args):
1084 self._do('dtag', args)
1085 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001086 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001087 def find_above(self, tagOrId):
1088 return self.find('above', tagOrId)
1089 def find_all(self):
1090 return self.find('all')
1091 def find_below(self, tagOrId):
1092 return self.find('below', tagOrId)
1093 def find_closest(self, x, y, halo=None, start=None):
1094 return self.find('closest', x, y, halo, start)
1095 def find_enclosed(self, x1, y1, x2, y2):
1096 return self.find('enclosed', x1, y1, x2, y2)
1097 def find_overlapping(self, x1, y1, x2, y2):
1098 return self.find('overlapping', x1, y1, x2, y2)
1099 def find_withtag(self, tagOrId):
1100 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001101 def focus(self, *args):
1102 return self._do('focus', args)
1103 def gettags(self, *args):
1104 return self.tk.splitlist(self._do('gettags', args))
1105 def icursor(self, *args):
1106 self._do('icursor', args)
1107 def index(self, *args):
1108 return self.tk.getint(self._do('index', args))
1109 def insert(self, *args):
1110 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001111 def itemcget(self, tagOrId, option):
1112 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001113 def itemconfig(self, tagOrId, cnf=None, **kw):
1114 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001115 cnf = {}
1116 for x in self.tk.split(
1117 self._do('itemconfigure', (tagOrId))):
1118 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1119 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001120 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001121 x = self.tk.split(self._do('itemconfigure',
1122 (tagOrId, '-'+cnf,)))
1123 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001124 self._do('itemconfigure', (tagOrId,)
1125 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001126 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def lower(self, *args):
1128 self._do('lower', args)
1129 def move(self, *args):
1130 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001131 def postscript(self, cnf={}, **kw):
1132 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001133 def tkraise(self, *args):
1134 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001135 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001136 def scale(self, *args):
1137 self._do('scale', args)
1138 def scan_mark(self, x, y):
1139 self.tk.call(self._w, 'scan', 'mark', x, y)
1140 def scan_dragto(self, x, y):
1141 self.tk.call(self._w, 'scan', 'dragto', x, y)
1142 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001143 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001144 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001145 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001146 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001147 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001148 def select_item(self):
1149 self.tk.call(self._w, 'select', 'item')
1150 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001151 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001152 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001153 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001154 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001155 if not args:
1156 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001157 apply(self.tk.call, (self._w, 'xview')+args)
1158 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001159 if not args:
1160 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001161 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001162
1163class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001164 def __init__(self, master=None, cnf={}, **kw):
1165 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001166 def deselect(self):
1167 self.tk.call(self._w, 'deselect')
1168 def flash(self):
1169 self.tk.call(self._w, 'flash')
1170 def invoke(self):
1171 self.tk.call(self._w, 'invoke')
1172 def select(self):
1173 self.tk.call(self._w, 'select')
1174 def toggle(self):
1175 self.tk.call(self._w, 'toggle')
1176
1177class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001178 def __init__(self, master=None, cnf={}, **kw):
1179 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001180 def delete(self, first, last=None):
1181 self.tk.call(self._w, 'delete', first, last)
1182 def get(self):
1183 return self.tk.call(self._w, 'get')
1184 def icursor(self, index):
1185 self.tk.call(self._w, 'icursor', index)
1186 def index(self, index):
1187 return self.tk.getint(self.tk.call(
1188 self._w, 'index', index))
1189 def insert(self, index, string):
1190 self.tk.call(self._w, 'insert', index, string)
1191 def scan_mark(self, x):
1192 self.tk.call(self._w, 'scan', 'mark', x)
1193 def scan_dragto(self, x):
1194 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001195 def selection_adjust(self, index):
1196 self.tk.call(self._w, 'selection', 'adjust', index)
1197 select_adjust = selection_adjust
1198 def selection_clear(self):
1199 self.tk.call(self._w, 'selection', 'clear')
1200 select_clear = selection_clear
1201 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001202 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001203 select_from = selection_from
1204 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001205 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001206 self.tk.call(self._w, 'selection', 'present'))
1207 select_present = selection_present
1208 def selection_range(self, start, end):
1209 self.tk.call(self._w, 'selection', 'range', start, end)
1210 select_range = selection_range
1211 def selection_to(self, index):
1212 self.tk.call(self._w, 'selection', 'to', index)
1213 select_to = selection_to
1214 def xview(self, index):
1215 self.tk.call(self._w, 'xview', index)
1216 def xview_moveto(self, fraction):
1217 self.tk.call(self._w, 'xview', 'moveto', fraction)
1218 def xview_scroll(self, number, what):
1219 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001220
1221class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001222 def __init__(self, master=None, cnf={}, **kw):
1223 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001224 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001225 if cnf.has_key('class_'):
1226 extra = ('-class', cnf['class_'])
1227 del cnf['class_']
1228 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001229 extra = ('-class', cnf['class'])
1230 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001231 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001232
1233class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001234 def __init__(self, master=None, cnf={}, **kw):
1235 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001236
Guido van Rossum18468821994-06-20 07:49:28 +00001237class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001238 def __init__(self, master=None, cnf={}, **kw):
1239 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001240 def activate(self, index):
1241 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001242 def bbox(self, *args):
1243 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001244 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001245 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001246 return self.tk.splitlist(self.tk.call(
1247 self._w, 'curselection'))
1248 def delete(self, first, last=None):
1249 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001250 def get(self, first, last=None):
1251 if last:
1252 return self.tk.splitlist(self.tk.call(
1253 self._w, 'get', first, last))
1254 else:
1255 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001256 def insert(self, index, *elements):
1257 apply(self.tk.call,
1258 (self._w, 'insert', index) + elements)
1259 def nearest(self, y):
1260 return self.tk.getint(self.tk.call(
1261 self._w, 'nearest', y))
1262 def scan_mark(self, x, y):
1263 self.tk.call(self._w, 'scan', 'mark', x, y)
1264 def scan_dragto(self, x, y):
1265 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001266 def see(self, index):
1267 self.tk.call(self._w, 'see', index)
1268 def index(self, index):
1269 i = self.tk.call(self._w, 'index', index)
1270 if i == 'none': return None
1271 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001272 def select_anchor(self, index):
1273 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001274 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001275 def select_clear(self, first, last=None):
1276 self.tk.call(self._w,
1277 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001278 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001279 def select_includes(self, index):
1280 return self.tk.getboolean(self.tk.call(
1281 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001282 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001283 def select_set(self, first, last=None):
1284 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001285 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001286 def size(self):
1287 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001288 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001289 if not what:
1290 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001291 apply(self.tk.call, (self._w, 'xview')+what)
1292 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001293 if not what:
1294 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001295 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001296
1297class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001298 def __init__(self, master=None, cnf={}, **kw):
1299 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001300 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001301 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001302 def tk_mbPost(self):
1303 self.tk.call('tk_mbPost', self._w)
1304 def tk_mbUnpost(self):
1305 self.tk.call('tk_mbUnpost')
1306 def tk_traverseToMenu(self, char):
1307 self.tk.call('tk_traverseToMenu', self._w, char)
1308 def tk_traverseWithinMenu(self, char):
1309 self.tk.call('tk_traverseWithinMenu', self._w, char)
1310 def tk_getMenuButtons(self):
1311 return self.tk.call('tk_getMenuButtons', self._w)
1312 def tk_nextMenu(self, count):
1313 self.tk.call('tk_nextMenu', count)
1314 def tk_nextMenuEntry(self, count):
1315 self.tk.call('tk_nextMenuEntry', count)
1316 def tk_invokeMenu(self):
1317 self.tk.call('tk_invokeMenu', self._w)
1318 def tk_firstMenu(self):
1319 self.tk.call('tk_firstMenu', self._w)
1320 def tk_mbButtonDown(self):
1321 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001322 def tk_popup(self, x, y, entry=""):
1323 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001324 def activate(self, index):
1325 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001326 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001327 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001328 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001329 def add_cascade(self, cnf={}, **kw):
1330 self.add('cascade', cnf or kw)
1331 def add_checkbutton(self, cnf={}, **kw):
1332 self.add('checkbutton', cnf or kw)
1333 def add_command(self, cnf={}, **kw):
1334 self.add('command', cnf or kw)
1335 def add_radiobutton(self, cnf={}, **kw):
1336 self.add('radiobutton', cnf or kw)
1337 def add_separator(self, cnf={}, **kw):
1338 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001339 def insert(self, index, itemType, cnf={}, **kw):
1340 apply(self.tk.call, (self._w, 'insert', index, itemType)
1341 + self._options(cnf, kw))
1342 def insert_cascade(self, index, cnf={}, **kw):
1343 self.insert(index, 'cascade', cnf or kw)
1344 def insert_checkbutton(self, index, cnf={}, **kw):
1345 self.insert(index, 'checkbutton', cnf or kw)
1346 def insert_command(self, index, cnf={}, **kw):
1347 self.insert(index, 'command', cnf or kw)
1348 def insert_radiobutton(self, index, cnf={}, **kw):
1349 self.insert(index, 'radiobutton', cnf or kw)
1350 def insert_separator(self, index, cnf={}, **kw):
1351 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001352 def delete(self, index1, index2=None):
1353 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001354 def entryconfig(self, index, cnf=None, **kw):
1355 if cnf is None and not kw:
1356 cnf = {}
1357 for x in self.tk.split(apply(self.tk.call,
1358 (self._w, 'entryconfigure', index))):
1359 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1360 return cnf
1361 if type(cnf) == StringType and not kw:
1362 x = self.tk.split(apply(self.tk.call,
1363 (self._w, 'entryconfigure', index, '-'+cnf)))
1364 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001365 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001366 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001367 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001368 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001369 i = self.tk.call(self._w, 'index', index)
1370 if i == 'none': return None
1371 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001372 def invoke(self, index):
1373 return self.tk.call(self._w, 'invoke', index)
1374 def post(self, x, y):
1375 self.tk.call(self._w, 'post', x, y)
1376 def unpost(self):
1377 self.tk.call(self._w, 'unpost')
1378 def yposition(self, index):
1379 return self.tk.getint(self.tk.call(
1380 self._w, 'yposition', index))
1381
1382class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001383 def __init__(self, master=None, cnf={}, **kw):
1384 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001385
1386class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001387 def __init__(self, master=None, cnf={}, **kw):
1388 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001389
1390class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001391 def __init__(self, master=None, cnf={}, **kw):
1392 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001393 def deselect(self):
1394 self.tk.call(self._w, 'deselect')
1395 def flash(self):
1396 self.tk.call(self._w, 'flash')
1397 def invoke(self):
1398 self.tk.call(self._w, 'invoke')
1399 def select(self):
1400 self.tk.call(self._w, 'select')
1401
1402class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001403 def __init__(self, master=None, cnf={}, **kw):
1404 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001405 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001406 value = self.tk.call(self._w, 'get')
1407 try:
1408 return self.tk.getint(value)
1409 except TclError:
1410 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001411 def set(self, value):
1412 self.tk.call(self._w, 'set', value)
1413
1414class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001415 def __init__(self, master=None, cnf={}, **kw):
1416 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001417 def activate(self, index):
1418 self.tk.call(self._w, 'activate', index)
1419 def delta(self, deltax, deltay):
1420 return self.getdouble(self.tk.call(
1421 self._w, 'delta', deltax, deltay))
1422 def fraction(self, x, y):
1423 return self.getdouble(self.tk.call(
1424 self._w, 'fraction', x, y))
1425 def identify(self, x, y):
1426 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001427 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001428 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001429 def set(self, *args):
1430 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001431
1432class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001433 def __init__(self, master=None, cnf={}, **kw):
1434 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001435 def bbox(self, *args):
1436 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001437 def tk_textSelectTo(self, index):
1438 self.tk.call('tk_textSelectTo', self._w, index)
1439 def tk_textBackspace(self):
1440 self.tk.call('tk_textBackspace', self._w)
1441 def tk_textIndexCloser(self, a, b, c):
1442 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1443 def tk_textResetAnchor(self, index):
1444 self.tk.call('tk_textResetAnchor', self._w, index)
1445 def compare(self, index1, op, index2):
1446 return self.tk.getboolean(self.tk.call(
1447 self._w, 'compare', index1, op, index2))
1448 def debug(self, boolean=None):
1449 return self.tk.getboolean(self.tk.call(
1450 self._w, 'debug', boolean))
1451 def delete(self, index1, index2=None):
1452 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001453 def dlineinfo(self, index):
1454 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001455 def get(self, index1, index2=None):
1456 return self.tk.call(self._w, 'get', index1, index2)
1457 def index(self, index):
1458 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001459 def insert(self, index, chars, *args):
1460 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001461 def mark_gravity(self, markName, direction=None):
1462 return apply(self.tk.call,
1463 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001464 def mark_names(self):
1465 return self.tk.splitlist(self.tk.call(
1466 self._w, 'mark', 'names'))
1467 def mark_set(self, markName, index):
1468 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001469 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001470 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001471 def scan_mark(self, x, y):
1472 self.tk.call(self._w, 'scan', 'mark', x, y)
1473 def scan_dragto(self, x, y):
1474 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001475 def search(self, pattern, index, stopindex=None,
1476 forwards=None, backwards=None, exact=None,
1477 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001478 args = [self._w, 'search']
1479 if forwards: args.append('-forwards')
1480 if backwards: args.append('-backwards')
1481 if exact: args.append('-exact')
1482 if regexp: args.append('-regexp')
1483 if nocase: args.append('-nocase')
1484 if count: args.append('-count'); args.append(count)
1485 if pattern[0] == '-': args.append('--')
1486 args.append(pattern)
1487 args.append(index)
1488 if stopindex: args.append(stopindex)
1489 return apply(self.tk.call, tuple(args))
1490 def see(self, index):
1491 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001492 def tag_add(self, tagName, index1, index2=None):
1493 self.tk.call(
1494 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001495 def tag_unbind(self, tagName, sequence):
1496 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001497 def tag_bind(self, tagName, sequence, func, add=None):
1498 return self._bind((self._w, 'tag', 'bind', tagName),
1499 sequence, func, add)
1500 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001501 if option[:1] != '-':
1502 option = '-' + option
1503 if option[-1:] == '_':
1504 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001505 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001506 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001507 if type(cnf) == StringType:
1508 x = self.tk.split(self.tk.call(
1509 self._w, 'tag', 'configure', tagName, '-'+cnf))
1510 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001511 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001512 (self._w, 'tag', 'configure', tagName)
1513 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001514 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001515 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001516 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001517 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001518 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001519 def tag_names(self, index=None):
1520 return self.tk.splitlist(
1521 self.tk.call(self._w, 'tag', 'names', index))
1522 def tag_nextrange(self, tagName, index1, index2=None):
1523 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001524 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001525 def tag_raise(self, tagName, aboveThis=None):
1526 self.tk.call(
1527 self._w, 'tag', 'raise', tagName, aboveThis)
1528 def tag_ranges(self, tagName):
1529 return self.tk.splitlist(self.tk.call(
1530 self._w, 'tag', 'ranges', tagName))
1531 def tag_remove(self, tagName, index1, index2=None):
1532 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001533 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001534 def window_cget(self, index, option):
1535 return self.tk.call(self._w, 'window', 'cget', index, option)
1536 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001537 if type(cnf) == StringType:
1538 x = self.tk.split(self.tk.call(
1539 self._w, 'window', 'configure',
1540 index, '-'+cnf))
1541 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001542 apply(self.tk.call,
1543 (self._w, 'window', 'configure', index)
1544 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001545 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001546 def window_create(self, index, cnf={}, **kw):
1547 apply(self.tk.call,
1548 (self._w, 'window', 'create', index)
1549 + self._options(cnf, kw))
1550 def window_names(self):
1551 return self.tk.splitlist(
1552 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001553 def xview(self, *what):
1554 if not what:
1555 return self._getdoubles(self.tk.call(self._w, 'xview'))
1556 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001557 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001558 if not what:
1559 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001560 apply(self.tk.call, (self._w, 'yview')+what)
1561 def yview_pickplace(self, *what):
1562 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001563
Guido van Rossum28574b51996-10-21 15:16:51 +00001564class _setit:
1565 def __init__(self, var, value):
1566 self.__value = value
1567 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001568 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001569 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001570
1571class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001572 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001573 kw = {"borderwidth": 2, "textvariable": variable,
1574 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1575 "highlightthickness": 2}
1576 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001577 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001578 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1579 self.menuname = menu._w
1580 menu.add_command(label=value, command=_setit(variable, value))
1581 for v in values:
1582 menu.add_command(label=v, command=_setit(variable, v))
1583 self["menu"] = menu
1584
1585 def __getitem__(self, name):
1586 if name == 'menu':
1587 return self.__menu
1588 return Widget.__getitem__(self, name)
1589
1590 def destroy(self):
1591 Menubutton.destroy(self)
1592 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001593
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001594class Image:
1595 def __init__(self, imgtype, name=None, cnf={}, **kw):
1596 self.name = None
1597 master = _default_root
1598 if not master: raise RuntimeError, 'Too early to create image'
1599 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001600 if not name:
1601 name = `id(self)`
1602 # The following is needed for systems where id(x)
1603 # can return a negative number, such as Linux/m68k:
1604 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001605 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1606 elif kw: cnf = kw
1607 options = ()
1608 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001609 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001610 v = self._register(v)
1611 options = options + ('-'+k, v)
1612 apply(self.tk.call,
1613 ('image', 'create', imgtype, name,) + options)
1614 self.name = name
1615 def __str__(self): return self.name
1616 def __del__(self):
1617 if self.name:
1618 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001619 def __setitem__(self, key, value):
1620 self.tk.call(self.name, 'configure', '-'+key, value)
1621 def __getitem__(self, key):
1622 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001623 def config(self, **kw):
1624 res = ()
1625 for k, v in _cnfmerge(kw).items():
1626 if v is not None:
1627 if k[-1] == '_': k = k[:-1]
1628 if callable(v):
1629 v = self._register(v)
1630 res = res + ('-'+k, v)
1631 apply(self.tk.call, (self.name, 'config') + res)
1632 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001633 def height(self):
1634 return self.tk.getint(
1635 self.tk.call('image', 'height', self.name))
1636 def type(self):
1637 return self.tk.call('image', 'type', self.name)
1638 def width(self):
1639 return self.tk.getint(
1640 self.tk.call('image', 'width', self.name))
1641
1642class PhotoImage(Image):
1643 def __init__(self, name=None, cnf={}, **kw):
1644 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001645 def blank(self):
1646 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001647 def cget(self, option):
1648 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001649 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001650 def __getitem__(self, key):
1651 return self.tk.call(self.name, 'cget', '-' + key)
1652 def copy(self):
1653 destImage = PhotoImage()
1654 self.tk.call(destImage, 'copy', self.name)
1655 return destImage
1656 def zoom(self,x,y=''):
1657 destImage = PhotoImage()
1658 if y=='': y=x
1659 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1660 return destImage
1661 def subsample(self,x,y=''):
1662 destImage = PhotoImage()
1663 if y=='': y=x
1664 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1665 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001666 def get(self, x, y):
1667 return self.tk.call(self.name, 'get', x, y)
1668 def put(self, data, to=None):
1669 args = (self.name, 'put', data)
1670 if to:
1671 args = args + to
1672 apply(self.tk.call, args)
1673 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001674 def write(self, filename, format=None, from_coords=None):
1675 args = (self.name, 'write', filename)
1676 if format:
1677 args = args + ('-format', format)
1678 if from_coords:
1679 args = args + ('-from',) + tuple(from_coords)
1680 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001681
1682class BitmapImage(Image):
1683 def __init__(self, name=None, cnf={}, **kw):
1684 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1685
1686def image_names(): return _default_root.tk.call('image', 'names')
1687def image_types(): return _default_root.tk.call('image', 'types')
1688
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001689######################################################################
1690# Extensions:
1691
1692class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001693 def __init__(self, master=None, cnf={}, **kw):
1694 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001695 self.bind('<Any-Enter>', self.tkButtonEnter)
1696 self.bind('<Any-Leave>', self.tkButtonLeave)
1697 self.bind('<1>', self.tkButtonDown)
1698 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001699
1700class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001701 def __init__(self, master=None, cnf={}, **kw):
1702 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001703 self.bind('<Any-Enter>', self.tkButtonEnter)
1704 self.bind('<Any-Leave>', self.tkButtonLeave)
1705 self.bind('<1>', self.tkButtonDown)
1706 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1707 self['fg'] = self['bg']
1708 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001709
Guido van Rossumc417ef81996-08-21 23:38:59 +00001710######################################################################
1711# Test:
1712
1713def _test():
1714 root = Tk()
1715 label = Label(root, text="Proof-of-existence test for Tk")
1716 label.pack()
1717 test = Button(root, text="Click me!",
1718 command=lambda root=root: root.test.config(
1719 text="[%s]" % root.test['text']))
1720 test.pack()
1721 root.test = test
1722 quit = Button(root, text="QUIT", command=root.destroy)
1723 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001724 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001725 root.mainloop()
1726
1727if __name__ == '__main__':
1728 _test()
1729
Guido van Rossum37dcab11996-05-16 16:00:19 +00001730
1731# Emacs cruft
1732# Local Variables:
1733# py-indent-offset: 8
1734# End: