blob: bc7dae753fb00231fc1c891463942eb6d9dba2b6 [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 Rossumc4570481998-03-20 20:45:49 +000054_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +000055_default_root = None
56
Guido van Rossumc4570481998-03-20 20:45:49 +000057def NoDefaultRoot():
58 global _support_default_root
59 _support_default_root = 0
60 del _default_root
61
Guido van Rossum45853db1994-06-20 12:19:19 +000062def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000063 pass
64
Guido van Rossum97aeca11994-07-07 13:12:12 +000065def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000066 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000067
Guido van Rossumaec5dc91994-06-27 07:55:12 +000068_varnum = 0
69class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000070 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000071 def __init__(self, master=None):
Guido van Rossumaec5dc91994-06-27 07:55:12 +000072 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +000073 if not master:
74 master = _default_root
75 self._master = master
76 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +000077 self._name = 'PY_VAR' + `_varnum`
78 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000079 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000080 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000081 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000082 def __str__(self):
83 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000084 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000085 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +000086 def trace_variable(self, mode, callback):
87 cbname = self._master._register(callback)
88 self._tk.call("trace", "variable", self._name, mode, cbname)
89 return cbname
90 trace = trace_variable
91 def trace_vdelete(self, mode, cbname):
92 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Guido van Rossum0001a111998-02-19 21:20:30 +000093 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +000094 def trace_vinfo(self):
95 return map(self._tk.split, self._tk.splitlist(
96 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000097
98class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000099 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000100 def __init__(self, master=None):
101 Variable.__init__(self, master)
102 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000103 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000104
105class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000106 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000107 def __init__(self, master=None):
108 Variable.__init__(self, master)
109 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000110 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000111
112class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000113 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000114 def __init__(self, master=None):
115 Variable.__init__(self, master)
116 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000117 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000118
119class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000120 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000121 def __init__(self, master=None):
122 Variable.__init__(self, master)
123 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000124 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000125
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000126def mainloop(n=0):
127 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000128
129def getint(s):
130 return _default_root.tk.getint(s)
131
132def getdouble(s):
133 return _default_root.tk.getdouble(s)
134
135def getboolean(s):
136 return _default_root.tk.getboolean(s)
137
Guido van Rossum368e06b1997-11-07 20:38:49 +0000138# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000139class Misc:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000140 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000141 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000142 def destroy(self):
143 if self._tclCommands is not None:
144 for name in self._tclCommands:
145 #print '- Tkinter: deleted command', name
146 self.tk.deletecommand(name)
147 self._tclCommands = None
148 def deletecommand(self, name):
149 #print '- Tkinter: deleted command', name
150 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000151 try:
152 self._tclCommands.remove(name)
153 except ValueError:
154 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000155 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000156 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000157 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000158 def tk_bisque(self):
159 self.tk.call('tk_bisque')
160 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000161 apply(self.tk.call, ('tk_setPalette',)
162 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000163 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000164 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000165 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000166 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000167 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000168 def wait_window(self, window=None):
169 if window == None:
170 window = self
171 self.tk.call('tkwait', 'window', window._w)
172 def wait_visibility(self, window=None):
173 if window == None:
174 window = self
175 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000176 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000177 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000178 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000179 return self.tk.getvar(name)
180 def getint(self, s):
181 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000182 def getdouble(self, s):
183 return self.tk.getdouble(s)
184 def getboolean(self, s):
185 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000186 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000187 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000188 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000189 def focus_force(self):
190 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000191 def focus_get(self):
192 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000193 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000194 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000195 def focus_displayof(self):
196 name = self.tk.call('focus', '-displayof', self._w)
197 if name == 'none' or not name: return None
198 return self._nametowidget(name)
199 def focus_lastfor(self):
200 name = self.tk.call('focus', '-lastfor', self._w)
201 if name == 'none' or not name: return None
202 return self._nametowidget(name)
203 def tk_focusFollowsMouse(self):
204 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000205 def tk_focusNext(self):
206 name = self.tk.call('tk_focusNext', self._w)
207 if not name: return None
208 return self._nametowidget(name)
209 def tk_focusPrev(self):
210 name = self.tk.call('tk_focusPrev', self._w)
211 if not name: return None
212 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000213 def after(self, ms, func=None, *args):
214 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000215 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000216 self.tk.call('after', ms)
217 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000218 # XXX Disgusting hack to clean up after calling func
219 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000220 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000221 try:
222 apply(func, args)
223 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000224 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000225 name = self._register(callit)
226 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000227 return self.tk.call('after', ms, name)
228 def after_idle(self, func, *args):
229 return apply(self.after, ('idle', func) + args)
230 def after_cancel(self, id):
231 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000232 def bell(self, displayof=0):
233 apply(self.tk.call, ('bell',) + self._displayof(displayof))
234 # Clipboard handling:
235 def clipboard_clear(self, **kw):
236 if not kw.has_key('displayof'): kw['displayof'] = self._w
237 apply(self.tk.call,
238 ('clipboard', 'clear') + self._options(kw))
239 def clipboard_append(self, string, **kw):
240 if not kw.has_key('displayof'): kw['displayof'] = self._w
241 apply(self.tk.call,
242 ('clipboard', 'append') + self._options(kw)
243 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000244 # XXX grab current w/o window argument
245 def grab_current(self):
246 name = self.tk.call('grab', 'current', self._w)
247 if not name: return None
248 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000249 def grab_release(self):
250 self.tk.call('grab', 'release', self._w)
251 def grab_set(self):
252 self.tk.call('grab', 'set', self._w)
253 def grab_set_global(self):
254 self.tk.call('grab', 'set', '-global', self._w)
255 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000256 status = self.tk.call('grab', 'status', self._w)
257 if status == 'none': status = None
258 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000259 def lower(self, belowThis=None):
260 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000261 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000262 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000263 def option_clear(self):
264 self.tk.call('option', 'clear')
265 def option_get(self, name, className):
266 return self.tk.call('option', 'get', self._w, name, className)
267 def option_readfile(self, fileName, priority = None):
268 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000269 def selection_clear(self, **kw):
270 if not kw.has_key('displayof'): kw['displayof'] = self._w
271 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
272 def selection_get(self, **kw):
273 if not kw.has_key('displayof'): kw['displayof'] = self._w
274 return apply(self.tk.call,
275 ('selection', 'get') + self._options(kw))
276 def selection_handle(self, command, **kw):
277 name = self._register(command)
278 apply(self.tk.call,
279 ('selection', 'handle') + self._options(kw)
280 + (self._w, name))
281 def selection_own(self, **kw):
282 "Become owner of X selection."
283 apply(self.tk.call,
284 ('selection', 'own') + self._options(kw) + (self._w,))
285 def selection_own_get(self, **kw):
286 "Find owner of X selection."
287 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000288 name = apply(self.tk.call,
289 ('selection', 'own') + self._options(kw))
290 if not name: return None
291 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000292 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000293 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000294 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000295 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000296 def tkraise(self, aboveThis=None):
297 self.tk.call('raise', self._w, aboveThis)
298 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000299 def colormodel(self, value=None):
300 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000301 def winfo_atom(self, name, displayof=0):
302 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
303 return self.tk.getint(apply(self.tk.call, args))
304 def winfo_atomname(self, id, displayof=0):
305 args = ('winfo', 'atomname') \
306 + self._displayof(displayof) + (id,)
307 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000308 def winfo_cells(self):
309 return self.tk.getint(
310 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000311 def winfo_children(self):
312 return map(self._nametowidget,
313 self.tk.splitlist(self.tk.call(
314 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000315 def winfo_class(self):
316 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000317 def winfo_colormapfull(self):
318 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000319 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000320 def winfo_containing(self, rootX, rootY, displayof=0):
321 args = ('winfo', 'containing') \
322 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000323 name = apply(self.tk.call, args)
324 if not name: return None
325 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000326 def winfo_depth(self):
327 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
328 def winfo_exists(self):
329 return self.tk.getint(
330 self.tk.call('winfo', 'exists', self._w))
331 def winfo_fpixels(self, number):
332 return self.tk.getdouble(self.tk.call(
333 'winfo', 'fpixels', self._w, number))
334 def winfo_geometry(self):
335 return self.tk.call('winfo', 'geometry', self._w)
336 def winfo_height(self):
337 return self.tk.getint(
338 self.tk.call('winfo', 'height', self._w))
339 def winfo_id(self):
340 return self.tk.getint(
341 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000342 def winfo_interps(self, displayof=0):
343 args = ('winfo', 'interps') + self._displayof(displayof)
344 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000345 def winfo_ismapped(self):
346 return self.tk.getint(
347 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000348 def winfo_manager(self):
349 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000350 def winfo_name(self):
351 return self.tk.call('winfo', 'name', self._w)
352 def winfo_parent(self):
353 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000354 def winfo_pathname(self, id, displayof=0):
355 args = ('winfo', 'pathname') \
356 + self._displayof(displayof) + (id,)
357 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000358 def winfo_pixels(self, number):
359 return self.tk.getint(
360 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000361 def winfo_pointerx(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'pointerx', self._w))
364 def winfo_pointerxy(self):
365 return self._getints(
366 self.tk.call('winfo', 'pointerxy', self._w))
367 def winfo_pointery(self):
368 return self.tk.getint(
369 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000370 def winfo_reqheight(self):
371 return self.tk.getint(
372 self.tk.call('winfo', 'reqheight', self._w))
373 def winfo_reqwidth(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'reqwidth', self._w))
376 def winfo_rgb(self, color):
377 return self._getints(
378 self.tk.call('winfo', 'rgb', self._w, color))
379 def winfo_rootx(self):
380 return self.tk.getint(
381 self.tk.call('winfo', 'rootx', self._w))
382 def winfo_rooty(self):
383 return self.tk.getint(
384 self.tk.call('winfo', 'rooty', self._w))
385 def winfo_screen(self):
386 return self.tk.call('winfo', 'screen', self._w)
387 def winfo_screencells(self):
388 return self.tk.getint(
389 self.tk.call('winfo', 'screencells', self._w))
390 def winfo_screendepth(self):
391 return self.tk.getint(
392 self.tk.call('winfo', 'screendepth', self._w))
393 def winfo_screenheight(self):
394 return self.tk.getint(
395 self.tk.call('winfo', 'screenheight', self._w))
396 def winfo_screenmmheight(self):
397 return self.tk.getint(
398 self.tk.call('winfo', 'screenmmheight', self._w))
399 def winfo_screenmmwidth(self):
400 return self.tk.getint(
401 self.tk.call('winfo', 'screenmmwidth', self._w))
402 def winfo_screenvisual(self):
403 return self.tk.call('winfo', 'screenvisual', self._w)
404 def winfo_screenwidth(self):
405 return self.tk.getint(
406 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000407 def winfo_server(self):
408 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000409 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000410 return self._nametowidget(self.tk.call(
411 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000412 def winfo_viewable(self):
413 return self.tk.getint(
414 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000415 def winfo_visual(self):
416 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000417 def winfo_visualid(self):
418 return self.tk.call('winfo', 'visualid', self._w)
419 def winfo_visualsavailable(self, includeids=0):
420 data = self.tk.split(
421 self.tk.call('winfo', 'visualsavailable', self._w,
422 includeids and 'includeids' or None))
423 def parseitem(x, self=self):
424 return x[:1] + tuple(map(self.tk.getint, x[1:]))
425 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000426 def winfo_vrootheight(self):
427 return self.tk.getint(
428 self.tk.call('winfo', 'vrootheight', self._w))
429 def winfo_vrootwidth(self):
430 return self.tk.getint(
431 self.tk.call('winfo', 'vrootwidth', self._w))
432 def winfo_vrootx(self):
433 return self.tk.getint(
434 self.tk.call('winfo', 'vrootx', self._w))
435 def winfo_vrooty(self):
436 return self.tk.getint(
437 self.tk.call('winfo', 'vrooty', self._w))
438 def winfo_width(self):
439 return self.tk.getint(
440 self.tk.call('winfo', 'width', self._w))
441 def winfo_x(self):
442 return self.tk.getint(
443 self.tk.call('winfo', 'x', self._w))
444 def winfo_y(self):
445 return self.tk.getint(
446 self.tk.call('winfo', 'y', self._w))
447 def update(self):
448 self.tk.call('update')
449 def update_idletasks(self):
450 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000451 def bindtags(self, tagList=None):
452 if tagList is None:
453 return self.tk.splitlist(
454 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000455 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000456 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000457 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000458 if func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000459 funcid = self._register(func, self._substitute,
460 needcleanup)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000461 cmd = ("%sset _tkinter_break [%s %s]\n"
462 'if {"$_tkinter_break" == "break"} break\n') \
463 % (add and '+' or '',
Guido van Rossum117a5a81998-03-27 21:26:51 +0000464 funcid,
Guido van Rossum37dcab11996-05-16 16:00:19 +0000465 _string.join(self._subst_format))
466 apply(self.tk.call, what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000467 return funcid
Guido van Rossum37dcab11996-05-16 16:00:19 +0000468 elif func == '':
469 apply(self.tk.call, what + (sequence, func))
470 else:
471 return apply(self.tk.call, what + (sequence,))
472 def bind(self, sequence=None, func=None, add=None):
473 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000474 def unbind(self, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +0000475 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000476 if funcid:
477 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000478 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000479 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000480 def unbind_all(self, sequence):
481 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000482 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000483 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000484 def unbind_class(self, className, sequence):
485 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000486 def mainloop(self, n=0):
487 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000488 def quit(self):
489 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000490 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000491 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000492 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
493 def _getdoubles(self, string):
494 if not string: return None
495 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000496 def _getboolean(self, string):
497 if string:
498 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000499 def _displayof(self, displayof):
500 if displayof:
501 return ('-displayof', displayof)
502 if displayof is None:
503 return ('-displayof', self._w)
504 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000505 def _options(self, cnf, kw = None):
506 if kw:
507 cnf = _cnfmerge((cnf, kw))
508 else:
509 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000510 res = ()
511 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000512 if v is not None:
513 if k[-1] == '_': k = k[:-1]
514 if callable(v):
515 v = self._register(v)
516 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000517 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000518 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000519 w = self
520 if name[0] == '.':
521 w = w._root()
522 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000523 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000524 while name:
525 i = find(name, '.')
526 if i >= 0:
527 name, tail = name[:i], name[i+1:]
528 else:
529 tail = ''
530 w = w.children[name]
531 name = tail
532 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000533 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000534 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000535 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000536 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000537 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000538 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000539 except AttributeError:
540 pass
541 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000542 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000543 except AttributeError:
544 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000545 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000546 if needcleanup:
547 if self._tclCommands is None:
548 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +0000549 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000550 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000551 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000552 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000553 def _root(self):
554 w = self
555 while w.master: w = w.master
556 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000557 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000558 '%s', '%t', '%w', '%x', '%y',
559 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
560 def _substitute(self, *args):
561 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000562 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000563 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
564 # Missing: (a, c, d, m, o, v, B, R)
565 e = Event()
566 e.serial = tk.getint(nsign)
567 e.num = tk.getint(b)
568 try: e.focus = tk.getboolean(f)
569 except TclError: pass
570 e.height = tk.getint(h)
571 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000572 # For Visibility events, event state is a string and
573 # not an integer:
574 try:
575 e.state = tk.getint(s)
576 except TclError:
577 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000578 e.time = tk.getint(t)
579 e.width = tk.getint(w)
580 e.x = tk.getint(x)
581 e.y = tk.getint(y)
582 e.char = A
583 try: e.send_event = tk.getboolean(E)
584 except TclError: pass
585 e.keysym = K
586 e.keysym_num = tk.getint(N)
587 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +0000588 try:
589 e.widget = self._nametowidget(W)
590 except KeyError:
591 e.widget = W
Guido van Rossum45853db1994-06-20 12:19:19 +0000592 e.x_root = tk.getint(X)
593 e.y_root = tk.getint(Y)
594 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000595 def _report_exception(self):
596 import sys
597 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
598 root = self._root()
599 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000600 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000601 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000602 # XXX ought to generalize this so tag_config etc. can use it
603 if kw:
604 cnf = _cnfmerge((cnf, kw))
605 elif cnf:
606 cnf = _cnfmerge(cnf)
607 if cnf is None:
608 cnf = {}
609 for x in self.tk.split(
610 self.tk.call(self._w, 'configure')):
611 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
612 return cnf
613 if type(cnf) is StringType:
614 x = self.tk.split(self.tk.call(
615 self._w, 'configure', '-'+cnf))
616 return (x[0][1:],) + x[1:]
617 apply(self.tk.call, (self._w, 'configure')
618 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000619 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000620 def cget(self, key):
621 return self.tk.call(self._w, 'cget', '-' + key)
622 __getitem__ = cget
623 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000624 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000625 def keys(self):
626 return map(lambda x: x[0][1:],
627 self.tk.split(self.tk.call(self._w, 'configure')))
628 def __str__(self):
629 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000630 # Pack methods that apply to the master
631 _noarg_ = ['_noarg_']
632 def pack_propagate(self, flag=_noarg_):
633 if flag is Misc._noarg_:
634 return self._getboolean(self.tk.call(
635 'pack', 'propagate', self._w))
636 else:
637 self.tk.call('pack', 'propagate', self._w, flag)
638 propagate = pack_propagate
639 def pack_slaves(self):
640 return map(self._nametowidget,
641 self.tk.splitlist(
642 self.tk.call('pack', 'slaves', self._w)))
643 slaves = pack_slaves
644 # Place method that applies to the master
645 def place_slaves(self):
646 return map(self._nametowidget,
647 self.tk.splitlist(
648 self.tk.call(
649 'place', 'slaves', self._w)))
650 # Grid methods that apply to the master
651 def grid_bbox(self, column, row):
652 return self._getints(
653 self.tk.call(
654 'grid', 'bbox', self._w, column, row)) or None
655 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000656 def _grid_configure(self, command, index, cnf, kw):
657 if type(cnf) is StringType and not kw:
658 if cnf[-1:] == '_':
659 cnf = cnf[:-1]
660 if cnf[:1] != '-':
661 cnf = '-'+cnf
662 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000663 else:
664 options = self._options(cnf, kw)
665 if not options:
666 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000667 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000668 words = self.tk.splitlist(res)
669 dict = {}
670 for i in range(0, len(words), 2):
671 key = words[i][1:]
672 value = words[i+1]
673 if not value:
674 value = None
675 elif '.' in value:
676 value = self.tk.getdouble(value)
677 else:
678 value = self.tk.getint(value)
679 dict[key] = value
680 return dict
681 res = apply(self.tk.call,
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000682 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000683 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000684 if len(options) == 1:
685 if not res: return None
686 # In Tk 7.5, -width can be a float
687 if '.' in res: return self.tk.getdouble(res)
688 return self.tk.getint(res)
689 def grid_columnconfigure(self, index, cnf={}, **kw):
690 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000691 columnconfigure = grid_columnconfigure
692 def grid_propagate(self, flag=_noarg_):
693 if flag is Misc._noarg_:
694 return self._getboolean(self.tk.call(
695 'grid', 'propagate', self._w))
696 else:
697 self.tk.call('grid', 'propagate', self._w, flag)
698 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000699 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000700 rowconfigure = grid_rowconfigure
701 def grid_size(self):
702 return self._getints(
703 self.tk.call('grid', 'size', self._w)) or None
704 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000705 def grid_slaves(self, row=None, column=None):
706 args = ()
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000707 if row:
708 args = args + ('-row', row)
709 if column:
710 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000711 return map(self._nametowidget,
712 self.tk.splitlist(
713 apply(self.tk.call,
714 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000715
Guido van Rossum80f8be81997-12-02 19:51:39 +0000716 # Support for the "event" command, new in Tk 4.2.
717 # By Case Roole.
718
Guido van Rossum56c04b81998-04-06 03:10:03 +0000719 def event_add(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000720 args = ('event', 'add', virtual) + sequences
Guido van Rossum56c04b81998-04-06 03:10:03 +0000721 apply(self.tk.call, args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000722
Guido van Rossum56c04b81998-04-06 03:10:03 +0000723 def event_delete(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000724 args = ('event', 'delete', virtual) + sequences
Guido van Rossum56c04b81998-04-06 03:10:03 +0000725 apply(self.tk.call, args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000726
727 def event_generate(self, sequence, **kw):
728 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +0000729 for k, v in kw.items():
730 args = args + ('-%s' % k, str(v))
731 apply(self.tk.call, args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000732
Guido van Rossum56c04b81998-04-06 03:10:03 +0000733 def event_info(self, virtual=None):
734 return self.tk.splitlist(
735 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +0000736
Guido van Rossumc2966511998-04-10 19:16:10 +0000737 # Image related commands
738
739 def image_names(self):
740 return self.tk.call('image', 'names')
741
742 def image_types(self):
743 return self.tk.call('image', 'types')
744
Guido van Rossum80f8be81997-12-02 19:51:39 +0000745
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000746class CallWrapper:
747 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000748 self.func = func
749 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000750 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000751 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000752 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000753 if self.subst:
754 args = apply(self.subst, args)
755 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000756 except SystemExit, msg:
757 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000758 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000759 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000760
761class Wm:
762 def aspect(self,
763 minNumer=None, minDenom=None,
764 maxNumer=None, maxDenom=None):
765 return self._getints(
766 self.tk.call('wm', 'aspect', self._w,
767 minNumer, minDenom,
768 maxNumer, maxDenom))
769 def client(self, name=None):
770 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000771 def colormapwindows(self, *wlist):
772 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
773 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000774 def command(self, value=None):
775 return self.tk.call('wm', 'command', self._w, value)
776 def deiconify(self):
777 return self.tk.call('wm', 'deiconify', self._w)
778 def focusmodel(self, model=None):
779 return self.tk.call('wm', 'focusmodel', self._w, model)
780 def frame(self):
781 return self.tk.call('wm', 'frame', self._w)
782 def geometry(self, newGeometry=None):
783 return self.tk.call('wm', 'geometry', self._w, newGeometry)
784 def grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000785 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000786 widthInc=None, heightInc=None):
787 return self._getints(self.tk.call(
788 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000789 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossum18468821994-06-20 07:49:28 +0000790 def group(self, pathName=None):
791 return self.tk.call('wm', 'group', self._w, pathName)
792 def iconbitmap(self, bitmap=None):
793 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
794 def iconify(self):
795 return self.tk.call('wm', 'iconify', self._w)
796 def iconmask(self, bitmap=None):
797 return self.tk.call('wm', 'iconmask', self._w, bitmap)
798 def iconname(self, newName=None):
799 return self.tk.call('wm', 'iconname', self._w, newName)
800 def iconposition(self, x=None, y=None):
801 return self._getints(self.tk.call(
802 'wm', 'iconposition', self._w, x, y))
803 def iconwindow(self, pathName=None):
804 return self.tk.call('wm', 'iconwindow', self._w, pathName)
805 def maxsize(self, width=None, height=None):
806 return self._getints(self.tk.call(
807 'wm', 'maxsize', self._w, width, height))
808 def minsize(self, width=None, height=None):
809 return self._getints(self.tk.call(
810 'wm', 'minsize', self._w, width, height))
811 def overrideredirect(self, boolean=None):
812 return self._getboolean(self.tk.call(
813 'wm', 'overrideredirect', self._w, boolean))
814 def positionfrom(self, who=None):
815 return self.tk.call('wm', 'positionfrom', self._w, who)
816 def protocol(self, name=None, func=None):
Guido van Rossumc4570481998-03-20 20:45:49 +0000817 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000818 command = self._register(func)
819 else:
820 command = func
821 return self.tk.call(
822 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000823 def resizable(self, width=None, height=None):
824 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000825 def sizefrom(self, who=None):
826 return self.tk.call('wm', 'sizefrom', self._w, who)
827 def state(self):
828 return self.tk.call('wm', 'state', self._w)
829 def title(self, string=None):
830 return self.tk.call('wm', 'title', self._w, string)
831 def transient(self, master=None):
832 return self.tk.call('wm', 'transient', self._w, master)
833 def withdraw(self):
834 return self.tk.call('wm', 'withdraw', self._w)
835
836class Tk(Misc, Wm):
837 _w = '.'
838 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000839 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000840 self.master = None
841 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000842 if baseName is None:
843 import sys, os
844 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000845 baseName, ext = os.path.splitext(baseName)
846 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000847 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000848 try:
849 # Disable event scanning except for Command-Period
850 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000851 try:
852 MacOS.SchedParams(1, 0)
853 except AttributeError:
854 # pre-1.5, use old routine
855 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000856 except ImportError:
857 pass
858 else:
859 # Work around nasty MacTk bug
860 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000861 # Version sanity checks
862 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000863 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000864 raise RuntimeError, \
865 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000866 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000867 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000868 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000869 raise RuntimeError, \
870 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000871 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000872 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000873 raise RuntimeError, \
874 "Tk 4.0 or higher is required; found Tk %s" \
875 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000876 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000877 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000878 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +0000879 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000880 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000881 def destroy(self):
882 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000883 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000884 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000885 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000886 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000887 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000888 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000889 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000890 if os.environ.has_key('HOME'): home = os.environ['HOME']
891 else: home = os.curdir
892 class_tcl = os.path.join(home, '.%s.tcl' % className)
893 class_py = os.path.join(home, '.%s.py' % className)
894 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
895 base_py = os.path.join(home, '.%s.py' % baseName)
896 dir = {'self': self}
897 exec 'from Tkinter import *' in dir
898 if os.path.isfile(class_tcl):
899 print 'source', `class_tcl`
900 self.tk.call('source', class_tcl)
901 if os.path.isfile(class_py):
902 print 'execfile', `class_py`
903 execfile(class_py, dir)
904 if os.path.isfile(base_tcl):
905 print 'source', `base_tcl`
906 self.tk.call('source', base_tcl)
907 if os.path.isfile(base_py):
908 print 'execfile', `base_py`
909 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000910 def report_callback_exception(self, exc, val, tb):
911 import traceback
912 print "Exception in Tkinter callback"
913 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000914
Guido van Rossum368e06b1997-11-07 20:38:49 +0000915# Ideally, the classes Pack, Place and Grid disappear, the
916# pack/place/grid methods are defined on the Widget class, and
917# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
918# ...), with pack(), place() and grid() being short for
919# pack_configure(), place_configure() and grid_columnconfigure(), and
920# forget() being short for pack_forget(). As a practical matter, I'm
921# afraid that there is too much code out there that may be using the
922# Pack, Place or Grid class, so I leave them intact -- but only as
923# backwards compatibility features. Also note that those methods that
924# take a master as argument (e.g. pack_propagate) have been moved to
925# the Misc class (which now incorporates all methods common between
926# toplevel and interior widgets). Again, for compatibility, these are
927# copied into the Pack, Place or Grid class.
928
Guido van Rossum18468821994-06-20 07:49:28 +0000929class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000930 def pack_configure(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000931 apply(self.tk.call,
932 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000933 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000934 pack = configure = config = pack_configure
935 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000936 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000937 forget = pack_forget
938 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000939 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000940 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000941 dict = {}
942 for i in range(0, len(words), 2):
943 key = words[i][1:]
944 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000945 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000946 value = self._nametowidget(value)
947 dict[key] = value
948 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000949 info = pack_info
950 propagate = pack_propagate = Misc.pack_propagate
951 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000952
953class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000954 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000955 for k in ['in_']:
956 if kw.has_key(k):
957 kw[k[:-1]] = kw[k]
958 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000959 apply(self.tk.call,
960 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000961 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000962 place = configure = config = place_configure
963 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000964 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000965 forget = place_forget
966 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000967 words = self.tk.splitlist(
968 self.tk.call('place', 'info', self._w))
969 dict = {}
970 for i in range(0, len(words), 2):
971 key = words[i][1:]
972 value = words[i+1]
973 if value[:1] == '.':
974 value = self._nametowidget(value)
975 dict[key] = value
976 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000977 info = place_info
978 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000979
Guido van Rossum37dcab11996-05-16 16:00:19 +0000980class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000981 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000982 def grid_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000983 apply(self.tk.call,
984 ('grid', 'configure', self._w)
985 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000986 grid = configure = config = grid_configure
987 bbox = grid_bbox = Misc.grid_bbox
988 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
989 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000990 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000991 forget = grid_forget
992 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000993 words = self.tk.splitlist(
994 self.tk.call('grid', 'info', self._w))
995 dict = {}
996 for i in range(0, len(words), 2):
997 key = words[i][1:]
998 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000999 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001000 value = self._nametowidget(value)
1001 dict[key] = value
1002 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001003 info = grid_info
1004 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001005 return self._getints(
1006 self.tk.call(
1007 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001008 location = grid_location
1009 propagate = grid_propagate = Misc.grid_propagate
1010 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1011 size = grid_size = Misc.grid_size
1012 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001013
Guido van Rossum368e06b1997-11-07 20:38:49 +00001014class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001015 def _setup(self, master, cnf):
Guido van Rossumc4570481998-03-20 20:45:49 +00001016 if _support_default_root:
1017 global _default_root
1018 if not master:
1019 if not _default_root:
1020 _default_root = Tk()
1021 master = _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +00001022 if not _default_root:
Guido van Rossumc4570481998-03-20 20:45:49 +00001023 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +00001024 self.master = master
1025 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001026 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001027 if cnf.has_key('name'):
1028 name = cnf['name']
1029 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001030 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001031 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001032 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001033 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001034 self._w = '.' + name
1035 else:
1036 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001037 self.children = {}
1038 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001039 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001040 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001041 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1042 if kw:
1043 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001044 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001045 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001046 classes = []
1047 for k in cnf.keys():
1048 if type(k) is ClassType:
1049 classes.append((k, cnf[k]))
1050 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001051 apply(self.tk.call,
1052 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001053 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001054 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001055 def destroy(self):
1056 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001057 if self.master.children.has_key(self._name):
1058 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001059 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001060 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001061 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001062 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001063
Guido van Rossum368e06b1997-11-07 20:38:49 +00001064class Widget(BaseWidget, Pack, Place, Grid):
1065 pass
1066
1067class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001068 def __init__(self, master=None, cnf={}, **kw):
1069 if kw:
1070 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001071 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001072 for wmkey in ['screen', 'class_', 'class', 'visual',
1073 'colormap']:
1074 if cnf.has_key(wmkey):
1075 val = cnf[wmkey]
1076 # TBD: a hack needed because some keys
1077 # are not valid as keyword arguments
1078 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1079 else: opt = '-'+wmkey
1080 extra = extra + (opt, val)
1081 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001082 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001083 root = self._root()
1084 self.iconname(root.iconname())
1085 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001086
1087class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001088 def __init__(self, master=None, cnf={}, **kw):
1089 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001090 def tkButtonEnter(self, *dummy):
1091 self.tk.call('tkButtonEnter', self._w)
1092 def tkButtonLeave(self, *dummy):
1093 self.tk.call('tkButtonLeave', self._w)
1094 def tkButtonDown(self, *dummy):
1095 self.tk.call('tkButtonDown', self._w)
1096 def tkButtonUp(self, *dummy):
1097 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001098 def tkButtonInvoke(self, *dummy):
1099 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001100 def flash(self):
1101 self.tk.call(self._w, 'flash')
1102 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001103 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001104
1105# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001106# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001107def AtEnd():
1108 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001109def AtInsert(*args):
1110 s = 'insert'
1111 for a in args:
1112 if a: s = s + (' ' + a)
1113 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001114def AtSelFirst():
1115 return 'sel.first'
1116def AtSelLast():
1117 return 'sel.last'
1118def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001119 if y is None:
1120 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001121 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001122 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001123
1124class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001125 def __init__(self, master=None, cnf={}, **kw):
1126 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def addtag(self, *args):
1128 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001129 def addtag_above(self, newtag, tagOrId):
1130 self.addtag(newtag, 'above', tagOrId)
1131 def addtag_all(self, newtag):
1132 self.addtag(newtag, 'all')
1133 def addtag_below(self, newtag, tagOrId):
1134 self.addtag(newtag, 'below', tagOrId)
1135 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1136 self.addtag(newtag, 'closest', x, y, halo, start)
1137 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1138 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1139 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1140 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1141 def addtag_withtag(self, newtag, tagOrId):
1142 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001143 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001144 return self._getints(self._do('bbox', args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001145 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001146 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001147 if funcid:
1148 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001149 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001150 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001151 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001152 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001153 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001154 self._w, 'canvasx', screenx, gridspacing))
1155 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001156 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001157 self._w, 'canvasy', screeny, gridspacing))
1158 def coords(self, *args):
Guido van Rossum0001a111998-02-19 21:20:30 +00001159 # XXX Should use _flatten on args
Guido van Rossumc8b47911996-07-30 16:31:32 +00001160 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001161 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001162 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001163 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001164 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001165 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001166 args = args[:-1]
1167 else:
1168 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001169 return self.tk.getint(apply(
1170 self.tk.call,
1171 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001172 + args + self._options(cnf, kw)))
1173 def create_arc(self, *args, **kw):
1174 return self._create('arc', args, kw)
1175 def create_bitmap(self, *args, **kw):
1176 return self._create('bitmap', args, kw)
1177 def create_image(self, *args, **kw):
1178 return self._create('image', args, kw)
1179 def create_line(self, *args, **kw):
1180 return self._create('line', args, kw)
1181 def create_oval(self, *args, **kw):
1182 return self._create('oval', args, kw)
1183 def create_polygon(self, *args, **kw):
1184 return self._create('polygon', args, kw)
1185 def create_rectangle(self, *args, **kw):
1186 return self._create('rectangle', args, kw)
1187 def create_text(self, *args, **kw):
1188 return self._create('text', args, kw)
1189 def create_window(self, *args, **kw):
1190 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001191 def dchars(self, *args):
1192 self._do('dchars', args)
1193 def delete(self, *args):
1194 self._do('delete', args)
1195 def dtag(self, *args):
1196 self._do('dtag', args)
1197 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001198 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001199 def find_above(self, tagOrId):
1200 return self.find('above', tagOrId)
1201 def find_all(self):
1202 return self.find('all')
1203 def find_below(self, tagOrId):
1204 return self.find('below', tagOrId)
1205 def find_closest(self, x, y, halo=None, start=None):
1206 return self.find('closest', x, y, halo, start)
1207 def find_enclosed(self, x1, y1, x2, y2):
1208 return self.find('enclosed', x1, y1, x2, y2)
1209 def find_overlapping(self, x1, y1, x2, y2):
1210 return self.find('overlapping', x1, y1, x2, y2)
1211 def find_withtag(self, tagOrId):
1212 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001213 def focus(self, *args):
1214 return self._do('focus', args)
1215 def gettags(self, *args):
1216 return self.tk.splitlist(self._do('gettags', args))
1217 def icursor(self, *args):
1218 self._do('icursor', args)
1219 def index(self, *args):
1220 return self.tk.getint(self._do('index', args))
1221 def insert(self, *args):
1222 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001223 def itemcget(self, tagOrId, option):
1224 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001225 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001226 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001227 cnf = {}
1228 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001229 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001230 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1231 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001232 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001233 x = self.tk.split(self._do('itemconfigure',
1234 (tagOrId, '-'+cnf,)))
1235 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001236 self._do('itemconfigure', (tagOrId,)
1237 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001238 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001239 def lower(self, *args):
1240 self._do('lower', args)
1241 def move(self, *args):
1242 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001243 def postscript(self, cnf={}, **kw):
1244 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001245 def tkraise(self, *args):
1246 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001247 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001248 def scale(self, *args):
1249 self._do('scale', args)
1250 def scan_mark(self, x, y):
1251 self.tk.call(self._w, 'scan', 'mark', x, y)
1252 def scan_dragto(self, x, y):
1253 self.tk.call(self._w, 'scan', 'dragto', x, y)
1254 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001255 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001256 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001257 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001258 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001259 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001260 def select_item(self):
1261 self.tk.call(self._w, 'select', 'item')
1262 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001263 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001264 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001265 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001266 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001267 if not args:
1268 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001269 apply(self.tk.call, (self._w, 'xview')+args)
1270 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001271 if not args:
1272 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001273 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001274
1275class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001276 def __init__(self, master=None, cnf={}, **kw):
1277 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001278 def deselect(self):
1279 self.tk.call(self._w, 'deselect')
1280 def flash(self):
1281 self.tk.call(self._w, 'flash')
1282 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001283 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001284 def select(self):
1285 self.tk.call(self._w, 'select')
1286 def toggle(self):
1287 self.tk.call(self._w, 'toggle')
1288
1289class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001290 def __init__(self, master=None, cnf={}, **kw):
1291 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001292 def delete(self, first, last=None):
1293 self.tk.call(self._w, 'delete', first, last)
1294 def get(self):
1295 return self.tk.call(self._w, 'get')
1296 def icursor(self, index):
1297 self.tk.call(self._w, 'icursor', index)
1298 def index(self, index):
1299 return self.tk.getint(self.tk.call(
1300 self._w, 'index', index))
1301 def insert(self, index, string):
1302 self.tk.call(self._w, 'insert', index, string)
1303 def scan_mark(self, x):
1304 self.tk.call(self._w, 'scan', 'mark', x)
1305 def scan_dragto(self, x):
1306 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001307 def selection_adjust(self, index):
1308 self.tk.call(self._w, 'selection', 'adjust', index)
1309 select_adjust = selection_adjust
1310 def selection_clear(self):
1311 self.tk.call(self._w, 'selection', 'clear')
1312 select_clear = selection_clear
1313 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001314 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001315 select_from = selection_from
1316 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001317 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001318 self.tk.call(self._w, 'selection', 'present'))
1319 select_present = selection_present
1320 def selection_range(self, start, end):
1321 self.tk.call(self._w, 'selection', 'range', start, end)
1322 select_range = selection_range
1323 def selection_to(self, index):
1324 self.tk.call(self._w, 'selection', 'to', index)
1325 select_to = selection_to
1326 def xview(self, index):
1327 self.tk.call(self._w, 'xview', index)
1328 def xview_moveto(self, fraction):
1329 self.tk.call(self._w, 'xview', 'moveto', fraction)
1330 def xview_scroll(self, number, what):
1331 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001332
1333class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001334 def __init__(self, master=None, cnf={}, **kw):
1335 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001336 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001337 if cnf.has_key('class_'):
1338 extra = ('-class', cnf['class_'])
1339 del cnf['class_']
1340 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001341 extra = ('-class', cnf['class'])
1342 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001343 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001344
1345class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001346 def __init__(self, master=None, cnf={}, **kw):
1347 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001348
Guido van Rossum18468821994-06-20 07:49:28 +00001349class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001350 def __init__(self, master=None, cnf={}, **kw):
1351 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001352 def activate(self, index):
1353 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001354 def bbox(self, *args):
1355 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001356 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001357 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001358 return self.tk.splitlist(self.tk.call(
1359 self._w, 'curselection'))
1360 def delete(self, first, last=None):
1361 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001362 def get(self, first, last=None):
1363 if last:
1364 return self.tk.splitlist(self.tk.call(
1365 self._w, 'get', first, last))
1366 else:
1367 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001368 def insert(self, index, *elements):
1369 apply(self.tk.call,
1370 (self._w, 'insert', index) + elements)
1371 def nearest(self, y):
1372 return self.tk.getint(self.tk.call(
1373 self._w, 'nearest', y))
1374 def scan_mark(self, x, y):
1375 self.tk.call(self._w, 'scan', 'mark', x, y)
1376 def scan_dragto(self, x, y):
1377 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001378 def see(self, index):
1379 self.tk.call(self._w, 'see', index)
1380 def index(self, index):
1381 i = self.tk.call(self._w, 'index', index)
1382 if i == 'none': return None
1383 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001384 def select_anchor(self, index):
1385 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001386 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001387 def select_clear(self, first, last=None):
1388 self.tk.call(self._w,
1389 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001390 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001391 def select_includes(self, index):
1392 return self.tk.getboolean(self.tk.call(
1393 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001394 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001395 def select_set(self, first, last=None):
1396 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001397 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001398 def size(self):
1399 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001400 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001401 if not what:
1402 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001403 apply(self.tk.call, (self._w, 'xview')+what)
1404 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001405 if not what:
1406 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001407 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001408
1409class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001410 def __init__(self, master=None, cnf={}, **kw):
1411 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001412 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001413 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001414 def tk_mbPost(self):
1415 self.tk.call('tk_mbPost', self._w)
1416 def tk_mbUnpost(self):
1417 self.tk.call('tk_mbUnpost')
1418 def tk_traverseToMenu(self, char):
1419 self.tk.call('tk_traverseToMenu', self._w, char)
1420 def tk_traverseWithinMenu(self, char):
1421 self.tk.call('tk_traverseWithinMenu', self._w, char)
1422 def tk_getMenuButtons(self):
1423 return self.tk.call('tk_getMenuButtons', self._w)
1424 def tk_nextMenu(self, count):
1425 self.tk.call('tk_nextMenu', count)
1426 def tk_nextMenuEntry(self, count):
1427 self.tk.call('tk_nextMenuEntry', count)
1428 def tk_invokeMenu(self):
1429 self.tk.call('tk_invokeMenu', self._w)
1430 def tk_firstMenu(self):
1431 self.tk.call('tk_firstMenu', self._w)
1432 def tk_mbButtonDown(self):
1433 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001434 def tk_popup(self, x, y, entry=""):
1435 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001436 def activate(self, index):
1437 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001438 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001439 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001440 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001441 def add_cascade(self, cnf={}, **kw):
1442 self.add('cascade', cnf or kw)
1443 def add_checkbutton(self, cnf={}, **kw):
1444 self.add('checkbutton', cnf or kw)
1445 def add_command(self, cnf={}, **kw):
1446 self.add('command', cnf or kw)
1447 def add_radiobutton(self, cnf={}, **kw):
1448 self.add('radiobutton', cnf or kw)
1449 def add_separator(self, cnf={}, **kw):
1450 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001451 def insert(self, index, itemType, cnf={}, **kw):
1452 apply(self.tk.call, (self._w, 'insert', index, itemType)
1453 + self._options(cnf, kw))
1454 def insert_cascade(self, index, cnf={}, **kw):
1455 self.insert(index, 'cascade', cnf or kw)
1456 def insert_checkbutton(self, index, cnf={}, **kw):
1457 self.insert(index, 'checkbutton', cnf or kw)
1458 def insert_command(self, index, cnf={}, **kw):
1459 self.insert(index, 'command', cnf or kw)
1460 def insert_radiobutton(self, index, cnf={}, **kw):
1461 self.insert(index, 'radiobutton', cnf or kw)
1462 def insert_separator(self, index, cnf={}, **kw):
1463 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001464 def delete(self, index1, index2=None):
1465 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001466 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001467 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001468 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001469 if cnf is None and not kw:
1470 cnf = {}
1471 for x in self.tk.split(apply(self.tk.call,
1472 (self._w, 'entryconfigure', index))):
1473 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1474 return cnf
1475 if type(cnf) == StringType and not kw:
1476 x = self.tk.split(apply(self.tk.call,
1477 (self._w, 'entryconfigure', index, '-'+cnf)))
1478 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001479 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001480 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001481 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001482 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001483 i = self.tk.call(self._w, 'index', index)
1484 if i == 'none': return None
1485 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001486 def invoke(self, index):
1487 return self.tk.call(self._w, 'invoke', index)
1488 def post(self, x, y):
1489 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001490 def type(self, index):
1491 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001492 def unpost(self):
1493 self.tk.call(self._w, 'unpost')
1494 def yposition(self, index):
1495 return self.tk.getint(self.tk.call(
1496 self._w, 'yposition', index))
1497
1498class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001499 def __init__(self, master=None, cnf={}, **kw):
1500 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001501
1502class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001503 def __init__(self, master=None, cnf={}, **kw):
1504 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001505
1506class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001507 def __init__(self, master=None, cnf={}, **kw):
1508 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001509 def deselect(self):
1510 self.tk.call(self._w, 'deselect')
1511 def flash(self):
1512 self.tk.call(self._w, 'flash')
1513 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001514 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001515 def select(self):
1516 self.tk.call(self._w, 'select')
1517
1518class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001519 def __init__(self, master=None, cnf={}, **kw):
1520 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001521 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001522 value = self.tk.call(self._w, 'get')
1523 try:
1524 return self.tk.getint(value)
1525 except TclError:
1526 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001527 def set(self, value):
1528 self.tk.call(self._w, 'set', value)
1529
1530class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001531 def __init__(self, master=None, cnf={}, **kw):
1532 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001533 def activate(self, index):
1534 self.tk.call(self._w, 'activate', index)
1535 def delta(self, deltax, deltay):
1536 return self.getdouble(self.tk.call(
1537 self._w, 'delta', deltax, deltay))
1538 def fraction(self, x, y):
1539 return self.getdouble(self.tk.call(
1540 self._w, 'fraction', x, y))
1541 def identify(self, x, y):
1542 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001543 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001544 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001545 def set(self, *args):
1546 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001547
1548class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001549 def __init__(self, master=None, cnf={}, **kw):
1550 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001551 def bbox(self, *args):
1552 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001553 def tk_textSelectTo(self, index):
1554 self.tk.call('tk_textSelectTo', self._w, index)
1555 def tk_textBackspace(self):
1556 self.tk.call('tk_textBackspace', self._w)
1557 def tk_textIndexCloser(self, a, b, c):
1558 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1559 def tk_textResetAnchor(self, index):
1560 self.tk.call('tk_textResetAnchor', self._w, index)
1561 def compare(self, index1, op, index2):
1562 return self.tk.getboolean(self.tk.call(
1563 self._w, 'compare', index1, op, index2))
1564 def debug(self, boolean=None):
1565 return self.tk.getboolean(self.tk.call(
1566 self._w, 'debug', boolean))
1567 def delete(self, index1, index2=None):
1568 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001569 def dlineinfo(self, index):
1570 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001571 def get(self, index1, index2=None):
1572 return self.tk.call(self._w, 'get', index1, index2)
1573 def index(self, index):
1574 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001575 def insert(self, index, chars, *args):
1576 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001577 def mark_gravity(self, markName, direction=None):
1578 return apply(self.tk.call,
1579 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001580 def mark_names(self):
1581 return self.tk.splitlist(self.tk.call(
1582 self._w, 'mark', 'names'))
1583 def mark_set(self, markName, index):
1584 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001585 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001586 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001587 def scan_mark(self, x, y):
1588 self.tk.call(self._w, 'scan', 'mark', x, y)
1589 def scan_dragto(self, x, y):
1590 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001591 def search(self, pattern, index, stopindex=None,
1592 forwards=None, backwards=None, exact=None,
1593 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001594 args = [self._w, 'search']
1595 if forwards: args.append('-forwards')
1596 if backwards: args.append('-backwards')
1597 if exact: args.append('-exact')
1598 if regexp: args.append('-regexp')
1599 if nocase: args.append('-nocase')
1600 if count: args.append('-count'); args.append(count)
1601 if pattern[0] == '-': args.append('--')
1602 args.append(pattern)
1603 args.append(index)
1604 if stopindex: args.append(stopindex)
1605 return apply(self.tk.call, tuple(args))
1606 def see(self, index):
1607 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001608 def tag_add(self, tagName, index1, index2=None):
1609 self.tk.call(
1610 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossum117a5a81998-03-27 21:26:51 +00001611 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001612 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001613 if funcid:
1614 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001615 def tag_bind(self, tagName, sequence, func, add=None):
1616 return self._bind((self._w, 'tag', 'bind', tagName),
1617 sequence, func, add)
1618 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001619 if option[:1] != '-':
1620 option = '-' + option
1621 if option[-1:] == '_':
1622 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001623 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001624 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001625 if type(cnf) == StringType:
1626 x = self.tk.split(self.tk.call(
1627 self._w, 'tag', 'configure', tagName, '-'+cnf))
1628 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001629 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001630 (self._w, 'tag', 'configure', tagName)
1631 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001632 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001633 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001634 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001635 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001636 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001637 def tag_names(self, index=None):
1638 return self.tk.splitlist(
1639 self.tk.call(self._w, 'tag', 'names', index))
1640 def tag_nextrange(self, tagName, index1, index2=None):
1641 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001642 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001643 def tag_prevrange(self, tagName, index1, index2=None):
1644 return self.tk.splitlist(self.tk.call(
1645 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001646 def tag_raise(self, tagName, aboveThis=None):
1647 self.tk.call(
1648 self._w, 'tag', 'raise', tagName, aboveThis)
1649 def tag_ranges(self, tagName):
1650 return self.tk.splitlist(self.tk.call(
1651 self._w, 'tag', 'ranges', tagName))
1652 def tag_remove(self, tagName, index1, index2=None):
1653 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001654 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001655 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001656 if option[:1] != '-':
1657 option = '-' + option
1658 if option[-1:] == '_':
1659 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001660 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001661 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001662 if type(cnf) == StringType:
1663 x = self.tk.split(self.tk.call(
1664 self._w, 'window', 'configure',
1665 index, '-'+cnf))
1666 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001667 apply(self.tk.call,
1668 (self._w, 'window', 'configure', index)
1669 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001670 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001671 def window_create(self, index, cnf={}, **kw):
1672 apply(self.tk.call,
1673 (self._w, 'window', 'create', index)
1674 + self._options(cnf, kw))
1675 def window_names(self):
1676 return self.tk.splitlist(
1677 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001678 def xview(self, *what):
1679 if not what:
1680 return self._getdoubles(self.tk.call(self._w, 'xview'))
1681 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001682 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001683 if not what:
1684 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001685 apply(self.tk.call, (self._w, 'yview')+what)
1686 def yview_pickplace(self, *what):
1687 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001688
Guido van Rossum28574b51996-10-21 15:16:51 +00001689class _setit:
1690 def __init__(self, var, value):
1691 self.__value = value
1692 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001693 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001694 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001695
1696class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001697 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001698 kw = {"borderwidth": 2, "textvariable": variable,
1699 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1700 "highlightthickness": 2}
1701 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001702 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001703 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1704 self.menuname = menu._w
1705 menu.add_command(label=value, command=_setit(variable, value))
1706 for v in values:
1707 menu.add_command(label=v, command=_setit(variable, v))
1708 self["menu"] = menu
1709
1710 def __getitem__(self, name):
1711 if name == 'menu':
1712 return self.__menu
1713 return Widget.__getitem__(self, name)
1714
1715 def destroy(self):
1716 Menubutton.destroy(self)
1717 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001718
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001719class Image:
Guido van Rossumc4570481998-03-20 20:45:49 +00001720 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001721 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00001722 if not master:
1723 master = _default_root
1724 if not master:
1725 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001726 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001727 if not name:
1728 name = `id(self)`
1729 # The following is needed for systems where id(x)
1730 # can return a negative number, such as Linux/m68k:
1731 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001732 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1733 elif kw: cnf = kw
1734 options = ()
1735 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001736 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001737 v = self._register(v)
1738 options = options + ('-'+k, v)
1739 apply(self.tk.call,
1740 ('image', 'create', imgtype, name,) + options)
1741 self.name = name
1742 def __str__(self): return self.name
1743 def __del__(self):
1744 if self.name:
1745 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001746 def __setitem__(self, key, value):
1747 self.tk.call(self.name, 'configure', '-'+key, value)
1748 def __getitem__(self, key):
1749 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001750 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001751 res = ()
1752 for k, v in _cnfmerge(kw).items():
1753 if v is not None:
1754 if k[-1] == '_': k = k[:-1]
1755 if callable(v):
1756 v = self._register(v)
1757 res = res + ('-'+k, v)
1758 apply(self.tk.call, (self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001759 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001760 def height(self):
1761 return self.tk.getint(
1762 self.tk.call('image', 'height', self.name))
1763 def type(self):
1764 return self.tk.call('image', 'type', self.name)
1765 def width(self):
1766 return self.tk.getint(
1767 self.tk.call('image', 'width', self.name))
1768
1769class PhotoImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001770 def __init__(self, name=None, cnf={}, master=None, **kw):
1771 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001772 def blank(self):
1773 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001774 def cget(self, option):
1775 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001776 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001777 def __getitem__(self, key):
1778 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001779 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001780 def copy(self):
1781 destImage = PhotoImage()
1782 self.tk.call(destImage, 'copy', self.name)
1783 return destImage
1784 def zoom(self,x,y=''):
1785 destImage = PhotoImage()
1786 if y=='': y=x
1787 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1788 return destImage
1789 def subsample(self,x,y=''):
1790 destImage = PhotoImage()
1791 if y=='': y=x
1792 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1793 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001794 def get(self, x, y):
1795 return self.tk.call(self.name, 'get', x, y)
1796 def put(self, data, to=None):
1797 args = (self.name, 'put', data)
1798 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001799 if to[0] == '-to':
1800 to = to[1:]
1801 args = args + ('-to',) + tuple(to)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001802 apply(self.tk.call, args)
1803 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001804 def write(self, filename, format=None, from_coords=None):
1805 args = (self.name, 'write', filename)
1806 if format:
1807 args = args + ('-format', format)
1808 if from_coords:
1809 args = args + ('-from',) + tuple(from_coords)
1810 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001811
1812class BitmapImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001813 def __init__(self, name=None, cnf={}, master=None, **kw):
1814 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001815
1816def image_names(): return _default_root.tk.call('image', 'names')
1817def image_types(): return _default_root.tk.call('image', 'types')
1818
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001819######################################################################
1820# Extensions:
1821
1822class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001823 def __init__(self, master=None, cnf={}, **kw):
1824 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001825 self.bind('<Any-Enter>', self.tkButtonEnter)
1826 self.bind('<Any-Leave>', self.tkButtonLeave)
1827 self.bind('<1>', self.tkButtonDown)
1828 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001829
1830class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001831 def __init__(self, master=None, cnf={}, **kw):
1832 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001833 self.bind('<Any-Enter>', self.tkButtonEnter)
1834 self.bind('<Any-Leave>', self.tkButtonLeave)
1835 self.bind('<1>', self.tkButtonDown)
1836 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1837 self['fg'] = self['bg']
1838 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001839
Guido van Rossumc417ef81996-08-21 23:38:59 +00001840######################################################################
1841# Test:
1842
1843def _test():
1844 root = Tk()
1845 label = Label(root, text="Proof-of-existence test for Tk")
1846 label.pack()
1847 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001848 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001849 text="[%s]" % root.test['text']))
1850 test.pack()
1851 root.test = test
1852 quit = Button(root, text="QUIT", command=root.destroy)
1853 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001854 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001855 root.mainloop()
1856
1857if __name__ == '__main__':
1858 _test()