blob: 1f3d46e1e27e6f649a046d484105eb972467dff0 [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):
459 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 Rossuma5773dd1995-09-07 19:22:00 +0000702class CallWrapper:
703 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000704 self.func = func
705 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000706 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000707 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000708 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000709 if self.subst:
710 args = apply(self.subst, args)
711 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000712 except SystemExit, msg:
713 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000714 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000715 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000716
717class Wm:
718 def aspect(self,
719 minNumer=None, minDenom=None,
720 maxNumer=None, maxDenom=None):
721 return self._getints(
722 self.tk.call('wm', 'aspect', self._w,
723 minNumer, minDenom,
724 maxNumer, maxDenom))
725 def client(self, name=None):
726 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000727 def colormapwindows(self, *wlist):
728 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
729 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000730 def command(self, value=None):
731 return self.tk.call('wm', 'command', self._w, value)
732 def deiconify(self):
733 return self.tk.call('wm', 'deiconify', self._w)
734 def focusmodel(self, model=None):
735 return self.tk.call('wm', 'focusmodel', self._w, model)
736 def frame(self):
737 return self.tk.call('wm', 'frame', self._w)
738 def geometry(self, newGeometry=None):
739 return self.tk.call('wm', 'geometry', self._w, newGeometry)
740 def grid(self,
741 baseWidht=None, baseHeight=None,
742 widthInc=None, heightInc=None):
743 return self._getints(self.tk.call(
744 'wm', 'grid', self._w,
745 baseWidht, baseHeight, widthInc, heightInc))
746 def group(self, pathName=None):
747 return self.tk.call('wm', 'group', self._w, pathName)
748 def iconbitmap(self, bitmap=None):
749 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
750 def iconify(self):
751 return self.tk.call('wm', 'iconify', self._w)
752 def iconmask(self, bitmap=None):
753 return self.tk.call('wm', 'iconmask', self._w, bitmap)
754 def iconname(self, newName=None):
755 return self.tk.call('wm', 'iconname', self._w, newName)
756 def iconposition(self, x=None, y=None):
757 return self._getints(self.tk.call(
758 'wm', 'iconposition', self._w, x, y))
759 def iconwindow(self, pathName=None):
760 return self.tk.call('wm', 'iconwindow', self._w, pathName)
761 def maxsize(self, width=None, height=None):
762 return self._getints(self.tk.call(
763 'wm', 'maxsize', self._w, width, height))
764 def minsize(self, width=None, height=None):
765 return self._getints(self.tk.call(
766 'wm', 'minsize', self._w, width, height))
767 def overrideredirect(self, boolean=None):
768 return self._getboolean(self.tk.call(
769 'wm', 'overrideredirect', self._w, boolean))
770 def positionfrom(self, who=None):
771 return self.tk.call('wm', 'positionfrom', self._w, who)
772 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000773 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000774 command = self._register(func)
775 else:
776 command = func
777 return self.tk.call(
778 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000779 def resizable(self, width=None, height=None):
780 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000781 def sizefrom(self, who=None):
782 return self.tk.call('wm', 'sizefrom', self._w, who)
783 def state(self):
784 return self.tk.call('wm', 'state', self._w)
785 def title(self, string=None):
786 return self.tk.call('wm', 'title', self._w, string)
787 def transient(self, master=None):
788 return self.tk.call('wm', 'transient', self._w, master)
789 def withdraw(self):
790 return self.tk.call('wm', 'withdraw', self._w)
791
792class Tk(Misc, Wm):
793 _w = '.'
794 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000795 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000796 self.master = None
797 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000798 if baseName is None:
799 import sys, os
800 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000801 baseName, ext = os.path.splitext(baseName)
802 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000803 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000804 try:
805 # Disable event scanning except for Command-Period
806 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000807 try:
808 MacOS.SchedParams(1, 0)
809 except AttributeError:
810 # pre-1.5, use old routine
811 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000812 except ImportError:
813 pass
814 else:
815 # Work around nasty MacTk bug
816 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000817 # Version sanity checks
818 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000819 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000820 raise RuntimeError, \
821 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000822 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000823 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000824 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000825 raise RuntimeError, \
826 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000827 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000828 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000829 raise RuntimeError, \
830 "Tk 4.0 or higher is required; found Tk %s" \
831 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000832 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000833 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000834 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000835 if not _default_root:
836 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000837 def destroy(self):
838 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000839 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000840 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000841 global _default_root
842 if _default_root is self:
843 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000844 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000845 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000846 if os.environ.has_key('HOME'): home = os.environ['HOME']
847 else: home = os.curdir
848 class_tcl = os.path.join(home, '.%s.tcl' % className)
849 class_py = os.path.join(home, '.%s.py' % className)
850 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
851 base_py = os.path.join(home, '.%s.py' % baseName)
852 dir = {'self': self}
853 exec 'from Tkinter import *' in dir
854 if os.path.isfile(class_tcl):
855 print 'source', `class_tcl`
856 self.tk.call('source', class_tcl)
857 if os.path.isfile(class_py):
858 print 'execfile', `class_py`
859 execfile(class_py, dir)
860 if os.path.isfile(base_tcl):
861 print 'source', `base_tcl`
862 self.tk.call('source', base_tcl)
863 if os.path.isfile(base_py):
864 print 'execfile', `base_py`
865 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000866 def report_callback_exception(self, exc, val, tb):
867 import traceback
868 print "Exception in Tkinter callback"
869 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000870
Guido van Rossum368e06b1997-11-07 20:38:49 +0000871# Ideally, the classes Pack, Place and Grid disappear, the
872# pack/place/grid methods are defined on the Widget class, and
873# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
874# ...), with pack(), place() and grid() being short for
875# pack_configure(), place_configure() and grid_columnconfigure(), and
876# forget() being short for pack_forget(). As a practical matter, I'm
877# afraid that there is too much code out there that may be using the
878# Pack, Place or Grid class, so I leave them intact -- but only as
879# backwards compatibility features. Also note that those methods that
880# take a master as argument (e.g. pack_propagate) have been moved to
881# the Misc class (which now incorporates all methods common between
882# toplevel and interior widgets). Again, for compatibility, these are
883# copied into the Pack, Place or Grid class.
884
Guido van Rossum18468821994-06-20 07:49:28 +0000885class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000886 def pack_configure(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000887 apply(self.tk.call,
888 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000889 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000890 pack = configure = config = pack_configure
891 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000892 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000893 forget = pack_forget
894 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000895 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000896 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000897 dict = {}
898 for i in range(0, len(words), 2):
899 key = words[i][1:]
900 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000901 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000902 value = self._nametowidget(value)
903 dict[key] = value
904 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000905 info = pack_info
906 propagate = pack_propagate = Misc.pack_propagate
907 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000908
909class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000910 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000911 for k in ['in_']:
912 if kw.has_key(k):
913 kw[k[:-1]] = kw[k]
914 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000915 apply(self.tk.call,
916 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000917 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000918 place = configure = config = place_configure
919 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000920 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000921 forget = place_forget
922 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000923 words = self.tk.splitlist(
924 self.tk.call('place', 'info', self._w))
925 dict = {}
926 for i in range(0, len(words), 2):
927 key = words[i][1:]
928 value = words[i+1]
929 if value[:1] == '.':
930 value = self._nametowidget(value)
931 dict[key] = value
932 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000933 info = place_info
934 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000935
Guido van Rossum37dcab11996-05-16 16:00:19 +0000936class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000937 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000938 def grid_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000939 apply(self.tk.call,
940 ('grid', 'configure', self._w)
941 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000942 grid = configure = config = grid_configure
943 bbox = grid_bbox = Misc.grid_bbox
944 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
945 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000946 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000947 forget = grid_forget
948 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000949 words = self.tk.splitlist(
950 self.tk.call('grid', 'info', self._w))
951 dict = {}
952 for i in range(0, len(words), 2):
953 key = words[i][1:]
954 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000955 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000956 value = self._nametowidget(value)
957 dict[key] = value
958 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000959 info = grid_info
960 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000961 return self._getints(
962 self.tk.call(
963 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +0000964 location = grid_location
965 propagate = grid_propagate = Misc.grid_propagate
966 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
967 size = grid_size = Misc.grid_size
968 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +0000969
Guido van Rossum368e06b1997-11-07 20:38:49 +0000970class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000971 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000972 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000973 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000974 if not _default_root:
975 _default_root = Tk()
976 master = _default_root
977 if not _default_root:
978 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000979 self.master = master
980 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000981 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000982 if cnf.has_key('name'):
983 name = cnf['name']
984 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000985 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000986 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000987 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000988 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000989 self._w = '.' + name
990 else:
991 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000992 self.children = {}
993 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000994 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000995 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000996 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
997 if kw:
998 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000999 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001000 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001001 classes = []
1002 for k in cnf.keys():
1003 if type(k) is ClassType:
1004 classes.append((k, cnf[k]))
1005 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001006 apply(self.tk.call,
1007 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001008 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001009 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001010 def destroy(self):
1011 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001012 if self.master.children.has_key(self._name):
1013 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001014 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001015 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001016 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001017 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001018
Guido van Rossum368e06b1997-11-07 20:38:49 +00001019class Widget(BaseWidget, Pack, Place, Grid):
1020 pass
1021
1022class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001023 def __init__(self, master=None, cnf={}, **kw):
1024 if kw:
1025 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001026 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001027 for wmkey in ['screen', 'class_', 'class', 'visual',
1028 'colormap']:
1029 if cnf.has_key(wmkey):
1030 val = cnf[wmkey]
1031 # TBD: a hack needed because some keys
1032 # are not valid as keyword arguments
1033 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1034 else: opt = '-'+wmkey
1035 extra = extra + (opt, val)
1036 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001037 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001038 root = self._root()
1039 self.iconname(root.iconname())
1040 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001041
1042class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001043 def __init__(self, master=None, cnf={}, **kw):
1044 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001045 def tkButtonEnter(self, *dummy):
1046 self.tk.call('tkButtonEnter', self._w)
1047 def tkButtonLeave(self, *dummy):
1048 self.tk.call('tkButtonLeave', self._w)
1049 def tkButtonDown(self, *dummy):
1050 self.tk.call('tkButtonDown', self._w)
1051 def tkButtonUp(self, *dummy):
1052 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001053 def tkButtonInvoke(self, *dummy):
1054 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001055 def flash(self):
1056 self.tk.call(self._w, 'flash')
1057 def invoke(self):
1058 self.tk.call(self._w, 'invoke')
1059
1060# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001061# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001062def AtEnd():
1063 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001064def AtInsert(*args):
1065 s = 'insert'
1066 for a in args:
1067 if a: s = s + (' ' + a)
1068 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001069def AtSelFirst():
1070 return 'sel.first'
1071def AtSelLast():
1072 return 'sel.last'
1073def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001074 if y is None:
1075 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001076 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001077 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001078
1079class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001080 def __init__(self, master=None, cnf={}, **kw):
1081 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001082 def addtag(self, *args):
1083 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001084 def addtag_above(self, newtag, tagOrId):
1085 self.addtag(newtag, 'above', tagOrId)
1086 def addtag_all(self, newtag):
1087 self.addtag(newtag, 'all')
1088 def addtag_below(self, newtag, tagOrId):
1089 self.addtag(newtag, 'below', tagOrId)
1090 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1091 self.addtag(newtag, 'closest', x, y, halo, start)
1092 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1093 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1094 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1095 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1096 def addtag_withtag(self, newtag, tagOrId):
1097 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001098 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001099 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001100 def tag_unbind(self, tagOrId, sequence):
1101 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001102 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001103 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001104 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001105 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001106 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001107 self._w, 'canvasx', screenx, gridspacing))
1108 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001109 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001110 self._w, 'canvasy', screeny, gridspacing))
1111 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001112 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001113 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001114 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001115 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001116 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001117 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001118 args = args[:-1]
1119 else:
1120 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001121 return self.tk.getint(apply(
1122 self.tk.call,
1123 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001124 + args + self._options(cnf, kw)))
1125 def create_arc(self, *args, **kw):
1126 return self._create('arc', args, kw)
1127 def create_bitmap(self, *args, **kw):
1128 return self._create('bitmap', args, kw)
1129 def create_image(self, *args, **kw):
1130 return self._create('image', args, kw)
1131 def create_line(self, *args, **kw):
1132 return self._create('line', args, kw)
1133 def create_oval(self, *args, **kw):
1134 return self._create('oval', args, kw)
1135 def create_polygon(self, *args, **kw):
1136 return self._create('polygon', args, kw)
1137 def create_rectangle(self, *args, **kw):
1138 return self._create('rectangle', args, kw)
1139 def create_text(self, *args, **kw):
1140 return self._create('text', args, kw)
1141 def create_window(self, *args, **kw):
1142 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001143 def dchars(self, *args):
1144 self._do('dchars', args)
1145 def delete(self, *args):
1146 self._do('delete', args)
1147 def dtag(self, *args):
1148 self._do('dtag', args)
1149 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001150 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001151 def find_above(self, tagOrId):
1152 return self.find('above', tagOrId)
1153 def find_all(self):
1154 return self.find('all')
1155 def find_below(self, tagOrId):
1156 return self.find('below', tagOrId)
1157 def find_closest(self, x, y, halo=None, start=None):
1158 return self.find('closest', x, y, halo, start)
1159 def find_enclosed(self, x1, y1, x2, y2):
1160 return self.find('enclosed', x1, y1, x2, y2)
1161 def find_overlapping(self, x1, y1, x2, y2):
1162 return self.find('overlapping', x1, y1, x2, y2)
1163 def find_withtag(self, tagOrId):
1164 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001165 def focus(self, *args):
1166 return self._do('focus', args)
1167 def gettags(self, *args):
1168 return self.tk.splitlist(self._do('gettags', args))
1169 def icursor(self, *args):
1170 self._do('icursor', args)
1171 def index(self, *args):
1172 return self.tk.getint(self._do('index', args))
1173 def insert(self, *args):
1174 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001175 def itemcget(self, tagOrId, option):
1176 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001177 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001178 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001179 cnf = {}
1180 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001181 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001182 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1183 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001184 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001185 x = self.tk.split(self._do('itemconfigure',
1186 (tagOrId, '-'+cnf,)))
1187 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001188 self._do('itemconfigure', (tagOrId,)
1189 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001190 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001191 def lower(self, *args):
1192 self._do('lower', args)
1193 def move(self, *args):
1194 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001195 def postscript(self, cnf={}, **kw):
1196 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001197 def tkraise(self, *args):
1198 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001199 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001200 def scale(self, *args):
1201 self._do('scale', args)
1202 def scan_mark(self, x, y):
1203 self.tk.call(self._w, 'scan', 'mark', x, y)
1204 def scan_dragto(self, x, y):
1205 self.tk.call(self._w, 'scan', 'dragto', x, y)
1206 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001207 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001208 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001209 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001210 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001211 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001212 def select_item(self):
1213 self.tk.call(self._w, 'select', 'item')
1214 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001215 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001216 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001217 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001218 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001219 if not args:
1220 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001221 apply(self.tk.call, (self._w, 'xview')+args)
1222 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001223 if not args:
1224 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001225 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001226
1227class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001228 def __init__(self, master=None, cnf={}, **kw):
1229 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001230 def deselect(self):
1231 self.tk.call(self._w, 'deselect')
1232 def flash(self):
1233 self.tk.call(self._w, 'flash')
1234 def invoke(self):
1235 self.tk.call(self._w, 'invoke')
1236 def select(self):
1237 self.tk.call(self._w, 'select')
1238 def toggle(self):
1239 self.tk.call(self._w, 'toggle')
1240
1241class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001242 def __init__(self, master=None, cnf={}, **kw):
1243 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001244 def delete(self, first, last=None):
1245 self.tk.call(self._w, 'delete', first, last)
1246 def get(self):
1247 return self.tk.call(self._w, 'get')
1248 def icursor(self, index):
1249 self.tk.call(self._w, 'icursor', index)
1250 def index(self, index):
1251 return self.tk.getint(self.tk.call(
1252 self._w, 'index', index))
1253 def insert(self, index, string):
1254 self.tk.call(self._w, 'insert', index, string)
1255 def scan_mark(self, x):
1256 self.tk.call(self._w, 'scan', 'mark', x)
1257 def scan_dragto(self, x):
1258 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001259 def selection_adjust(self, index):
1260 self.tk.call(self._w, 'selection', 'adjust', index)
1261 select_adjust = selection_adjust
1262 def selection_clear(self):
1263 self.tk.call(self._w, 'selection', 'clear')
1264 select_clear = selection_clear
1265 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001266 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001267 select_from = selection_from
1268 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001269 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001270 self.tk.call(self._w, 'selection', 'present'))
1271 select_present = selection_present
1272 def selection_range(self, start, end):
1273 self.tk.call(self._w, 'selection', 'range', start, end)
1274 select_range = selection_range
1275 def selection_to(self, index):
1276 self.tk.call(self._w, 'selection', 'to', index)
1277 select_to = selection_to
1278 def xview(self, index):
1279 self.tk.call(self._w, 'xview', index)
1280 def xview_moveto(self, fraction):
1281 self.tk.call(self._w, 'xview', 'moveto', fraction)
1282 def xview_scroll(self, number, what):
1283 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001284
1285class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001286 def __init__(self, master=None, cnf={}, **kw):
1287 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001288 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001289 if cnf.has_key('class_'):
1290 extra = ('-class', cnf['class_'])
1291 del cnf['class_']
1292 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001293 extra = ('-class', cnf['class'])
1294 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001295 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001296
1297class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001298 def __init__(self, master=None, cnf={}, **kw):
1299 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001300
Guido van Rossum18468821994-06-20 07:49:28 +00001301class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001302 def __init__(self, master=None, cnf={}, **kw):
1303 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001304 def activate(self, index):
1305 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001306 def bbox(self, *args):
1307 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001308 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001309 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001310 return self.tk.splitlist(self.tk.call(
1311 self._w, 'curselection'))
1312 def delete(self, first, last=None):
1313 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001314 def get(self, first, last=None):
1315 if last:
1316 return self.tk.splitlist(self.tk.call(
1317 self._w, 'get', first, last))
1318 else:
1319 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001320 def insert(self, index, *elements):
1321 apply(self.tk.call,
1322 (self._w, 'insert', index) + elements)
1323 def nearest(self, y):
1324 return self.tk.getint(self.tk.call(
1325 self._w, 'nearest', y))
1326 def scan_mark(self, x, y):
1327 self.tk.call(self._w, 'scan', 'mark', x, y)
1328 def scan_dragto(self, x, y):
1329 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001330 def see(self, index):
1331 self.tk.call(self._w, 'see', index)
1332 def index(self, index):
1333 i = self.tk.call(self._w, 'index', index)
1334 if i == 'none': return None
1335 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001336 def select_anchor(self, index):
1337 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001338 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001339 def select_clear(self, first, last=None):
1340 self.tk.call(self._w,
1341 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001342 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001343 def select_includes(self, index):
1344 return self.tk.getboolean(self.tk.call(
1345 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001346 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001347 def select_set(self, first, last=None):
1348 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001349 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001350 def size(self):
1351 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001352 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001353 if not what:
1354 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001355 apply(self.tk.call, (self._w, 'xview')+what)
1356 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001357 if not what:
1358 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001359 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001360
1361class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001362 def __init__(self, master=None, cnf={}, **kw):
1363 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001364 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001365 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001366 def tk_mbPost(self):
1367 self.tk.call('tk_mbPost', self._w)
1368 def tk_mbUnpost(self):
1369 self.tk.call('tk_mbUnpost')
1370 def tk_traverseToMenu(self, char):
1371 self.tk.call('tk_traverseToMenu', self._w, char)
1372 def tk_traverseWithinMenu(self, char):
1373 self.tk.call('tk_traverseWithinMenu', self._w, char)
1374 def tk_getMenuButtons(self):
1375 return self.tk.call('tk_getMenuButtons', self._w)
1376 def tk_nextMenu(self, count):
1377 self.tk.call('tk_nextMenu', count)
1378 def tk_nextMenuEntry(self, count):
1379 self.tk.call('tk_nextMenuEntry', count)
1380 def tk_invokeMenu(self):
1381 self.tk.call('tk_invokeMenu', self._w)
1382 def tk_firstMenu(self):
1383 self.tk.call('tk_firstMenu', self._w)
1384 def tk_mbButtonDown(self):
1385 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001386 def tk_popup(self, x, y, entry=""):
1387 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001388 def activate(self, index):
1389 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001390 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001391 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001392 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001393 def add_cascade(self, cnf={}, **kw):
1394 self.add('cascade', cnf or kw)
1395 def add_checkbutton(self, cnf={}, **kw):
1396 self.add('checkbutton', cnf or kw)
1397 def add_command(self, cnf={}, **kw):
1398 self.add('command', cnf or kw)
1399 def add_radiobutton(self, cnf={}, **kw):
1400 self.add('radiobutton', cnf or kw)
1401 def add_separator(self, cnf={}, **kw):
1402 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001403 def insert(self, index, itemType, cnf={}, **kw):
1404 apply(self.tk.call, (self._w, 'insert', index, itemType)
1405 + self._options(cnf, kw))
1406 def insert_cascade(self, index, cnf={}, **kw):
1407 self.insert(index, 'cascade', cnf or kw)
1408 def insert_checkbutton(self, index, cnf={}, **kw):
1409 self.insert(index, 'checkbutton', cnf or kw)
1410 def insert_command(self, index, cnf={}, **kw):
1411 self.insert(index, 'command', cnf or kw)
1412 def insert_radiobutton(self, index, cnf={}, **kw):
1413 self.insert(index, 'radiobutton', cnf or kw)
1414 def insert_separator(self, index, cnf={}, **kw):
1415 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001416 def delete(self, index1, index2=None):
1417 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001418 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001419 if cnf is None and not kw:
1420 cnf = {}
1421 for x in self.tk.split(apply(self.tk.call,
1422 (self._w, 'entryconfigure', index))):
1423 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1424 return cnf
1425 if type(cnf) == StringType and not kw:
1426 x = self.tk.split(apply(self.tk.call,
1427 (self._w, 'entryconfigure', index, '-'+cnf)))
1428 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001429 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001430 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001431 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001432 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001433 i = self.tk.call(self._w, 'index', index)
1434 if i == 'none': return None
1435 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001436 def invoke(self, index):
1437 return self.tk.call(self._w, 'invoke', index)
1438 def post(self, x, y):
1439 self.tk.call(self._w, 'post', x, y)
1440 def unpost(self):
1441 self.tk.call(self._w, 'unpost')
1442 def yposition(self, index):
1443 return self.tk.getint(self.tk.call(
1444 self._w, 'yposition', index))
1445
1446class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001447 def __init__(self, master=None, cnf={}, **kw):
1448 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001449
1450class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001451 def __init__(self, master=None, cnf={}, **kw):
1452 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001453
1454class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001455 def __init__(self, master=None, cnf={}, **kw):
1456 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001457 def deselect(self):
1458 self.tk.call(self._w, 'deselect')
1459 def flash(self):
1460 self.tk.call(self._w, 'flash')
1461 def invoke(self):
1462 self.tk.call(self._w, 'invoke')
1463 def select(self):
1464 self.tk.call(self._w, 'select')
1465
1466class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001467 def __init__(self, master=None, cnf={}, **kw):
1468 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001469 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001470 value = self.tk.call(self._w, 'get')
1471 try:
1472 return self.tk.getint(value)
1473 except TclError:
1474 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001475 def set(self, value):
1476 self.tk.call(self._w, 'set', value)
1477
1478class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001479 def __init__(self, master=None, cnf={}, **kw):
1480 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001481 def activate(self, index):
1482 self.tk.call(self._w, 'activate', index)
1483 def delta(self, deltax, deltay):
1484 return self.getdouble(self.tk.call(
1485 self._w, 'delta', deltax, deltay))
1486 def fraction(self, x, y):
1487 return self.getdouble(self.tk.call(
1488 self._w, 'fraction', x, y))
1489 def identify(self, x, y):
1490 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001491 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001492 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001493 def set(self, *args):
1494 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001495
1496class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001497 def __init__(self, master=None, cnf={}, **kw):
1498 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001499 def bbox(self, *args):
1500 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001501 def tk_textSelectTo(self, index):
1502 self.tk.call('tk_textSelectTo', self._w, index)
1503 def tk_textBackspace(self):
1504 self.tk.call('tk_textBackspace', self._w)
1505 def tk_textIndexCloser(self, a, b, c):
1506 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1507 def tk_textResetAnchor(self, index):
1508 self.tk.call('tk_textResetAnchor', self._w, index)
1509 def compare(self, index1, op, index2):
1510 return self.tk.getboolean(self.tk.call(
1511 self._w, 'compare', index1, op, index2))
1512 def debug(self, boolean=None):
1513 return self.tk.getboolean(self.tk.call(
1514 self._w, 'debug', boolean))
1515 def delete(self, index1, index2=None):
1516 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001517 def dlineinfo(self, index):
1518 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001519 def get(self, index1, index2=None):
1520 return self.tk.call(self._w, 'get', index1, index2)
1521 def index(self, index):
1522 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001523 def insert(self, index, chars, *args):
1524 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001525 def mark_gravity(self, markName, direction=None):
1526 return apply(self.tk.call,
1527 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001528 def mark_names(self):
1529 return self.tk.splitlist(self.tk.call(
1530 self._w, 'mark', 'names'))
1531 def mark_set(self, markName, index):
1532 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001533 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001534 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001535 def scan_mark(self, x, y):
1536 self.tk.call(self._w, 'scan', 'mark', x, y)
1537 def scan_dragto(self, x, y):
1538 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001539 def search(self, pattern, index, stopindex=None,
1540 forwards=None, backwards=None, exact=None,
1541 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001542 args = [self._w, 'search']
1543 if forwards: args.append('-forwards')
1544 if backwards: args.append('-backwards')
1545 if exact: args.append('-exact')
1546 if regexp: args.append('-regexp')
1547 if nocase: args.append('-nocase')
1548 if count: args.append('-count'); args.append(count)
1549 if pattern[0] == '-': args.append('--')
1550 args.append(pattern)
1551 args.append(index)
1552 if stopindex: args.append(stopindex)
1553 return apply(self.tk.call, tuple(args))
1554 def see(self, index):
1555 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001556 def tag_add(self, tagName, index1, index2=None):
1557 self.tk.call(
1558 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001559 def tag_unbind(self, tagName, sequence):
1560 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001561 def tag_bind(self, tagName, sequence, func, add=None):
1562 return self._bind((self._w, 'tag', 'bind', tagName),
1563 sequence, func, add)
1564 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001565 if option[:1] != '-':
1566 option = '-' + option
1567 if option[-1:] == '_':
1568 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001569 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001570 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001571 if type(cnf) == StringType:
1572 x = self.tk.split(self.tk.call(
1573 self._w, 'tag', 'configure', tagName, '-'+cnf))
1574 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001575 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001576 (self._w, 'tag', 'configure', tagName)
1577 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001578 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001579 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001580 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001581 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001582 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001583 def tag_names(self, index=None):
1584 return self.tk.splitlist(
1585 self.tk.call(self._w, 'tag', 'names', index))
1586 def tag_nextrange(self, tagName, index1, index2=None):
1587 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001588 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001589 def tag_raise(self, tagName, aboveThis=None):
1590 self.tk.call(
1591 self._w, 'tag', 'raise', tagName, aboveThis)
1592 def tag_ranges(self, tagName):
1593 return self.tk.splitlist(self.tk.call(
1594 self._w, 'tag', 'ranges', tagName))
1595 def tag_remove(self, tagName, index1, index2=None):
1596 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001597 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001598 def window_cget(self, index, option):
1599 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001600 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001601 if type(cnf) == StringType:
1602 x = self.tk.split(self.tk.call(
1603 self._w, 'window', 'configure',
1604 index, '-'+cnf))
1605 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001606 apply(self.tk.call,
1607 (self._w, 'window', 'configure', index)
1608 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001609 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001610 def window_create(self, index, cnf={}, **kw):
1611 apply(self.tk.call,
1612 (self._w, 'window', 'create', index)
1613 + self._options(cnf, kw))
1614 def window_names(self):
1615 return self.tk.splitlist(
1616 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001617 def xview(self, *what):
1618 if not what:
1619 return self._getdoubles(self.tk.call(self._w, 'xview'))
1620 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001621 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001622 if not what:
1623 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001624 apply(self.tk.call, (self._w, 'yview')+what)
1625 def yview_pickplace(self, *what):
1626 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001627
Guido van Rossum28574b51996-10-21 15:16:51 +00001628class _setit:
1629 def __init__(self, var, value):
1630 self.__value = value
1631 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001632 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001633 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001634
1635class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001636 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001637 kw = {"borderwidth": 2, "textvariable": variable,
1638 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1639 "highlightthickness": 2}
1640 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001641 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001642 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1643 self.menuname = menu._w
1644 menu.add_command(label=value, command=_setit(variable, value))
1645 for v in values:
1646 menu.add_command(label=v, command=_setit(variable, v))
1647 self["menu"] = menu
1648
1649 def __getitem__(self, name):
1650 if name == 'menu':
1651 return self.__menu
1652 return Widget.__getitem__(self, name)
1653
1654 def destroy(self):
1655 Menubutton.destroy(self)
1656 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001657
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001658class Image:
1659 def __init__(self, imgtype, name=None, cnf={}, **kw):
1660 self.name = None
1661 master = _default_root
1662 if not master: raise RuntimeError, 'Too early to create image'
1663 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001664 if not name:
1665 name = `id(self)`
1666 # The following is needed for systems where id(x)
1667 # can return a negative number, such as Linux/m68k:
1668 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001669 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1670 elif kw: cnf = kw
1671 options = ()
1672 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001673 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001674 v = self._register(v)
1675 options = options + ('-'+k, v)
1676 apply(self.tk.call,
1677 ('image', 'create', imgtype, name,) + options)
1678 self.name = name
1679 def __str__(self): return self.name
1680 def __del__(self):
1681 if self.name:
1682 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001683 def __setitem__(self, key, value):
1684 self.tk.call(self.name, 'configure', '-'+key, value)
1685 def __getitem__(self, key):
1686 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001687 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001688 res = ()
1689 for k, v in _cnfmerge(kw).items():
1690 if v is not None:
1691 if k[-1] == '_': k = k[:-1]
1692 if callable(v):
1693 v = self._register(v)
1694 res = res + ('-'+k, v)
1695 apply(self.tk.call, (self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001696 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001697 def height(self):
1698 return self.tk.getint(
1699 self.tk.call('image', 'height', self.name))
1700 def type(self):
1701 return self.tk.call('image', 'type', self.name)
1702 def width(self):
1703 return self.tk.getint(
1704 self.tk.call('image', 'width', self.name))
1705
1706class PhotoImage(Image):
1707 def __init__(self, name=None, cnf={}, **kw):
1708 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001709 def blank(self):
1710 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001711 def cget(self, option):
1712 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001713 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001714 def __getitem__(self, key):
1715 return self.tk.call(self.name, 'cget', '-' + key)
1716 def copy(self):
1717 destImage = PhotoImage()
1718 self.tk.call(destImage, 'copy', self.name)
1719 return destImage
1720 def zoom(self,x,y=''):
1721 destImage = PhotoImage()
1722 if y=='': y=x
1723 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1724 return destImage
1725 def subsample(self,x,y=''):
1726 destImage = PhotoImage()
1727 if y=='': y=x
1728 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1729 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001730 def get(self, x, y):
1731 return self.tk.call(self.name, 'get', x, y)
1732 def put(self, data, to=None):
1733 args = (self.name, 'put', data)
1734 if to:
1735 args = args + to
1736 apply(self.tk.call, args)
1737 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001738 def write(self, filename, format=None, from_coords=None):
1739 args = (self.name, 'write', filename)
1740 if format:
1741 args = args + ('-format', format)
1742 if from_coords:
1743 args = args + ('-from',) + tuple(from_coords)
1744 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001745
1746class BitmapImage(Image):
1747 def __init__(self, name=None, cnf={}, **kw):
1748 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1749
1750def image_names(): return _default_root.tk.call('image', 'names')
1751def image_types(): return _default_root.tk.call('image', 'types')
1752
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001753######################################################################
1754# Extensions:
1755
1756class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001757 def __init__(self, master=None, cnf={}, **kw):
1758 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001759 self.bind('<Any-Enter>', self.tkButtonEnter)
1760 self.bind('<Any-Leave>', self.tkButtonLeave)
1761 self.bind('<1>', self.tkButtonDown)
1762 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001763
1764class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001765 def __init__(self, master=None, cnf={}, **kw):
1766 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001767 self.bind('<Any-Enter>', self.tkButtonEnter)
1768 self.bind('<Any-Leave>', self.tkButtonLeave)
1769 self.bind('<1>', self.tkButtonDown)
1770 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1771 self['fg'] = self['bg']
1772 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001773
Guido van Rossumc417ef81996-08-21 23:38:59 +00001774######################################################################
1775# Test:
1776
1777def _test():
1778 root = Tk()
1779 label = Label(root, text="Proof-of-existence test for Tk")
1780 label.pack()
1781 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001782 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001783 text="[%s]" % root.test['text']))
1784 test.pack()
1785 root.test = test
1786 quit = Button(root, text="QUIT", command=root.destroy)
1787 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001788 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001789 root.mainloop()
1790
1791if __name__ == '__main__':
1792 _test()
1793
Guido van Rossum37dcab11996-05-16 16:00:19 +00001794
1795# Emacs cruft
1796# Local Variables:
1797# py-indent-offset: 8
1798# End: