blob: ddb017ee0f11e304f0139daa132de1eeea0e311c [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 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 Rossuma5773dd1995-09-07 19:22:00 +0000664 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000665 self.master = None
666 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000667 if baseName is None:
668 import sys, os
669 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000670 baseName, ext = os.path.splitext(baseName)
671 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000672 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000673 try:
674 # Disable event scanning except for Command-Period
675 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000676 try:
677 MacOS.SchedParams(1, 0)
678 except AttributeError:
679 # pre-1.5, use old routine
680 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000681 except ImportError:
682 pass
683 else:
684 # Work around nasty MacTk bug
685 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000686 # Version sanity checks
687 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000688 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000689 raise RuntimeError, \
690 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000691 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000692 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000693 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000694 raise RuntimeError, \
695 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000696 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000697 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000698 raise RuntimeError, \
699 "Tk 4.0 or higher is required; found Tk %s" \
700 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000701 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000702 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000703 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000704 if not _default_root:
705 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000706 def destroy(self):
707 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000708 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000709 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000710 global _default_root
711 if _default_root is self:
712 _default_root = None
Guido van Rossum18468821994-06-20 07:49:28 +0000713 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000714 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000715 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000716 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000717 if os.environ.has_key('HOME'): home = os.environ['HOME']
718 else: home = os.curdir
719 class_tcl = os.path.join(home, '.%s.tcl' % className)
720 class_py = os.path.join(home, '.%s.py' % className)
721 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
722 base_py = os.path.join(home, '.%s.py' % baseName)
723 dir = {'self': self}
724 exec 'from Tkinter import *' in dir
725 if os.path.isfile(class_tcl):
726 print 'source', `class_tcl`
727 self.tk.call('source', class_tcl)
728 if os.path.isfile(class_py):
729 print 'execfile', `class_py`
730 execfile(class_py, dir)
731 if os.path.isfile(base_tcl):
732 print 'source', `base_tcl`
733 self.tk.call('source', base_tcl)
734 if os.path.isfile(base_py):
735 print 'execfile', `base_py`
736 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000737 def report_callback_exception(self, exc, val, tb):
738 import traceback
739 print "Exception in Tkinter callback"
740 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000741
742class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000743 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000744 apply(self.tk.call,
745 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000746 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000747 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000748 pack = config
749 def __setitem__(self, key, value):
750 Pack.config({key: value})
751 def forget(self):
752 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000753 pack_forget = forget
754 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000755 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000756 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000757 dict = {}
758 for i in range(0, len(words), 2):
759 key = words[i][1:]
760 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000761 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000762 value = self._nametowidget(value)
763 dict[key] = value
764 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000765 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000766 _noarg_ = ['_noarg_']
767 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000768 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000769 return self._getboolean(self.tk.call(
770 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000771 else:
772 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000773 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000774 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000775 return map(self._nametowidget,
776 self.tk.splitlist(
777 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000778 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000779
780class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000781 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000782 for k in ['in_']:
783 if kw.has_key(k):
784 kw[k[:-1]] = kw[k]
785 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000786 apply(self.tk.call,
787 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000788 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000789 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000790 place = config
791 def __setitem__(self, key, value):
792 Place.config({key: value})
793 def forget(self):
794 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000795 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000796 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000797 words = self.tk.splitlist(
798 self.tk.call('place', 'info', self._w))
799 dict = {}
800 for i in range(0, len(words), 2):
801 key = words[i][1:]
802 value = words[i+1]
803 if value[:1] == '.':
804 value = self._nametowidget(value)
805 dict[key] = value
806 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000807 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000808 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000809 return map(self._nametowidget,
810 self.tk.splitlist(
811 self.tk.call(
812 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000813 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000814
Guido van Rossum37dcab11996-05-16 16:00:19 +0000815class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000816 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000817 def config(self, cnf={}, **kw):
818 apply(self.tk.call,
819 ('grid', 'configure', self._w)
820 + self._options(cnf, kw))
821 grid = config
822 def __setitem__(self, key, value):
823 Grid.config({key: value})
824 def bbox(self, column, row):
825 return self._getints(
826 self.tk.call(
827 'grid', 'bbox', self._w, column, row)) or None
828 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000829 def columnconfigure(self, index, cnf={}, **kw):
830 if type(cnf) is not DictionaryType and not kw:
831 options = self._options({cnf: None})
832 else:
833 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000834 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000835 ('grid', 'columnconfigure', self._w, index)
836 + options)
837 if options == ('-minsize', None):
838 return self.tk.getint(res) or None
839 elif options == ('-weight', None):
840 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000841 def forget(self):
842 self.tk.call('grid', 'forget', self._w)
843 grid_forget = forget
844 def info(self):
845 words = self.tk.splitlist(
846 self.tk.call('grid', 'info', self._w))
847 dict = {}
848 for i in range(0, len(words), 2):
849 key = words[i][1:]
850 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000851 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000852 value = self._nametowidget(value)
853 dict[key] = value
854 return dict
855 grid_info = info
856 def location(self, x, y):
857 return self._getints(
858 self.tk.call(
859 'grid', 'location', self._w, x, y)) or None
860 _noarg_ = ['_noarg_']
861 def propagate(self, flag=_noarg_):
862 if flag is Grid._noarg_:
863 return self._getboolean(self.tk.call(
864 'grid', 'propagate', self._w))
865 else:
866 self.tk.call('grid', 'propagate', self._w, flag)
867 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000868 def rowconfigure(self, index, cnf={}, **kw):
869 if type(cnf) is not DictionaryType and not kw:
870 options = self._options({cnf: None})
871 else:
872 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000873 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000874 ('grid', 'rowconfigure', self._w, index)
875 + options)
876 if options == ('-minsize', None):
877 return self.tk.getint(res) or None
878 elif options == ('-weight', None):
879 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000880 def size(self):
881 return self._getints(
882 self.tk.call('grid', 'size', self._w)) or None
883 def slaves(self, *args):
884 return map(self._nametowidget,
885 self.tk.splitlist(
886 apply(self.tk.call,
887 ('grid', 'slaves', self._w) + args)))
888 grid_slaves = slaves
889
890class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000891 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000892 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000893 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000894 if not _default_root:
895 _default_root = Tk()
896 master = _default_root
897 if not _default_root:
898 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000899 self.master = master
900 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000901 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000902 if cnf.has_key('name'):
903 name = cnf['name']
904 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000905 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000906 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000907 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000908 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000909 self._w = '.' + name
910 else:
911 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000912 self.children = {}
913 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000914 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000915 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000916 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
917 if kw:
918 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000919 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000920 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000921 classes = []
922 for k in cnf.keys():
923 if type(k) is ClassType:
924 classes.append((k, cnf[k]))
925 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000926 apply(self.tk.call,
927 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000928 for k, v in classes:
929 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000930 def config(self, cnf=None, **kw):
931 # XXX ought to generalize this so tag_config etc. can use it
932 if kw:
933 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000934 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000935 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000936 if cnf is None:
937 cnf = {}
938 for x in self.tk.split(
939 self.tk.call(self._w, 'configure')):
940 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
941 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000942 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000943 x = self.tk.split(self.tk.call(
944 self._w, 'configure', '-'+cnf))
945 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000946 apply(self.tk.call, (self._w, 'configure')
947 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000948 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000949 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000950 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000951 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000952 def __setitem__(self, key, value):
953 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000954 def keys(self):
955 return map(lambda x: x[0][1:],
956 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000957 def __str__(self):
958 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000959 def destroy(self):
960 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000961 if self.master.children.has_key(self._name):
962 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000963 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000964 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000965 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000966 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000967
968class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000969 def __init__(self, master=None, cnf={}, **kw):
970 if kw:
971 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000972 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000973 for wmkey in ['screen', 'class_', 'class', 'visual',
974 'colormap']:
975 if cnf.has_key(wmkey):
976 val = cnf[wmkey]
977 # TBD: a hack needed because some keys
978 # are not valid as keyword arguments
979 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
980 else: opt = '-'+wmkey
981 extra = extra + (opt, val)
982 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000983 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000984 root = self._root()
985 self.iconname(root.iconname())
986 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000987
988class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000989 def __init__(self, master=None, cnf={}, **kw):
990 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000991 def tkButtonEnter(self, *dummy):
992 self.tk.call('tkButtonEnter', self._w)
993 def tkButtonLeave(self, *dummy):
994 self.tk.call('tkButtonLeave', self._w)
995 def tkButtonDown(self, *dummy):
996 self.tk.call('tkButtonDown', self._w)
997 def tkButtonUp(self, *dummy):
998 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000999 def tkButtonInvoke(self, *dummy):
1000 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001001 def flash(self):
1002 self.tk.call(self._w, 'flash')
1003 def invoke(self):
1004 self.tk.call(self._w, 'invoke')
1005
1006# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001007# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001008def AtEnd():
1009 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001010def AtInsert(*args):
1011 s = 'insert'
1012 for a in args:
1013 if a: s = s + (' ' + a)
1014 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001015def AtSelFirst():
1016 return 'sel.first'
1017def AtSelLast():
1018 return 'sel.last'
1019def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001020 if y is None:
1021 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001022 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001023 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001024
1025class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001026 def __init__(self, master=None, cnf={}, **kw):
1027 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001028 def addtag(self, *args):
1029 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001030 def addtag_above(self, newtag, tagOrId):
1031 self.addtag(newtag, 'above', tagOrId)
1032 def addtag_all(self, newtag):
1033 self.addtag(newtag, 'all')
1034 def addtag_below(self, newtag, tagOrId):
1035 self.addtag(newtag, 'below', tagOrId)
1036 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1037 self.addtag(newtag, 'closest', x, y, halo, start)
1038 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1039 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1040 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1041 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1042 def addtag_withtag(self, newtag, tagOrId):
1043 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001044 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001045 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001046 def tag_unbind(self, tagOrId, sequence):
1047 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001048 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001049 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001050 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001051 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001052 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001053 self._w, 'canvasx', screenx, gridspacing))
1054 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001055 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001056 self._w, 'canvasy', screeny, gridspacing))
1057 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001058 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001059 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001060 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001061 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001062 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001063 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001064 args = args[:-1]
1065 else:
1066 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001067 return self.tk.getint(apply(
1068 self.tk.call,
1069 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001070 + args + self._options(cnf, kw)))
1071 def create_arc(self, *args, **kw):
1072 return self._create('arc', args, kw)
1073 def create_bitmap(self, *args, **kw):
1074 return self._create('bitmap', args, kw)
1075 def create_image(self, *args, **kw):
1076 return self._create('image', args, kw)
1077 def create_line(self, *args, **kw):
1078 return self._create('line', args, kw)
1079 def create_oval(self, *args, **kw):
1080 return self._create('oval', args, kw)
1081 def create_polygon(self, *args, **kw):
1082 return self._create('polygon', args, kw)
1083 def create_rectangle(self, *args, **kw):
1084 return self._create('rectangle', args, kw)
1085 def create_text(self, *args, **kw):
1086 return self._create('text', args, kw)
1087 def create_window(self, *args, **kw):
1088 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001089 def dchars(self, *args):
1090 self._do('dchars', args)
1091 def delete(self, *args):
1092 self._do('delete', args)
1093 def dtag(self, *args):
1094 self._do('dtag', args)
1095 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001096 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001097 def find_above(self, tagOrId):
1098 return self.find('above', tagOrId)
1099 def find_all(self):
1100 return self.find('all')
1101 def find_below(self, tagOrId):
1102 return self.find('below', tagOrId)
1103 def find_closest(self, x, y, halo=None, start=None):
1104 return self.find('closest', x, y, halo, start)
1105 def find_enclosed(self, x1, y1, x2, y2):
1106 return self.find('enclosed', x1, y1, x2, y2)
1107 def find_overlapping(self, x1, y1, x2, y2):
1108 return self.find('overlapping', x1, y1, x2, y2)
1109 def find_withtag(self, tagOrId):
1110 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001111 def focus(self, *args):
1112 return self._do('focus', args)
1113 def gettags(self, *args):
1114 return self.tk.splitlist(self._do('gettags', args))
1115 def icursor(self, *args):
1116 self._do('icursor', args)
1117 def index(self, *args):
1118 return self.tk.getint(self._do('index', args))
1119 def insert(self, *args):
1120 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001121 def itemcget(self, tagOrId, option):
1122 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001123 def itemconfig(self, tagOrId, cnf=None, **kw):
1124 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001125 cnf = {}
1126 for x in self.tk.split(
1127 self._do('itemconfigure', (tagOrId))):
1128 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1129 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001130 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001131 x = self.tk.split(self._do('itemconfigure',
1132 (tagOrId, '-'+cnf,)))
1133 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001134 self._do('itemconfigure', (tagOrId,)
1135 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001136 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001137 def lower(self, *args):
1138 self._do('lower', args)
1139 def move(self, *args):
1140 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001141 def postscript(self, cnf={}, **kw):
1142 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001143 def tkraise(self, *args):
1144 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001145 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001146 def scale(self, *args):
1147 self._do('scale', args)
1148 def scan_mark(self, x, y):
1149 self.tk.call(self._w, 'scan', 'mark', x, y)
1150 def scan_dragto(self, x, y):
1151 self.tk.call(self._w, 'scan', 'dragto', x, y)
1152 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001153 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001154 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001155 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001156 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001157 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001158 def select_item(self):
1159 self.tk.call(self._w, 'select', 'item')
1160 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001161 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001162 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001163 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001164 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001165 if not args:
1166 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001167 apply(self.tk.call, (self._w, 'xview')+args)
1168 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001169 if not args:
1170 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001171 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001172
1173class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001174 def __init__(self, master=None, cnf={}, **kw):
1175 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001176 def deselect(self):
1177 self.tk.call(self._w, 'deselect')
1178 def flash(self):
1179 self.tk.call(self._w, 'flash')
1180 def invoke(self):
1181 self.tk.call(self._w, 'invoke')
1182 def select(self):
1183 self.tk.call(self._w, 'select')
1184 def toggle(self):
1185 self.tk.call(self._w, 'toggle')
1186
1187class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001188 def __init__(self, master=None, cnf={}, **kw):
1189 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001190 def delete(self, first, last=None):
1191 self.tk.call(self._w, 'delete', first, last)
1192 def get(self):
1193 return self.tk.call(self._w, 'get')
1194 def icursor(self, index):
1195 self.tk.call(self._w, 'icursor', index)
1196 def index(self, index):
1197 return self.tk.getint(self.tk.call(
1198 self._w, 'index', index))
1199 def insert(self, index, string):
1200 self.tk.call(self._w, 'insert', index, string)
1201 def scan_mark(self, x):
1202 self.tk.call(self._w, 'scan', 'mark', x)
1203 def scan_dragto(self, x):
1204 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001205 def selection_adjust(self, index):
1206 self.tk.call(self._w, 'selection', 'adjust', index)
1207 select_adjust = selection_adjust
1208 def selection_clear(self):
1209 self.tk.call(self._w, 'selection', 'clear')
1210 select_clear = selection_clear
1211 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001212 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001213 select_from = selection_from
1214 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001215 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001216 self.tk.call(self._w, 'selection', 'present'))
1217 select_present = selection_present
1218 def selection_range(self, start, end):
1219 self.tk.call(self._w, 'selection', 'range', start, end)
1220 select_range = selection_range
1221 def selection_to(self, index):
1222 self.tk.call(self._w, 'selection', 'to', index)
1223 select_to = selection_to
1224 def xview(self, index):
1225 self.tk.call(self._w, 'xview', index)
1226 def xview_moveto(self, fraction):
1227 self.tk.call(self._w, 'xview', 'moveto', fraction)
1228 def xview_scroll(self, number, what):
1229 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001230
1231class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001232 def __init__(self, master=None, cnf={}, **kw):
1233 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001234 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001235 if cnf.has_key('class_'):
1236 extra = ('-class', cnf['class_'])
1237 del cnf['class_']
1238 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001239 extra = ('-class', cnf['class'])
1240 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001241 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001242
1243class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001244 def __init__(self, master=None, cnf={}, **kw):
1245 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001246
Guido van Rossum18468821994-06-20 07:49:28 +00001247class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001248 def __init__(self, master=None, cnf={}, **kw):
1249 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001250 def activate(self, index):
1251 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001252 def bbox(self, *args):
1253 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001254 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001255 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001256 return self.tk.splitlist(self.tk.call(
1257 self._w, 'curselection'))
1258 def delete(self, first, last=None):
1259 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001260 def get(self, first, last=None):
1261 if last:
1262 return self.tk.splitlist(self.tk.call(
1263 self._w, 'get', first, last))
1264 else:
1265 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001266 def insert(self, index, *elements):
1267 apply(self.tk.call,
1268 (self._w, 'insert', index) + elements)
1269 def nearest(self, y):
1270 return self.tk.getint(self.tk.call(
1271 self._w, 'nearest', y))
1272 def scan_mark(self, x, y):
1273 self.tk.call(self._w, 'scan', 'mark', x, y)
1274 def scan_dragto(self, x, y):
1275 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001276 def see(self, index):
1277 self.tk.call(self._w, 'see', index)
1278 def index(self, index):
1279 i = self.tk.call(self._w, 'index', index)
1280 if i == 'none': return None
1281 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001282 def select_anchor(self, index):
1283 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001284 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001285 def select_clear(self, first, last=None):
1286 self.tk.call(self._w,
1287 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001288 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001289 def select_includes(self, index):
1290 return self.tk.getboolean(self.tk.call(
1291 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001292 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001293 def select_set(self, first, last=None):
1294 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001295 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001296 def size(self):
1297 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001298 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001299 if not what:
1300 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001301 apply(self.tk.call, (self._w, 'xview')+what)
1302 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001303 if not what:
1304 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001305 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001306
1307class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001308 def __init__(self, master=None, cnf={}, **kw):
1309 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001310 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001311 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001312 def tk_mbPost(self):
1313 self.tk.call('tk_mbPost', self._w)
1314 def tk_mbUnpost(self):
1315 self.tk.call('tk_mbUnpost')
1316 def tk_traverseToMenu(self, char):
1317 self.tk.call('tk_traverseToMenu', self._w, char)
1318 def tk_traverseWithinMenu(self, char):
1319 self.tk.call('tk_traverseWithinMenu', self._w, char)
1320 def tk_getMenuButtons(self):
1321 return self.tk.call('tk_getMenuButtons', self._w)
1322 def tk_nextMenu(self, count):
1323 self.tk.call('tk_nextMenu', count)
1324 def tk_nextMenuEntry(self, count):
1325 self.tk.call('tk_nextMenuEntry', count)
1326 def tk_invokeMenu(self):
1327 self.tk.call('tk_invokeMenu', self._w)
1328 def tk_firstMenu(self):
1329 self.tk.call('tk_firstMenu', self._w)
1330 def tk_mbButtonDown(self):
1331 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001332 def tk_popup(self, x, y, entry=""):
1333 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001334 def activate(self, index):
1335 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001336 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001337 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001338 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001339 def add_cascade(self, cnf={}, **kw):
1340 self.add('cascade', cnf or kw)
1341 def add_checkbutton(self, cnf={}, **kw):
1342 self.add('checkbutton', cnf or kw)
1343 def add_command(self, cnf={}, **kw):
1344 self.add('command', cnf or kw)
1345 def add_radiobutton(self, cnf={}, **kw):
1346 self.add('radiobutton', cnf or kw)
1347 def add_separator(self, cnf={}, **kw):
1348 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001349 def insert(self, index, itemType, cnf={}, **kw):
1350 apply(self.tk.call, (self._w, 'insert', index, itemType)
1351 + self._options(cnf, kw))
1352 def insert_cascade(self, index, cnf={}, **kw):
1353 self.insert(index, 'cascade', cnf or kw)
1354 def insert_checkbutton(self, index, cnf={}, **kw):
1355 self.insert(index, 'checkbutton', cnf or kw)
1356 def insert_command(self, index, cnf={}, **kw):
1357 self.insert(index, 'command', cnf or kw)
1358 def insert_radiobutton(self, index, cnf={}, **kw):
1359 self.insert(index, 'radiobutton', cnf or kw)
1360 def insert_separator(self, index, cnf={}, **kw):
1361 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001362 def delete(self, index1, index2=None):
1363 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001364 def entryconfig(self, index, cnf=None, **kw):
1365 if cnf is None and not kw:
1366 cnf = {}
1367 for x in self.tk.split(apply(self.tk.call,
1368 (self._w, 'entryconfigure', index))):
1369 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1370 return cnf
1371 if type(cnf) == StringType and not kw:
1372 x = self.tk.split(apply(self.tk.call,
1373 (self._w, 'entryconfigure', index, '-'+cnf)))
1374 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001375 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001376 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001377 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001378 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001379 i = self.tk.call(self._w, 'index', index)
1380 if i == 'none': return None
1381 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001382 def invoke(self, index):
1383 return self.tk.call(self._w, 'invoke', index)
1384 def post(self, x, y):
1385 self.tk.call(self._w, 'post', x, y)
1386 def unpost(self):
1387 self.tk.call(self._w, 'unpost')
1388 def yposition(self, index):
1389 return self.tk.getint(self.tk.call(
1390 self._w, 'yposition', index))
1391
1392class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001393 def __init__(self, master=None, cnf={}, **kw):
1394 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001395
1396class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001397 def __init__(self, master=None, cnf={}, **kw):
1398 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001399
1400class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001401 def __init__(self, master=None, cnf={}, **kw):
1402 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001403 def deselect(self):
1404 self.tk.call(self._w, 'deselect')
1405 def flash(self):
1406 self.tk.call(self._w, 'flash')
1407 def invoke(self):
1408 self.tk.call(self._w, 'invoke')
1409 def select(self):
1410 self.tk.call(self._w, 'select')
1411
1412class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001413 def __init__(self, master=None, cnf={}, **kw):
1414 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001415 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001416 value = self.tk.call(self._w, 'get')
1417 try:
1418 return self.tk.getint(value)
1419 except TclError:
1420 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001421 def set(self, value):
1422 self.tk.call(self._w, 'set', value)
1423
1424class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001425 def __init__(self, master=None, cnf={}, **kw):
1426 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001427 def activate(self, index):
1428 self.tk.call(self._w, 'activate', index)
1429 def delta(self, deltax, deltay):
1430 return self.getdouble(self.tk.call(
1431 self._w, 'delta', deltax, deltay))
1432 def fraction(self, x, y):
1433 return self.getdouble(self.tk.call(
1434 self._w, 'fraction', x, y))
1435 def identify(self, x, y):
1436 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001437 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001438 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001439 def set(self, *args):
1440 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001441
1442class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001443 def __init__(self, master=None, cnf={}, **kw):
1444 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001445 def bbox(self, *args):
1446 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001447 def tk_textSelectTo(self, index):
1448 self.tk.call('tk_textSelectTo', self._w, index)
1449 def tk_textBackspace(self):
1450 self.tk.call('tk_textBackspace', self._w)
1451 def tk_textIndexCloser(self, a, b, c):
1452 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1453 def tk_textResetAnchor(self, index):
1454 self.tk.call('tk_textResetAnchor', self._w, index)
1455 def compare(self, index1, op, index2):
1456 return self.tk.getboolean(self.tk.call(
1457 self._w, 'compare', index1, op, index2))
1458 def debug(self, boolean=None):
1459 return self.tk.getboolean(self.tk.call(
1460 self._w, 'debug', boolean))
1461 def delete(self, index1, index2=None):
1462 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001463 def dlineinfo(self, index):
1464 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001465 def get(self, index1, index2=None):
1466 return self.tk.call(self._w, 'get', index1, index2)
1467 def index(self, index):
1468 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001469 def insert(self, index, chars, *args):
1470 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001471 def mark_gravity(self, markName, direction=None):
1472 return apply(self.tk.call,
1473 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001474 def mark_names(self):
1475 return self.tk.splitlist(self.tk.call(
1476 self._w, 'mark', 'names'))
1477 def mark_set(self, markName, index):
1478 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001479 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001480 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001481 def scan_mark(self, x, y):
1482 self.tk.call(self._w, 'scan', 'mark', x, y)
1483 def scan_dragto(self, x, y):
1484 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001485 def search(self, pattern, index, stopindex=None,
1486 forwards=None, backwards=None, exact=None,
1487 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001488 args = [self._w, 'search']
1489 if forwards: args.append('-forwards')
1490 if backwards: args.append('-backwards')
1491 if exact: args.append('-exact')
1492 if regexp: args.append('-regexp')
1493 if nocase: args.append('-nocase')
1494 if count: args.append('-count'); args.append(count)
1495 if pattern[0] == '-': args.append('--')
1496 args.append(pattern)
1497 args.append(index)
1498 if stopindex: args.append(stopindex)
1499 return apply(self.tk.call, tuple(args))
1500 def see(self, index):
1501 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001502 def tag_add(self, tagName, index1, index2=None):
1503 self.tk.call(
1504 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001505 def tag_unbind(self, tagName, sequence):
1506 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001507 def tag_bind(self, tagName, sequence, func, add=None):
1508 return self._bind((self._w, 'tag', 'bind', tagName),
1509 sequence, func, add)
1510 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001511 if option[:1] != '-':
1512 option = '-' + option
1513 if option[-1:] == '_':
1514 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001515 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001516 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001517 if type(cnf) == StringType:
1518 x = self.tk.split(self.tk.call(
1519 self._w, 'tag', 'configure', tagName, '-'+cnf))
1520 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001521 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001522 (self._w, 'tag', 'configure', tagName)
1523 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001524 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001525 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001526 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001527 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001528 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001529 def tag_names(self, index=None):
1530 return self.tk.splitlist(
1531 self.tk.call(self._w, 'tag', 'names', index))
1532 def tag_nextrange(self, tagName, index1, index2=None):
1533 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001534 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001535 def tag_raise(self, tagName, aboveThis=None):
1536 self.tk.call(
1537 self._w, 'tag', 'raise', tagName, aboveThis)
1538 def tag_ranges(self, tagName):
1539 return self.tk.splitlist(self.tk.call(
1540 self._w, 'tag', 'ranges', tagName))
1541 def tag_remove(self, tagName, index1, index2=None):
1542 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001543 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001544 def window_cget(self, index, option):
1545 return self.tk.call(self._w, 'window', 'cget', index, option)
1546 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001547 if type(cnf) == StringType:
1548 x = self.tk.split(self.tk.call(
1549 self._w, 'window', 'configure',
1550 index, '-'+cnf))
1551 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001552 apply(self.tk.call,
1553 (self._w, 'window', 'configure', index)
1554 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001555 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001556 def window_create(self, index, cnf={}, **kw):
1557 apply(self.tk.call,
1558 (self._w, 'window', 'create', index)
1559 + self._options(cnf, kw))
1560 def window_names(self):
1561 return self.tk.splitlist(
1562 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001563 def xview(self, *what):
1564 if not what:
1565 return self._getdoubles(self.tk.call(self._w, 'xview'))
1566 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001567 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001568 if not what:
1569 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001570 apply(self.tk.call, (self._w, 'yview')+what)
1571 def yview_pickplace(self, *what):
1572 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001573
Guido van Rossum28574b51996-10-21 15:16:51 +00001574class _setit:
1575 def __init__(self, var, value):
1576 self.__value = value
1577 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001578 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001579 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001580
1581class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001582 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001583 kw = {"borderwidth": 2, "textvariable": variable,
1584 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1585 "highlightthickness": 2}
1586 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001587 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001588 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1589 self.menuname = menu._w
1590 menu.add_command(label=value, command=_setit(variable, value))
1591 for v in values:
1592 menu.add_command(label=v, command=_setit(variable, v))
1593 self["menu"] = menu
1594
1595 def __getitem__(self, name):
1596 if name == 'menu':
1597 return self.__menu
1598 return Widget.__getitem__(self, name)
1599
1600 def destroy(self):
1601 Menubutton.destroy(self)
1602 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001603
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001604class Image:
1605 def __init__(self, imgtype, name=None, cnf={}, **kw):
1606 self.name = None
1607 master = _default_root
1608 if not master: raise RuntimeError, 'Too early to create image'
1609 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001610 if not name:
1611 name = `id(self)`
1612 # The following is needed for systems where id(x)
1613 # can return a negative number, such as Linux/m68k:
1614 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001615 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1616 elif kw: cnf = kw
1617 options = ()
1618 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001619 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001620 v = self._register(v)
1621 options = options + ('-'+k, v)
1622 apply(self.tk.call,
1623 ('image', 'create', imgtype, name,) + options)
1624 self.name = name
1625 def __str__(self): return self.name
1626 def __del__(self):
1627 if self.name:
1628 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001629 def __setitem__(self, key, value):
1630 self.tk.call(self.name, 'configure', '-'+key, value)
1631 def __getitem__(self, key):
1632 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001633 def config(self, **kw):
1634 res = ()
1635 for k, v in _cnfmerge(kw).items():
1636 if v is not None:
1637 if k[-1] == '_': k = k[:-1]
1638 if callable(v):
1639 v = self._register(v)
1640 res = res + ('-'+k, v)
1641 apply(self.tk.call, (self.name, 'config') + res)
1642 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001643 def height(self):
1644 return self.tk.getint(
1645 self.tk.call('image', 'height', self.name))
1646 def type(self):
1647 return self.tk.call('image', 'type', self.name)
1648 def width(self):
1649 return self.tk.getint(
1650 self.tk.call('image', 'width', self.name))
1651
1652class PhotoImage(Image):
1653 def __init__(self, name=None, cnf={}, **kw):
1654 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001655 def blank(self):
1656 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001657 def cget(self, option):
1658 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001659 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001660 def __getitem__(self, key):
1661 return self.tk.call(self.name, 'cget', '-' + key)
1662 def copy(self):
1663 destImage = PhotoImage()
1664 self.tk.call(destImage, 'copy', self.name)
1665 return destImage
1666 def zoom(self,x,y=''):
1667 destImage = PhotoImage()
1668 if y=='': y=x
1669 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1670 return destImage
1671 def subsample(self,x,y=''):
1672 destImage = PhotoImage()
1673 if y=='': y=x
1674 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1675 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001676 def get(self, x, y):
1677 return self.tk.call(self.name, 'get', x, y)
1678 def put(self, data, to=None):
1679 args = (self.name, 'put', data)
1680 if to:
1681 args = args + to
1682 apply(self.tk.call, args)
1683 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001684 def write(self, filename, format=None, from_coords=None):
1685 args = (self.name, 'write', filename)
1686 if format:
1687 args = args + ('-format', format)
1688 if from_coords:
1689 args = args + ('-from',) + tuple(from_coords)
1690 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001691
1692class BitmapImage(Image):
1693 def __init__(self, name=None, cnf={}, **kw):
1694 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1695
1696def image_names(): return _default_root.tk.call('image', 'names')
1697def image_types(): return _default_root.tk.call('image', 'types')
1698
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001699######################################################################
1700# Extensions:
1701
1702class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001703 def __init__(self, master=None, cnf={}, **kw):
1704 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001705 self.bind('<Any-Enter>', self.tkButtonEnter)
1706 self.bind('<Any-Leave>', self.tkButtonLeave)
1707 self.bind('<1>', self.tkButtonDown)
1708 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001709
1710class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001711 def __init__(self, master=None, cnf={}, **kw):
1712 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001713 self.bind('<Any-Enter>', self.tkButtonEnter)
1714 self.bind('<Any-Leave>', self.tkButtonLeave)
1715 self.bind('<1>', self.tkButtonDown)
1716 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1717 self['fg'] = self['bg']
1718 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001719
Guido van Rossumc417ef81996-08-21 23:38:59 +00001720######################################################################
1721# Test:
1722
1723def _test():
1724 root = Tk()
1725 label = Label(root, text="Proof-of-existence test for Tk")
1726 label.pack()
1727 test = Button(root, text="Click me!",
1728 command=lambda root=root: root.test.config(
1729 text="[%s]" % root.test['text']))
1730 test.pack()
1731 root.test = test
1732 quit = Button(root, text="QUIT", command=root.destroy)
1733 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001734 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001735 root.mainloop()
1736
1737if __name__ == '__main__':
1738 _test()
1739
Guido van Rossum37dcab11996-05-16 16:00:19 +00001740
1741# Emacs cruft
1742# Local Variables:
1743# py-indent-offset: 8
1744# End: