blob: e59c3d536f07e824426528e4c3a77542dd0a9402 [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:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000124 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000125 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000126 def destroy(self):
127 if self._tclCommands is not None:
128 for name in self._tclCommands:
129 #print '- Tkinter: deleted command', name
130 self.tk.deletecommand(name)
131 self._tclCommands = None
132 def deletecommand(self, name):
133 #print '- Tkinter: deleted command', name
134 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000135 try:
136 self._tclCommands.remove(name)
137 except ValueError:
138 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000139 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000140 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000141 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000142 def tk_bisque(self):
143 self.tk.call('tk_bisque')
144 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000145 apply(self.tk.call, ('tk_setPalette',)
146 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000147 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000148 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000149 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000150 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000151 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000152 def wait_window(self, window=None):
153 if window == None:
154 window = self
155 self.tk.call('tkwait', 'window', window._w)
156 def wait_visibility(self, window=None):
157 if window == None:
158 window = self
159 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000160 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000161 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000162 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000163 return self.tk.getvar(name)
164 def getint(self, s):
165 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000166 def getdouble(self, s):
167 return self.tk.getdouble(s)
168 def getboolean(self, s):
169 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000170 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000171 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000172 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000173 def focus_force(self):
174 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000175 def focus_get(self):
176 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000177 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000178 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000179 def focus_displayof(self):
180 name = self.tk.call('focus', '-displayof', self._w)
181 if name == 'none' or not name: return None
182 return self._nametowidget(name)
183 def focus_lastfor(self):
184 name = self.tk.call('focus', '-lastfor', self._w)
185 if name == 'none' or not name: return None
186 return self._nametowidget(name)
187 def tk_focusFollowsMouse(self):
188 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000189 def tk_focusNext(self):
190 name = self.tk.call('tk_focusNext', self._w)
191 if not name: return None
192 return self._nametowidget(name)
193 def tk_focusPrev(self):
194 name = self.tk.call('tk_focusPrev', self._w)
195 if not name: return None
196 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000197 def after(self, ms, func=None, *args):
198 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000199 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000200 self.tk.call('after', ms)
201 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000202 # XXX Disgusting hack to clean up after calling func
203 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000204 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000205 try:
206 apply(func, args)
207 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000208 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000209 name = self._register(callit)
210 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000211 return self.tk.call('after', ms, name)
212 def after_idle(self, func, *args):
213 return apply(self.after, ('idle', func) + args)
214 def after_cancel(self, id):
215 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000216 def bell(self, displayof=0):
217 apply(self.tk.call, ('bell',) + self._displayof(displayof))
218 # Clipboard handling:
219 def clipboard_clear(self, **kw):
220 if not kw.has_key('displayof'): kw['displayof'] = self._w
221 apply(self.tk.call,
222 ('clipboard', 'clear') + self._options(kw))
223 def clipboard_append(self, string, **kw):
224 if not kw.has_key('displayof'): kw['displayof'] = self._w
225 apply(self.tk.call,
226 ('clipboard', 'append') + self._options(kw)
227 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000228 # XXX grab current w/o window argument
229 def grab_current(self):
230 name = self.tk.call('grab', 'current', self._w)
231 if not name: return None
232 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000233 def grab_release(self):
234 self.tk.call('grab', 'release', self._w)
235 def grab_set(self):
236 self.tk.call('grab', 'set', self._w)
237 def grab_set_global(self):
238 self.tk.call('grab', 'set', '-global', self._w)
239 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000240 status = self.tk.call('grab', 'status', self._w)
241 if status == 'none': status = None
242 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000243 def lower(self, belowThis=None):
244 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000245 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000246 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000247 def option_clear(self):
248 self.tk.call('option', 'clear')
249 def option_get(self, name, className):
250 return self.tk.call('option', 'get', self._w, name, className)
251 def option_readfile(self, fileName, priority = None):
252 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000253 def selection_clear(self, **kw):
254 if not kw.has_key('displayof'): kw['displayof'] = self._w
255 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
256 def selection_get(self, **kw):
257 if not kw.has_key('displayof'): kw['displayof'] = self._w
258 return apply(self.tk.call,
259 ('selection', 'get') + self._options(kw))
260 def selection_handle(self, command, **kw):
261 name = self._register(command)
262 apply(self.tk.call,
263 ('selection', 'handle') + self._options(kw)
264 + (self._w, name))
265 def selection_own(self, **kw):
266 "Become owner of X selection."
267 apply(self.tk.call,
268 ('selection', 'own') + self._options(kw) + (self._w,))
269 def selection_own_get(self, **kw):
270 "Find owner of X selection."
271 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000272 name = apply(self.tk.call,
273 ('selection', 'own') + self._options(kw))
274 if not name: return None
275 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000276 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000277 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000278 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000279 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000280 def tkraise(self, aboveThis=None):
281 self.tk.call('raise', self._w, aboveThis)
282 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000283 def colormodel(self, value=None):
284 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000285 def winfo_atom(self, name, displayof=0):
286 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
287 return self.tk.getint(apply(self.tk.call, args))
288 def winfo_atomname(self, id, displayof=0):
289 args = ('winfo', 'atomname') \
290 + self._displayof(displayof) + (id,)
291 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000292 def winfo_cells(self):
293 return self.tk.getint(
294 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000295 def winfo_children(self):
296 return map(self._nametowidget,
297 self.tk.splitlist(self.tk.call(
298 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000299 def winfo_class(self):
300 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000301 def winfo_colormapfull(self):
302 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000303 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000304 def winfo_containing(self, rootX, rootY, displayof=0):
305 args = ('winfo', 'containing') \
306 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000307 name = apply(self.tk.call, args)
308 if not name: return None
309 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000310 def winfo_depth(self):
311 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
312 def winfo_exists(self):
313 return self.tk.getint(
314 self.tk.call('winfo', 'exists', self._w))
315 def winfo_fpixels(self, number):
316 return self.tk.getdouble(self.tk.call(
317 'winfo', 'fpixels', self._w, number))
318 def winfo_geometry(self):
319 return self.tk.call('winfo', 'geometry', self._w)
320 def winfo_height(self):
321 return self.tk.getint(
322 self.tk.call('winfo', 'height', self._w))
323 def winfo_id(self):
324 return self.tk.getint(
325 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000326 def winfo_interps(self, displayof=0):
327 args = ('winfo', 'interps') + self._displayof(displayof)
328 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000329 def winfo_ismapped(self):
330 return self.tk.getint(
331 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000332 def winfo_manager(self):
333 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000334 def winfo_name(self):
335 return self.tk.call('winfo', 'name', self._w)
336 def winfo_parent(self):
337 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000338 def winfo_pathname(self, id, displayof=0):
339 args = ('winfo', 'pathname') \
340 + self._displayof(displayof) + (id,)
341 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000342 def winfo_pixels(self, number):
343 return self.tk.getint(
344 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000345 def winfo_pointerx(self):
346 return self.tk.getint(
347 self.tk.call('winfo', 'pointerx', self._w))
348 def winfo_pointerxy(self):
349 return self._getints(
350 self.tk.call('winfo', 'pointerxy', self._w))
351 def winfo_pointery(self):
352 return self.tk.getint(
353 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000354 def winfo_reqheight(self):
355 return self.tk.getint(
356 self.tk.call('winfo', 'reqheight', self._w))
357 def winfo_reqwidth(self):
358 return self.tk.getint(
359 self.tk.call('winfo', 'reqwidth', self._w))
360 def winfo_rgb(self, color):
361 return self._getints(
362 self.tk.call('winfo', 'rgb', self._w, color))
363 def winfo_rootx(self):
364 return self.tk.getint(
365 self.tk.call('winfo', 'rootx', self._w))
366 def winfo_rooty(self):
367 return self.tk.getint(
368 self.tk.call('winfo', 'rooty', self._w))
369 def winfo_screen(self):
370 return self.tk.call('winfo', 'screen', self._w)
371 def winfo_screencells(self):
372 return self.tk.getint(
373 self.tk.call('winfo', 'screencells', self._w))
374 def winfo_screendepth(self):
375 return self.tk.getint(
376 self.tk.call('winfo', 'screendepth', self._w))
377 def winfo_screenheight(self):
378 return self.tk.getint(
379 self.tk.call('winfo', 'screenheight', self._w))
380 def winfo_screenmmheight(self):
381 return self.tk.getint(
382 self.tk.call('winfo', 'screenmmheight', self._w))
383 def winfo_screenmmwidth(self):
384 return self.tk.getint(
385 self.tk.call('winfo', 'screenmmwidth', self._w))
386 def winfo_screenvisual(self):
387 return self.tk.call('winfo', 'screenvisual', self._w)
388 def winfo_screenwidth(self):
389 return self.tk.getint(
390 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000391 def winfo_server(self):
392 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000393 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000394 return self._nametowidget(self.tk.call(
395 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000396 def winfo_viewable(self):
397 return self.tk.getint(
398 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000399 def winfo_visual(self):
400 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000401 def winfo_visualid(self):
402 return self.tk.call('winfo', 'visualid', self._w)
403 def winfo_visualsavailable(self, includeids=0):
404 data = self.tk.split(
405 self.tk.call('winfo', 'visualsavailable', self._w,
406 includeids and 'includeids' or None))
407 def parseitem(x, self=self):
408 return x[:1] + tuple(map(self.tk.getint, x[1:]))
409 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000410 def winfo_vrootheight(self):
411 return self.tk.getint(
412 self.tk.call('winfo', 'vrootheight', self._w))
413 def winfo_vrootwidth(self):
414 return self.tk.getint(
415 self.tk.call('winfo', 'vrootwidth', self._w))
416 def winfo_vrootx(self):
417 return self.tk.getint(
418 self.tk.call('winfo', 'vrootx', self._w))
419 def winfo_vrooty(self):
420 return self.tk.getint(
421 self.tk.call('winfo', 'vrooty', self._w))
422 def winfo_width(self):
423 return self.tk.getint(
424 self.tk.call('winfo', 'width', self._w))
425 def winfo_x(self):
426 return self.tk.getint(
427 self.tk.call('winfo', 'x', self._w))
428 def winfo_y(self):
429 return self.tk.getint(
430 self.tk.call('winfo', 'y', self._w))
431 def update(self):
432 self.tk.call('update')
433 def update_idletasks(self):
434 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000435 def bindtags(self, tagList=None):
436 if tagList is None:
437 return self.tk.splitlist(
438 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000439 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000440 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000441 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000442 if func:
443 cmd = ("%sset _tkinter_break [%s %s]\n"
444 'if {"$_tkinter_break" == "break"} break\n') \
445 % (add and '+' or '',
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000446 self._register(func, self._substitute,
447 needcleanup),
Guido van Rossum37dcab11996-05-16 16:00:19 +0000448 _string.join(self._subst_format))
449 apply(self.tk.call, what + (sequence, cmd))
450 elif func == '':
451 apply(self.tk.call, what + (sequence, func))
452 else:
453 return apply(self.tk.call, what + (sequence,))
454 def bind(self, sequence=None, func=None, add=None):
455 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000456 def unbind(self, sequence):
457 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000458 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000459 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000460 def unbind_all(self, sequence):
461 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000462 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000463 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000464 def unbind_class(self, className, sequence):
465 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000466 def mainloop(self, n=0):
467 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000468 def quit(self):
469 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000470 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000471 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000472 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
473 def _getdoubles(self, string):
474 if not string: return None
475 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000476 def _getboolean(self, string):
477 if string:
478 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000479 def _displayof(self, displayof):
480 if displayof:
481 return ('-displayof', displayof)
482 if displayof is None:
483 return ('-displayof', self._w)
484 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000485 def _options(self, cnf, kw = None):
486 if kw:
487 cnf = _cnfmerge((cnf, kw))
488 else:
489 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000490 res = ()
491 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000492 if v is not None:
493 if k[-1] == '_': k = k[:-1]
494 if callable(v):
495 v = self._register(v)
496 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000497 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000498 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000499 w = self
500 if name[0] == '.':
501 w = w._root()
502 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000503 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000504 while name:
505 i = find(name, '.')
506 if i >= 0:
507 name, tail = name[:i], name[i+1:]
508 else:
509 tail = ''
510 w = w.children[name]
511 name = tail
512 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000513 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000514 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000515 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000516 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000517 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000518 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000519 except AttributeError:
520 pass
521 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000522 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000523 except AttributeError:
524 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000525 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000526 if needcleanup:
527 if self._tclCommands is None:
528 self._tclCommands = []
529 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000530 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000531 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000532 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000533 def _root(self):
534 w = self
535 while w.master: w = w.master
536 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000537 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000538 '%s', '%t', '%w', '%x', '%y',
539 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
540 def _substitute(self, *args):
541 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000542 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000543 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
544 # Missing: (a, c, d, m, o, v, B, R)
545 e = Event()
546 e.serial = tk.getint(nsign)
547 e.num = tk.getint(b)
548 try: e.focus = tk.getboolean(f)
549 except TclError: pass
550 e.height = tk.getint(h)
551 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000552 # For Visibility events, event state is a string and
553 # not an integer:
554 try:
555 e.state = tk.getint(s)
556 except TclError:
557 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000558 e.time = tk.getint(t)
559 e.width = tk.getint(w)
560 e.x = tk.getint(x)
561 e.y = tk.getint(y)
562 e.char = A
563 try: e.send_event = tk.getboolean(E)
564 except TclError: pass
565 e.keysym = K
566 e.keysym_num = tk.getint(N)
567 e.type = T
568 e.widget = self._nametowidget(W)
569 e.x_root = tk.getint(X)
570 e.y_root = tk.getint(Y)
571 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000572 def _report_exception(self):
573 import sys
574 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
575 root = self._root()
576 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000577 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000578 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000579 # XXX ought to generalize this so tag_config etc. can use it
580 if kw:
581 cnf = _cnfmerge((cnf, kw))
582 elif cnf:
583 cnf = _cnfmerge(cnf)
584 if cnf is None:
585 cnf = {}
586 for x in self.tk.split(
587 self.tk.call(self._w, 'configure')):
588 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
589 return cnf
590 if type(cnf) is StringType:
591 x = self.tk.split(self.tk.call(
592 self._w, 'configure', '-'+cnf))
593 return (x[0][1:],) + x[1:]
594 apply(self.tk.call, (self._w, 'configure')
595 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000596 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000597 def cget(self, key):
598 return self.tk.call(self._w, 'cget', '-' + key)
599 __getitem__ = cget
600 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000601 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000602 def keys(self):
603 return map(lambda x: x[0][1:],
604 self.tk.split(self.tk.call(self._w, 'configure')))
605 def __str__(self):
606 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000607 # Pack methods that apply to the master
608 _noarg_ = ['_noarg_']
609 def pack_propagate(self, flag=_noarg_):
610 if flag is Misc._noarg_:
611 return self._getboolean(self.tk.call(
612 'pack', 'propagate', self._w))
613 else:
614 self.tk.call('pack', 'propagate', self._w, flag)
615 propagate = pack_propagate
616 def pack_slaves(self):
617 return map(self._nametowidget,
618 self.tk.splitlist(
619 self.tk.call('pack', 'slaves', self._w)))
620 slaves = pack_slaves
621 # Place method that applies to the master
622 def place_slaves(self):
623 return map(self._nametowidget,
624 self.tk.splitlist(
625 self.tk.call(
626 'place', 'slaves', self._w)))
627 # Grid methods that apply to the master
628 def grid_bbox(self, column, row):
629 return self._getints(
630 self.tk.call(
631 'grid', 'bbox', self._w, column, row)) or None
632 bbox = grid_bbox
633 def grid_columnconfigure(self, index, cnf={}, **kw):
634 if type(cnf) is not DictionaryType and not kw:
635 options = self._options({cnf: None})
636 else:
637 options = self._options(cnf, kw)
638 if not options:
639 res = self.tk.call('grid',
640 'columnconfigure', self._w, index)
641 words = self.tk.splitlist(res)
642 dict = {}
643 for i in range(0, len(words), 2):
644 key = words[i][1:]
645 value = words[i+1]
646 if not value:
647 value = None
648 elif '.' in value:
649 value = self.tk.getdouble(value)
650 else:
651 value = self.tk.getint(value)
652 dict[key] = value
653 return dict
654 res = apply(self.tk.call,
655 ('grid', 'columnconfigure', self._w, index)
656 + options)
657 if options == ('-minsize', None):
658 return self.tk.getint(res) or None
659 elif options == ('-weight', None):
660 return self.tk.getdouble(res) or None
661 columnconfigure = grid_columnconfigure
662 def grid_propagate(self, flag=_noarg_):
663 if flag is Misc._noarg_:
664 return self._getboolean(self.tk.call(
665 'grid', 'propagate', self._w))
666 else:
667 self.tk.call('grid', 'propagate', self._w, flag)
668 def grid_rowconfigure(self, index, cnf={}, **kw):
669 if type(cnf) is not DictionaryType and not kw:
670 options = self._options({cnf: None})
671 else:
672 options = self._options(cnf, kw)
673 if not options:
674 res = self.tk.call('grid',
675 'rowconfigure', self._w, index)
676 words = self.tk.splitlist(res)
677 dict = {}
678 for i in range(0, len(words), 2):
679 key = words[i][1:]
680 value = words[i+1]
681 if not value:
682 value = None
683 elif '.' in value:
684 value = self.tk.getdouble(value)
685 else:
686 value = self.tk.getint(value)
687 dict[key] = value
688 return dict
689 res = apply(self.tk.call,
690 ('grid', 'rowconfigure', self._w, index)
691 + options)
692 if len(options) == 2 and options[-1] is None:
693 if not res: return None
694 # In Tk 7.5, -width can be a float
695 if '.' in res: return self.tk.getdouble(res)
696 return self.tk.getint(res)
697 rowconfigure = grid_rowconfigure
698 def grid_size(self):
699 return self._getints(
700 self.tk.call('grid', 'size', self._w)) or None
701 size = grid_size
702 def grid_slaves(self, *args):
703 return map(self._nametowidget,
704 self.tk.splitlist(
705 apply(self.tk.call,
706 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000707
Guido van Rossum80f8be81997-12-02 19:51:39 +0000708 # Support for the "event" command, new in Tk 4.2.
709 # By Case Roole.
710
711 def event_add(self,virtual, *sequences):
712 args = ('event', 'add', virtual) + sequences
713 apply( _default_root.tk.call, args )
714
715 def event_delete(self,virtual,*sequences):
716 args = ('event', 'delete', virtual) + sequences
717 apply( _default_root.tk.call, args )
718
719 def event_generate(self, sequence, **kw):
720 args = ('event', 'generate', self._w, sequence)
721 for k,v in kw.items():
722 args = args + ('-%s' % k,str(v))
723 apply( _default_root.tk.call, args )
724
725 def event_info(self,virtual=None):
726 args = ('event', 'info')
727 if virtual is not None: args = args + (virtual,)
728 s = apply( _default_root.tk.call, args )
729 return _string.split(s)
730
731
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000732class CallWrapper:
733 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000734 self.func = func
735 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000736 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000737 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000738 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000739 if self.subst:
740 args = apply(self.subst, args)
741 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000742 except SystemExit, msg:
743 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000744 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000745 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000746
747class Wm:
748 def aspect(self,
749 minNumer=None, minDenom=None,
750 maxNumer=None, maxDenom=None):
751 return self._getints(
752 self.tk.call('wm', 'aspect', self._w,
753 minNumer, minDenom,
754 maxNumer, maxDenom))
755 def client(self, name=None):
756 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000757 def colormapwindows(self, *wlist):
758 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
759 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000760 def command(self, value=None):
761 return self.tk.call('wm', 'command', self._w, value)
762 def deiconify(self):
763 return self.tk.call('wm', 'deiconify', self._w)
764 def focusmodel(self, model=None):
765 return self.tk.call('wm', 'focusmodel', self._w, model)
766 def frame(self):
767 return self.tk.call('wm', 'frame', self._w)
768 def geometry(self, newGeometry=None):
769 return self.tk.call('wm', 'geometry', self._w, newGeometry)
770 def grid(self,
771 baseWidht=None, baseHeight=None,
772 widthInc=None, heightInc=None):
773 return self._getints(self.tk.call(
774 'wm', 'grid', self._w,
775 baseWidht, baseHeight, widthInc, heightInc))
776 def group(self, pathName=None):
777 return self.tk.call('wm', 'group', self._w, pathName)
778 def iconbitmap(self, bitmap=None):
779 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
780 def iconify(self):
781 return self.tk.call('wm', 'iconify', self._w)
782 def iconmask(self, bitmap=None):
783 return self.tk.call('wm', 'iconmask', self._w, bitmap)
784 def iconname(self, newName=None):
785 return self.tk.call('wm', 'iconname', self._w, newName)
786 def iconposition(self, x=None, y=None):
787 return self._getints(self.tk.call(
788 'wm', 'iconposition', self._w, x, y))
789 def iconwindow(self, pathName=None):
790 return self.tk.call('wm', 'iconwindow', self._w, pathName)
791 def maxsize(self, width=None, height=None):
792 return self._getints(self.tk.call(
793 'wm', 'maxsize', self._w, width, height))
794 def minsize(self, width=None, height=None):
795 return self._getints(self.tk.call(
796 'wm', 'minsize', self._w, width, height))
797 def overrideredirect(self, boolean=None):
798 return self._getboolean(self.tk.call(
799 'wm', 'overrideredirect', self._w, boolean))
800 def positionfrom(self, who=None):
801 return self.tk.call('wm', 'positionfrom', self._w, who)
802 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000804 command = self._register(func)
805 else:
806 command = func
807 return self.tk.call(
808 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000809 def resizable(self, width=None, height=None):
810 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000811 def sizefrom(self, who=None):
812 return self.tk.call('wm', 'sizefrom', self._w, who)
813 def state(self):
814 return self.tk.call('wm', 'state', self._w)
815 def title(self, string=None):
816 return self.tk.call('wm', 'title', self._w, string)
817 def transient(self, master=None):
818 return self.tk.call('wm', 'transient', self._w, master)
819 def withdraw(self):
820 return self.tk.call('wm', 'withdraw', self._w)
821
822class Tk(Misc, Wm):
823 _w = '.'
824 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000825 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000826 self.master = None
827 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000828 if baseName is None:
829 import sys, os
830 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000831 baseName, ext = os.path.splitext(baseName)
832 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000833 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000834 try:
835 # Disable event scanning except for Command-Period
836 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000837 try:
838 MacOS.SchedParams(1, 0)
839 except AttributeError:
840 # pre-1.5, use old routine
841 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000842 except ImportError:
843 pass
844 else:
845 # Work around nasty MacTk bug
846 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000847 # Version sanity checks
848 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000849 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000850 raise RuntimeError, \
851 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000852 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000853 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000854 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000855 raise RuntimeError, \
856 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000857 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000858 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000859 raise RuntimeError, \
860 "Tk 4.0 or higher is required; found Tk %s" \
861 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000862 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000863 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000864 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000865 if not _default_root:
866 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000867 def destroy(self):
868 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000869 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000870 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000871 global _default_root
872 if _default_root is self:
873 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000874 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000875 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000876 if os.environ.has_key('HOME'): home = os.environ['HOME']
877 else: home = os.curdir
878 class_tcl = os.path.join(home, '.%s.tcl' % className)
879 class_py = os.path.join(home, '.%s.py' % className)
880 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
881 base_py = os.path.join(home, '.%s.py' % baseName)
882 dir = {'self': self}
883 exec 'from Tkinter import *' in dir
884 if os.path.isfile(class_tcl):
885 print 'source', `class_tcl`
886 self.tk.call('source', class_tcl)
887 if os.path.isfile(class_py):
888 print 'execfile', `class_py`
889 execfile(class_py, dir)
890 if os.path.isfile(base_tcl):
891 print 'source', `base_tcl`
892 self.tk.call('source', base_tcl)
893 if os.path.isfile(base_py):
894 print 'execfile', `base_py`
895 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000896 def report_callback_exception(self, exc, val, tb):
897 import traceback
898 print "Exception in Tkinter callback"
899 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000900
Guido van Rossum368e06b1997-11-07 20:38:49 +0000901# Ideally, the classes Pack, Place and Grid disappear, the
902# pack/place/grid methods are defined on the Widget class, and
903# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
904# ...), with pack(), place() and grid() being short for
905# pack_configure(), place_configure() and grid_columnconfigure(), and
906# forget() being short for pack_forget(). As a practical matter, I'm
907# afraid that there is too much code out there that may be using the
908# Pack, Place or Grid class, so I leave them intact -- but only as
909# backwards compatibility features. Also note that those methods that
910# take a master as argument (e.g. pack_propagate) have been moved to
911# the Misc class (which now incorporates all methods common between
912# toplevel and interior widgets). Again, for compatibility, these are
913# copied into the Pack, Place or Grid class.
914
Guido van Rossum18468821994-06-20 07:49:28 +0000915class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000916 def pack_configure(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000917 apply(self.tk.call,
918 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000919 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000920 pack = configure = config = pack_configure
921 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000922 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000923 forget = pack_forget
924 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000925 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000926 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000927 dict = {}
928 for i in range(0, len(words), 2):
929 key = words[i][1:]
930 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000931 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000932 value = self._nametowidget(value)
933 dict[key] = value
934 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000935 info = pack_info
936 propagate = pack_propagate = Misc.pack_propagate
937 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000938
939class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000940 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000941 for k in ['in_']:
942 if kw.has_key(k):
943 kw[k[:-1]] = kw[k]
944 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000945 apply(self.tk.call,
946 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000947 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000948 place = configure = config = place_configure
949 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000950 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000951 forget = place_forget
952 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000953 words = self.tk.splitlist(
954 self.tk.call('place', 'info', self._w))
955 dict = {}
956 for i in range(0, len(words), 2):
957 key = words[i][1:]
958 value = words[i+1]
959 if value[:1] == '.':
960 value = self._nametowidget(value)
961 dict[key] = value
962 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000963 info = place_info
964 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000965
Guido van Rossum37dcab11996-05-16 16:00:19 +0000966class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000967 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000968 def grid_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000969 apply(self.tk.call,
970 ('grid', 'configure', self._w)
971 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000972 grid = configure = config = grid_configure
973 bbox = grid_bbox = Misc.grid_bbox
974 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
975 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000976 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000977 forget = grid_forget
978 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000979 words = self.tk.splitlist(
980 self.tk.call('grid', 'info', self._w))
981 dict = {}
982 for i in range(0, len(words), 2):
983 key = words[i][1:]
984 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000985 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000986 value = self._nametowidget(value)
987 dict[key] = value
988 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000989 info = grid_info
990 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000991 return self._getints(
992 self.tk.call(
993 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +0000994 location = grid_location
995 propagate = grid_propagate = Misc.grid_propagate
996 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
997 size = grid_size = Misc.grid_size
998 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +0000999
Guido van Rossum368e06b1997-11-07 20:38:49 +00001000class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001001 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +00001002 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +00001003 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +00001004 if not _default_root:
1005 _default_root = Tk()
1006 master = _default_root
1007 if not _default_root:
1008 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +00001009 self.master = master
1010 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001011 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001012 if cnf.has_key('name'):
1013 name = cnf['name']
1014 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001015 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001016 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001017 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001018 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001019 self._w = '.' + name
1020 else:
1021 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001022 self.children = {}
1023 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001024 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001025 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001026 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1027 if kw:
1028 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001029 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001030 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001031 classes = []
1032 for k in cnf.keys():
1033 if type(k) is ClassType:
1034 classes.append((k, cnf[k]))
1035 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001036 apply(self.tk.call,
1037 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001038 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001039 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001040 def destroy(self):
1041 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001042 if self.master.children.has_key(self._name):
1043 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001044 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001045 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001046 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001047 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001048
Guido van Rossum368e06b1997-11-07 20:38:49 +00001049class Widget(BaseWidget, Pack, Place, Grid):
1050 pass
1051
1052class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001053 def __init__(self, master=None, cnf={}, **kw):
1054 if kw:
1055 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001056 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001057 for wmkey in ['screen', 'class_', 'class', 'visual',
1058 'colormap']:
1059 if cnf.has_key(wmkey):
1060 val = cnf[wmkey]
1061 # TBD: a hack needed because some keys
1062 # are not valid as keyword arguments
1063 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1064 else: opt = '-'+wmkey
1065 extra = extra + (opt, val)
1066 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001067 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001068 root = self._root()
1069 self.iconname(root.iconname())
1070 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001071
1072class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001073 def __init__(self, master=None, cnf={}, **kw):
1074 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001075 def tkButtonEnter(self, *dummy):
1076 self.tk.call('tkButtonEnter', self._w)
1077 def tkButtonLeave(self, *dummy):
1078 self.tk.call('tkButtonLeave', self._w)
1079 def tkButtonDown(self, *dummy):
1080 self.tk.call('tkButtonDown', self._w)
1081 def tkButtonUp(self, *dummy):
1082 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001083 def tkButtonInvoke(self, *dummy):
1084 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001085 def flash(self):
1086 self.tk.call(self._w, 'flash')
1087 def invoke(self):
1088 self.tk.call(self._w, 'invoke')
1089
1090# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001091# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001092def AtEnd():
1093 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001094def AtInsert(*args):
1095 s = 'insert'
1096 for a in args:
1097 if a: s = s + (' ' + a)
1098 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001099def AtSelFirst():
1100 return 'sel.first'
1101def AtSelLast():
1102 return 'sel.last'
1103def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001104 if y is None:
1105 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001106 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001107 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001108
1109class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001110 def __init__(self, master=None, cnf={}, **kw):
1111 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001112 def addtag(self, *args):
1113 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001114 def addtag_above(self, newtag, tagOrId):
1115 self.addtag(newtag, 'above', tagOrId)
1116 def addtag_all(self, newtag):
1117 self.addtag(newtag, 'all')
1118 def addtag_below(self, newtag, tagOrId):
1119 self.addtag(newtag, 'below', tagOrId)
1120 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1121 self.addtag(newtag, 'closest', x, y, halo, start)
1122 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1123 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1124 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1125 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1126 def addtag_withtag(self, newtag, tagOrId):
1127 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001128 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001129 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001130 def tag_unbind(self, tagOrId, sequence):
1131 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001132 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001133 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001134 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001135 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001136 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001137 self._w, 'canvasx', screenx, gridspacing))
1138 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001139 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001140 self._w, 'canvasy', screeny, gridspacing))
1141 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001142 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001143 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001144 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001145 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001146 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001147 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001148 args = args[:-1]
1149 else:
1150 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001151 return self.tk.getint(apply(
1152 self.tk.call,
1153 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001154 + args + self._options(cnf, kw)))
1155 def create_arc(self, *args, **kw):
1156 return self._create('arc', args, kw)
1157 def create_bitmap(self, *args, **kw):
1158 return self._create('bitmap', args, kw)
1159 def create_image(self, *args, **kw):
1160 return self._create('image', args, kw)
1161 def create_line(self, *args, **kw):
1162 return self._create('line', args, kw)
1163 def create_oval(self, *args, **kw):
1164 return self._create('oval', args, kw)
1165 def create_polygon(self, *args, **kw):
1166 return self._create('polygon', args, kw)
1167 def create_rectangle(self, *args, **kw):
1168 return self._create('rectangle', args, kw)
1169 def create_text(self, *args, **kw):
1170 return self._create('text', args, kw)
1171 def create_window(self, *args, **kw):
1172 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001173 def dchars(self, *args):
1174 self._do('dchars', args)
1175 def delete(self, *args):
1176 self._do('delete', args)
1177 def dtag(self, *args):
1178 self._do('dtag', args)
1179 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001180 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001181 def find_above(self, tagOrId):
1182 return self.find('above', tagOrId)
1183 def find_all(self):
1184 return self.find('all')
1185 def find_below(self, tagOrId):
1186 return self.find('below', tagOrId)
1187 def find_closest(self, x, y, halo=None, start=None):
1188 return self.find('closest', x, y, halo, start)
1189 def find_enclosed(self, x1, y1, x2, y2):
1190 return self.find('enclosed', x1, y1, x2, y2)
1191 def find_overlapping(self, x1, y1, x2, y2):
1192 return self.find('overlapping', x1, y1, x2, y2)
1193 def find_withtag(self, tagOrId):
1194 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001195 def focus(self, *args):
1196 return self._do('focus', args)
1197 def gettags(self, *args):
1198 return self.tk.splitlist(self._do('gettags', args))
1199 def icursor(self, *args):
1200 self._do('icursor', args)
1201 def index(self, *args):
1202 return self.tk.getint(self._do('index', args))
1203 def insert(self, *args):
1204 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001205 def itemcget(self, tagOrId, option):
1206 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001207 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001208 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001209 cnf = {}
1210 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001211 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001212 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1213 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001214 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001215 x = self.tk.split(self._do('itemconfigure',
1216 (tagOrId, '-'+cnf,)))
1217 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001218 self._do('itemconfigure', (tagOrId,)
1219 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001220 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001221 def lower(self, *args):
1222 self._do('lower', args)
1223 def move(self, *args):
1224 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001225 def postscript(self, cnf={}, **kw):
1226 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001227 def tkraise(self, *args):
1228 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001229 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001230 def scale(self, *args):
1231 self._do('scale', args)
1232 def scan_mark(self, x, y):
1233 self.tk.call(self._w, 'scan', 'mark', x, y)
1234 def scan_dragto(self, x, y):
1235 self.tk.call(self._w, 'scan', 'dragto', x, y)
1236 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001237 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001238 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001239 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001240 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001241 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001242 def select_item(self):
1243 self.tk.call(self._w, 'select', 'item')
1244 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001245 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001246 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001247 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001248 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001249 if not args:
1250 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001251 apply(self.tk.call, (self._w, 'xview')+args)
1252 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001253 if not args:
1254 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001255 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001256
1257class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001258 def __init__(self, master=None, cnf={}, **kw):
1259 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001260 def deselect(self):
1261 self.tk.call(self._w, 'deselect')
1262 def flash(self):
1263 self.tk.call(self._w, 'flash')
1264 def invoke(self):
1265 self.tk.call(self._w, 'invoke')
1266 def select(self):
1267 self.tk.call(self._w, 'select')
1268 def toggle(self):
1269 self.tk.call(self._w, 'toggle')
1270
1271class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001272 def __init__(self, master=None, cnf={}, **kw):
1273 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001274 def delete(self, first, last=None):
1275 self.tk.call(self._w, 'delete', first, last)
1276 def get(self):
1277 return self.tk.call(self._w, 'get')
1278 def icursor(self, index):
1279 self.tk.call(self._w, 'icursor', index)
1280 def index(self, index):
1281 return self.tk.getint(self.tk.call(
1282 self._w, 'index', index))
1283 def insert(self, index, string):
1284 self.tk.call(self._w, 'insert', index, string)
1285 def scan_mark(self, x):
1286 self.tk.call(self._w, 'scan', 'mark', x)
1287 def scan_dragto(self, x):
1288 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001289 def selection_adjust(self, index):
1290 self.tk.call(self._w, 'selection', 'adjust', index)
1291 select_adjust = selection_adjust
1292 def selection_clear(self):
1293 self.tk.call(self._w, 'selection', 'clear')
1294 select_clear = selection_clear
1295 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001296 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001297 select_from = selection_from
1298 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001299 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001300 self.tk.call(self._w, 'selection', 'present'))
1301 select_present = selection_present
1302 def selection_range(self, start, end):
1303 self.tk.call(self._w, 'selection', 'range', start, end)
1304 select_range = selection_range
1305 def selection_to(self, index):
1306 self.tk.call(self._w, 'selection', 'to', index)
1307 select_to = selection_to
1308 def xview(self, index):
1309 self.tk.call(self._w, 'xview', index)
1310 def xview_moveto(self, fraction):
1311 self.tk.call(self._w, 'xview', 'moveto', fraction)
1312 def xview_scroll(self, number, what):
1313 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001314
1315class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001316 def __init__(self, master=None, cnf={}, **kw):
1317 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001318 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001319 if cnf.has_key('class_'):
1320 extra = ('-class', cnf['class_'])
1321 del cnf['class_']
1322 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001323 extra = ('-class', cnf['class'])
1324 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001325 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001326
1327class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001328 def __init__(self, master=None, cnf={}, **kw):
1329 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001330
Guido van Rossum18468821994-06-20 07:49:28 +00001331class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001332 def __init__(self, master=None, cnf={}, **kw):
1333 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001334 def activate(self, index):
1335 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001336 def bbox(self, *args):
1337 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001338 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001339 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001340 return self.tk.splitlist(self.tk.call(
1341 self._w, 'curselection'))
1342 def delete(self, first, last=None):
1343 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001344 def get(self, first, last=None):
1345 if last:
1346 return self.tk.splitlist(self.tk.call(
1347 self._w, 'get', first, last))
1348 else:
1349 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001350 def insert(self, index, *elements):
1351 apply(self.tk.call,
1352 (self._w, 'insert', index) + elements)
1353 def nearest(self, y):
1354 return self.tk.getint(self.tk.call(
1355 self._w, 'nearest', y))
1356 def scan_mark(self, x, y):
1357 self.tk.call(self._w, 'scan', 'mark', x, y)
1358 def scan_dragto(self, x, y):
1359 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001360 def see(self, index):
1361 self.tk.call(self._w, 'see', index)
1362 def index(self, index):
1363 i = self.tk.call(self._w, 'index', index)
1364 if i == 'none': return None
1365 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001366 def select_anchor(self, index):
1367 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001368 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001369 def select_clear(self, first, last=None):
1370 self.tk.call(self._w,
1371 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001372 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001373 def select_includes(self, index):
1374 return self.tk.getboolean(self.tk.call(
1375 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001376 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001377 def select_set(self, first, last=None):
1378 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001379 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001380 def size(self):
1381 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001382 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001383 if not what:
1384 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001385 apply(self.tk.call, (self._w, 'xview')+what)
1386 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001387 if not what:
1388 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001389 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001390
1391class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001392 def __init__(self, master=None, cnf={}, **kw):
1393 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001394 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001395 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001396 def tk_mbPost(self):
1397 self.tk.call('tk_mbPost', self._w)
1398 def tk_mbUnpost(self):
1399 self.tk.call('tk_mbUnpost')
1400 def tk_traverseToMenu(self, char):
1401 self.tk.call('tk_traverseToMenu', self._w, char)
1402 def tk_traverseWithinMenu(self, char):
1403 self.tk.call('tk_traverseWithinMenu', self._w, char)
1404 def tk_getMenuButtons(self):
1405 return self.tk.call('tk_getMenuButtons', self._w)
1406 def tk_nextMenu(self, count):
1407 self.tk.call('tk_nextMenu', count)
1408 def tk_nextMenuEntry(self, count):
1409 self.tk.call('tk_nextMenuEntry', count)
1410 def tk_invokeMenu(self):
1411 self.tk.call('tk_invokeMenu', self._w)
1412 def tk_firstMenu(self):
1413 self.tk.call('tk_firstMenu', self._w)
1414 def tk_mbButtonDown(self):
1415 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001416 def tk_popup(self, x, y, entry=""):
1417 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001418 def activate(self, index):
1419 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001420 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001421 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001422 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001423 def add_cascade(self, cnf={}, **kw):
1424 self.add('cascade', cnf or kw)
1425 def add_checkbutton(self, cnf={}, **kw):
1426 self.add('checkbutton', cnf or kw)
1427 def add_command(self, cnf={}, **kw):
1428 self.add('command', cnf or kw)
1429 def add_radiobutton(self, cnf={}, **kw):
1430 self.add('radiobutton', cnf or kw)
1431 def add_separator(self, cnf={}, **kw):
1432 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001433 def insert(self, index, itemType, cnf={}, **kw):
1434 apply(self.tk.call, (self._w, 'insert', index, itemType)
1435 + self._options(cnf, kw))
1436 def insert_cascade(self, index, cnf={}, **kw):
1437 self.insert(index, 'cascade', cnf or kw)
1438 def insert_checkbutton(self, index, cnf={}, **kw):
1439 self.insert(index, 'checkbutton', cnf or kw)
1440 def insert_command(self, index, cnf={}, **kw):
1441 self.insert(index, 'command', cnf or kw)
1442 def insert_radiobutton(self, index, cnf={}, **kw):
1443 self.insert(index, 'radiobutton', cnf or kw)
1444 def insert_separator(self, index, cnf={}, **kw):
1445 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001446 def delete(self, index1, index2=None):
1447 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001448 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001449 if cnf is None and not kw:
1450 cnf = {}
1451 for x in self.tk.split(apply(self.tk.call,
1452 (self._w, 'entryconfigure', index))):
1453 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1454 return cnf
1455 if type(cnf) == StringType and not kw:
1456 x = self.tk.split(apply(self.tk.call,
1457 (self._w, 'entryconfigure', index, '-'+cnf)))
1458 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001459 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001460 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001461 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001462 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001463 i = self.tk.call(self._w, 'index', index)
1464 if i == 'none': return None
1465 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001466 def invoke(self, index):
1467 return self.tk.call(self._w, 'invoke', index)
1468 def post(self, x, y):
1469 self.tk.call(self._w, 'post', x, y)
1470 def unpost(self):
1471 self.tk.call(self._w, 'unpost')
1472 def yposition(self, index):
1473 return self.tk.getint(self.tk.call(
1474 self._w, 'yposition', index))
1475
1476class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001477 def __init__(self, master=None, cnf={}, **kw):
1478 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001479
1480class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001481 def __init__(self, master=None, cnf={}, **kw):
1482 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001483
1484class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001485 def __init__(self, master=None, cnf={}, **kw):
1486 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001487 def deselect(self):
1488 self.tk.call(self._w, 'deselect')
1489 def flash(self):
1490 self.tk.call(self._w, 'flash')
1491 def invoke(self):
1492 self.tk.call(self._w, 'invoke')
1493 def select(self):
1494 self.tk.call(self._w, 'select')
1495
1496class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001497 def __init__(self, master=None, cnf={}, **kw):
1498 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001499 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001500 value = self.tk.call(self._w, 'get')
1501 try:
1502 return self.tk.getint(value)
1503 except TclError:
1504 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001505 def set(self, value):
1506 self.tk.call(self._w, 'set', value)
1507
1508class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001509 def __init__(self, master=None, cnf={}, **kw):
1510 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001511 def activate(self, index):
1512 self.tk.call(self._w, 'activate', index)
1513 def delta(self, deltax, deltay):
1514 return self.getdouble(self.tk.call(
1515 self._w, 'delta', deltax, deltay))
1516 def fraction(self, x, y):
1517 return self.getdouble(self.tk.call(
1518 self._w, 'fraction', x, y))
1519 def identify(self, x, y):
1520 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001521 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001522 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001523 def set(self, *args):
1524 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001525
1526class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001527 def __init__(self, master=None, cnf={}, **kw):
1528 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001529 def bbox(self, *args):
1530 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001531 def tk_textSelectTo(self, index):
1532 self.tk.call('tk_textSelectTo', self._w, index)
1533 def tk_textBackspace(self):
1534 self.tk.call('tk_textBackspace', self._w)
1535 def tk_textIndexCloser(self, a, b, c):
1536 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1537 def tk_textResetAnchor(self, index):
1538 self.tk.call('tk_textResetAnchor', self._w, index)
1539 def compare(self, index1, op, index2):
1540 return self.tk.getboolean(self.tk.call(
1541 self._w, 'compare', index1, op, index2))
1542 def debug(self, boolean=None):
1543 return self.tk.getboolean(self.tk.call(
1544 self._w, 'debug', boolean))
1545 def delete(self, index1, index2=None):
1546 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001547 def dlineinfo(self, index):
1548 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001549 def get(self, index1, index2=None):
1550 return self.tk.call(self._w, 'get', index1, index2)
1551 def index(self, index):
1552 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001553 def insert(self, index, chars, *args):
1554 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001555 def mark_gravity(self, markName, direction=None):
1556 return apply(self.tk.call,
1557 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001558 def mark_names(self):
1559 return self.tk.splitlist(self.tk.call(
1560 self._w, 'mark', 'names'))
1561 def mark_set(self, markName, index):
1562 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001563 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001564 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001565 def scan_mark(self, x, y):
1566 self.tk.call(self._w, 'scan', 'mark', x, y)
1567 def scan_dragto(self, x, y):
1568 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001569 def search(self, pattern, index, stopindex=None,
1570 forwards=None, backwards=None, exact=None,
1571 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001572 args = [self._w, 'search']
1573 if forwards: args.append('-forwards')
1574 if backwards: args.append('-backwards')
1575 if exact: args.append('-exact')
1576 if regexp: args.append('-regexp')
1577 if nocase: args.append('-nocase')
1578 if count: args.append('-count'); args.append(count)
1579 if pattern[0] == '-': args.append('--')
1580 args.append(pattern)
1581 args.append(index)
1582 if stopindex: args.append(stopindex)
1583 return apply(self.tk.call, tuple(args))
1584 def see(self, index):
1585 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001586 def tag_add(self, tagName, index1, index2=None):
1587 self.tk.call(
1588 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001589 def tag_unbind(self, tagName, sequence):
1590 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001591 def tag_bind(self, tagName, sequence, func, add=None):
1592 return self._bind((self._w, 'tag', 'bind', tagName),
1593 sequence, func, add)
1594 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001595 if option[:1] != '-':
1596 option = '-' + option
1597 if option[-1:] == '_':
1598 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001599 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001600 def tag_configure(self, tagName, 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, 'tag', 'configure', tagName, '-'+cnf))
1604 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001605 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001606 (self._w, 'tag', 'configure', tagName)
1607 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001608 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001609 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001610 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001611 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001612 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001613 def tag_names(self, index=None):
1614 return self.tk.splitlist(
1615 self.tk.call(self._w, 'tag', 'names', index))
1616 def tag_nextrange(self, tagName, index1, index2=None):
1617 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001618 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001619 def tag_raise(self, tagName, aboveThis=None):
1620 self.tk.call(
1621 self._w, 'tag', 'raise', tagName, aboveThis)
1622 def tag_ranges(self, tagName):
1623 return self.tk.splitlist(self.tk.call(
1624 self._w, 'tag', 'ranges', tagName))
1625 def tag_remove(self, tagName, index1, index2=None):
1626 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001627 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001628 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001629 if option[:1] != '-':
1630 option = '-' + option
1631 if option[-1:] == '_':
1632 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001633 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001634 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001635 if type(cnf) == StringType:
1636 x = self.tk.split(self.tk.call(
1637 self._w, 'window', 'configure',
1638 index, '-'+cnf))
1639 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001640 apply(self.tk.call,
1641 (self._w, 'window', 'configure', index)
1642 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001643 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001644 def window_create(self, index, cnf={}, **kw):
1645 apply(self.tk.call,
1646 (self._w, 'window', 'create', index)
1647 + self._options(cnf, kw))
1648 def window_names(self):
1649 return self.tk.splitlist(
1650 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001651 def xview(self, *what):
1652 if not what:
1653 return self._getdoubles(self.tk.call(self._w, 'xview'))
1654 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001655 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001656 if not what:
1657 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001658 apply(self.tk.call, (self._w, 'yview')+what)
1659 def yview_pickplace(self, *what):
1660 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001661
Guido van Rossum28574b51996-10-21 15:16:51 +00001662class _setit:
1663 def __init__(self, var, value):
1664 self.__value = value
1665 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001666 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001667 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001668
1669class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001670 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001671 kw = {"borderwidth": 2, "textvariable": variable,
1672 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1673 "highlightthickness": 2}
1674 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001675 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001676 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1677 self.menuname = menu._w
1678 menu.add_command(label=value, command=_setit(variable, value))
1679 for v in values:
1680 menu.add_command(label=v, command=_setit(variable, v))
1681 self["menu"] = menu
1682
1683 def __getitem__(self, name):
1684 if name == 'menu':
1685 return self.__menu
1686 return Widget.__getitem__(self, name)
1687
1688 def destroy(self):
1689 Menubutton.destroy(self)
1690 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001691
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001692class Image:
1693 def __init__(self, imgtype, name=None, cnf={}, **kw):
1694 self.name = None
1695 master = _default_root
1696 if not master: raise RuntimeError, 'Too early to create image'
1697 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001698 if not name:
1699 name = `id(self)`
1700 # The following is needed for systems where id(x)
1701 # can return a negative number, such as Linux/m68k:
1702 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001703 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1704 elif kw: cnf = kw
1705 options = ()
1706 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001707 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001708 v = self._register(v)
1709 options = options + ('-'+k, v)
1710 apply(self.tk.call,
1711 ('image', 'create', imgtype, name,) + options)
1712 self.name = name
1713 def __str__(self): return self.name
1714 def __del__(self):
1715 if self.name:
1716 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001717 def __setitem__(self, key, value):
1718 self.tk.call(self.name, 'configure', '-'+key, value)
1719 def __getitem__(self, key):
1720 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001721 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001722 res = ()
1723 for k, v in _cnfmerge(kw).items():
1724 if v is not None:
1725 if k[-1] == '_': k = k[:-1]
1726 if callable(v):
1727 v = self._register(v)
1728 res = res + ('-'+k, v)
1729 apply(self.tk.call, (self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001730 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001731 def height(self):
1732 return self.tk.getint(
1733 self.tk.call('image', 'height', self.name))
1734 def type(self):
1735 return self.tk.call('image', 'type', self.name)
1736 def width(self):
1737 return self.tk.getint(
1738 self.tk.call('image', 'width', self.name))
1739
1740class PhotoImage(Image):
1741 def __init__(self, name=None, cnf={}, **kw):
1742 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001743 def blank(self):
1744 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001745 def cget(self, option):
1746 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001747 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001748 def __getitem__(self, key):
1749 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001750 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001751 def copy(self):
1752 destImage = PhotoImage()
1753 self.tk.call(destImage, 'copy', self.name)
1754 return destImage
1755 def zoom(self,x,y=''):
1756 destImage = PhotoImage()
1757 if y=='': y=x
1758 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1759 return destImage
1760 def subsample(self,x,y=''):
1761 destImage = PhotoImage()
1762 if y=='': y=x
1763 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1764 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001765 def get(self, x, y):
1766 return self.tk.call(self.name, 'get', x, y)
1767 def put(self, data, to=None):
1768 args = (self.name, 'put', data)
1769 if to:
1770 args = args + to
1771 apply(self.tk.call, args)
1772 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001773 def write(self, filename, format=None, from_coords=None):
1774 args = (self.name, 'write', filename)
1775 if format:
1776 args = args + ('-format', format)
1777 if from_coords:
1778 args = args + ('-from',) + tuple(from_coords)
1779 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001780
1781class BitmapImage(Image):
1782 def __init__(self, name=None, cnf={}, **kw):
1783 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1784
1785def image_names(): return _default_root.tk.call('image', 'names')
1786def image_types(): return _default_root.tk.call('image', 'types')
1787
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001788######################################################################
1789# Extensions:
1790
1791class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001792 def __init__(self, master=None, cnf={}, **kw):
1793 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001794 self.bind('<Any-Enter>', self.tkButtonEnter)
1795 self.bind('<Any-Leave>', self.tkButtonLeave)
1796 self.bind('<1>', self.tkButtonDown)
1797 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001798
1799class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001800 def __init__(self, master=None, cnf={}, **kw):
1801 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001802 self.bind('<Any-Enter>', self.tkButtonEnter)
1803 self.bind('<Any-Leave>', self.tkButtonLeave)
1804 self.bind('<1>', self.tkButtonDown)
1805 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1806 self['fg'] = self['bg']
1807 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001808
Guido van Rossumc417ef81996-08-21 23:38:59 +00001809######################################################################
1810# Test:
1811
1812def _test():
1813 root = Tk()
1814 label = Label(root, text="Proof-of-existence test for Tk")
1815 label.pack()
1816 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001817 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001818 text="[%s]" % root.test['text']))
1819 test.pack()
1820 root.test = test
1821 quit = Button(root, text="QUIT", command=root.destroy)
1822 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001823 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001824 root.mainloop()
1825
1826if __name__ == '__main__':
1827 _test()
1828
Guido van Rossum37dcab11996-05-16 16:00:19 +00001829
1830# Emacs cruft
1831# Local Variables:
1832# py-indent-offset: 8
1833# End: