blob: f009afd03e6c2d46fce0ca9fbebad714c6fd7624 [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 Rossumf53c86c1997-08-14 14:15:54 +000018
19# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
20try: _tkinter.createfilehandler
21except AttributeError: _tkinter.createfilehandler = None
22try: _tkinter.deletefilehandler
23except AttributeError: _tkinter.deletefilehandler = None
Guido van Rossum36269991996-05-16 17:11:27 +000024
25
Guido van Rossum2dcf5291994-07-06 09:23:20 +000026def _flatten(tuple):
27 res = ()
28 for item in tuple:
29 if type(item) in (TupleType, ListType):
30 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000031 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000032 res = res + (item,)
33 return res
34
35def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000036 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000037 return cnfs
38 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000039 return cnfs
40 else:
41 cnf = {}
42 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000043 try:
44 cnf.update(c)
45 except (AttributeError, TypeError), msg:
46 print "_cnfmerge: fallback due to:", msg
47 for k, v in c.items():
48 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000049 return cnf
50
51class Event:
52 pass
53
Guido van Rossumaec5dc91994-06-27 07:55:12 +000054_default_root = None
55
Guido van Rossum45853db1994-06-20 12:19:19 +000056def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000057 pass
58
Guido van Rossum97aeca11994-07-07 13:12:12 +000059def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000060 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000061
Guido van Rossumaec5dc91994-06-27 07:55:12 +000062_varnum = 0
63class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000064 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000065 def __init__(self, master=None):
66 global _default_root
67 global _varnum
68 if master:
69 self._tk = master.tk
70 else:
71 self._tk = _default_root.tk
72 self._name = 'PY_VAR' + `_varnum`
73 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000074 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000075 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000076 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000077 def __str__(self):
78 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000079 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000080 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000081
82class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000083 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000084 def __init__(self, master=None):
85 Variable.__init__(self, master)
86 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000087 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000088
89class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000090 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000091 def __init__(self, master=None):
92 Variable.__init__(self, master)
93 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000094 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000095
96class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000097 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000098 def __init__(self, master=None):
99 Variable.__init__(self, master)
100 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000101 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000102
103class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000104 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000105 def __init__(self, master=None):
106 Variable.__init__(self, master)
107 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000108 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000109
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000110def mainloop(n=0):
111 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000112
113def getint(s):
114 return _default_root.tk.getint(s)
115
116def getdouble(s):
117 return _default_root.tk.getdouble(s)
118
119def getboolean(s):
120 return _default_root.tk.getboolean(s)
121
Guido van Rossum18468821994-06-20 07:49:28 +0000122class Misc:
Fred Drake526749b1997-05-03 04:16:23 +0000123 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000124 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 Rossum83bd9a91997-09-29 23:24:52 +0000570 # These used to be defined in Widget:
571 def config(self, cnf=None, **kw):
572 # XXX ought to generalize this so tag_config etc. can use it
573 if kw:
574 cnf = _cnfmerge((cnf, kw))
575 elif cnf:
576 cnf = _cnfmerge(cnf)
577 if cnf is None:
578 cnf = {}
579 for x in self.tk.split(
580 self.tk.call(self._w, 'configure')):
581 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
582 return cnf
583 if type(cnf) is StringType:
584 x = self.tk.split(self.tk.call(
585 self._w, 'configure', '-'+cnf))
586 return (x[0][1:],) + x[1:]
587 apply(self.tk.call, (self._w, 'configure')
588 + self._options(cnf))
589 configure = config
590 def cget(self, key):
591 return self.tk.call(self._w, 'cget', '-' + key)
592 __getitem__ = cget
593 def __setitem__(self, key, value):
594 Widget.config(self, {key: value})
595 def keys(self):
596 return map(lambda x: x[0][1:],
597 self.tk.split(self.tk.call(self._w, 'configure')))
598 def __str__(self):
599 return self._w
Guido van Rossum18468821994-06-20 07:49:28 +0000600
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000601class CallWrapper:
602 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000603 self.func = func
604 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000605 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000606 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000607 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000608 if self.subst:
609 args = apply(self.subst, args)
610 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000611 except SystemExit, msg:
612 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000613 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000614 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000615
616class Wm:
617 def aspect(self,
618 minNumer=None, minDenom=None,
619 maxNumer=None, maxDenom=None):
620 return self._getints(
621 self.tk.call('wm', 'aspect', self._w,
622 minNumer, minDenom,
623 maxNumer, maxDenom))
624 def client(self, name=None):
625 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000626 def colormapwindows(self, *wlist):
627 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
628 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000629 def command(self, value=None):
630 return self.tk.call('wm', 'command', self._w, value)
631 def deiconify(self):
632 return self.tk.call('wm', 'deiconify', self._w)
633 def focusmodel(self, model=None):
634 return self.tk.call('wm', 'focusmodel', self._w, model)
635 def frame(self):
636 return self.tk.call('wm', 'frame', self._w)
637 def geometry(self, newGeometry=None):
638 return self.tk.call('wm', 'geometry', self._w, newGeometry)
639 def grid(self,
640 baseWidht=None, baseHeight=None,
641 widthInc=None, heightInc=None):
642 return self._getints(self.tk.call(
643 'wm', 'grid', self._w,
644 baseWidht, baseHeight, widthInc, heightInc))
645 def group(self, pathName=None):
646 return self.tk.call('wm', 'group', self._w, pathName)
647 def iconbitmap(self, bitmap=None):
648 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
649 def iconify(self):
650 return self.tk.call('wm', 'iconify', self._w)
651 def iconmask(self, bitmap=None):
652 return self.tk.call('wm', 'iconmask', self._w, bitmap)
653 def iconname(self, newName=None):
654 return self.tk.call('wm', 'iconname', self._w, newName)
655 def iconposition(self, x=None, y=None):
656 return self._getints(self.tk.call(
657 'wm', 'iconposition', self._w, x, y))
658 def iconwindow(self, pathName=None):
659 return self.tk.call('wm', 'iconwindow', self._w, pathName)
660 def maxsize(self, width=None, height=None):
661 return self._getints(self.tk.call(
662 'wm', 'maxsize', self._w, width, height))
663 def minsize(self, width=None, height=None):
664 return self._getints(self.tk.call(
665 'wm', 'minsize', self._w, width, height))
666 def overrideredirect(self, boolean=None):
667 return self._getboolean(self.tk.call(
668 'wm', 'overrideredirect', self._w, boolean))
669 def positionfrom(self, who=None):
670 return self.tk.call('wm', 'positionfrom', self._w, who)
671 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000672 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000673 command = self._register(func)
674 else:
675 command = func
676 return self.tk.call(
677 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000678 def resizable(self, width=None, height=None):
679 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000680 def sizefrom(self, who=None):
681 return self.tk.call('wm', 'sizefrom', self._w, who)
682 def state(self):
683 return self.tk.call('wm', 'state', self._w)
684 def title(self, string=None):
685 return self.tk.call('wm', 'title', self._w, string)
686 def transient(self, master=None):
687 return self.tk.call('wm', 'transient', self._w, master)
688 def withdraw(self):
689 return self.tk.call('wm', 'withdraw', self._w)
690
691class Tk(Misc, Wm):
692 _w = '.'
693 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000694 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000695 self.master = None
696 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000697 if baseName is None:
698 import sys, os
699 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000700 baseName, ext = os.path.splitext(baseName)
701 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000702 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000703 try:
704 # Disable event scanning except for Command-Period
705 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000706 try:
707 MacOS.SchedParams(1, 0)
708 except AttributeError:
709 # pre-1.5, use old routine
710 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000711 except ImportError:
712 pass
713 else:
714 # Work around nasty MacTk bug
715 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000716 # Version sanity checks
717 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000718 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000719 raise RuntimeError, \
720 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000721 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000722 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000723 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000724 raise RuntimeError, \
725 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000726 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000727 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000728 raise RuntimeError, \
729 "Tk 4.0 or higher is required; found Tk %s" \
730 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000731 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000732 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000733 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000734 if not _default_root:
735 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000736 def destroy(self):
737 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000738 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000739 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000740 global _default_root
741 if _default_root is self:
742 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000743 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000744 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000745 if os.environ.has_key('HOME'): home = os.environ['HOME']
746 else: home = os.curdir
747 class_tcl = os.path.join(home, '.%s.tcl' % className)
748 class_py = os.path.join(home, '.%s.py' % className)
749 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
750 base_py = os.path.join(home, '.%s.py' % baseName)
751 dir = {'self': self}
752 exec 'from Tkinter import *' in dir
753 if os.path.isfile(class_tcl):
754 print 'source', `class_tcl`
755 self.tk.call('source', class_tcl)
756 if os.path.isfile(class_py):
757 print 'execfile', `class_py`
758 execfile(class_py, dir)
759 if os.path.isfile(base_tcl):
760 print 'source', `base_tcl`
761 self.tk.call('source', base_tcl)
762 if os.path.isfile(base_py):
763 print 'execfile', `base_py`
764 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000765 def report_callback_exception(self, exc, val, tb):
766 import traceback
767 print "Exception in Tkinter callback"
768 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000769
770class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000771 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000772 apply(self.tk.call,
773 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000774 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000775 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000776 pack = config
777 def __setitem__(self, key, value):
778 Pack.config({key: value})
779 def forget(self):
780 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000781 pack_forget = forget
782 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000783 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000784 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000785 dict = {}
786 for i in range(0, len(words), 2):
787 key = words[i][1:]
788 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000789 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000790 value = self._nametowidget(value)
791 dict[key] = value
792 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000793 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000794 _noarg_ = ['_noarg_']
795 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000796 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000797 return self._getboolean(self.tk.call(
798 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000799 else:
800 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000801 pack_propagate = propagate
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('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000806 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000807
808class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000809 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000810 for k in ['in_']:
811 if kw.has_key(k):
812 kw[k[:-1]] = kw[k]
813 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000814 apply(self.tk.call,
815 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000816 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000817 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000818 place = config
819 def __setitem__(self, key, value):
820 Place.config({key: value})
821 def forget(self):
822 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000823 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000824 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000825 words = self.tk.splitlist(
826 self.tk.call('place', 'info', self._w))
827 dict = {}
828 for i in range(0, len(words), 2):
829 key = words[i][1:]
830 value = words[i+1]
831 if value[:1] == '.':
832 value = self._nametowidget(value)
833 dict[key] = value
834 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000835 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000836 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000837 return map(self._nametowidget,
838 self.tk.splitlist(
839 self.tk.call(
840 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000841 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000842
Guido van Rossum37dcab11996-05-16 16:00:19 +0000843class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000844 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000845 def config(self, cnf={}, **kw):
846 apply(self.tk.call,
847 ('grid', 'configure', self._w)
848 + self._options(cnf, kw))
849 grid = config
850 def __setitem__(self, key, value):
851 Grid.config({key: value})
852 def bbox(self, column, row):
853 return self._getints(
854 self.tk.call(
855 'grid', 'bbox', self._w, column, row)) or None
856 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000857 def columnconfigure(self, index, cnf={}, **kw):
858 if type(cnf) is not DictionaryType and not kw:
859 options = self._options({cnf: None})
860 else:
861 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000862 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000863 ('grid', 'columnconfigure', self._w, index)
864 + options)
865 if options == ('-minsize', None):
866 return self.tk.getint(res) or None
867 elif options == ('-weight', None):
868 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000869 def forget(self):
870 self.tk.call('grid', 'forget', self._w)
871 grid_forget = forget
872 def info(self):
873 words = self.tk.splitlist(
874 self.tk.call('grid', 'info', self._w))
875 dict = {}
876 for i in range(0, len(words), 2):
877 key = words[i][1:]
878 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000879 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000880 value = self._nametowidget(value)
881 dict[key] = value
882 return dict
883 grid_info = info
884 def location(self, x, y):
885 return self._getints(
886 self.tk.call(
887 'grid', 'location', self._w, x, y)) or None
888 _noarg_ = ['_noarg_']
889 def propagate(self, flag=_noarg_):
890 if flag is Grid._noarg_:
891 return self._getboolean(self.tk.call(
892 'grid', 'propagate', self._w))
893 else:
894 self.tk.call('grid', 'propagate', self._w, flag)
895 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000896 def rowconfigure(self, index, cnf={}, **kw):
897 if type(cnf) is not DictionaryType and not kw:
898 options = self._options({cnf: None})
899 else:
900 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000901 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000902 ('grid', 'rowconfigure', self._w, index)
903 + options)
904 if options == ('-minsize', None):
905 return self.tk.getint(res) or None
906 elif options == ('-weight', None):
907 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000908 def size(self):
909 return self._getints(
910 self.tk.call('grid', 'size', self._w)) or None
911 def slaves(self, *args):
912 return map(self._nametowidget,
913 self.tk.splitlist(
914 apply(self.tk.call,
915 ('grid', 'slaves', self._w) + args)))
916 grid_slaves = slaves
917
918class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000919 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000920 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000921 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000922 if not _default_root:
923 _default_root = Tk()
924 master = _default_root
925 if not _default_root:
926 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000927 self.master = master
928 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000929 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000930 if cnf.has_key('name'):
931 name = cnf['name']
932 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000933 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000934 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000935 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000936 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000937 self._w = '.' + name
938 else:
939 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000940 self.children = {}
941 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000942 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000943 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000944 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
945 if kw:
946 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000947 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000948 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000949 classes = []
950 for k in cnf.keys():
951 if type(k) is ClassType:
952 classes.append((k, cnf[k]))
953 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000954 apply(self.tk.call,
955 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000956 for k, v in classes:
957 k.config(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +0000958 def destroy(self):
959 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000960 if self.master.children.has_key(self._name):
961 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000962 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000963 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000964 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000965 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000966
967class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000968 def __init__(self, master=None, cnf={}, **kw):
969 if kw:
970 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000971 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000972 for wmkey in ['screen', 'class_', 'class', 'visual',
973 'colormap']:
974 if cnf.has_key(wmkey):
975 val = cnf[wmkey]
976 # TBD: a hack needed because some keys
977 # are not valid as keyword arguments
978 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
979 else: opt = '-'+wmkey
980 extra = extra + (opt, val)
981 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000982 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000983 root = self._root()
984 self.iconname(root.iconname())
985 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000986
987class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000988 def __init__(self, master=None, cnf={}, **kw):
989 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000990 def tkButtonEnter(self, *dummy):
991 self.tk.call('tkButtonEnter', self._w)
992 def tkButtonLeave(self, *dummy):
993 self.tk.call('tkButtonLeave', self._w)
994 def tkButtonDown(self, *dummy):
995 self.tk.call('tkButtonDown', self._w)
996 def tkButtonUp(self, *dummy):
997 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000998 def tkButtonInvoke(self, *dummy):
999 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001000 def flash(self):
1001 self.tk.call(self._w, 'flash')
1002 def invoke(self):
1003 self.tk.call(self._w, 'invoke')
1004
1005# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001006# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001007def AtEnd():
1008 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001009def AtInsert(*args):
1010 s = 'insert'
1011 for a in args:
1012 if a: s = s + (' ' + a)
1013 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001014def AtSelFirst():
1015 return 'sel.first'
1016def AtSelLast():
1017 return 'sel.last'
1018def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001019 if y is None:
1020 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001021 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001022 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001023
1024class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001025 def __init__(self, master=None, cnf={}, **kw):
1026 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001027 def addtag(self, *args):
1028 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001029 def addtag_above(self, newtag, tagOrId):
1030 self.addtag(newtag, 'above', tagOrId)
1031 def addtag_all(self, newtag):
1032 self.addtag(newtag, 'all')
1033 def addtag_below(self, newtag, tagOrId):
1034 self.addtag(newtag, 'below', tagOrId)
1035 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1036 self.addtag(newtag, 'closest', x, y, halo, start)
1037 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1038 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1039 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1040 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1041 def addtag_withtag(self, newtag, tagOrId):
1042 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001043 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001044 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001045 def tag_unbind(self, tagOrId, sequence):
1046 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001047 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001048 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001049 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001050 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001051 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001052 self._w, 'canvasx', screenx, gridspacing))
1053 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001054 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001055 self._w, 'canvasy', screeny, gridspacing))
1056 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001057 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001058 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001059 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001060 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001061 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001062 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001063 args = args[:-1]
1064 else:
1065 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001066 return self.tk.getint(apply(
1067 self.tk.call,
1068 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001069 + args + self._options(cnf, kw)))
1070 def create_arc(self, *args, **kw):
1071 return self._create('arc', args, kw)
1072 def create_bitmap(self, *args, **kw):
1073 return self._create('bitmap', args, kw)
1074 def create_image(self, *args, **kw):
1075 return self._create('image', args, kw)
1076 def create_line(self, *args, **kw):
1077 return self._create('line', args, kw)
1078 def create_oval(self, *args, **kw):
1079 return self._create('oval', args, kw)
1080 def create_polygon(self, *args, **kw):
1081 return self._create('polygon', args, kw)
1082 def create_rectangle(self, *args, **kw):
1083 return self._create('rectangle', args, kw)
1084 def create_text(self, *args, **kw):
1085 return self._create('text', args, kw)
1086 def create_window(self, *args, **kw):
1087 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001088 def dchars(self, *args):
1089 self._do('dchars', args)
1090 def delete(self, *args):
1091 self._do('delete', args)
1092 def dtag(self, *args):
1093 self._do('dtag', args)
1094 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001095 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001096 def find_above(self, tagOrId):
1097 return self.find('above', tagOrId)
1098 def find_all(self):
1099 return self.find('all')
1100 def find_below(self, tagOrId):
1101 return self.find('below', tagOrId)
1102 def find_closest(self, x, y, halo=None, start=None):
1103 return self.find('closest', x, y, halo, start)
1104 def find_enclosed(self, x1, y1, x2, y2):
1105 return self.find('enclosed', x1, y1, x2, y2)
1106 def find_overlapping(self, x1, y1, x2, y2):
1107 return self.find('overlapping', x1, y1, x2, y2)
1108 def find_withtag(self, tagOrId):
1109 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001110 def focus(self, *args):
1111 return self._do('focus', args)
1112 def gettags(self, *args):
1113 return self.tk.splitlist(self._do('gettags', args))
1114 def icursor(self, *args):
1115 self._do('icursor', args)
1116 def index(self, *args):
1117 return self.tk.getint(self._do('index', args))
1118 def insert(self, *args):
1119 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001120 def itemcget(self, tagOrId, option):
1121 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001122 def itemconfig(self, tagOrId, cnf=None, **kw):
1123 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001124 cnf = {}
1125 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001126 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001127 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1128 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001129 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001130 x = self.tk.split(self._do('itemconfigure',
1131 (tagOrId, '-'+cnf,)))
1132 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001133 self._do('itemconfigure', (tagOrId,)
1134 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001135 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001136 def lower(self, *args):
1137 self._do('lower', args)
1138 def move(self, *args):
1139 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001140 def postscript(self, cnf={}, **kw):
1141 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001142 def tkraise(self, *args):
1143 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001144 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001145 def scale(self, *args):
1146 self._do('scale', args)
1147 def scan_mark(self, x, y):
1148 self.tk.call(self._w, 'scan', 'mark', x, y)
1149 def scan_dragto(self, x, y):
1150 self.tk.call(self._w, 'scan', 'dragto', x, y)
1151 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001152 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001153 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001154 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001155 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001156 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001157 def select_item(self):
1158 self.tk.call(self._w, 'select', 'item')
1159 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001160 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001161 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001162 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001163 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001164 if not args:
1165 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001166 apply(self.tk.call, (self._w, 'xview')+args)
1167 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001168 if not args:
1169 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001170 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001171
1172class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001173 def __init__(self, master=None, cnf={}, **kw):
1174 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001175 def deselect(self):
1176 self.tk.call(self._w, 'deselect')
1177 def flash(self):
1178 self.tk.call(self._w, 'flash')
1179 def invoke(self):
1180 self.tk.call(self._w, 'invoke')
1181 def select(self):
1182 self.tk.call(self._w, 'select')
1183 def toggle(self):
1184 self.tk.call(self._w, 'toggle')
1185
1186class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001187 def __init__(self, master=None, cnf={}, **kw):
1188 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001189 def delete(self, first, last=None):
1190 self.tk.call(self._w, 'delete', first, last)
1191 def get(self):
1192 return self.tk.call(self._w, 'get')
1193 def icursor(self, index):
1194 self.tk.call(self._w, 'icursor', index)
1195 def index(self, index):
1196 return self.tk.getint(self.tk.call(
1197 self._w, 'index', index))
1198 def insert(self, index, string):
1199 self.tk.call(self._w, 'insert', index, string)
1200 def scan_mark(self, x):
1201 self.tk.call(self._w, 'scan', 'mark', x)
1202 def scan_dragto(self, x):
1203 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001204 def selection_adjust(self, index):
1205 self.tk.call(self._w, 'selection', 'adjust', index)
1206 select_adjust = selection_adjust
1207 def selection_clear(self):
1208 self.tk.call(self._w, 'selection', 'clear')
1209 select_clear = selection_clear
1210 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001211 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001212 select_from = selection_from
1213 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001214 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001215 self.tk.call(self._w, 'selection', 'present'))
1216 select_present = selection_present
1217 def selection_range(self, start, end):
1218 self.tk.call(self._w, 'selection', 'range', start, end)
1219 select_range = selection_range
1220 def selection_to(self, index):
1221 self.tk.call(self._w, 'selection', 'to', index)
1222 select_to = selection_to
1223 def xview(self, index):
1224 self.tk.call(self._w, 'xview', index)
1225 def xview_moveto(self, fraction):
1226 self.tk.call(self._w, 'xview', 'moveto', fraction)
1227 def xview_scroll(self, number, what):
1228 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001229
1230class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001231 def __init__(self, master=None, cnf={}, **kw):
1232 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001233 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001234 if cnf.has_key('class_'):
1235 extra = ('-class', cnf['class_'])
1236 del cnf['class_']
1237 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001238 extra = ('-class', cnf['class'])
1239 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001240 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001241
1242class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001243 def __init__(self, master=None, cnf={}, **kw):
1244 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001245
Guido van Rossum18468821994-06-20 07:49:28 +00001246class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001247 def __init__(self, master=None, cnf={}, **kw):
1248 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001249 def activate(self, index):
1250 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001251 def bbox(self, *args):
1252 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001253 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001254 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001255 return self.tk.splitlist(self.tk.call(
1256 self._w, 'curselection'))
1257 def delete(self, first, last=None):
1258 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001259 def get(self, first, last=None):
1260 if last:
1261 return self.tk.splitlist(self.tk.call(
1262 self._w, 'get', first, last))
1263 else:
1264 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001265 def insert(self, index, *elements):
1266 apply(self.tk.call,
1267 (self._w, 'insert', index) + elements)
1268 def nearest(self, y):
1269 return self.tk.getint(self.tk.call(
1270 self._w, 'nearest', y))
1271 def scan_mark(self, x, y):
1272 self.tk.call(self._w, 'scan', 'mark', x, y)
1273 def scan_dragto(self, x, y):
1274 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001275 def see(self, index):
1276 self.tk.call(self._w, 'see', index)
1277 def index(self, index):
1278 i = self.tk.call(self._w, 'index', index)
1279 if i == 'none': return None
1280 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001281 def select_anchor(self, index):
1282 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001283 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001284 def select_clear(self, first, last=None):
1285 self.tk.call(self._w,
1286 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001287 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001288 def select_includes(self, index):
1289 return self.tk.getboolean(self.tk.call(
1290 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001291 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001292 def select_set(self, first, last=None):
1293 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001294 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001295 def size(self):
1296 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001297 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001298 if not what:
1299 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001300 apply(self.tk.call, (self._w, 'xview')+what)
1301 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001302 if not what:
1303 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001304 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001305
1306class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001307 def __init__(self, master=None, cnf={}, **kw):
1308 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001309 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001310 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001311 def tk_mbPost(self):
1312 self.tk.call('tk_mbPost', self._w)
1313 def tk_mbUnpost(self):
1314 self.tk.call('tk_mbUnpost')
1315 def tk_traverseToMenu(self, char):
1316 self.tk.call('tk_traverseToMenu', self._w, char)
1317 def tk_traverseWithinMenu(self, char):
1318 self.tk.call('tk_traverseWithinMenu', self._w, char)
1319 def tk_getMenuButtons(self):
1320 return self.tk.call('tk_getMenuButtons', self._w)
1321 def tk_nextMenu(self, count):
1322 self.tk.call('tk_nextMenu', count)
1323 def tk_nextMenuEntry(self, count):
1324 self.tk.call('tk_nextMenuEntry', count)
1325 def tk_invokeMenu(self):
1326 self.tk.call('tk_invokeMenu', self._w)
1327 def tk_firstMenu(self):
1328 self.tk.call('tk_firstMenu', self._w)
1329 def tk_mbButtonDown(self):
1330 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001331 def tk_popup(self, x, y, entry=""):
1332 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001333 def activate(self, index):
1334 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001335 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001336 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001337 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001338 def add_cascade(self, cnf={}, **kw):
1339 self.add('cascade', cnf or kw)
1340 def add_checkbutton(self, cnf={}, **kw):
1341 self.add('checkbutton', cnf or kw)
1342 def add_command(self, cnf={}, **kw):
1343 self.add('command', cnf or kw)
1344 def add_radiobutton(self, cnf={}, **kw):
1345 self.add('radiobutton', cnf or kw)
1346 def add_separator(self, cnf={}, **kw):
1347 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001348 def insert(self, index, itemType, cnf={}, **kw):
1349 apply(self.tk.call, (self._w, 'insert', index, itemType)
1350 + self._options(cnf, kw))
1351 def insert_cascade(self, index, cnf={}, **kw):
1352 self.insert(index, 'cascade', cnf or kw)
1353 def insert_checkbutton(self, index, cnf={}, **kw):
1354 self.insert(index, 'checkbutton', cnf or kw)
1355 def insert_command(self, index, cnf={}, **kw):
1356 self.insert(index, 'command', cnf or kw)
1357 def insert_radiobutton(self, index, cnf={}, **kw):
1358 self.insert(index, 'radiobutton', cnf or kw)
1359 def insert_separator(self, index, cnf={}, **kw):
1360 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001361 def delete(self, index1, index2=None):
1362 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001363 def entryconfig(self, index, cnf=None, **kw):
1364 if cnf is None and not kw:
1365 cnf = {}
1366 for x in self.tk.split(apply(self.tk.call,
1367 (self._w, 'entryconfigure', index))):
1368 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1369 return cnf
1370 if type(cnf) == StringType and not kw:
1371 x = self.tk.split(apply(self.tk.call,
1372 (self._w, 'entryconfigure', index, '-'+cnf)))
1373 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001374 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001375 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001376 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001377 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001378 i = self.tk.call(self._w, 'index', index)
1379 if i == 'none': return None
1380 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001381 def invoke(self, index):
1382 return self.tk.call(self._w, 'invoke', index)
1383 def post(self, x, y):
1384 self.tk.call(self._w, 'post', x, y)
1385 def unpost(self):
1386 self.tk.call(self._w, 'unpost')
1387 def yposition(self, index):
1388 return self.tk.getint(self.tk.call(
1389 self._w, 'yposition', index))
1390
1391class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001392 def __init__(self, master=None, cnf={}, **kw):
1393 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001394
1395class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001396 def __init__(self, master=None, cnf={}, **kw):
1397 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001398
1399class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001400 def __init__(self, master=None, cnf={}, **kw):
1401 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001402 def deselect(self):
1403 self.tk.call(self._w, 'deselect')
1404 def flash(self):
1405 self.tk.call(self._w, 'flash')
1406 def invoke(self):
1407 self.tk.call(self._w, 'invoke')
1408 def select(self):
1409 self.tk.call(self._w, 'select')
1410
1411class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001412 def __init__(self, master=None, cnf={}, **kw):
1413 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001414 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001415 value = self.tk.call(self._w, 'get')
1416 try:
1417 return self.tk.getint(value)
1418 except TclError:
1419 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001420 def set(self, value):
1421 self.tk.call(self._w, 'set', value)
1422
1423class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001424 def __init__(self, master=None, cnf={}, **kw):
1425 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001426 def activate(self, index):
1427 self.tk.call(self._w, 'activate', index)
1428 def delta(self, deltax, deltay):
1429 return self.getdouble(self.tk.call(
1430 self._w, 'delta', deltax, deltay))
1431 def fraction(self, x, y):
1432 return self.getdouble(self.tk.call(
1433 self._w, 'fraction', x, y))
1434 def identify(self, x, y):
1435 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001436 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001437 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001438 def set(self, *args):
1439 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001440
1441class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001442 def __init__(self, master=None, cnf={}, **kw):
1443 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001444 def bbox(self, *args):
1445 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001446 def tk_textSelectTo(self, index):
1447 self.tk.call('tk_textSelectTo', self._w, index)
1448 def tk_textBackspace(self):
1449 self.tk.call('tk_textBackspace', self._w)
1450 def tk_textIndexCloser(self, a, b, c):
1451 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1452 def tk_textResetAnchor(self, index):
1453 self.tk.call('tk_textResetAnchor', self._w, index)
1454 def compare(self, index1, op, index2):
1455 return self.tk.getboolean(self.tk.call(
1456 self._w, 'compare', index1, op, index2))
1457 def debug(self, boolean=None):
1458 return self.tk.getboolean(self.tk.call(
1459 self._w, 'debug', boolean))
1460 def delete(self, index1, index2=None):
1461 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001462 def dlineinfo(self, index):
1463 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001464 def get(self, index1, index2=None):
1465 return self.tk.call(self._w, 'get', index1, index2)
1466 def index(self, index):
1467 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001468 def insert(self, index, chars, *args):
1469 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001470 def mark_gravity(self, markName, direction=None):
1471 return apply(self.tk.call,
1472 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001473 def mark_names(self):
1474 return self.tk.splitlist(self.tk.call(
1475 self._w, 'mark', 'names'))
1476 def mark_set(self, markName, index):
1477 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001478 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001479 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001480 def scan_mark(self, x, y):
1481 self.tk.call(self._w, 'scan', 'mark', x, y)
1482 def scan_dragto(self, x, y):
1483 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001484 def search(self, pattern, index, stopindex=None,
1485 forwards=None, backwards=None, exact=None,
1486 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001487 args = [self._w, 'search']
1488 if forwards: args.append('-forwards')
1489 if backwards: args.append('-backwards')
1490 if exact: args.append('-exact')
1491 if regexp: args.append('-regexp')
1492 if nocase: args.append('-nocase')
1493 if count: args.append('-count'); args.append(count)
1494 if pattern[0] == '-': args.append('--')
1495 args.append(pattern)
1496 args.append(index)
1497 if stopindex: args.append(stopindex)
1498 return apply(self.tk.call, tuple(args))
1499 def see(self, index):
1500 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001501 def tag_add(self, tagName, index1, index2=None):
1502 self.tk.call(
1503 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001504 def tag_unbind(self, tagName, sequence):
1505 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001506 def tag_bind(self, tagName, sequence, func, add=None):
1507 return self._bind((self._w, 'tag', 'bind', tagName),
1508 sequence, func, add)
1509 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001510 if option[:1] != '-':
1511 option = '-' + option
1512 if option[-1:] == '_':
1513 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001514 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001515 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001516 if type(cnf) == StringType:
1517 x = self.tk.split(self.tk.call(
1518 self._w, 'tag', 'configure', tagName, '-'+cnf))
1519 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001520 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001521 (self._w, 'tag', 'configure', tagName)
1522 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001523 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001524 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001525 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001526 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001527 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001528 def tag_names(self, index=None):
1529 return self.tk.splitlist(
1530 self.tk.call(self._w, 'tag', 'names', index))
1531 def tag_nextrange(self, tagName, index1, index2=None):
1532 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001533 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001534 def tag_raise(self, tagName, aboveThis=None):
1535 self.tk.call(
1536 self._w, 'tag', 'raise', tagName, aboveThis)
1537 def tag_ranges(self, tagName):
1538 return self.tk.splitlist(self.tk.call(
1539 self._w, 'tag', 'ranges', tagName))
1540 def tag_remove(self, tagName, index1, index2=None):
1541 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001542 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001543 def window_cget(self, index, option):
1544 return self.tk.call(self._w, 'window', 'cget', index, option)
1545 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001546 if type(cnf) == StringType:
1547 x = self.tk.split(self.tk.call(
1548 self._w, 'window', 'configure',
1549 index, '-'+cnf))
1550 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001551 apply(self.tk.call,
1552 (self._w, 'window', 'configure', index)
1553 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001554 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001555 def window_create(self, index, cnf={}, **kw):
1556 apply(self.tk.call,
1557 (self._w, 'window', 'create', index)
1558 + self._options(cnf, kw))
1559 def window_names(self):
1560 return self.tk.splitlist(
1561 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001562 def xview(self, *what):
1563 if not what:
1564 return self._getdoubles(self.tk.call(self._w, 'xview'))
1565 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001566 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001567 if not what:
1568 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001569 apply(self.tk.call, (self._w, 'yview')+what)
1570 def yview_pickplace(self, *what):
1571 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001572
Guido van Rossum28574b51996-10-21 15:16:51 +00001573class _setit:
1574 def __init__(self, var, value):
1575 self.__value = value
1576 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001577 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001578 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001579
1580class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001581 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001582 kw = {"borderwidth": 2, "textvariable": variable,
1583 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1584 "highlightthickness": 2}
1585 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001586 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001587 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1588 self.menuname = menu._w
1589 menu.add_command(label=value, command=_setit(variable, value))
1590 for v in values:
1591 menu.add_command(label=v, command=_setit(variable, v))
1592 self["menu"] = menu
1593
1594 def __getitem__(self, name):
1595 if name == 'menu':
1596 return self.__menu
1597 return Widget.__getitem__(self, name)
1598
1599 def destroy(self):
1600 Menubutton.destroy(self)
1601 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001602
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001603class Image:
1604 def __init__(self, imgtype, name=None, cnf={}, **kw):
1605 self.name = None
1606 master = _default_root
1607 if not master: raise RuntimeError, 'Too early to create image'
1608 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001609 if not name:
1610 name = `id(self)`
1611 # The following is needed for systems where id(x)
1612 # can return a negative number, such as Linux/m68k:
1613 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001614 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1615 elif kw: cnf = kw
1616 options = ()
1617 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001618 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001619 v = self._register(v)
1620 options = options + ('-'+k, v)
1621 apply(self.tk.call,
1622 ('image', 'create', imgtype, name,) + options)
1623 self.name = name
1624 def __str__(self): return self.name
1625 def __del__(self):
1626 if self.name:
1627 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001628 def __setitem__(self, key, value):
1629 self.tk.call(self.name, 'configure', '-'+key, value)
1630 def __getitem__(self, key):
1631 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001632 def config(self, **kw):
1633 res = ()
1634 for k, v in _cnfmerge(kw).items():
1635 if v is not None:
1636 if k[-1] == '_': k = k[:-1]
1637 if callable(v):
1638 v = self._register(v)
1639 res = res + ('-'+k, v)
1640 apply(self.tk.call, (self.name, 'config') + res)
1641 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001642 def height(self):
1643 return self.tk.getint(
1644 self.tk.call('image', 'height', self.name))
1645 def type(self):
1646 return self.tk.call('image', 'type', self.name)
1647 def width(self):
1648 return self.tk.getint(
1649 self.tk.call('image', 'width', self.name))
1650
1651class PhotoImage(Image):
1652 def __init__(self, name=None, cnf={}, **kw):
1653 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001654 def blank(self):
1655 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001656 def cget(self, option):
1657 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001658 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001659 def __getitem__(self, key):
1660 return self.tk.call(self.name, 'cget', '-' + key)
1661 def copy(self):
1662 destImage = PhotoImage()
1663 self.tk.call(destImage, 'copy', self.name)
1664 return destImage
1665 def zoom(self,x,y=''):
1666 destImage = PhotoImage()
1667 if y=='': y=x
1668 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1669 return destImage
1670 def subsample(self,x,y=''):
1671 destImage = PhotoImage()
1672 if y=='': y=x
1673 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1674 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001675 def get(self, x, y):
1676 return self.tk.call(self.name, 'get', x, y)
1677 def put(self, data, to=None):
1678 args = (self.name, 'put', data)
1679 if to:
1680 args = args + to
1681 apply(self.tk.call, args)
1682 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001683 def write(self, filename, format=None, from_coords=None):
1684 args = (self.name, 'write', filename)
1685 if format:
1686 args = args + ('-format', format)
1687 if from_coords:
1688 args = args + ('-from',) + tuple(from_coords)
1689 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001690
1691class BitmapImage(Image):
1692 def __init__(self, name=None, cnf={}, **kw):
1693 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1694
1695def image_names(): return _default_root.tk.call('image', 'names')
1696def image_types(): return _default_root.tk.call('image', 'types')
1697
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001698######################################################################
1699# Extensions:
1700
1701class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001702 def __init__(self, master=None, cnf={}, **kw):
1703 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001704 self.bind('<Any-Enter>', self.tkButtonEnter)
1705 self.bind('<Any-Leave>', self.tkButtonLeave)
1706 self.bind('<1>', self.tkButtonDown)
1707 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001708
1709class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001710 def __init__(self, master=None, cnf={}, **kw):
1711 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001712 self.bind('<Any-Enter>', self.tkButtonEnter)
1713 self.bind('<Any-Leave>', self.tkButtonLeave)
1714 self.bind('<1>', self.tkButtonDown)
1715 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1716 self['fg'] = self['bg']
1717 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001718
Guido van Rossumc417ef81996-08-21 23:38:59 +00001719######################################################################
1720# Test:
1721
1722def _test():
1723 root = Tk()
1724 label = Label(root, text="Proof-of-existence test for Tk")
1725 label.pack()
1726 test = Button(root, text="Click me!",
1727 command=lambda root=root: root.test.config(
1728 text="[%s]" % root.test['text']))
1729 test.pack()
1730 root.test = test
1731 quit = Button(root, text="QUIT", command=root.destroy)
1732 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001733 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001734 root.mainloop()
1735
1736if __name__ == '__main__':
1737 _test()
1738
Guido van Rossum37dcab11996-05-16 16:00:19 +00001739
1740# Emacs cruft
1741# Local Variables:
1742# py-indent-offset: 8
1743# End: