blob: 8d6cf7d540c6d973e8e0b4e856afca8cf25ba5a5 [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum37dcab11996-05-16 16:00:19 +00003__version__ = "$Revision$"
4
Guido van Rossum95806091997-02-15 18:33:24 +00005import _tkinter # If this fails your Python is not configured for Tk
6tkinter = _tkinter # b/w compat for export
7TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00008from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +00009from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000010import string; _string = string; del string
Guido van Rossum18468821994-06-20 07:49:28 +000011
Guido van Rossum95806091997-02-15 18:33:24 +000012TkVersion = _string.atof(_tkinter.TK_VERSION)
13TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000014
Guido van Rossum36269991996-05-16 17:11:27 +000015######################################################################
16# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
17# they are defined here (and not in Tkconstants):
18######################################################################
19if TkVersion >= 4.1:
20 READABLE = 2
21 WRITABLE = 4
22 EXCEPTION = 8
23else:
24 READABLE = 1
25 WRITABLE = 2
26 EXCEPTION = 4
27
28
Guido van Rossum2dcf5291994-07-06 09:23:20 +000029def _flatten(tuple):
30 res = ()
31 for item in tuple:
32 if type(item) in (TupleType, ListType):
33 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000034 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000035 res = res + (item,)
36 return res
37
38def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000039 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000040 return cnfs
41 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000042 return cnfs
43 else:
44 cnf = {}
45 for c in _flatten(cnfs):
46 for k, v in c.items():
47 cnf[k] = v
48 return cnf
49
50class Event:
51 pass
52
Guido van Rossumaec5dc91994-06-27 07:55:12 +000053_default_root = None
54
Guido van Rossum45853db1994-06-20 12:19:19 +000055def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000056 pass
57
Guido van Rossum97aeca11994-07-07 13:12:12 +000058def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000059 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000060
Guido van Rossumaec5dc91994-06-27 07:55:12 +000061_varnum = 0
62class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000063 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000064 def __init__(self, master=None):
65 global _default_root
66 global _varnum
67 if master:
68 self._tk = master.tk
69 else:
70 self._tk = _default_root.tk
71 self._name = 'PY_VAR' + `_varnum`
72 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000073 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000074 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000075 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076 def __str__(self):
77 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000079 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000080
81class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000082 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000083 def __init__(self, master=None):
84 Variable.__init__(self, master)
85 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000086 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000087
88class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000089 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000090 def __init__(self, master=None):
91 Variable.__init__(self, master)
92 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000093 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000094
95class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000096 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000097 def __init__(self, master=None):
98 Variable.__init__(self, master)
99 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000100 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000101
102class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000103 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000104 def __init__(self, master=None):
105 Variable.__init__(self, master)
106 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000107 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000108
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000109def mainloop(n=0):
110 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000111
112def getint(s):
113 return _default_root.tk.getint(s)
114
115def getdouble(s):
116 return _default_root.tk.getdouble(s)
117
118def getboolean(s):
119 return _default_root.tk.getboolean(s)
120
Guido van Rossum18468821994-06-20 07:49:28 +0000121class Misc:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000122 def __init__(self):
123 self._tclCommands = None
124 def destroy(self):
125 if self._tclCommands is not None:
126 for name in self._tclCommands:
127 #print '- Tkinter: deleted command', name
128 self.tk.deletecommand(name)
129 self._tclCommands = None
130 def deletecommand(self, name):
131 #print '- Tkinter: deleted command', name
132 self.tk.deletecommand(name)
133 index = self._tclCommands.index(name)
134 del self._tclCommands[index]
Guido van Rossum18468821994-06-20 07:49:28 +0000135 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000136 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000137 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000138 def tk_bisque(self):
139 self.tk.call('tk_bisque')
140 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000141 apply(self.tk.call, ('tk_setPalette',)
142 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000143 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000144 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000145 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000146 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000147 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000148 def wait_window(self, window=None):
149 if window == None:
150 window = self
151 self.tk.call('tkwait', 'window', window._w)
152 def wait_visibility(self, window=None):
153 if window == None:
154 window = self
155 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000156 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000157 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000158 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000159 return self.tk.getvar(name)
160 def getint(self, s):
161 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000162 def getdouble(self, s):
163 return self.tk.getdouble(s)
164 def getboolean(self, s):
165 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000166 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000167 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000168 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000169 def focus_force(self):
170 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000171 def focus_get(self):
172 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000173 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000174 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000175 def focus_displayof(self):
176 name = self.tk.call('focus', '-displayof', self._w)
177 if name == 'none' or not name: return None
178 return self._nametowidget(name)
179 def focus_lastfor(self):
180 name = self.tk.call('focus', '-lastfor', self._w)
181 if name == 'none' or not name: return None
182 return self._nametowidget(name)
183 def tk_focusFollowsMouse(self):
184 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000185 def tk_focusNext(self):
186 name = self.tk.call('tk_focusNext', self._w)
187 if not name: return None
188 return self._nametowidget(name)
189 def tk_focusPrev(self):
190 name = self.tk.call('tk_focusPrev', self._w)
191 if not name: return None
192 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000193 def after(self, ms, func=None, *args):
194 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000195 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000196 self.tk.call('after', ms)
197 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000198 # XXX Disgusting hack to clean up after calling func
199 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000200 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000201 try:
202 apply(func, args)
203 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000204 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000205 name = self._register(callit)
206 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000207 return self.tk.call('after', ms, name)
208 def after_idle(self, func, *args):
209 return apply(self.after, ('idle', func) + args)
210 def after_cancel(self, id):
211 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000212 def bell(self, displayof=0):
213 apply(self.tk.call, ('bell',) + self._displayof(displayof))
214 # Clipboard handling:
215 def clipboard_clear(self, **kw):
216 if not kw.has_key('displayof'): kw['displayof'] = self._w
217 apply(self.tk.call,
218 ('clipboard', 'clear') + self._options(kw))
219 def clipboard_append(self, string, **kw):
220 if not kw.has_key('displayof'): kw['displayof'] = self._w
221 apply(self.tk.call,
222 ('clipboard', 'append') + self._options(kw)
223 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000224 # XXX grab current w/o window argument
225 def grab_current(self):
226 name = self.tk.call('grab', 'current', self._w)
227 if not name: return None
228 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000229 def grab_release(self):
230 self.tk.call('grab', 'release', self._w)
231 def grab_set(self):
232 self.tk.call('grab', 'set', self._w)
233 def grab_set_global(self):
234 self.tk.call('grab', 'set', '-global', self._w)
235 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000236 status = self.tk.call('grab', 'status', self._w)
237 if status == 'none': status = None
238 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000239 def lower(self, belowThis=None):
240 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000241 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000242 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000243 def option_clear(self):
244 self.tk.call('option', 'clear')
245 def option_get(self, name, className):
246 return self.tk.call('option', 'get', self._w, name, className)
247 def option_readfile(self, fileName, priority = None):
248 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000249 def selection_clear(self, **kw):
250 if not kw.has_key('displayof'): kw['displayof'] = self._w
251 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
252 def selection_get(self, **kw):
253 if not kw.has_key('displayof'): kw['displayof'] = self._w
254 return apply(self.tk.call,
255 ('selection', 'get') + self._options(kw))
256 def selection_handle(self, command, **kw):
257 name = self._register(command)
258 apply(self.tk.call,
259 ('selection', 'handle') + self._options(kw)
260 + (self._w, name))
261 def selection_own(self, **kw):
262 "Become owner of X selection."
263 apply(self.tk.call,
264 ('selection', 'own') + self._options(kw) + (self._w,))
265 def selection_own_get(self, **kw):
266 "Find owner of X selection."
267 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000268 name = apply(self.tk.call,
269 ('selection', 'own') + self._options(kw))
270 if not name: return None
271 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000272 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000273 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000274 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000275 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000276 def tkraise(self, aboveThis=None):
277 self.tk.call('raise', self._w, aboveThis)
278 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000279 def colormodel(self, value=None):
280 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000281 def winfo_atom(self, name, displayof=0):
282 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
283 return self.tk.getint(apply(self.tk.call, args))
284 def winfo_atomname(self, id, displayof=0):
285 args = ('winfo', 'atomname') \
286 + self._displayof(displayof) + (id,)
287 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000288 def winfo_cells(self):
289 return self.tk.getint(
290 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000291 def winfo_children(self):
292 return map(self._nametowidget,
293 self.tk.splitlist(self.tk.call(
294 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000295 def winfo_class(self):
296 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000297 def winfo_colormapfull(self):
298 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000299 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000300 def winfo_containing(self, rootX, rootY, displayof=0):
301 args = ('winfo', 'containing') \
302 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000303 name = apply(self.tk.call, args)
304 if not name: return None
305 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000306 def winfo_depth(self):
307 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
308 def winfo_exists(self):
309 return self.tk.getint(
310 self.tk.call('winfo', 'exists', self._w))
311 def winfo_fpixels(self, number):
312 return self.tk.getdouble(self.tk.call(
313 'winfo', 'fpixels', self._w, number))
314 def winfo_geometry(self):
315 return self.tk.call('winfo', 'geometry', self._w)
316 def winfo_height(self):
317 return self.tk.getint(
318 self.tk.call('winfo', 'height', self._w))
319 def winfo_id(self):
320 return self.tk.getint(
321 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000322 def winfo_interps(self, displayof=0):
323 args = ('winfo', 'interps') + self._displayof(displayof)
324 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000325 def winfo_ismapped(self):
326 return self.tk.getint(
327 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000328 def winfo_manager(self):
329 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000330 def winfo_name(self):
331 return self.tk.call('winfo', 'name', self._w)
332 def winfo_parent(self):
333 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000334 def winfo_pathname(self, id, displayof=0):
335 args = ('winfo', 'pathname') \
336 + self._displayof(displayof) + (id,)
337 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000338 def winfo_pixels(self, number):
339 return self.tk.getint(
340 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000341 def winfo_pointerx(self):
342 return self.tk.getint(
343 self.tk.call('winfo', 'pointerx', self._w))
344 def winfo_pointerxy(self):
345 return self._getints(
346 self.tk.call('winfo', 'pointerxy', self._w))
347 def winfo_pointery(self):
348 return self.tk.getint(
349 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000350 def winfo_reqheight(self):
351 return self.tk.getint(
352 self.tk.call('winfo', 'reqheight', self._w))
353 def winfo_reqwidth(self):
354 return self.tk.getint(
355 self.tk.call('winfo', 'reqwidth', self._w))
356 def winfo_rgb(self, color):
357 return self._getints(
358 self.tk.call('winfo', 'rgb', self._w, color))
359 def winfo_rootx(self):
360 return self.tk.getint(
361 self.tk.call('winfo', 'rootx', self._w))
362 def winfo_rooty(self):
363 return self.tk.getint(
364 self.tk.call('winfo', 'rooty', self._w))
365 def winfo_screen(self):
366 return self.tk.call('winfo', 'screen', self._w)
367 def winfo_screencells(self):
368 return self.tk.getint(
369 self.tk.call('winfo', 'screencells', self._w))
370 def winfo_screendepth(self):
371 return self.tk.getint(
372 self.tk.call('winfo', 'screendepth', self._w))
373 def winfo_screenheight(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'screenheight', self._w))
376 def winfo_screenmmheight(self):
377 return self.tk.getint(
378 self.tk.call('winfo', 'screenmmheight', self._w))
379 def winfo_screenmmwidth(self):
380 return self.tk.getint(
381 self.tk.call('winfo', 'screenmmwidth', self._w))
382 def winfo_screenvisual(self):
383 return self.tk.call('winfo', 'screenvisual', self._w)
384 def winfo_screenwidth(self):
385 return self.tk.getint(
386 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000387 def winfo_server(self):
388 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000389 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000390 return self._nametowidget(self.tk.call(
391 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000392 def winfo_viewable(self):
393 return self.tk.getint(
394 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000395 def winfo_visual(self):
396 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000397 def winfo_visualid(self):
398 return self.tk.call('winfo', 'visualid', self._w)
399 def winfo_visualsavailable(self, includeids=0):
400 data = self.tk.split(
401 self.tk.call('winfo', 'visualsavailable', self._w,
402 includeids and 'includeids' or None))
403 def parseitem(x, self=self):
404 return x[:1] + tuple(map(self.tk.getint, x[1:]))
405 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000406 def winfo_vrootheight(self):
407 return self.tk.getint(
408 self.tk.call('winfo', 'vrootheight', self._w))
409 def winfo_vrootwidth(self):
410 return self.tk.getint(
411 self.tk.call('winfo', 'vrootwidth', self._w))
412 def winfo_vrootx(self):
413 return self.tk.getint(
414 self.tk.call('winfo', 'vrootx', self._w))
415 def winfo_vrooty(self):
416 return self.tk.getint(
417 self.tk.call('winfo', 'vrooty', self._w))
418 def winfo_width(self):
419 return self.tk.getint(
420 self.tk.call('winfo', 'width', self._w))
421 def winfo_x(self):
422 return self.tk.getint(
423 self.tk.call('winfo', 'x', self._w))
424 def winfo_y(self):
425 return self.tk.getint(
426 self.tk.call('winfo', 'y', self._w))
427 def update(self):
428 self.tk.call('update')
429 def update_idletasks(self):
430 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000431 def bindtags(self, tagList=None):
432 if tagList is None:
433 return self.tk.splitlist(
434 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000435 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000436 self.tk.call('bindtags', self._w, tagList)
437 def _bind(self, what, sequence, func, add):
438 if func:
439 cmd = ("%sset _tkinter_break [%s %s]\n"
440 'if {"$_tkinter_break" == "break"} break\n') \
441 % (add and '+' or '',
442 self._register(func, self._substitute),
443 _string.join(self._subst_format))
444 apply(self.tk.call, what + (sequence, cmd))
445 elif func == '':
446 apply(self.tk.call, what + (sequence, func))
447 else:
448 return apply(self.tk.call, what + (sequence,))
449 def bind(self, sequence=None, func=None, add=None):
450 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000451 def unbind(self, sequence):
452 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000453 def bind_all(self, sequence=None, func=None, add=None):
454 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000455 def unbind_all(self, sequence):
456 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000457 def bind_class(self, className, sequence=None, func=None, add=None):
458 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000459 def unbind_class(self, className, sequence):
460 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000461 def mainloop(self, n=0):
462 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000463 def quit(self):
464 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000465 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000466 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000467 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
468 def _getdoubles(self, string):
469 if not string: return None
470 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000471 def _getboolean(self, string):
472 if string:
473 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000474 def _displayof(self, displayof):
475 if displayof:
476 return ('-displayof', displayof)
477 if displayof is None:
478 return ('-displayof', self._w)
479 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000480 def _options(self, cnf, kw = None):
481 if kw:
482 cnf = _cnfmerge((cnf, kw))
483 else:
484 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000485 res = ()
486 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000487 if v is not None:
488 if k[-1] == '_': k = k[:-1]
489 if callable(v):
490 v = self._register(v)
491 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000492 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000493 def _nametowidget(self, name):
494 w = self
495 if name[0] == '.':
496 w = w._root()
497 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000498 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000499 while name:
500 i = find(name, '.')
501 if i >= 0:
502 name, tail = name[:i], name[i+1:]
503 else:
504 tail = ''
505 w = w.children[name]
506 name = tail
507 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000508 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000509 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000510 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000511 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000512 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000513 except AttributeError:
514 pass
515 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000516 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000517 except AttributeError:
518 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000519 self.tk.createcommand(name, f)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000520 if self._tclCommands is None:
521 self._tclCommands = []
522 self._tclCommands.append(name)
523 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000524 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000525 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000526 def _root(self):
527 w = self
528 while w.master: w = w.master
529 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000530 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000531 '%s', '%t', '%w', '%x', '%y',
532 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
533 def _substitute(self, *args):
534 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000535 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000536 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
537 # Missing: (a, c, d, m, o, v, B, R)
538 e = Event()
539 e.serial = tk.getint(nsign)
540 e.num = tk.getint(b)
541 try: e.focus = tk.getboolean(f)
542 except TclError: pass
543 e.height = tk.getint(h)
544 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000545 # For Visibility events, event state is a string and
546 # not an integer:
547 try:
548 e.state = tk.getint(s)
549 except TclError:
550 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000551 e.time = tk.getint(t)
552 e.width = tk.getint(w)
553 e.x = tk.getint(x)
554 e.y = tk.getint(y)
555 e.char = A
556 try: e.send_event = tk.getboolean(E)
557 except TclError: pass
558 e.keysym = K
559 e.keysym_num = tk.getint(N)
560 e.type = T
561 e.widget = self._nametowidget(W)
562 e.x_root = tk.getint(X)
563 e.y_root = tk.getint(Y)
564 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000565 def _report_exception(self):
566 import sys
567 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
568 root = self._root()
569 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000570
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000571class CallWrapper:
572 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000573 self.func = func
574 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000575 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000576 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000577 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000578 if self.subst:
579 args = apply(self.subst, args)
580 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000581 except SystemExit, msg:
582 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000583 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000584 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000585
586class Wm:
587 def aspect(self,
588 minNumer=None, minDenom=None,
589 maxNumer=None, maxDenom=None):
590 return self._getints(
591 self.tk.call('wm', 'aspect', self._w,
592 minNumer, minDenom,
593 maxNumer, maxDenom))
594 def client(self, name=None):
595 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000596 def colormapwindows(self, *wlist):
597 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
598 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000599 def command(self, value=None):
600 return self.tk.call('wm', 'command', self._w, value)
601 def deiconify(self):
602 return self.tk.call('wm', 'deiconify', self._w)
603 def focusmodel(self, model=None):
604 return self.tk.call('wm', 'focusmodel', self._w, model)
605 def frame(self):
606 return self.tk.call('wm', 'frame', self._w)
607 def geometry(self, newGeometry=None):
608 return self.tk.call('wm', 'geometry', self._w, newGeometry)
609 def grid(self,
610 baseWidht=None, baseHeight=None,
611 widthInc=None, heightInc=None):
612 return self._getints(self.tk.call(
613 'wm', 'grid', self._w,
614 baseWidht, baseHeight, widthInc, heightInc))
615 def group(self, pathName=None):
616 return self.tk.call('wm', 'group', self._w, pathName)
617 def iconbitmap(self, bitmap=None):
618 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
619 def iconify(self):
620 return self.tk.call('wm', 'iconify', self._w)
621 def iconmask(self, bitmap=None):
622 return self.tk.call('wm', 'iconmask', self._w, bitmap)
623 def iconname(self, newName=None):
624 return self.tk.call('wm', 'iconname', self._w, newName)
625 def iconposition(self, x=None, y=None):
626 return self._getints(self.tk.call(
627 'wm', 'iconposition', self._w, x, y))
628 def iconwindow(self, pathName=None):
629 return self.tk.call('wm', 'iconwindow', self._w, pathName)
630 def maxsize(self, width=None, height=None):
631 return self._getints(self.tk.call(
632 'wm', 'maxsize', self._w, width, height))
633 def minsize(self, width=None, height=None):
634 return self._getints(self.tk.call(
635 'wm', 'minsize', self._w, width, height))
636 def overrideredirect(self, boolean=None):
637 return self._getboolean(self.tk.call(
638 'wm', 'overrideredirect', self._w, boolean))
639 def positionfrom(self, who=None):
640 return self.tk.call('wm', 'positionfrom', self._w, who)
641 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000642 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000643 command = self._register(func)
644 else:
645 command = func
646 return self.tk.call(
647 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000648 def resizable(self, width=None, height=None):
649 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000650 def sizefrom(self, who=None):
651 return self.tk.call('wm', 'sizefrom', self._w, who)
652 def state(self):
653 return self.tk.call('wm', 'state', self._w)
654 def title(self, string=None):
655 return self.tk.call('wm', 'title', self._w, string)
656 def transient(self, master=None):
657 return self.tk.call('wm', 'transient', self._w, master)
658 def withdraw(self):
659 return self.tk.call('wm', 'withdraw', self._w)
660
661class Tk(Misc, Wm):
662 _w = '.'
663 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000664 Misc.__init__(self)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000665 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000666 self.master = None
667 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000668 if baseName is None:
669 import sys, os
670 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000671 baseName, ext = os.path.splitext(baseName)
672 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000673 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000674 try:
675 # Disable event scanning except for Command-Period
676 import MacOS
677 MacOS.EnableAppswitch(0)
678 except ImportError:
679 pass
680 else:
681 # Work around nasty MacTk bug
682 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000683 # Version sanity checks
684 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000685 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000686 raise RuntimeError, \
687 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000688 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000689 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000690 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000691 raise RuntimeError, \
692 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000693 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000694 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000695 raise RuntimeError, \
696 "Tk 4.0 or higher is required; found Tk %s" \
697 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000698 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000699 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000700 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000701 if not _default_root:
702 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000703 def destroy(self):
704 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000705 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000706 Misc.destroy(self)
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
895 if cnf.has_key('name'):
896 name = cnf['name']
897 del cnf['name']
898 else:
899 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000900 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000901 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000902 self._w = '.' + name
903 else:
904 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000905 self.children = {}
906 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000907 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000908 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000909 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000910 Misc.__init__(self)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000911 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()
1728 root.mainloop()
1729
1730if __name__ == '__main__':
1731 _test()
1732
Guido van Rossum37dcab11996-05-16 16:00:19 +00001733
1734# Emacs cruft
1735# Local Variables:
1736# py-indent-offset: 8
1737# End: