blob: 3cd5768d342c5d0d1e57ed626335fe7d8fc66c0e [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 Rossum368e06b1997-11-07 20:38:49 +0000122# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000123class Misc:
Fred Drake526749b1997-05-03 04:16:23 +0000124 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000125 def destroy(self):
126 if self._tclCommands is not None:
127 for name in self._tclCommands:
128 #print '- Tkinter: deleted command', name
129 self.tk.deletecommand(name)
130 self._tclCommands = None
131 def deletecommand(self, name):
132 #print '- Tkinter: deleted command', name
133 self.tk.deletecommand(name)
134 index = self._tclCommands.index(name)
135 del self._tclCommands[index]
Guido van Rossum18468821994-06-20 07:49:28 +0000136 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000137 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000138 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000139 def tk_bisque(self):
140 self.tk.call('tk_bisque')
141 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000142 apply(self.tk.call, ('tk_setPalette',)
143 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000144 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000145 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000146 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000147 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000148 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000149 def wait_window(self, window=None):
150 if window == None:
151 window = self
152 self.tk.call('tkwait', 'window', window._w)
153 def wait_visibility(self, window=None):
154 if window == None:
155 window = self
156 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000157 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000158 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000159 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000160 return self.tk.getvar(name)
161 def getint(self, s):
162 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000163 def getdouble(self, s):
164 return self.tk.getdouble(s)
165 def getboolean(self, s):
166 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000167 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000168 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000169 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000170 def focus_force(self):
171 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000172 def focus_get(self):
173 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000174 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000175 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000176 def focus_displayof(self):
177 name = self.tk.call('focus', '-displayof', self._w)
178 if name == 'none' or not name: return None
179 return self._nametowidget(name)
180 def focus_lastfor(self):
181 name = self.tk.call('focus', '-lastfor', self._w)
182 if name == 'none' or not name: return None
183 return self._nametowidget(name)
184 def tk_focusFollowsMouse(self):
185 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000186 def tk_focusNext(self):
187 name = self.tk.call('tk_focusNext', self._w)
188 if not name: return None
189 return self._nametowidget(name)
190 def tk_focusPrev(self):
191 name = self.tk.call('tk_focusPrev', self._w)
192 if not name: return None
193 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000194 def after(self, ms, func=None, *args):
195 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000196 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000197 self.tk.call('after', ms)
198 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000199 # XXX Disgusting hack to clean up after calling func
200 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000201 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000202 try:
203 apply(func, args)
204 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000205 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000206 name = self._register(callit)
207 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000208 return self.tk.call('after', ms, name)
209 def after_idle(self, func, *args):
210 return apply(self.after, ('idle', func) + args)
211 def after_cancel(self, id):
212 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000213 def bell(self, displayof=0):
214 apply(self.tk.call, ('bell',) + self._displayof(displayof))
215 # Clipboard handling:
216 def clipboard_clear(self, **kw):
217 if not kw.has_key('displayof'): kw['displayof'] = self._w
218 apply(self.tk.call,
219 ('clipboard', 'clear') + self._options(kw))
220 def clipboard_append(self, string, **kw):
221 if not kw.has_key('displayof'): kw['displayof'] = self._w
222 apply(self.tk.call,
223 ('clipboard', 'append') + self._options(kw)
224 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000225 # XXX grab current w/o window argument
226 def grab_current(self):
227 name = self.tk.call('grab', 'current', self._w)
228 if not name: return None
229 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000230 def grab_release(self):
231 self.tk.call('grab', 'release', self._w)
232 def grab_set(self):
233 self.tk.call('grab', 'set', self._w)
234 def grab_set_global(self):
235 self.tk.call('grab', 'set', '-global', self._w)
236 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000237 status = self.tk.call('grab', 'status', self._w)
238 if status == 'none': status = None
239 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000240 def lower(self, belowThis=None):
241 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000242 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000243 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000244 def option_clear(self):
245 self.tk.call('option', 'clear')
246 def option_get(self, name, className):
247 return self.tk.call('option', 'get', self._w, name, className)
248 def option_readfile(self, fileName, priority = None):
249 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000250 def selection_clear(self, **kw):
251 if not kw.has_key('displayof'): kw['displayof'] = self._w
252 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
253 def selection_get(self, **kw):
254 if not kw.has_key('displayof'): kw['displayof'] = self._w
255 return apply(self.tk.call,
256 ('selection', 'get') + self._options(kw))
257 def selection_handle(self, command, **kw):
258 name = self._register(command)
259 apply(self.tk.call,
260 ('selection', 'handle') + self._options(kw)
261 + (self._w, name))
262 def selection_own(self, **kw):
263 "Become owner of X selection."
264 apply(self.tk.call,
265 ('selection', 'own') + self._options(kw) + (self._w,))
266 def selection_own_get(self, **kw):
267 "Find owner of X selection."
268 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000269 name = apply(self.tk.call,
270 ('selection', 'own') + self._options(kw))
271 if not name: return None
272 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000273 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000274 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000275 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000276 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000277 def tkraise(self, aboveThis=None):
278 self.tk.call('raise', self._w, aboveThis)
279 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000280 def colormodel(self, value=None):
281 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000282 def winfo_atom(self, name, displayof=0):
283 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
284 return self.tk.getint(apply(self.tk.call, args))
285 def winfo_atomname(self, id, displayof=0):
286 args = ('winfo', 'atomname') \
287 + self._displayof(displayof) + (id,)
288 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000289 def winfo_cells(self):
290 return self.tk.getint(
291 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000292 def winfo_children(self):
293 return map(self._nametowidget,
294 self.tk.splitlist(self.tk.call(
295 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000296 def winfo_class(self):
297 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000298 def winfo_colormapfull(self):
299 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000300 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000301 def winfo_containing(self, rootX, rootY, displayof=0):
302 args = ('winfo', 'containing') \
303 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000304 name = apply(self.tk.call, args)
305 if not name: return None
306 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000307 def winfo_depth(self):
308 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
309 def winfo_exists(self):
310 return self.tk.getint(
311 self.tk.call('winfo', 'exists', self._w))
312 def winfo_fpixels(self, number):
313 return self.tk.getdouble(self.tk.call(
314 'winfo', 'fpixels', self._w, number))
315 def winfo_geometry(self):
316 return self.tk.call('winfo', 'geometry', self._w)
317 def winfo_height(self):
318 return self.tk.getint(
319 self.tk.call('winfo', 'height', self._w))
320 def winfo_id(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000323 def winfo_interps(self, displayof=0):
324 args = ('winfo', 'interps') + self._displayof(displayof)
325 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000326 def winfo_ismapped(self):
327 return self.tk.getint(
328 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000329 def winfo_manager(self):
330 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000331 def winfo_name(self):
332 return self.tk.call('winfo', 'name', self._w)
333 def winfo_parent(self):
334 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000335 def winfo_pathname(self, id, displayof=0):
336 args = ('winfo', 'pathname') \
337 + self._displayof(displayof) + (id,)
338 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000339 def winfo_pixels(self, number):
340 return self.tk.getint(
341 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000342 def winfo_pointerx(self):
343 return self.tk.getint(
344 self.tk.call('winfo', 'pointerx', self._w))
345 def winfo_pointerxy(self):
346 return self._getints(
347 self.tk.call('winfo', 'pointerxy', self._w))
348 def winfo_pointery(self):
349 return self.tk.getint(
350 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000351 def winfo_reqheight(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'reqheight', self._w))
354 def winfo_reqwidth(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'reqwidth', self._w))
357 def winfo_rgb(self, color):
358 return self._getints(
359 self.tk.call('winfo', 'rgb', self._w, color))
360 def winfo_rootx(self):
361 return self.tk.getint(
362 self.tk.call('winfo', 'rootx', self._w))
363 def winfo_rooty(self):
364 return self.tk.getint(
365 self.tk.call('winfo', 'rooty', self._w))
366 def winfo_screen(self):
367 return self.tk.call('winfo', 'screen', self._w)
368 def winfo_screencells(self):
369 return self.tk.getint(
370 self.tk.call('winfo', 'screencells', self._w))
371 def winfo_screendepth(self):
372 return self.tk.getint(
373 self.tk.call('winfo', 'screendepth', self._w))
374 def winfo_screenheight(self):
375 return self.tk.getint(
376 self.tk.call('winfo', 'screenheight', self._w))
377 def winfo_screenmmheight(self):
378 return self.tk.getint(
379 self.tk.call('winfo', 'screenmmheight', self._w))
380 def winfo_screenmmwidth(self):
381 return self.tk.getint(
382 self.tk.call('winfo', 'screenmmwidth', self._w))
383 def winfo_screenvisual(self):
384 return self.tk.call('winfo', 'screenvisual', self._w)
385 def winfo_screenwidth(self):
386 return self.tk.getint(
387 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000388 def winfo_server(self):
389 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000390 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000391 return self._nametowidget(self.tk.call(
392 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000393 def winfo_viewable(self):
394 return self.tk.getint(
395 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000396 def winfo_visual(self):
397 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000398 def winfo_visualid(self):
399 return self.tk.call('winfo', 'visualid', self._w)
400 def winfo_visualsavailable(self, includeids=0):
401 data = self.tk.split(
402 self.tk.call('winfo', 'visualsavailable', self._w,
403 includeids and 'includeids' or None))
404 def parseitem(x, self=self):
405 return x[:1] + tuple(map(self.tk.getint, x[1:]))
406 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000407 def winfo_vrootheight(self):
408 return self.tk.getint(
409 self.tk.call('winfo', 'vrootheight', self._w))
410 def winfo_vrootwidth(self):
411 return self.tk.getint(
412 self.tk.call('winfo', 'vrootwidth', self._w))
413 def winfo_vrootx(self):
414 return self.tk.getint(
415 self.tk.call('winfo', 'vrootx', self._w))
416 def winfo_vrooty(self):
417 return self.tk.getint(
418 self.tk.call('winfo', 'vrooty', self._w))
419 def winfo_width(self):
420 return self.tk.getint(
421 self.tk.call('winfo', 'width', self._w))
422 def winfo_x(self):
423 return self.tk.getint(
424 self.tk.call('winfo', 'x', self._w))
425 def winfo_y(self):
426 return self.tk.getint(
427 self.tk.call('winfo', 'y', self._w))
428 def update(self):
429 self.tk.call('update')
430 def update_idletasks(self):
431 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000432 def bindtags(self, tagList=None):
433 if tagList is None:
434 return self.tk.splitlist(
435 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000436 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000437 self.tk.call('bindtags', self._w, tagList)
438 def _bind(self, what, sequence, func, add):
439 if func:
440 cmd = ("%sset _tkinter_break [%s %s]\n"
441 'if {"$_tkinter_break" == "break"} break\n') \
442 % (add and '+' or '',
443 self._register(func, self._substitute),
444 _string.join(self._subst_format))
445 apply(self.tk.call, what + (sequence, cmd))
446 elif func == '':
447 apply(self.tk.call, what + (sequence, func))
448 else:
449 return apply(self.tk.call, what + (sequence,))
450 def bind(self, sequence=None, func=None, add=None):
451 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000452 def unbind(self, sequence):
453 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000454 def bind_all(self, sequence=None, func=None, add=None):
455 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000456 def unbind_all(self, sequence):
457 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000458 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossumc0b93191997-11-22 21:49:56 +0000459 return self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000460 def unbind_class(self, className, sequence):
461 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000462 def mainloop(self, n=0):
463 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000464 def quit(self):
465 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000466 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000467 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000468 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
469 def _getdoubles(self, string):
470 if not string: return None
471 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000472 def _getboolean(self, string):
473 if string:
474 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000475 def _displayof(self, displayof):
476 if displayof:
477 return ('-displayof', displayof)
478 if displayof is None:
479 return ('-displayof', self._w)
480 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000481 def _options(self, cnf, kw = None):
482 if kw:
483 cnf = _cnfmerge((cnf, kw))
484 else:
485 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000486 res = ()
487 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000488 if v is not None:
489 if k[-1] == '_': k = k[:-1]
490 if callable(v):
491 v = self._register(v)
492 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000493 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000494 def _nametowidget(self, name):
495 w = self
496 if name[0] == '.':
497 w = w._root()
498 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000499 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000500 while name:
501 i = find(name, '.')
502 if i >= 0:
503 name, tail = name[:i], name[i+1:]
504 else:
505 tail = ''
506 w = w.children[name]
507 name = tail
508 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000509 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000510 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000511 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000512 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000513 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000514 except AttributeError:
515 pass
516 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000517 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000518 except AttributeError:
519 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000520 self.tk.createcommand(name, f)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000521 if self._tclCommands is None:
522 self._tclCommands = []
523 self._tclCommands.append(name)
524 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000525 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000526 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000527 def _root(self):
528 w = self
529 while w.master: w = w.master
530 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000531 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000532 '%s', '%t', '%w', '%x', '%y',
533 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
534 def _substitute(self, *args):
535 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000536 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000537 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
538 # Missing: (a, c, d, m, o, v, B, R)
539 e = Event()
540 e.serial = tk.getint(nsign)
541 e.num = tk.getint(b)
542 try: e.focus = tk.getboolean(f)
543 except TclError: pass
544 e.height = tk.getint(h)
545 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000546 # For Visibility events, event state is a string and
547 # not an integer:
548 try:
549 e.state = tk.getint(s)
550 except TclError:
551 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000552 e.time = tk.getint(t)
553 e.width = tk.getint(w)
554 e.x = tk.getint(x)
555 e.y = tk.getint(y)
556 e.char = A
557 try: e.send_event = tk.getboolean(E)
558 except TclError: pass
559 e.keysym = K
560 e.keysym_num = tk.getint(N)
561 e.type = T
562 e.widget = self._nametowidget(W)
563 e.x_root = tk.getint(X)
564 e.y_root = tk.getint(Y)
565 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000566 def _report_exception(self):
567 import sys
568 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
569 root = self._root()
570 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000571 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000572 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000573 # XXX ought to generalize this so tag_config etc. can use it
574 if kw:
575 cnf = _cnfmerge((cnf, kw))
576 elif cnf:
577 cnf = _cnfmerge(cnf)
578 if cnf is None:
579 cnf = {}
580 for x in self.tk.split(
581 self.tk.call(self._w, 'configure')):
582 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
583 return cnf
584 if type(cnf) is StringType:
585 x = self.tk.split(self.tk.call(
586 self._w, 'configure', '-'+cnf))
587 return (x[0][1:],) + x[1:]
588 apply(self.tk.call, (self._w, 'configure')
589 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000590 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000591 def cget(self, key):
592 return self.tk.call(self._w, 'cget', '-' + key)
593 __getitem__ = cget
594 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000595 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000596 def keys(self):
597 return map(lambda x: x[0][1:],
598 self.tk.split(self.tk.call(self._w, 'configure')))
599 def __str__(self):
600 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000601 # Pack methods that apply to the master
602 _noarg_ = ['_noarg_']
603 def pack_propagate(self, flag=_noarg_):
604 if flag is Misc._noarg_:
605 return self._getboolean(self.tk.call(
606 'pack', 'propagate', self._w))
607 else:
608 self.tk.call('pack', 'propagate', self._w, flag)
609 propagate = pack_propagate
610 def pack_slaves(self):
611 return map(self._nametowidget,
612 self.tk.splitlist(
613 self.tk.call('pack', 'slaves', self._w)))
614 slaves = pack_slaves
615 # Place method that applies to the master
616 def place_slaves(self):
617 return map(self._nametowidget,
618 self.tk.splitlist(
619 self.tk.call(
620 'place', 'slaves', self._w)))
621 # Grid methods that apply to the master
622 def grid_bbox(self, column, row):
623 return self._getints(
624 self.tk.call(
625 'grid', 'bbox', self._w, column, row)) or None
626 bbox = grid_bbox
627 def grid_columnconfigure(self, index, cnf={}, **kw):
628 if type(cnf) is not DictionaryType and not kw:
629 options = self._options({cnf: None})
630 else:
631 options = self._options(cnf, kw)
632 if not options:
633 res = self.tk.call('grid',
634 'columnconfigure', self._w, index)
635 words = self.tk.splitlist(res)
636 dict = {}
637 for i in range(0, len(words), 2):
638 key = words[i][1:]
639 value = words[i+1]
640 if not value:
641 value = None
642 elif '.' in value:
643 value = self.tk.getdouble(value)
644 else:
645 value = self.tk.getint(value)
646 dict[key] = value
647 return dict
648 res = apply(self.tk.call,
649 ('grid', 'columnconfigure', self._w, index)
650 + options)
651 if options == ('-minsize', None):
652 return self.tk.getint(res) or None
653 elif options == ('-weight', None):
654 return self.tk.getdouble(res) or None
655 columnconfigure = grid_columnconfigure
656 def grid_propagate(self, flag=_noarg_):
657 if flag is Misc._noarg_:
658 return self._getboolean(self.tk.call(
659 'grid', 'propagate', self._w))
660 else:
661 self.tk.call('grid', 'propagate', self._w, flag)
662 def grid_rowconfigure(self, index, cnf={}, **kw):
663 if type(cnf) is not DictionaryType and not kw:
664 options = self._options({cnf: None})
665 else:
666 options = self._options(cnf, kw)
667 if not options:
668 res = self.tk.call('grid',
669 'rowconfigure', self._w, index)
670 words = self.tk.splitlist(res)
671 dict = {}
672 for i in range(0, len(words), 2):
673 key = words[i][1:]
674 value = words[i+1]
675 if not value:
676 value = None
677 elif '.' in value:
678 value = self.tk.getdouble(value)
679 else:
680 value = self.tk.getint(value)
681 dict[key] = value
682 return dict
683 res = apply(self.tk.call,
684 ('grid', 'rowconfigure', self._w, index)
685 + options)
686 if len(options) == 2 and options[-1] is None:
687 if not res: return None
688 # In Tk 7.5, -width can be a float
689 if '.' in res: return self.tk.getdouble(res)
690 return self.tk.getint(res)
691 rowconfigure = grid_rowconfigure
692 def grid_size(self):
693 return self._getints(
694 self.tk.call('grid', 'size', self._w)) or None
695 size = grid_size
696 def grid_slaves(self, *args):
697 return map(self._nametowidget,
698 self.tk.splitlist(
699 apply(self.tk.call,
700 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000701
Guido van Rossum80f8be81997-12-02 19:51:39 +0000702 # Support for the "event" command, new in Tk 4.2.
703 # By Case Roole.
704
705 def event_add(self,virtual, *sequences):
706 args = ('event', 'add', virtual) + sequences
707 apply( _default_root.tk.call, args )
708
709 def event_delete(self,virtual,*sequences):
710 args = ('event', 'delete', virtual) + sequences
711 apply( _default_root.tk.call, args )
712
713 def event_generate(self, sequence, **kw):
714 args = ('event', 'generate', self._w, sequence)
715 for k,v in kw.items():
716 args = args + ('-%s' % k,str(v))
717 apply( _default_root.tk.call, args )
718
719 def event_info(self,virtual=None):
720 args = ('event', 'info')
721 if virtual is not None: args = args + (virtual,)
722 s = apply( _default_root.tk.call, args )
723 return _string.split(s)
724
725
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000726class CallWrapper:
727 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000728 self.func = func
729 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000730 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000731 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000732 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000733 if self.subst:
734 args = apply(self.subst, args)
735 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000736 except SystemExit, msg:
737 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000738 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000739 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000740
741class Wm:
742 def aspect(self,
743 minNumer=None, minDenom=None,
744 maxNumer=None, maxDenom=None):
745 return self._getints(
746 self.tk.call('wm', 'aspect', self._w,
747 minNumer, minDenom,
748 maxNumer, maxDenom))
749 def client(self, name=None):
750 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000751 def colormapwindows(self, *wlist):
752 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
753 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000754 def command(self, value=None):
755 return self.tk.call('wm', 'command', self._w, value)
756 def deiconify(self):
757 return self.tk.call('wm', 'deiconify', self._w)
758 def focusmodel(self, model=None):
759 return self.tk.call('wm', 'focusmodel', self._w, model)
760 def frame(self):
761 return self.tk.call('wm', 'frame', self._w)
762 def geometry(self, newGeometry=None):
763 return self.tk.call('wm', 'geometry', self._w, newGeometry)
764 def grid(self,
765 baseWidht=None, baseHeight=None,
766 widthInc=None, heightInc=None):
767 return self._getints(self.tk.call(
768 'wm', 'grid', self._w,
769 baseWidht, baseHeight, widthInc, heightInc))
770 def group(self, pathName=None):
771 return self.tk.call('wm', 'group', self._w, pathName)
772 def iconbitmap(self, bitmap=None):
773 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
774 def iconify(self):
775 return self.tk.call('wm', 'iconify', self._w)
776 def iconmask(self, bitmap=None):
777 return self.tk.call('wm', 'iconmask', self._w, bitmap)
778 def iconname(self, newName=None):
779 return self.tk.call('wm', 'iconname', self._w, newName)
780 def iconposition(self, x=None, y=None):
781 return self._getints(self.tk.call(
782 'wm', 'iconposition', self._w, x, y))
783 def iconwindow(self, pathName=None):
784 return self.tk.call('wm', 'iconwindow', self._w, pathName)
785 def maxsize(self, width=None, height=None):
786 return self._getints(self.tk.call(
787 'wm', 'maxsize', self._w, width, height))
788 def minsize(self, width=None, height=None):
789 return self._getints(self.tk.call(
790 'wm', 'minsize', self._w, width, height))
791 def overrideredirect(self, boolean=None):
792 return self._getboolean(self.tk.call(
793 'wm', 'overrideredirect', self._w, boolean))
794 def positionfrom(self, who=None):
795 return self.tk.call('wm', 'positionfrom', self._w, who)
796 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000797 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000798 command = self._register(func)
799 else:
800 command = func
801 return self.tk.call(
802 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 def resizable(self, width=None, height=None):
804 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000805 def sizefrom(self, who=None):
806 return self.tk.call('wm', 'sizefrom', self._w, who)
807 def state(self):
808 return self.tk.call('wm', 'state', self._w)
809 def title(self, string=None):
810 return self.tk.call('wm', 'title', self._w, string)
811 def transient(self, master=None):
812 return self.tk.call('wm', 'transient', self._w, master)
813 def withdraw(self):
814 return self.tk.call('wm', 'withdraw', self._w)
815
816class Tk(Misc, Wm):
817 _w = '.'
818 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000819 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000820 self.master = None
821 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000822 if baseName is None:
823 import sys, os
824 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000825 baseName, ext = os.path.splitext(baseName)
826 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000827 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000828 try:
829 # Disable event scanning except for Command-Period
830 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000831 try:
832 MacOS.SchedParams(1, 0)
833 except AttributeError:
834 # pre-1.5, use old routine
835 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000836 except ImportError:
837 pass
838 else:
839 # Work around nasty MacTk bug
840 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000841 # Version sanity checks
842 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000843 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000844 raise RuntimeError, \
845 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000846 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000847 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000848 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000849 raise RuntimeError, \
850 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000851 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000852 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000853 raise RuntimeError, \
854 "Tk 4.0 or higher is required; found Tk %s" \
855 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000856 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000857 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000858 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000859 if not _default_root:
860 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000861 def destroy(self):
862 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000863 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000864 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000865 global _default_root
866 if _default_root is self:
867 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000868 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000869 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000870 if os.environ.has_key('HOME'): home = os.environ['HOME']
871 else: home = os.curdir
872 class_tcl = os.path.join(home, '.%s.tcl' % className)
873 class_py = os.path.join(home, '.%s.py' % className)
874 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
875 base_py = os.path.join(home, '.%s.py' % baseName)
876 dir = {'self': self}
877 exec 'from Tkinter import *' in dir
878 if os.path.isfile(class_tcl):
879 print 'source', `class_tcl`
880 self.tk.call('source', class_tcl)
881 if os.path.isfile(class_py):
882 print 'execfile', `class_py`
883 execfile(class_py, dir)
884 if os.path.isfile(base_tcl):
885 print 'source', `base_tcl`
886 self.tk.call('source', base_tcl)
887 if os.path.isfile(base_py):
888 print 'execfile', `base_py`
889 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000890 def report_callback_exception(self, exc, val, tb):
891 import traceback
892 print "Exception in Tkinter callback"
893 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000894
Guido van Rossum368e06b1997-11-07 20:38:49 +0000895# Ideally, the classes Pack, Place and Grid disappear, the
896# pack/place/grid methods are defined on the Widget class, and
897# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
898# ...), with pack(), place() and grid() being short for
899# pack_configure(), place_configure() and grid_columnconfigure(), and
900# forget() being short for pack_forget(). As a practical matter, I'm
901# afraid that there is too much code out there that may be using the
902# Pack, Place or Grid class, so I leave them intact -- but only as
903# backwards compatibility features. Also note that those methods that
904# take a master as argument (e.g. pack_propagate) have been moved to
905# the Misc class (which now incorporates all methods common between
906# toplevel and interior widgets). Again, for compatibility, these are
907# copied into the Pack, Place or Grid class.
908
Guido van Rossum18468821994-06-20 07:49:28 +0000909class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000910 def pack_configure(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000911 apply(self.tk.call,
912 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000913 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000914 pack = configure = config = pack_configure
915 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000916 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000917 forget = pack_forget
918 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000919 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000920 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000921 dict = {}
922 for i in range(0, len(words), 2):
923 key = words[i][1:]
924 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000925 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000926 value = self._nametowidget(value)
927 dict[key] = value
928 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000929 info = pack_info
930 propagate = pack_propagate = Misc.pack_propagate
931 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000932
933class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000934 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000935 for k in ['in_']:
936 if kw.has_key(k):
937 kw[k[:-1]] = kw[k]
938 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000939 apply(self.tk.call,
940 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000941 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000942 place = configure = config = place_configure
943 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000944 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000945 forget = place_forget
946 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000947 words = self.tk.splitlist(
948 self.tk.call('place', 'info', self._w))
949 dict = {}
950 for i in range(0, len(words), 2):
951 key = words[i][1:]
952 value = words[i+1]
953 if value[:1] == '.':
954 value = self._nametowidget(value)
955 dict[key] = value
956 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000957 info = place_info
958 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000959
Guido van Rossum37dcab11996-05-16 16:00:19 +0000960class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000961 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000962 def grid_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000963 apply(self.tk.call,
964 ('grid', 'configure', self._w)
965 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000966 grid = configure = config = grid_configure
967 bbox = grid_bbox = Misc.grid_bbox
968 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
969 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000970 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000971 forget = grid_forget
972 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000973 words = self.tk.splitlist(
974 self.tk.call('grid', 'info', self._w))
975 dict = {}
976 for i in range(0, len(words), 2):
977 key = words[i][1:]
978 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000979 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000980 value = self._nametowidget(value)
981 dict[key] = value
982 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000983 info = grid_info
984 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000985 return self._getints(
986 self.tk.call(
987 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +0000988 location = grid_location
989 propagate = grid_propagate = Misc.grid_propagate
990 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
991 size = grid_size = Misc.grid_size
992 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +0000993
Guido van Rossum368e06b1997-11-07 20:38:49 +0000994class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000995 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000996 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000997 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000998 if not _default_root:
999 _default_root = Tk()
1000 master = _default_root
1001 if not _default_root:
1002 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +00001003 self.master = master
1004 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001005 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001006 if cnf.has_key('name'):
1007 name = cnf['name']
1008 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001009 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001010 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001011 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001012 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001013 self._w = '.' + name
1014 else:
1015 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001016 self.children = {}
1017 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001018 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001019 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001020 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1021 if kw:
1022 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001023 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001024 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001025 classes = []
1026 for k in cnf.keys():
1027 if type(k) is ClassType:
1028 classes.append((k, cnf[k]))
1029 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001030 apply(self.tk.call,
1031 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001032 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001033 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001034 def destroy(self):
1035 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001036 if self.master.children.has_key(self._name):
1037 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001038 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001039 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001040 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001041 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001042
Guido van Rossum368e06b1997-11-07 20:38:49 +00001043class Widget(BaseWidget, Pack, Place, Grid):
1044 pass
1045
1046class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001047 def __init__(self, master=None, cnf={}, **kw):
1048 if kw:
1049 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001050 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001051 for wmkey in ['screen', 'class_', 'class', 'visual',
1052 'colormap']:
1053 if cnf.has_key(wmkey):
1054 val = cnf[wmkey]
1055 # TBD: a hack needed because some keys
1056 # are not valid as keyword arguments
1057 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1058 else: opt = '-'+wmkey
1059 extra = extra + (opt, val)
1060 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001061 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001062 root = self._root()
1063 self.iconname(root.iconname())
1064 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001065
1066class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001067 def __init__(self, master=None, cnf={}, **kw):
1068 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001069 def tkButtonEnter(self, *dummy):
1070 self.tk.call('tkButtonEnter', self._w)
1071 def tkButtonLeave(self, *dummy):
1072 self.tk.call('tkButtonLeave', self._w)
1073 def tkButtonDown(self, *dummy):
1074 self.tk.call('tkButtonDown', self._w)
1075 def tkButtonUp(self, *dummy):
1076 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001077 def tkButtonInvoke(self, *dummy):
1078 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001079 def flash(self):
1080 self.tk.call(self._w, 'flash')
1081 def invoke(self):
1082 self.tk.call(self._w, 'invoke')
1083
1084# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001085# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001086def AtEnd():
1087 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001088def AtInsert(*args):
1089 s = 'insert'
1090 for a in args:
1091 if a: s = s + (' ' + a)
1092 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001093def AtSelFirst():
1094 return 'sel.first'
1095def AtSelLast():
1096 return 'sel.last'
1097def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001098 if y is None:
1099 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001100 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001101 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001102
1103class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001104 def __init__(self, master=None, cnf={}, **kw):
1105 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001106 def addtag(self, *args):
1107 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001108 def addtag_above(self, newtag, tagOrId):
1109 self.addtag(newtag, 'above', tagOrId)
1110 def addtag_all(self, newtag):
1111 self.addtag(newtag, 'all')
1112 def addtag_below(self, newtag, tagOrId):
1113 self.addtag(newtag, 'below', tagOrId)
1114 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1115 self.addtag(newtag, 'closest', x, y, halo, start)
1116 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1117 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1118 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1119 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1120 def addtag_withtag(self, newtag, tagOrId):
1121 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001122 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001123 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001124 def tag_unbind(self, tagOrId, sequence):
1125 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001126 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001127 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001128 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001129 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001130 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001131 self._w, 'canvasx', screenx, gridspacing))
1132 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001133 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001134 self._w, 'canvasy', screeny, gridspacing))
1135 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001136 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001137 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001138 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001139 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001140 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001141 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001142 args = args[:-1]
1143 else:
1144 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001145 return self.tk.getint(apply(
1146 self.tk.call,
1147 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001148 + args + self._options(cnf, kw)))
1149 def create_arc(self, *args, **kw):
1150 return self._create('arc', args, kw)
1151 def create_bitmap(self, *args, **kw):
1152 return self._create('bitmap', args, kw)
1153 def create_image(self, *args, **kw):
1154 return self._create('image', args, kw)
1155 def create_line(self, *args, **kw):
1156 return self._create('line', args, kw)
1157 def create_oval(self, *args, **kw):
1158 return self._create('oval', args, kw)
1159 def create_polygon(self, *args, **kw):
1160 return self._create('polygon', args, kw)
1161 def create_rectangle(self, *args, **kw):
1162 return self._create('rectangle', args, kw)
1163 def create_text(self, *args, **kw):
1164 return self._create('text', args, kw)
1165 def create_window(self, *args, **kw):
1166 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001167 def dchars(self, *args):
1168 self._do('dchars', args)
1169 def delete(self, *args):
1170 self._do('delete', args)
1171 def dtag(self, *args):
1172 self._do('dtag', args)
1173 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001174 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001175 def find_above(self, tagOrId):
1176 return self.find('above', tagOrId)
1177 def find_all(self):
1178 return self.find('all')
1179 def find_below(self, tagOrId):
1180 return self.find('below', tagOrId)
1181 def find_closest(self, x, y, halo=None, start=None):
1182 return self.find('closest', x, y, halo, start)
1183 def find_enclosed(self, x1, y1, x2, y2):
1184 return self.find('enclosed', x1, y1, x2, y2)
1185 def find_overlapping(self, x1, y1, x2, y2):
1186 return self.find('overlapping', x1, y1, x2, y2)
1187 def find_withtag(self, tagOrId):
1188 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001189 def focus(self, *args):
1190 return self._do('focus', args)
1191 def gettags(self, *args):
1192 return self.tk.splitlist(self._do('gettags', args))
1193 def icursor(self, *args):
1194 self._do('icursor', args)
1195 def index(self, *args):
1196 return self.tk.getint(self._do('index', args))
1197 def insert(self, *args):
1198 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001199 def itemcget(self, tagOrId, option):
1200 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001201 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001202 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001203 cnf = {}
1204 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001205 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001206 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1207 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001208 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001209 x = self.tk.split(self._do('itemconfigure',
1210 (tagOrId, '-'+cnf,)))
1211 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001212 self._do('itemconfigure', (tagOrId,)
1213 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001214 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001215 def lower(self, *args):
1216 self._do('lower', args)
1217 def move(self, *args):
1218 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001219 def postscript(self, cnf={}, **kw):
1220 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001221 def tkraise(self, *args):
1222 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001223 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001224 def scale(self, *args):
1225 self._do('scale', args)
1226 def scan_mark(self, x, y):
1227 self.tk.call(self._w, 'scan', 'mark', x, y)
1228 def scan_dragto(self, x, y):
1229 self.tk.call(self._w, 'scan', 'dragto', x, y)
1230 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001231 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001232 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001233 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001234 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001235 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001236 def select_item(self):
1237 self.tk.call(self._w, 'select', 'item')
1238 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001239 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001240 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001241 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001242 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001243 if not args:
1244 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001245 apply(self.tk.call, (self._w, 'xview')+args)
1246 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001247 if not args:
1248 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001249 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001250
1251class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001252 def __init__(self, master=None, cnf={}, **kw):
1253 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001254 def deselect(self):
1255 self.tk.call(self._w, 'deselect')
1256 def flash(self):
1257 self.tk.call(self._w, 'flash')
1258 def invoke(self):
1259 self.tk.call(self._w, 'invoke')
1260 def select(self):
1261 self.tk.call(self._w, 'select')
1262 def toggle(self):
1263 self.tk.call(self._w, 'toggle')
1264
1265class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001266 def __init__(self, master=None, cnf={}, **kw):
1267 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001268 def delete(self, first, last=None):
1269 self.tk.call(self._w, 'delete', first, last)
1270 def get(self):
1271 return self.tk.call(self._w, 'get')
1272 def icursor(self, index):
1273 self.tk.call(self._w, 'icursor', index)
1274 def index(self, index):
1275 return self.tk.getint(self.tk.call(
1276 self._w, 'index', index))
1277 def insert(self, index, string):
1278 self.tk.call(self._w, 'insert', index, string)
1279 def scan_mark(self, x):
1280 self.tk.call(self._w, 'scan', 'mark', x)
1281 def scan_dragto(self, x):
1282 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001283 def selection_adjust(self, index):
1284 self.tk.call(self._w, 'selection', 'adjust', index)
1285 select_adjust = selection_adjust
1286 def selection_clear(self):
1287 self.tk.call(self._w, 'selection', 'clear')
1288 select_clear = selection_clear
1289 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001290 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001291 select_from = selection_from
1292 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001293 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001294 self.tk.call(self._w, 'selection', 'present'))
1295 select_present = selection_present
1296 def selection_range(self, start, end):
1297 self.tk.call(self._w, 'selection', 'range', start, end)
1298 select_range = selection_range
1299 def selection_to(self, index):
1300 self.tk.call(self._w, 'selection', 'to', index)
1301 select_to = selection_to
1302 def xview(self, index):
1303 self.tk.call(self._w, 'xview', index)
1304 def xview_moveto(self, fraction):
1305 self.tk.call(self._w, 'xview', 'moveto', fraction)
1306 def xview_scroll(self, number, what):
1307 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001308
1309class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001310 def __init__(self, master=None, cnf={}, **kw):
1311 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001312 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001313 if cnf.has_key('class_'):
1314 extra = ('-class', cnf['class_'])
1315 del cnf['class_']
1316 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001317 extra = ('-class', cnf['class'])
1318 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001319 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001320
1321class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001322 def __init__(self, master=None, cnf={}, **kw):
1323 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001324
Guido van Rossum18468821994-06-20 07:49:28 +00001325class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001326 def __init__(self, master=None, cnf={}, **kw):
1327 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001328 def activate(self, index):
1329 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001330 def bbox(self, *args):
1331 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001332 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001333 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001334 return self.tk.splitlist(self.tk.call(
1335 self._w, 'curselection'))
1336 def delete(self, first, last=None):
1337 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001338 def get(self, first, last=None):
1339 if last:
1340 return self.tk.splitlist(self.tk.call(
1341 self._w, 'get', first, last))
1342 else:
1343 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001344 def insert(self, index, *elements):
1345 apply(self.tk.call,
1346 (self._w, 'insert', index) + elements)
1347 def nearest(self, y):
1348 return self.tk.getint(self.tk.call(
1349 self._w, 'nearest', y))
1350 def scan_mark(self, x, y):
1351 self.tk.call(self._w, 'scan', 'mark', x, y)
1352 def scan_dragto(self, x, y):
1353 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001354 def see(self, index):
1355 self.tk.call(self._w, 'see', index)
1356 def index(self, index):
1357 i = self.tk.call(self._w, 'index', index)
1358 if i == 'none': return None
1359 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001360 def select_anchor(self, index):
1361 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001362 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001363 def select_clear(self, first, last=None):
1364 self.tk.call(self._w,
1365 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001366 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001367 def select_includes(self, index):
1368 return self.tk.getboolean(self.tk.call(
1369 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001370 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001371 def select_set(self, first, last=None):
1372 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001373 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001374 def size(self):
1375 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001376 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001377 if not what:
1378 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001379 apply(self.tk.call, (self._w, 'xview')+what)
1380 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001381 if not what:
1382 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001383 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001384
1385class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001386 def __init__(self, master=None, cnf={}, **kw):
1387 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001388 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001389 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001390 def tk_mbPost(self):
1391 self.tk.call('tk_mbPost', self._w)
1392 def tk_mbUnpost(self):
1393 self.tk.call('tk_mbUnpost')
1394 def tk_traverseToMenu(self, char):
1395 self.tk.call('tk_traverseToMenu', self._w, char)
1396 def tk_traverseWithinMenu(self, char):
1397 self.tk.call('tk_traverseWithinMenu', self._w, char)
1398 def tk_getMenuButtons(self):
1399 return self.tk.call('tk_getMenuButtons', self._w)
1400 def tk_nextMenu(self, count):
1401 self.tk.call('tk_nextMenu', count)
1402 def tk_nextMenuEntry(self, count):
1403 self.tk.call('tk_nextMenuEntry', count)
1404 def tk_invokeMenu(self):
1405 self.tk.call('tk_invokeMenu', self._w)
1406 def tk_firstMenu(self):
1407 self.tk.call('tk_firstMenu', self._w)
1408 def tk_mbButtonDown(self):
1409 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001410 def tk_popup(self, x, y, entry=""):
1411 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001412 def activate(self, index):
1413 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001414 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001415 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001416 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001417 def add_cascade(self, cnf={}, **kw):
1418 self.add('cascade', cnf or kw)
1419 def add_checkbutton(self, cnf={}, **kw):
1420 self.add('checkbutton', cnf or kw)
1421 def add_command(self, cnf={}, **kw):
1422 self.add('command', cnf or kw)
1423 def add_radiobutton(self, cnf={}, **kw):
1424 self.add('radiobutton', cnf or kw)
1425 def add_separator(self, cnf={}, **kw):
1426 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001427 def insert(self, index, itemType, cnf={}, **kw):
1428 apply(self.tk.call, (self._w, 'insert', index, itemType)
1429 + self._options(cnf, kw))
1430 def insert_cascade(self, index, cnf={}, **kw):
1431 self.insert(index, 'cascade', cnf or kw)
1432 def insert_checkbutton(self, index, cnf={}, **kw):
1433 self.insert(index, 'checkbutton', cnf or kw)
1434 def insert_command(self, index, cnf={}, **kw):
1435 self.insert(index, 'command', cnf or kw)
1436 def insert_radiobutton(self, index, cnf={}, **kw):
1437 self.insert(index, 'radiobutton', cnf or kw)
1438 def insert_separator(self, index, cnf={}, **kw):
1439 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001440 def delete(self, index1, index2=None):
1441 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001442 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001443 if cnf is None and not kw:
1444 cnf = {}
1445 for x in self.tk.split(apply(self.tk.call,
1446 (self._w, 'entryconfigure', index))):
1447 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1448 return cnf
1449 if type(cnf) == StringType and not kw:
1450 x = self.tk.split(apply(self.tk.call,
1451 (self._w, 'entryconfigure', index, '-'+cnf)))
1452 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001453 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001454 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001455 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001456 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001457 i = self.tk.call(self._w, 'index', index)
1458 if i == 'none': return None
1459 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001460 def invoke(self, index):
1461 return self.tk.call(self._w, 'invoke', index)
1462 def post(self, x, y):
1463 self.tk.call(self._w, 'post', x, y)
1464 def unpost(self):
1465 self.tk.call(self._w, 'unpost')
1466 def yposition(self, index):
1467 return self.tk.getint(self.tk.call(
1468 self._w, 'yposition', index))
1469
1470class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001471 def __init__(self, master=None, cnf={}, **kw):
1472 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001473
1474class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001475 def __init__(self, master=None, cnf={}, **kw):
1476 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001477
1478class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001479 def __init__(self, master=None, cnf={}, **kw):
1480 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001481 def deselect(self):
1482 self.tk.call(self._w, 'deselect')
1483 def flash(self):
1484 self.tk.call(self._w, 'flash')
1485 def invoke(self):
1486 self.tk.call(self._w, 'invoke')
1487 def select(self):
1488 self.tk.call(self._w, 'select')
1489
1490class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001491 def __init__(self, master=None, cnf={}, **kw):
1492 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001493 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001494 value = self.tk.call(self._w, 'get')
1495 try:
1496 return self.tk.getint(value)
1497 except TclError:
1498 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001499 def set(self, value):
1500 self.tk.call(self._w, 'set', value)
1501
1502class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001503 def __init__(self, master=None, cnf={}, **kw):
1504 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001505 def activate(self, index):
1506 self.tk.call(self._w, 'activate', index)
1507 def delta(self, deltax, deltay):
1508 return self.getdouble(self.tk.call(
1509 self._w, 'delta', deltax, deltay))
1510 def fraction(self, x, y):
1511 return self.getdouble(self.tk.call(
1512 self._w, 'fraction', x, y))
1513 def identify(self, x, y):
1514 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001515 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001516 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001517 def set(self, *args):
1518 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001519
1520class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001521 def __init__(self, master=None, cnf={}, **kw):
1522 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001523 def bbox(self, *args):
1524 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001525 def tk_textSelectTo(self, index):
1526 self.tk.call('tk_textSelectTo', self._w, index)
1527 def tk_textBackspace(self):
1528 self.tk.call('tk_textBackspace', self._w)
1529 def tk_textIndexCloser(self, a, b, c):
1530 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1531 def tk_textResetAnchor(self, index):
1532 self.tk.call('tk_textResetAnchor', self._w, index)
1533 def compare(self, index1, op, index2):
1534 return self.tk.getboolean(self.tk.call(
1535 self._w, 'compare', index1, op, index2))
1536 def debug(self, boolean=None):
1537 return self.tk.getboolean(self.tk.call(
1538 self._w, 'debug', boolean))
1539 def delete(self, index1, index2=None):
1540 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001541 def dlineinfo(self, index):
1542 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001543 def get(self, index1, index2=None):
1544 return self.tk.call(self._w, 'get', index1, index2)
1545 def index(self, index):
1546 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001547 def insert(self, index, chars, *args):
1548 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001549 def mark_gravity(self, markName, direction=None):
1550 return apply(self.tk.call,
1551 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001552 def mark_names(self):
1553 return self.tk.splitlist(self.tk.call(
1554 self._w, 'mark', 'names'))
1555 def mark_set(self, markName, index):
1556 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001557 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001558 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001559 def scan_mark(self, x, y):
1560 self.tk.call(self._w, 'scan', 'mark', x, y)
1561 def scan_dragto(self, x, y):
1562 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001563 def search(self, pattern, index, stopindex=None,
1564 forwards=None, backwards=None, exact=None,
1565 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001566 args = [self._w, 'search']
1567 if forwards: args.append('-forwards')
1568 if backwards: args.append('-backwards')
1569 if exact: args.append('-exact')
1570 if regexp: args.append('-regexp')
1571 if nocase: args.append('-nocase')
1572 if count: args.append('-count'); args.append(count)
1573 if pattern[0] == '-': args.append('--')
1574 args.append(pattern)
1575 args.append(index)
1576 if stopindex: args.append(stopindex)
1577 return apply(self.tk.call, tuple(args))
1578 def see(self, index):
1579 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001580 def tag_add(self, tagName, index1, index2=None):
1581 self.tk.call(
1582 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001583 def tag_unbind(self, tagName, sequence):
1584 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001585 def tag_bind(self, tagName, sequence, func, add=None):
1586 return self._bind((self._w, 'tag', 'bind', tagName),
1587 sequence, func, add)
1588 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001589 if option[:1] != '-':
1590 option = '-' + option
1591 if option[-1:] == '_':
1592 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001593 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001594 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001595 if type(cnf) == StringType:
1596 x = self.tk.split(self.tk.call(
1597 self._w, 'tag', 'configure', tagName, '-'+cnf))
1598 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001599 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001600 (self._w, 'tag', 'configure', tagName)
1601 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001602 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001603 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001604 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001605 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001606 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001607 def tag_names(self, index=None):
1608 return self.tk.splitlist(
1609 self.tk.call(self._w, 'tag', 'names', index))
1610 def tag_nextrange(self, tagName, index1, index2=None):
1611 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001612 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001613 def tag_raise(self, tagName, aboveThis=None):
1614 self.tk.call(
1615 self._w, 'tag', 'raise', tagName, aboveThis)
1616 def tag_ranges(self, tagName):
1617 return self.tk.splitlist(self.tk.call(
1618 self._w, 'tag', 'ranges', tagName))
1619 def tag_remove(self, tagName, index1, index2=None):
1620 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001621 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001622 def window_cget(self, index, option):
1623 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001624 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001625 if type(cnf) == StringType:
1626 x = self.tk.split(self.tk.call(
1627 self._w, 'window', 'configure',
1628 index, '-'+cnf))
1629 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001630 apply(self.tk.call,
1631 (self._w, 'window', 'configure', index)
1632 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001633 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001634 def window_create(self, index, cnf={}, **kw):
1635 apply(self.tk.call,
1636 (self._w, 'window', 'create', index)
1637 + self._options(cnf, kw))
1638 def window_names(self):
1639 return self.tk.splitlist(
1640 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001641 def xview(self, *what):
1642 if not what:
1643 return self._getdoubles(self.tk.call(self._w, 'xview'))
1644 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001645 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001646 if not what:
1647 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001648 apply(self.tk.call, (self._w, 'yview')+what)
1649 def yview_pickplace(self, *what):
1650 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001651
Guido van Rossum28574b51996-10-21 15:16:51 +00001652class _setit:
1653 def __init__(self, var, value):
1654 self.__value = value
1655 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001656 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001657 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001658
1659class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001660 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001661 kw = {"borderwidth": 2, "textvariable": variable,
1662 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1663 "highlightthickness": 2}
1664 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001665 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001666 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1667 self.menuname = menu._w
1668 menu.add_command(label=value, command=_setit(variable, value))
1669 for v in values:
1670 menu.add_command(label=v, command=_setit(variable, v))
1671 self["menu"] = menu
1672
1673 def __getitem__(self, name):
1674 if name == 'menu':
1675 return self.__menu
1676 return Widget.__getitem__(self, name)
1677
1678 def destroy(self):
1679 Menubutton.destroy(self)
1680 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001681
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001682class Image:
1683 def __init__(self, imgtype, name=None, cnf={}, **kw):
1684 self.name = None
1685 master = _default_root
1686 if not master: raise RuntimeError, 'Too early to create image'
1687 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001688 if not name:
1689 name = `id(self)`
1690 # The following is needed for systems where id(x)
1691 # can return a negative number, such as Linux/m68k:
1692 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001693 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1694 elif kw: cnf = kw
1695 options = ()
1696 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001697 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001698 v = self._register(v)
1699 options = options + ('-'+k, v)
1700 apply(self.tk.call,
1701 ('image', 'create', imgtype, name,) + options)
1702 self.name = name
1703 def __str__(self): return self.name
1704 def __del__(self):
1705 if self.name:
1706 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001707 def __setitem__(self, key, value):
1708 self.tk.call(self.name, 'configure', '-'+key, value)
1709 def __getitem__(self, key):
1710 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001711 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001712 res = ()
1713 for k, v in _cnfmerge(kw).items():
1714 if v is not None:
1715 if k[-1] == '_': k = k[:-1]
1716 if callable(v):
1717 v = self._register(v)
1718 res = res + ('-'+k, v)
1719 apply(self.tk.call, (self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001720 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001721 def height(self):
1722 return self.tk.getint(
1723 self.tk.call('image', 'height', self.name))
1724 def type(self):
1725 return self.tk.call('image', 'type', self.name)
1726 def width(self):
1727 return self.tk.getint(
1728 self.tk.call('image', 'width', self.name))
1729
1730class PhotoImage(Image):
1731 def __init__(self, name=None, cnf={}, **kw):
1732 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001733 def blank(self):
1734 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001735 def cget(self, option):
1736 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001737 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001738 def __getitem__(self, key):
1739 return self.tk.call(self.name, 'cget', '-' + key)
1740 def copy(self):
1741 destImage = PhotoImage()
1742 self.tk.call(destImage, 'copy', self.name)
1743 return destImage
1744 def zoom(self,x,y=''):
1745 destImage = PhotoImage()
1746 if y=='': y=x
1747 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1748 return destImage
1749 def subsample(self,x,y=''):
1750 destImage = PhotoImage()
1751 if y=='': y=x
1752 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1753 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001754 def get(self, x, y):
1755 return self.tk.call(self.name, 'get', x, y)
1756 def put(self, data, to=None):
1757 args = (self.name, 'put', data)
1758 if to:
1759 args = args + to
1760 apply(self.tk.call, args)
1761 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001762 def write(self, filename, format=None, from_coords=None):
1763 args = (self.name, 'write', filename)
1764 if format:
1765 args = args + ('-format', format)
1766 if from_coords:
1767 args = args + ('-from',) + tuple(from_coords)
1768 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001769
1770class BitmapImage(Image):
1771 def __init__(self, name=None, cnf={}, **kw):
1772 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1773
1774def image_names(): return _default_root.tk.call('image', 'names')
1775def image_types(): return _default_root.tk.call('image', 'types')
1776
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001777######################################################################
1778# Extensions:
1779
1780class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001781 def __init__(self, master=None, cnf={}, **kw):
1782 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001783 self.bind('<Any-Enter>', self.tkButtonEnter)
1784 self.bind('<Any-Leave>', self.tkButtonLeave)
1785 self.bind('<1>', self.tkButtonDown)
1786 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001787
1788class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001789 def __init__(self, master=None, cnf={}, **kw):
1790 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001791 self.bind('<Any-Enter>', self.tkButtonEnter)
1792 self.bind('<Any-Leave>', self.tkButtonLeave)
1793 self.bind('<1>', self.tkButtonDown)
1794 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1795 self['fg'] = self['bg']
1796 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001797
Guido van Rossumc417ef81996-08-21 23:38:59 +00001798######################################################################
1799# Test:
1800
1801def _test():
1802 root = Tk()
1803 label = Label(root, text="Proof-of-existence test for Tk")
1804 label.pack()
1805 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001806 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001807 text="[%s]" % root.test['text']))
1808 test.pack()
1809 root.test = test
1810 quit = Button(root, text="QUIT", command=root.destroy)
1811 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001812 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001813 root.mainloop()
1814
1815if __name__ == '__main__':
1816 _test()
1817
Guido van Rossum37dcab11996-05-16 16:00:19 +00001818
1819# Emacs cruft
1820# Local Variables:
1821# py-indent-offset: 8
1822# End: