blob: b4c9ccc911f658d94e031603cceed22efc588c81 [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 Rossumf8d579c1999-01-04 18:06:45 +00005import sys
6if sys.platform == "win32":
Guido van Rossumc55b0ca1999-02-08 15:26:49 +00007 import FixTk # Attempt to configure Tcl/Tk without requiring PATH
Guido van Rossumf8d579c1999-01-04 18:06:45 +00008import _tkinter # If this fails your Python may not be configured for Tk
Guido van Rossum95806091997-02-15 18:33:24 +00009tkinter = _tkinter # b/w compat for export
10TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +000011from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +000012from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000013import string; _string = string; del string
Guido van Rossumf0c891a1998-04-29 21:43:36 +000014try:
15 import MacOS; _MacOS = MacOS; del MacOS
16except ImportError:
17 _MacOS = None
Guido van Rossum18468821994-06-20 07:49:28 +000018
Guido van Rossum95806091997-02-15 18:33:24 +000019TkVersion = _string.atof(_tkinter.TK_VERSION)
20TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000021
Guido van Rossumd6615ab1997-08-05 02:35:01 +000022READABLE = _tkinter.READABLE
23WRITABLE = _tkinter.WRITABLE
24EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000025
26# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
27try: _tkinter.createfilehandler
28except AttributeError: _tkinter.createfilehandler = None
29try: _tkinter.deletefilehandler
30except AttributeError: _tkinter.deletefilehandler = None
Guido van Rossum36269991996-05-16 17:11:27 +000031
32
Guido van Rossum2dcf5291994-07-06 09:23:20 +000033def _flatten(tuple):
34 res = ()
35 for item in tuple:
36 if type(item) in (TupleType, ListType):
37 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000038 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000039 res = res + (item,)
40 return res
41
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000042try: _flatten = _tkinter._flatten
43except AttributeError: pass
44
Guido van Rossum2dcf5291994-07-06 09:23:20 +000045def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000046 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000047 return cnfs
48 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000049 return cnfs
50 else:
51 cnf = {}
52 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000053 try:
54 cnf.update(c)
55 except (AttributeError, TypeError), msg:
56 print "_cnfmerge: fallback due to:", msg
57 for k, v in c.items():
58 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000059 return cnf
60
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000061try: _cnfmerge = _tkinter._cnfmerge
62except AttributeError: pass
63
Guido van Rossum2dcf5291994-07-06 09:23:20 +000064class Event:
65 pass
66
Guido van Rossumc4570481998-03-20 20:45:49 +000067_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +000068_default_root = None
69
Guido van Rossumc4570481998-03-20 20:45:49 +000070def NoDefaultRoot():
71 global _support_default_root
72 _support_default_root = 0
Guido van Rossumda654501998-10-06 19:06:27 +000073 global _default_root
74 _default_root = None
Guido van Rossumc4570481998-03-20 20:45:49 +000075 del _default_root
76
Guido van Rossum45853db1994-06-20 12:19:19 +000077def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000078 pass
79
Guido van Rossum97aeca11994-07-07 13:12:12 +000080def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000081 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000082
Guido van Rossumaec5dc91994-06-27 07:55:12 +000083_varnum = 0
84class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000085 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000086 def __init__(self, master=None):
Guido van Rossumaec5dc91994-06-27 07:55:12 +000087 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +000088 if not master:
89 master = _default_root
90 self._master = master
91 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +000092 self._name = 'PY_VAR' + `_varnum`
93 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000094 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000095 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000096 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000097 def __str__(self):
98 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000099 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000100 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +0000101 def trace_variable(self, mode, callback):
102 cbname = self._master._register(callback)
103 self._tk.call("trace", "variable", self._name, mode, cbname)
104 return cbname
105 trace = trace_variable
106 def trace_vdelete(self, mode, cbname):
107 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Guido van Rossum0001a111998-02-19 21:20:30 +0000108 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +0000109 def trace_vinfo(self):
110 return map(self._tk.split, self._tk.splitlist(
111 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000112
113class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000114 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000115 def __init__(self, master=None):
116 Variable.__init__(self, master)
117 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000118 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000119
120class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000121 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000122 def __init__(self, master=None):
123 Variable.__init__(self, master)
124 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000125 return getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000126
127class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000128 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000129 def __init__(self, master=None):
130 Variable.__init__(self, master)
131 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000132 return getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000133
134class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000135 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000136 def __init__(self, master=None):
137 Variable.__init__(self, master)
138 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000139 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000140
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000141def mainloop(n=0):
142 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000143
Guido van Rossum0132f691998-04-30 17:50:36 +0000144getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000145
Guido van Rossum0132f691998-04-30 17:50:36 +0000146getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000147
148def getboolean(s):
149 return _default_root.tk.getboolean(s)
150
Guido van Rossum368e06b1997-11-07 20:38:49 +0000151# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000152class Misc:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000153 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000154 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000155 def destroy(self):
156 if self._tclCommands is not None:
157 for name in self._tclCommands:
158 #print '- Tkinter: deleted command', name
159 self.tk.deletecommand(name)
160 self._tclCommands = None
161 def deletecommand(self, name):
162 #print '- Tkinter: deleted command', name
163 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000164 try:
165 self._tclCommands.remove(name)
166 except ValueError:
167 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000168 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000169 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000170 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000171 def tk_bisque(self):
172 self.tk.call('tk_bisque')
173 def tk_setPalette(self, *args, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000174 self.tk.call(('tk_setPalette',)
Fred Drake3faf9b41996-10-04 19:23:04 +0000175 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000176 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000177 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000178 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000179 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000180 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000181 def wait_window(self, window=None):
182 if window == None:
183 window = self
184 self.tk.call('tkwait', 'window', window._w)
185 def wait_visibility(self, window=None):
186 if window == None:
187 window = self
188 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000189 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000190 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000191 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000192 return self.tk.getvar(name)
Guido van Rossum0132f691998-04-30 17:50:36 +0000193 getint = int
194 getdouble = float
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000195 def getboolean(self, s):
196 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000197 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000198 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000199 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000200 def focus_force(self):
201 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000202 def focus_get(self):
203 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000204 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000205 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000206 def focus_displayof(self):
207 name = self.tk.call('focus', '-displayof', self._w)
208 if name == 'none' or not name: return None
209 return self._nametowidget(name)
210 def focus_lastfor(self):
211 name = self.tk.call('focus', '-lastfor', self._w)
212 if name == 'none' or not name: return None
213 return self._nametowidget(name)
214 def tk_focusFollowsMouse(self):
215 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000216 def tk_focusNext(self):
217 name = self.tk.call('tk_focusNext', self._w)
218 if not name: return None
219 return self._nametowidget(name)
220 def tk_focusPrev(self):
221 name = self.tk.call('tk_focusPrev', self._w)
222 if not name: return None
223 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000224 def after(self, ms, func=None, *args):
225 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000226 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000227 self.tk.call('after', ms)
228 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000229 # XXX Disgusting hack to clean up after calling func
230 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000231 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000232 try:
233 apply(func, args)
234 finally:
Guido van Rossum0c920001998-09-14 19:06:39 +0000235 try:
236 self.deletecommand(tmp[0])
237 except TclError:
238 pass
Guido van Rossum08a40381994-06-21 11:44:21 +0000239 name = self._register(callit)
240 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000241 return self.tk.call('after', ms, name)
242 def after_idle(self, func, *args):
243 return apply(self.after, ('idle', func) + args)
244 def after_cancel(self, id):
245 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000246 def bell(self, displayof=0):
Guido van Rossumf9756991998-04-29 21:57:08 +0000247 self.tk.call(('bell',) + self._displayof(displayof))
Fred Drake3c602d71996-09-27 14:06:54 +0000248 # Clipboard handling:
249 def clipboard_clear(self, **kw):
250 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000251 self.tk.call(('clipboard', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000252 def clipboard_append(self, string, **kw):
253 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000254 self.tk.call(('clipboard', 'append') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000255 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000256 # XXX grab current w/o window argument
257 def grab_current(self):
258 name = self.tk.call('grab', 'current', self._w)
259 if not name: return None
260 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000261 def grab_release(self):
262 self.tk.call('grab', 'release', self._w)
263 def grab_set(self):
264 self.tk.call('grab', 'set', self._w)
265 def grab_set_global(self):
266 self.tk.call('grab', 'set', '-global', self._w)
267 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000268 status = self.tk.call('grab', 'status', self._w)
269 if status == 'none': status = None
270 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000271 def lower(self, belowThis=None):
272 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000273 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000274 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000275 def option_clear(self):
276 self.tk.call('option', 'clear')
277 def option_get(self, name, className):
278 return self.tk.call('option', 'get', self._w, name, className)
279 def option_readfile(self, fileName, priority = None):
280 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000281 def selection_clear(self, **kw):
282 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000283 self.tk.call(('selection', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000284 def selection_get(self, **kw):
285 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000286 return self.tk.call(('selection', 'get') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000287 def selection_handle(self, command, **kw):
288 name = self._register(command)
Guido van Rossumf9756991998-04-29 21:57:08 +0000289 self.tk.call(('selection', 'handle') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000290 + (self._w, name))
291 def selection_own(self, **kw):
292 "Become owner of X selection."
Guido van Rossumf9756991998-04-29 21:57:08 +0000293 self.tk.call(('selection', 'own') +
294 self._options(kw) + (self._w,))
Fred Drake3c602d71996-09-27 14:06:54 +0000295 def selection_own_get(self, **kw):
296 "Find owner of X selection."
297 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000298 name = self.tk.call(('selection', 'own') + self._options(kw))
Guido van Rossum76f587b1997-01-21 23:22:03 +0000299 if not name: return None
300 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000301 def send(self, interp, cmd, *args):
Guido van Rossumf9756991998-04-29 21:57:08 +0000302 return self.tk.call(('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000303 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000304 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000305 def tkraise(self, aboveThis=None):
306 self.tk.call('raise', self._w, aboveThis)
307 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000308 def colormodel(self, value=None):
309 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000310 def winfo_atom(self, name, displayof=0):
311 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Guido van Rossum0132f691998-04-30 17:50:36 +0000312 return getint(self.tk.call(args))
Fred Drake3c602d71996-09-27 14:06:54 +0000313 def winfo_atomname(self, id, displayof=0):
314 args = ('winfo', 'atomname') \
315 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000316 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000317 def winfo_cells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000318 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000319 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000320 def winfo_children(self):
321 return map(self._nametowidget,
322 self.tk.splitlist(self.tk.call(
323 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def winfo_class(self):
325 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000326 def winfo_colormapfull(self):
327 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000328 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000329 def winfo_containing(self, rootX, rootY, displayof=0):
330 args = ('winfo', 'containing') \
331 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumf9756991998-04-29 21:57:08 +0000332 name = self.tk.call(args)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000333 if not name: return None
334 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000335 def winfo_depth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000336 return getint(self.tk.call('winfo', 'depth', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000337 def winfo_exists(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000338 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000339 self.tk.call('winfo', 'exists', self._w))
340 def winfo_fpixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000341 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000342 'winfo', 'fpixels', self._w, number))
343 def winfo_geometry(self):
344 return self.tk.call('winfo', 'geometry', self._w)
345 def winfo_height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000346 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000347 self.tk.call('winfo', 'height', self._w))
348 def winfo_id(self):
Guido van Rossumcef4c841998-06-19 04:35:45 +0000349 return self.tk.getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000350 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000351 def winfo_interps(self, displayof=0):
352 args = ('winfo', 'interps') + self._displayof(displayof)
Guido van Rossumf9756991998-04-29 21:57:08 +0000353 return self.tk.splitlist(self.tk.call(args))
Guido van Rossum18468821994-06-20 07:49:28 +0000354 def winfo_ismapped(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000355 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000356 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000357 def winfo_manager(self):
358 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000359 def winfo_name(self):
360 return self.tk.call('winfo', 'name', self._w)
361 def winfo_parent(self):
362 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000363 def winfo_pathname(self, id, displayof=0):
364 args = ('winfo', 'pathname') \
365 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000366 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000367 def winfo_pixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000368 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000369 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000370 def winfo_pointerx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000371 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000372 self.tk.call('winfo', 'pointerx', self._w))
373 def winfo_pointerxy(self):
374 return self._getints(
375 self.tk.call('winfo', 'pointerxy', self._w))
376 def winfo_pointery(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000377 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000378 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000379 def winfo_reqheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000380 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000381 self.tk.call('winfo', 'reqheight', self._w))
382 def winfo_reqwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000383 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000384 self.tk.call('winfo', 'reqwidth', self._w))
385 def winfo_rgb(self, color):
386 return self._getints(
387 self.tk.call('winfo', 'rgb', self._w, color))
388 def winfo_rootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000389 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000390 self.tk.call('winfo', 'rootx', self._w))
391 def winfo_rooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000392 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000393 self.tk.call('winfo', 'rooty', self._w))
394 def winfo_screen(self):
395 return self.tk.call('winfo', 'screen', self._w)
396 def winfo_screencells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000397 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000398 self.tk.call('winfo', 'screencells', self._w))
399 def winfo_screendepth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000400 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000401 self.tk.call('winfo', 'screendepth', self._w))
402 def winfo_screenheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000403 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000404 self.tk.call('winfo', 'screenheight', self._w))
405 def winfo_screenmmheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000406 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000407 self.tk.call('winfo', 'screenmmheight', self._w))
408 def winfo_screenmmwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000409 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000410 self.tk.call('winfo', 'screenmmwidth', self._w))
411 def winfo_screenvisual(self):
412 return self.tk.call('winfo', 'screenvisual', self._w)
413 def winfo_screenwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000414 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000415 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000416 def winfo_server(self):
417 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000418 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000419 return self._nametowidget(self.tk.call(
420 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000421 def winfo_viewable(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000422 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000423 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000424 def winfo_visual(self):
425 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000426 def winfo_visualid(self):
427 return self.tk.call('winfo', 'visualid', self._w)
428 def winfo_visualsavailable(self, includeids=0):
429 data = self.tk.split(
430 self.tk.call('winfo', 'visualsavailable', self._w,
431 includeids and 'includeids' or None))
Guido van Rossumf8d8e071999-10-18 22:06:38 +0000432 return map(self.__winfo_parseitem, data)
433 def __winfo_parseitem(self, t):
434 return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
435 def __winfo_getint(self, x):
436 return _string.atoi(x, 0)
Guido van Rossum18468821994-06-20 07:49:28 +0000437 def winfo_vrootheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000438 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000439 self.tk.call('winfo', 'vrootheight', self._w))
440 def winfo_vrootwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000441 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000442 self.tk.call('winfo', 'vrootwidth', self._w))
443 def winfo_vrootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000444 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000445 self.tk.call('winfo', 'vrootx', self._w))
446 def winfo_vrooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000447 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000448 self.tk.call('winfo', 'vrooty', self._w))
449 def winfo_width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000450 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000451 self.tk.call('winfo', 'width', self._w))
452 def winfo_x(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000453 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000454 self.tk.call('winfo', 'x', self._w))
455 def winfo_y(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000456 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000457 self.tk.call('winfo', 'y', self._w))
458 def update(self):
459 self.tk.call('update')
460 def update_idletasks(self):
461 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000462 def bindtags(self, tagList=None):
463 if tagList is None:
464 return self.tk.splitlist(
465 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000466 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000467 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000468 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum88b63b81998-06-25 18:54:49 +0000469 if type(func) is StringType:
470 self.tk.call(what + (sequence, func))
471 elif func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000472 funcid = self._register(func, self._substitute,
473 needcleanup)
Guido van Rossumdc593401998-04-29 22:16:57 +0000474 cmd = ('%sif {"[%s %s]" == "break"} break\n'
475 %
476 (add and '+' or '',
477 funcid,
478 _string.join(self._subst_format)))
Guido van Rossumf9756991998-04-29 21:57:08 +0000479 self.tk.call(what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000480 return funcid
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000481 elif sequence:
Guido van Rossumf9756991998-04-29 21:57:08 +0000482 return self.tk.call(what + (sequence,))
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000483 else:
484 return self.tk.splitlist(self.tk.call(what))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000485 def bind(self, sequence=None, func=None, add=None):
486 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000487 def unbind(self, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +0000488 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000489 if funcid:
490 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000491 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000492 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000493 def unbind_all(self, sequence):
494 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000495 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000496 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000497 def unbind_class(self, className, sequence):
498 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000499 def mainloop(self, n=0):
500 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000501 def quit(self):
502 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000503 def _getints(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000504 if string:
505 return tuple(map(getint, self.tk.splitlist(string)))
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000506 def _getdoubles(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000507 if string:
508 return tuple(map(getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000509 def _getboolean(self, string):
510 if string:
511 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000512 def _displayof(self, displayof):
513 if displayof:
514 return ('-displayof', displayof)
515 if displayof is None:
516 return ('-displayof', self._w)
517 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000518 def _options(self, cnf, kw = None):
519 if kw:
520 cnf = _cnfmerge((cnf, kw))
521 else:
522 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000523 res = ()
524 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000525 if v is not None:
526 if k[-1] == '_': k = k[:-1]
527 if callable(v):
528 v = self._register(v)
529 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000530 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000531 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000532 w = self
533 if name[0] == '.':
534 w = w._root()
535 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000536 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000537 while name:
538 i = find(name, '.')
539 if i >= 0:
540 name, tail = name[:i], name[i+1:]
541 else:
542 tail = ''
543 w = w.children[name]
544 name = tail
545 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000546 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000547 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000548 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000549 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000550 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000551 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000552 except AttributeError:
553 pass
554 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000555 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000556 except AttributeError:
557 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000558 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000559 if needcleanup:
560 if self._tclCommands is None:
561 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +0000562 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000563 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000564 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000565 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000566 def _root(self):
567 w = self
568 while w.master: w = w.master
569 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000570 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000571 '%s', '%t', '%w', '%x', '%y',
572 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
573 def _substitute(self, *args):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000574 if len(args) != len(self._subst_format): return args
Guido van Rossum0132f691998-04-30 17:50:36 +0000575 getboolean = self.tk.getboolean
576 getint = int
Guido van Rossum45853db1994-06-20 12:19:19 +0000577 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
578 # Missing: (a, c, d, m, o, v, B, R)
579 e = Event()
Guido van Rossum0132f691998-04-30 17:50:36 +0000580 e.serial = getint(nsign)
581 e.num = getint(b)
582 try: e.focus = getboolean(f)
Guido van Rossum45853db1994-06-20 12:19:19 +0000583 except TclError: pass
Guido van Rossum0132f691998-04-30 17:50:36 +0000584 e.height = getint(h)
585 e.keycode = getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000586 # For Visibility events, event state is a string and
587 # not an integer:
588 try:
Guido van Rossum0132f691998-04-30 17:50:36 +0000589 e.state = getint(s)
Guido van Rossumfe02efd1998-06-09 02:37:45 +0000590 except ValueError:
Guido van Rossum36269991996-05-16 17:11:27 +0000591 e.state = s
Guido van Rossum0132f691998-04-30 17:50:36 +0000592 e.time = getint(t)
593 e.width = getint(w)
594 e.x = getint(x)
595 e.y = getint(y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000596 e.char = A
Guido van Rossum0132f691998-04-30 17:50:36 +0000597 try: e.send_event = getboolean(E)
Guido van Rossum45853db1994-06-20 12:19:19 +0000598 except TclError: pass
599 e.keysym = K
Guido van Rossum0132f691998-04-30 17:50:36 +0000600 e.keysym_num = getint(N)
Guido van Rossum45853db1994-06-20 12:19:19 +0000601 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +0000602 try:
603 e.widget = self._nametowidget(W)
604 except KeyError:
605 e.widget = W
Guido van Rossum0132f691998-04-30 17:50:36 +0000606 e.x_root = getint(X)
607 e.y_root = getint(Y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000608 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000609 def _report_exception(self):
610 import sys
611 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
612 root = self._root()
613 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000614 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000615 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000616 # XXX ought to generalize this so tag_config etc. can use it
617 if kw:
618 cnf = _cnfmerge((cnf, kw))
619 elif cnf:
620 cnf = _cnfmerge(cnf)
621 if cnf is None:
622 cnf = {}
623 for x in self.tk.split(
624 self.tk.call(self._w, 'configure')):
625 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
626 return cnf
627 if type(cnf) is StringType:
628 x = self.tk.split(self.tk.call(
629 self._w, 'configure', '-'+cnf))
630 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +0000631 self.tk.call((self._w, 'configure')
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000632 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000633 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000634 def cget(self, key):
635 return self.tk.call(self._w, 'cget', '-' + key)
636 __getitem__ = cget
637 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000638 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000639 def keys(self):
640 return map(lambda x: x[0][1:],
641 self.tk.split(self.tk.call(self._w, 'configure')))
642 def __str__(self):
643 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000644 # Pack methods that apply to the master
645 _noarg_ = ['_noarg_']
646 def pack_propagate(self, flag=_noarg_):
647 if flag is Misc._noarg_:
648 return self._getboolean(self.tk.call(
649 'pack', 'propagate', self._w))
650 else:
651 self.tk.call('pack', 'propagate', self._w, flag)
652 propagate = pack_propagate
653 def pack_slaves(self):
654 return map(self._nametowidget,
655 self.tk.splitlist(
656 self.tk.call('pack', 'slaves', self._w)))
657 slaves = pack_slaves
658 # Place method that applies to the master
659 def place_slaves(self):
660 return map(self._nametowidget,
661 self.tk.splitlist(
662 self.tk.call(
663 'place', 'slaves', self._w)))
664 # Grid methods that apply to the master
Barry Warsaw107e6231998-12-15 00:44:15 +0000665 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
666 args = ('grid', 'bbox', self._w)
667 if column is not None and row is not None:
668 args = args + (column, row)
669 if col2 is not None and row2 is not None:
670 args = args + (col2, row2)
671 return self._getints(apply(self.tk.call, args)) or None
672
Guido van Rossum368e06b1997-11-07 20:38:49 +0000673 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000674 def _grid_configure(self, command, index, cnf, kw):
675 if type(cnf) is StringType and not kw:
676 if cnf[-1:] == '_':
677 cnf = cnf[:-1]
678 if cnf[:1] != '-':
679 cnf = '-'+cnf
680 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000681 else:
682 options = self._options(cnf, kw)
683 if not options:
684 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000685 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000686 words = self.tk.splitlist(res)
687 dict = {}
688 for i in range(0, len(words), 2):
689 key = words[i][1:]
690 value = words[i+1]
691 if not value:
692 value = None
693 elif '.' in value:
Guido van Rossum0132f691998-04-30 17:50:36 +0000694 value = getdouble(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000695 else:
Guido van Rossum0132f691998-04-30 17:50:36 +0000696 value = getint(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000697 dict[key] = value
698 return dict
Guido van Rossumf9756991998-04-29 21:57:08 +0000699 res = self.tk.call(
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000700 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000701 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000702 if len(options) == 1:
703 if not res: return None
704 # In Tk 7.5, -width can be a float
Guido van Rossum0132f691998-04-30 17:50:36 +0000705 if '.' in res: return getdouble(res)
706 return getint(res)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000707 def grid_columnconfigure(self, index, cnf={}, **kw):
708 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000709 columnconfigure = grid_columnconfigure
710 def grid_propagate(self, flag=_noarg_):
711 if flag is Misc._noarg_:
712 return self._getboolean(self.tk.call(
713 'grid', 'propagate', self._w))
714 else:
715 self.tk.call('grid', 'propagate', self._w, flag)
716 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000717 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000718 rowconfigure = grid_rowconfigure
719 def grid_size(self):
720 return self._getints(
721 self.tk.call('grid', 'size', self._w)) or None
722 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000723 def grid_slaves(self, row=None, column=None):
724 args = ()
Guido van Rossumeb354b31999-03-16 21:54:50 +0000725 if row is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000726 args = args + ('-row', row)
Guido van Rossumeb354b31999-03-16 21:54:50 +0000727 if column is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000728 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000729 return map(self._nametowidget,
Guido van Rossumf9756991998-04-29 21:57:08 +0000730 self.tk.splitlist(self.tk.call(
731 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000732
Guido van Rossum80f8be81997-12-02 19:51:39 +0000733 # Support for the "event" command, new in Tk 4.2.
734 # By Case Roole.
735
Guido van Rossum56c04b81998-04-06 03:10:03 +0000736 def event_add(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000737 args = ('event', 'add', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000738 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000739
Guido van Rossum56c04b81998-04-06 03:10:03 +0000740 def event_delete(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000741 args = ('event', 'delete', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000742 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000743
744 def event_generate(self, sequence, **kw):
745 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +0000746 for k, v in kw.items():
747 args = args + ('-%s' % k, str(v))
Guido van Rossumf9756991998-04-29 21:57:08 +0000748 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000749
Guido van Rossum56c04b81998-04-06 03:10:03 +0000750 def event_info(self, virtual=None):
751 return self.tk.splitlist(
752 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +0000753
Guido van Rossumc2966511998-04-10 19:16:10 +0000754 # Image related commands
755
756 def image_names(self):
757 return self.tk.call('image', 'names')
758
759 def image_types(self):
760 return self.tk.call('image', 'types')
761
Guido van Rossum80f8be81997-12-02 19:51:39 +0000762
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000763class CallWrapper:
764 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000765 self.func = func
766 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000767 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000768 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000769 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000770 if self.subst:
771 args = apply(self.subst, args)
772 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000773 except SystemExit, msg:
774 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000775 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000776 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000777
Guido van Rossume365a591998-05-01 19:48:20 +0000778
Guido van Rossum18468821994-06-20 07:49:28 +0000779class Wm:
Guido van Rossume365a591998-05-01 19:48:20 +0000780 def wm_aspect(self,
Guido van Rossum18468821994-06-20 07:49:28 +0000781 minNumer=None, minDenom=None,
782 maxNumer=None, maxDenom=None):
783 return self._getints(
784 self.tk.call('wm', 'aspect', self._w,
785 minNumer, minDenom,
786 maxNumer, maxDenom))
Guido van Rossume365a591998-05-01 19:48:20 +0000787 aspect = wm_aspect
788 def wm_client(self, name=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000789 return self.tk.call('wm', 'client', self._w, name)
Guido van Rossume365a591998-05-01 19:48:20 +0000790 client = wm_client
791 def wm_colormapwindows(self, *wlist):
Guido van Rossum8fa42af1999-10-20 12:29:56 +0000792 if len(wlist) > 1:
793 wlist = (wlist,) # Tk needs a list of windows here
794 args = ('wm', 'colormapwindows', self._w) + wlist
Guido van Rossumf9756991998-04-29 21:57:08 +0000795 return map(self._nametowidget, self.tk.call(args))
Guido van Rossume365a591998-05-01 19:48:20 +0000796 colormapwindows = wm_colormapwindows
797 def wm_command(self, value=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000798 return self.tk.call('wm', 'command', self._w, value)
Guido van Rossume365a591998-05-01 19:48:20 +0000799 command = wm_command
800 def wm_deiconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000801 return self.tk.call('wm', 'deiconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000802 deiconify = wm_deiconify
803 def wm_focusmodel(self, model=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000804 return self.tk.call('wm', 'focusmodel', self._w, model)
Guido van Rossume365a591998-05-01 19:48:20 +0000805 focusmodel = wm_focusmodel
806 def wm_frame(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000807 return self.tk.call('wm', 'frame', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000808 frame = wm_frame
809 def wm_geometry(self, newGeometry=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000810 return self.tk.call('wm', 'geometry', self._w, newGeometry)
Guido van Rossume365a591998-05-01 19:48:20 +0000811 geometry = wm_geometry
812 def wm_grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000813 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000814 widthInc=None, heightInc=None):
815 return self._getints(self.tk.call(
816 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000817 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossume365a591998-05-01 19:48:20 +0000818 grid = wm_grid
819 def wm_group(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000820 return self.tk.call('wm', 'group', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000821 group = wm_group
822 def wm_iconbitmap(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000823 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000824 iconbitmap = wm_iconbitmap
825 def wm_iconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000826 return self.tk.call('wm', 'iconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000827 iconify = wm_iconify
828 def wm_iconmask(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000829 return self.tk.call('wm', 'iconmask', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000830 iconmask = wm_iconmask
831 def wm_iconname(self, newName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000832 return self.tk.call('wm', 'iconname', self._w, newName)
Guido van Rossume365a591998-05-01 19:48:20 +0000833 iconname = wm_iconname
834 def wm_iconposition(self, x=None, y=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000835 return self._getints(self.tk.call(
836 'wm', 'iconposition', self._w, x, y))
Guido van Rossume365a591998-05-01 19:48:20 +0000837 iconposition = wm_iconposition
838 def wm_iconwindow(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000839 return self.tk.call('wm', 'iconwindow', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000840 iconwindow = wm_iconwindow
841 def wm_maxsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000842 return self._getints(self.tk.call(
843 'wm', 'maxsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000844 maxsize = wm_maxsize
845 def wm_minsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000846 return self._getints(self.tk.call(
847 'wm', 'minsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000848 minsize = wm_minsize
849 def wm_overrideredirect(self, boolean=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000850 return self._getboolean(self.tk.call(
851 'wm', 'overrideredirect', self._w, boolean))
Guido van Rossume365a591998-05-01 19:48:20 +0000852 overrideredirect = wm_overrideredirect
853 def wm_positionfrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000854 return self.tk.call('wm', 'positionfrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000855 positionfrom = wm_positionfrom
856 def wm_protocol(self, name=None, func=None):
Guido van Rossumc4570481998-03-20 20:45:49 +0000857 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000858 command = self._register(func)
859 else:
860 command = func
861 return self.tk.call(
862 'wm', 'protocol', self._w, name, command)
Guido van Rossume365a591998-05-01 19:48:20 +0000863 protocol = wm_protocol
864 def wm_resizable(self, width=None, height=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000865 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossume365a591998-05-01 19:48:20 +0000866 resizable = wm_resizable
867 def wm_sizefrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000868 return self.tk.call('wm', 'sizefrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000869 sizefrom = wm_sizefrom
870 def wm_state(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000871 return self.tk.call('wm', 'state', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000872 state = wm_state
873 def wm_title(self, string=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000874 return self.tk.call('wm', 'title', self._w, string)
Guido van Rossume365a591998-05-01 19:48:20 +0000875 title = wm_title
876 def wm_transient(self, master=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000877 return self.tk.call('wm', 'transient', self._w, master)
Guido van Rossume365a591998-05-01 19:48:20 +0000878 transient = wm_transient
879 def wm_withdraw(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000880 return self.tk.call('wm', 'withdraw', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000881 withdraw = wm_withdraw
882
Guido van Rossum18468821994-06-20 07:49:28 +0000883
884class Tk(Misc, Wm):
885 _w = '.'
886 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000887 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000888 self.master = None
889 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000890 if baseName is None:
891 import sys, os
892 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000893 baseName, ext = os.path.splitext(baseName)
Fred Drake182c5901998-07-15 04:36:56 +0000894 if ext not in ('.py', '.pyc', '.pyo'):
895 baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000896 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000897 if _MacOS:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000898 # Disable event scanning except for Command-Period
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000899 _MacOS.SchedParams(1, 0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000900 # Work around nasty MacTk bug
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000901 # XXX Is this one still needed?
Guido van Rossum37dcab11996-05-16 16:00:19 +0000902 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000903 # Version sanity checks
904 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000905 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000906 raise RuntimeError, \
907 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000908 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000909 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000910 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000911 raise RuntimeError, \
912 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000913 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000914 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000915 raise RuntimeError, \
916 "Tk 4.0 or higher is required; found Tk %s" \
917 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000918 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000919 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000920 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +0000921 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000922 _default_root = self
Guido van Rossume61e98d1999-08-20 18:26:06 +0000923 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum45853db1994-06-20 12:19:19 +0000924 def destroy(self):
925 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000926 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000927 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000928 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000929 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000930 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000931 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000932 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000933 if os.environ.has_key('HOME'): home = os.environ['HOME']
934 else: home = os.curdir
935 class_tcl = os.path.join(home, '.%s.tcl' % className)
936 class_py = os.path.join(home, '.%s.py' % className)
937 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
938 base_py = os.path.join(home, '.%s.py' % baseName)
939 dir = {'self': self}
940 exec 'from Tkinter import *' in dir
941 if os.path.isfile(class_tcl):
942 print 'source', `class_tcl`
943 self.tk.call('source', class_tcl)
944 if os.path.isfile(class_py):
945 print 'execfile', `class_py`
946 execfile(class_py, dir)
947 if os.path.isfile(base_tcl):
948 print 'source', `base_tcl`
949 self.tk.call('source', base_tcl)
950 if os.path.isfile(base_py):
951 print 'execfile', `base_py`
952 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000953 def report_callback_exception(self, exc, val, tb):
Guido van Rossumda654501998-10-06 19:06:27 +0000954 import traceback, sys
955 sys.stderr.write("Exception in Tkinter callback\n")
Guido van Rossum9f1292d1998-10-13 20:02:39 +0000956 sys.last_type = exc
957 sys.last_value = val
958 sys.last_traceback = tb
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000959 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000960
Guido van Rossum368e06b1997-11-07 20:38:49 +0000961# Ideally, the classes Pack, Place and Grid disappear, the
962# pack/place/grid methods are defined on the Widget class, and
963# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
964# ...), with pack(), place() and grid() being short for
965# pack_configure(), place_configure() and grid_columnconfigure(), and
966# forget() being short for pack_forget(). As a practical matter, I'm
967# afraid that there is too much code out there that may be using the
968# Pack, Place or Grid class, so I leave them intact -- but only as
969# backwards compatibility features. Also note that those methods that
970# take a master as argument (e.g. pack_propagate) have been moved to
971# the Misc class (which now incorporates all methods common between
972# toplevel and interior widgets). Again, for compatibility, these are
973# copied into the Pack, Place or Grid class.
974
Guido van Rossum18468821994-06-20 07:49:28 +0000975class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000976 def pack_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000977 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000978 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000979 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000980 pack = configure = config = pack_configure
981 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000982 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000983 forget = pack_forget
984 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000985 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000986 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000987 dict = {}
988 for i in range(0, len(words), 2):
989 key = words[i][1:]
990 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000991 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000992 value = self._nametowidget(value)
993 dict[key] = value
994 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000995 info = pack_info
996 propagate = pack_propagate = Misc.pack_propagate
997 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000998
999class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001000 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001001 for k in ['in_']:
1002 if kw.has_key(k):
1003 kw[k[:-1]] = kw[k]
1004 del kw[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001005 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001006 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001007 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001008 place = configure = config = place_configure
1009 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +00001010 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001011 forget = place_forget
1012 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +00001013 words = self.tk.splitlist(
1014 self.tk.call('place', 'info', self._w))
1015 dict = {}
1016 for i in range(0, len(words), 2):
1017 key = words[i][1:]
1018 value = words[i+1]
1019 if value[:1] == '.':
1020 value = self._nametowidget(value)
1021 dict[key] = value
1022 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001023 info = place_info
1024 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001025
Guido van Rossum37dcab11996-05-16 16:00:19 +00001026class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001027 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001028 def grid_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001029 self.tk.call(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001030 ('grid', 'configure', self._w)
1031 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001032 grid = configure = config = grid_configure
1033 bbox = grid_bbox = Misc.grid_bbox
1034 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1035 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001036 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001037 forget = grid_forget
Guido van Rossum8e407591999-05-05 23:03:48 +00001038 def grid_remove(self):
1039 self.tk.call('grid', 'remove', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001040 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001041 words = self.tk.splitlist(
1042 self.tk.call('grid', 'info', self._w))
1043 dict = {}
1044 for i in range(0, len(words), 2):
1045 key = words[i][1:]
1046 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001047 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001048 value = self._nametowidget(value)
1049 dict[key] = value
1050 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001051 info = grid_info
1052 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001053 return self._getints(
1054 self.tk.call(
1055 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001056 location = grid_location
1057 propagate = grid_propagate = Misc.grid_propagate
1058 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1059 size = grid_size = Misc.grid_size
1060 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001061
Guido van Rossum368e06b1997-11-07 20:38:49 +00001062class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001063 def _setup(self, master, cnf):
Guido van Rossumc4570481998-03-20 20:45:49 +00001064 if _support_default_root:
1065 global _default_root
1066 if not master:
1067 if not _default_root:
1068 _default_root = Tk()
1069 master = _default_root
Guido van Rossum18468821994-06-20 07:49:28 +00001070 self.master = master
1071 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001072 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001073 if cnf.has_key('name'):
1074 name = cnf['name']
1075 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001076 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001077 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001078 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001079 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001080 self._w = '.' + name
1081 else:
1082 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001083 self.children = {}
1084 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001085 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001086 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001087 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1088 if kw:
1089 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001090 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001091 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001092 classes = []
1093 for k in cnf.keys():
1094 if type(k) is ClassType:
1095 classes.append((k, cnf[k]))
1096 del cnf[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001097 self.tk.call(
1098 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001099 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001100 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001101 def destroy(self):
1102 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001103 if self.master.children.has_key(self._name):
1104 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001105 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001106 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001107 def _do(self, name, args=()):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001108 # XXX Obsolete -- better use self.tk.call directly!
Guido van Rossumf9756991998-04-29 21:57:08 +00001109 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001110
Guido van Rossum368e06b1997-11-07 20:38:49 +00001111class Widget(BaseWidget, Pack, Place, Grid):
1112 pass
1113
1114class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001115 def __init__(self, master=None, cnf={}, **kw):
1116 if kw:
1117 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001118 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001119 for wmkey in ['screen', 'class_', 'class', 'visual',
1120 'colormap']:
1121 if cnf.has_key(wmkey):
1122 val = cnf[wmkey]
1123 # TBD: a hack needed because some keys
1124 # are not valid as keyword arguments
1125 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1126 else: opt = '-'+wmkey
1127 extra = extra + (opt, val)
1128 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001129 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001130 root = self._root()
1131 self.iconname(root.iconname())
1132 self.title(root.title())
Guido van Rossume61e98d1999-08-20 18:26:06 +00001133 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum18468821994-06-20 07:49:28 +00001134
1135class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001136 def __init__(self, master=None, cnf={}, **kw):
1137 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001138 def tkButtonEnter(self, *dummy):
1139 self.tk.call('tkButtonEnter', self._w)
1140 def tkButtonLeave(self, *dummy):
1141 self.tk.call('tkButtonLeave', self._w)
1142 def tkButtonDown(self, *dummy):
1143 self.tk.call('tkButtonDown', self._w)
1144 def tkButtonUp(self, *dummy):
1145 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001146 def tkButtonInvoke(self, *dummy):
1147 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001148 def flash(self):
1149 self.tk.call(self._w, 'flash')
1150 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001151 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001152
1153# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001154# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001155def AtEnd():
1156 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001157def AtInsert(*args):
1158 s = 'insert'
1159 for a in args:
1160 if a: s = s + (' ' + a)
1161 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001162def AtSelFirst():
1163 return 'sel.first'
1164def AtSelLast():
1165 return 'sel.last'
1166def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001167 if y is None:
1168 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001169 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001170 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001171
1172class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001173 def __init__(self, master=None, cnf={}, **kw):
1174 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001175 def addtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001176 self.tk.call((self._w, 'addtag') + args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001177 def addtag_above(self, newtag, tagOrId):
1178 self.addtag(newtag, 'above', tagOrId)
1179 def addtag_all(self, newtag):
1180 self.addtag(newtag, 'all')
1181 def addtag_below(self, newtag, tagOrId):
1182 self.addtag(newtag, 'below', tagOrId)
1183 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1184 self.addtag(newtag, 'closest', x, y, halo, start)
1185 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1186 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1187 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1188 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1189 def addtag_withtag(self, newtag, tagOrId):
1190 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001191 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001192 return self._getints(
1193 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001194 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001195 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001196 if funcid:
1197 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001198 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum42b334d1999-07-30 12:22:12 +00001199 return self._bind((self._w, 'bind', tagOrId),
1200 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001201 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001202 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001203 self._w, 'canvasx', screenx, gridspacing))
1204 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001205 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001206 self._w, 'canvasy', screeny, gridspacing))
1207 def coords(self, *args):
Guido van Rossum0001a111998-02-19 21:20:30 +00001208 # XXX Should use _flatten on args
Guido van Rossum0132f691998-04-30 17:50:36 +00001209 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00001210 self.tk.splitlist(
1211 self.tk.call((self._w, 'coords') + args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001212 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001213 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001214 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001215 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001216 args = args[:-1]
1217 else:
1218 cnf = {}
Guido van Rossum0132f691998-04-30 17:50:36 +00001219 return getint(apply(
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001220 self.tk.call,
1221 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001222 + args + self._options(cnf, kw)))
1223 def create_arc(self, *args, **kw):
1224 return self._create('arc', args, kw)
1225 def create_bitmap(self, *args, **kw):
1226 return self._create('bitmap', args, kw)
1227 def create_image(self, *args, **kw):
1228 return self._create('image', args, kw)
1229 def create_line(self, *args, **kw):
1230 return self._create('line', args, kw)
1231 def create_oval(self, *args, **kw):
1232 return self._create('oval', args, kw)
1233 def create_polygon(self, *args, **kw):
1234 return self._create('polygon', args, kw)
1235 def create_rectangle(self, *args, **kw):
1236 return self._create('rectangle', args, kw)
1237 def create_text(self, *args, **kw):
1238 return self._create('text', args, kw)
1239 def create_window(self, *args, **kw):
1240 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001241 def dchars(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001242 self.tk.call((self._w, 'dchars') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001243 def delete(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001244 self.tk.call((self._w, 'delete') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001245 def dtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001246 self.tk.call((self._w, 'dtag') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001247 def find(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001248 return self._getints(
1249 self.tk.call((self._w, 'find') + args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001250 def find_above(self, tagOrId):
1251 return self.find('above', tagOrId)
1252 def find_all(self):
1253 return self.find('all')
1254 def find_below(self, tagOrId):
1255 return self.find('below', tagOrId)
1256 def find_closest(self, x, y, halo=None, start=None):
1257 return self.find('closest', x, y, halo, start)
1258 def find_enclosed(self, x1, y1, x2, y2):
1259 return self.find('enclosed', x1, y1, x2, y2)
1260 def find_overlapping(self, x1, y1, x2, y2):
1261 return self.find('overlapping', x1, y1, x2, y2)
1262 def find_withtag(self, tagOrId):
1263 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001264 def focus(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001265 return self.tk.call((self._w, 'focus') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001266 def gettags(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001267 return self.tk.splitlist(
1268 self.tk.call((self._w, 'gettags') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001269 def icursor(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001270 self.tk.call((self._w, 'icursor') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001271 def index(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001272 return getint(self.tk.call((self._w, 'index') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001273 def insert(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001274 self.tk.call((self._w, 'insert') + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001275 def itemcget(self, tagOrId, option):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001276 return self.tk.call(
1277 (self._w, 'itemcget') + (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001278 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001279 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001280 cnf = {}
1281 for x in self.tk.split(
Guido van Rossum0bd54331998-05-19 21:18:13 +00001282 self.tk.call(self._w,
1283 'itemconfigure', tagOrId)):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001284 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1285 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001286 if type(cnf) == StringType and not kw:
Guido van Rossum0bd54331998-05-19 21:18:13 +00001287 x = self.tk.split(self.tk.call(
1288 self._w, 'itemconfigure', tagOrId, '-'+cnf))
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001289 return (x[0][1:],) + x[1:]
Guido van Rossum0bd54331998-05-19 21:18:13 +00001290 self.tk.call((self._w, 'itemconfigure', tagOrId) +
1291 self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001292 itemconfig = itemconfigure
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001293 # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
1294 # so the preferred name for them is tag_lower, tag_raise
1295 # (similar to tag_bind, and similar to the Text widget);
1296 # unfortunately can't delete the old ones yet (maybe in 1.6)
1297 def tag_lower(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001298 self.tk.call((self._w, 'lower') + args)
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001299 lower = tag_lower
Guido van Rossum18468821994-06-20 07:49:28 +00001300 def move(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001301 self.tk.call((self._w, 'move') + args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001302 def postscript(self, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001303 return self.tk.call((self._w, 'postscript') +
1304 self._options(cnf, kw))
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001305 def tag_raise(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001306 self.tk.call((self._w, 'raise') + args)
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001307 lift = tkraise = tag_raise
Guido van Rossum18468821994-06-20 07:49:28 +00001308 def scale(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001309 self.tk.call((self._w, 'scale') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001310 def scan_mark(self, x, y):
1311 self.tk.call(self._w, 'scan', 'mark', x, y)
1312 def scan_dragto(self, x, y):
1313 self.tk.call(self._w, 'scan', 'dragto', x, y)
1314 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001315 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001316 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001317 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001318 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001319 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001320 def select_item(self):
1321 self.tk.call(self._w, 'select', 'item')
1322 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001323 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001324 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001325 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001326 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001327 if not args:
1328 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001329 self.tk.call((self._w, 'xview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001330 def xview_moveto(self, fraction):
1331 self.tk.call(self._w, 'xview', 'moveto', fraction)
1332 def xview_scroll(self, number, what):
1333 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001334 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001335 if not args:
1336 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001337 self.tk.call((self._w, 'yview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001338 def yview_moveto(self, fraction):
1339 self.tk.call(self._w, 'yview', 'moveto', fraction)
1340 def yview_scroll(self, number, what):
1341 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001342
1343class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001344 def __init__(self, master=None, cnf={}, **kw):
1345 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001346 def deselect(self):
1347 self.tk.call(self._w, 'deselect')
1348 def flash(self):
1349 self.tk.call(self._w, 'flash')
1350 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001351 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001352 def select(self):
1353 self.tk.call(self._w, 'select')
1354 def toggle(self):
1355 self.tk.call(self._w, 'toggle')
1356
1357class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001358 def __init__(self, master=None, cnf={}, **kw):
1359 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001360 def delete(self, first, last=None):
1361 self.tk.call(self._w, 'delete', first, last)
1362 def get(self):
1363 return self.tk.call(self._w, 'get')
1364 def icursor(self, index):
1365 self.tk.call(self._w, 'icursor', index)
1366 def index(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001367 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001368 self._w, 'index', index))
1369 def insert(self, index, string):
1370 self.tk.call(self._w, 'insert', index, string)
1371 def scan_mark(self, x):
1372 self.tk.call(self._w, 'scan', 'mark', x)
1373 def scan_dragto(self, x):
1374 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001375 def selection_adjust(self, index):
1376 self.tk.call(self._w, 'selection', 'adjust', index)
1377 select_adjust = selection_adjust
1378 def selection_clear(self):
1379 self.tk.call(self._w, 'selection', 'clear')
1380 select_clear = selection_clear
1381 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001382 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001383 select_from = selection_from
1384 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001385 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001386 self.tk.call(self._w, 'selection', 'present'))
1387 select_present = selection_present
1388 def selection_range(self, start, end):
1389 self.tk.call(self._w, 'selection', 'range', start, end)
1390 select_range = selection_range
1391 def selection_to(self, index):
1392 self.tk.call(self._w, 'selection', 'to', index)
1393 select_to = selection_to
1394 def xview(self, index):
1395 self.tk.call(self._w, 'xview', index)
1396 def xview_moveto(self, fraction):
1397 self.tk.call(self._w, 'xview', 'moveto', fraction)
1398 def xview_scroll(self, number, what):
1399 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001400
1401class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001402 def __init__(self, master=None, cnf={}, **kw):
1403 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001404 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001405 if cnf.has_key('class_'):
1406 extra = ('-class', cnf['class_'])
1407 del cnf['class_']
1408 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001409 extra = ('-class', cnf['class'])
1410 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001411 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001412
1413class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001414 def __init__(self, master=None, cnf={}, **kw):
1415 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001416
Guido van Rossum18468821994-06-20 07:49:28 +00001417class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001418 def __init__(self, master=None, cnf={}, **kw):
1419 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001420 def activate(self, index):
1421 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001422 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001423 return self._getints(
1424 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001425 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001426 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001427 return self.tk.splitlist(self.tk.call(
1428 self._w, 'curselection'))
1429 def delete(self, first, last=None):
1430 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001431 def get(self, first, last=None):
1432 if last:
1433 return self.tk.splitlist(self.tk.call(
1434 self._w, 'get', first, last))
1435 else:
1436 return self.tk.call(self._w, 'get', first)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001437 def index(self, index):
1438 i = self.tk.call(self._w, 'index', index)
1439 if i == 'none': return None
1440 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001441 def insert(self, index, *elements):
Guido van Rossumf9756991998-04-29 21:57:08 +00001442 self.tk.call((self._w, 'insert', index) + elements)
Guido van Rossum18468821994-06-20 07:49:28 +00001443 def nearest(self, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001444 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001445 self._w, 'nearest', y))
1446 def scan_mark(self, x, y):
1447 self.tk.call(self._w, 'scan', 'mark', x, y)
1448 def scan_dragto(self, x, y):
1449 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001450 def see(self, index):
1451 self.tk.call(self._w, 'see', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001452 def selection_anchor(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001453 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001454 select_anchor = selection_anchor
1455 def selection_clear(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001456 self.tk.call(self._w,
1457 'selection', 'clear', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001458 select_clear = selection_clear
1459 def selection_includes(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001460 return self.tk.getboolean(self.tk.call(
1461 self._w, 'selection', 'includes', index))
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001462 select_includes = selection_includes
1463 def selection_set(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001464 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001465 select_set = selection_set
Guido van Rossum18468821994-06-20 07:49:28 +00001466 def size(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001467 return getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001468 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001469 if not what:
1470 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001471 self.tk.call((self._w, 'xview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001472 def xview_moveto(self, fraction):
1473 self.tk.call(self._w, 'xview', 'moveto', fraction)
1474 def xview_scroll(self, number, what):
1475 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001476 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001477 if not what:
1478 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001479 self.tk.call((self._w, 'yview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001480 def yview_moveto(self, fraction):
1481 self.tk.call(self._w, 'yview', 'moveto', fraction)
1482 def yview_scroll(self, number, what):
1483 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001484
1485class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001486 def __init__(self, master=None, cnf={}, **kw):
1487 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001488 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001489 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001490 def tk_mbPost(self):
1491 self.tk.call('tk_mbPost', self._w)
1492 def tk_mbUnpost(self):
1493 self.tk.call('tk_mbUnpost')
1494 def tk_traverseToMenu(self, char):
1495 self.tk.call('tk_traverseToMenu', self._w, char)
1496 def tk_traverseWithinMenu(self, char):
1497 self.tk.call('tk_traverseWithinMenu', self._w, char)
1498 def tk_getMenuButtons(self):
1499 return self.tk.call('tk_getMenuButtons', self._w)
1500 def tk_nextMenu(self, count):
1501 self.tk.call('tk_nextMenu', count)
1502 def tk_nextMenuEntry(self, count):
1503 self.tk.call('tk_nextMenuEntry', count)
1504 def tk_invokeMenu(self):
1505 self.tk.call('tk_invokeMenu', self._w)
1506 def tk_firstMenu(self):
1507 self.tk.call('tk_firstMenu', self._w)
1508 def tk_mbButtonDown(self):
1509 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001510 def tk_popup(self, x, y, entry=""):
1511 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001512 def activate(self, index):
1513 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001514 def add(self, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001515 self.tk.call((self._w, 'add', itemType) +
1516 self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001517 def add_cascade(self, cnf={}, **kw):
1518 self.add('cascade', cnf or kw)
1519 def add_checkbutton(self, cnf={}, **kw):
1520 self.add('checkbutton', cnf or kw)
1521 def add_command(self, cnf={}, **kw):
1522 self.add('command', cnf or kw)
1523 def add_radiobutton(self, cnf={}, **kw):
1524 self.add('radiobutton', cnf or kw)
1525 def add_separator(self, cnf={}, **kw):
1526 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001527 def insert(self, index, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001528 self.tk.call((self._w, 'insert', index, itemType) +
1529 self._options(cnf, kw))
Guido van Rossum2caac731996-09-05 16:46:31 +00001530 def insert_cascade(self, index, cnf={}, **kw):
1531 self.insert(index, 'cascade', cnf or kw)
1532 def insert_checkbutton(self, index, cnf={}, **kw):
1533 self.insert(index, 'checkbutton', cnf or kw)
1534 def insert_command(self, index, cnf={}, **kw):
1535 self.insert(index, 'command', cnf or kw)
1536 def insert_radiobutton(self, index, cnf={}, **kw):
1537 self.insert(index, 'radiobutton', cnf or kw)
1538 def insert_separator(self, index, cnf={}, **kw):
1539 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001540 def delete(self, index1, index2=None):
1541 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001542 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001543 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001544 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001545 if cnf is None and not kw:
1546 cnf = {}
Guido van Rossumf9756991998-04-29 21:57:08 +00001547 for x in self.tk.split(self.tk.call(
1548 (self._w, 'entryconfigure', index))):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001549 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1550 return cnf
1551 if type(cnf) == StringType and not kw:
Guido van Rossumf9756991998-04-29 21:57:08 +00001552 x = self.tk.split(self.tk.call(
1553 (self._w, 'entryconfigure', index, '-'+cnf)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001554 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001555 self.tk.call((self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001556 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001557 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001558 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001559 i = self.tk.call(self._w, 'index', index)
1560 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00001561 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001562 def invoke(self, index):
1563 return self.tk.call(self._w, 'invoke', index)
1564 def post(self, x, y):
1565 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001566 def type(self, index):
1567 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001568 def unpost(self):
1569 self.tk.call(self._w, 'unpost')
1570 def yposition(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001571 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001572 self._w, 'yposition', index))
1573
1574class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001575 def __init__(self, master=None, cnf={}, **kw):
1576 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001577
1578class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001579 def __init__(self, master=None, cnf={}, **kw):
1580 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001581
1582class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001583 def __init__(self, master=None, cnf={}, **kw):
1584 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001585 def deselect(self):
1586 self.tk.call(self._w, 'deselect')
1587 def flash(self):
1588 self.tk.call(self._w, 'flash')
1589 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001590 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001591 def select(self):
1592 self.tk.call(self._w, 'select')
1593
1594class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001595 def __init__(self, master=None, cnf={}, **kw):
1596 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001597 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001598 value = self.tk.call(self._w, 'get')
1599 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00001600 return getint(value)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00001601 except ValueError:
Guido van Rossum0132f691998-04-30 17:50:36 +00001602 return getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001603 def set(self, value):
1604 self.tk.call(self._w, 'set', value)
Guido van Rossumb4750db1998-08-11 19:07:14 +00001605 def coords(self, value=None):
1606 return self._getints(self.tk.call(self._w, 'coords', value))
1607 def identify(self, x, y):
1608 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001609
1610class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001611 def __init__(self, master=None, cnf={}, **kw):
1612 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001613 def activate(self, index):
1614 self.tk.call(self._w, 'activate', index)
1615 def delta(self, deltax, deltay):
Guido van Rossum0132f691998-04-30 17:50:36 +00001616 return getdouble(
1617 self.tk.call(self._w, 'delta', deltax, deltay))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001618 def fraction(self, x, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001619 return getdouble(self.tk.call(self._w, 'fraction', x, y))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001620 def identify(self, x, y):
1621 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001622 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001623 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001624 def set(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001625 self.tk.call((self._w, 'set') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001626
1627class Text(Widget):
Guido van Rossumdfaac4d1998-12-21 18:25:03 +00001628 # XXX Add dump()
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001629 def __init__(self, master=None, cnf={}, **kw):
1630 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001631 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001632 return self._getints(
1633 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001634 def tk_textSelectTo(self, index):
1635 self.tk.call('tk_textSelectTo', self._w, index)
1636 def tk_textBackspace(self):
1637 self.tk.call('tk_textBackspace', self._w)
1638 def tk_textIndexCloser(self, a, b, c):
1639 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1640 def tk_textResetAnchor(self, index):
1641 self.tk.call('tk_textResetAnchor', self._w, index)
1642 def compare(self, index1, op, index2):
1643 return self.tk.getboolean(self.tk.call(
1644 self._w, 'compare', index1, op, index2))
1645 def debug(self, boolean=None):
1646 return self.tk.getboolean(self.tk.call(
1647 self._w, 'debug', boolean))
1648 def delete(self, index1, index2=None):
1649 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001650 def dlineinfo(self, index):
1651 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001652 def get(self, index1, index2=None):
1653 return self.tk.call(self._w, 'get', index1, index2)
Guido van Rossumc86b7c61998-08-31 16:54:33 +00001654 # (Image commands are new in 8.0)
1655 def image_cget(self, index, option):
1656 if option[:1] != "-":
1657 option = "-" + option
1658 if option[-1:] == "_":
1659 option = option[:-1]
1660 return self.tk.call(self._w, "image", "cget", index, option)
1661 def image_configure(self, index, cnf={}, **kw):
1662 if not cnf and not kw:
1663 cnf = {}
1664 for x in self.tk.split(
1665 self.tk.call(
1666 self._w, "image", "configure", index)):
1667 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1668 return cnf
1669 apply(self.tk.call,
1670 (self._w, "image", "configure", index)
1671 + self._options(cnf, kw))
1672 def image_create(self, index, cnf={}, **kw):
1673 return apply(self.tk.call,
1674 (self._w, "image", "create", index)
1675 + self._options(cnf, kw))
1676 def image_names(self):
1677 return self.tk.call(self._w, "image", "names")
Guido van Rossum18468821994-06-20 07:49:28 +00001678 def index(self, index):
1679 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001680 def insert(self, index, chars, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001681 self.tk.call((self._w, 'insert', index, chars) + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001682 def mark_gravity(self, markName, direction=None):
Guido van Rossumf9756991998-04-29 21:57:08 +00001683 return self.tk.call(
1684 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001685 def mark_names(self):
1686 return self.tk.splitlist(self.tk.call(
1687 self._w, 'mark', 'names'))
1688 def mark_set(self, markName, index):
1689 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001690 def mark_unset(self, *markNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001691 self.tk.call((self._w, 'mark', 'unset') + markNames)
Guido van Rossum1a03cf51999-06-21 14:13:30 +00001692 def mark_next(self, index):
1693 return self.tk.call(self._w, 'mark', 'next', index) or None
1694 def mark_previous(self, index):
1695 return self.tk.call(self._w, 'mark', 'previous', index) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +00001696 def scan_mark(self, x, y):
1697 self.tk.call(self._w, 'scan', 'mark', x, y)
1698 def scan_dragto(self, x, y):
1699 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001700 def search(self, pattern, index, stopindex=None,
1701 forwards=None, backwards=None, exact=None,
1702 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001703 args = [self._w, 'search']
1704 if forwards: args.append('-forwards')
1705 if backwards: args.append('-backwards')
1706 if exact: args.append('-exact')
1707 if regexp: args.append('-regexp')
1708 if nocase: args.append('-nocase')
1709 if count: args.append('-count'); args.append(count)
1710 if pattern[0] == '-': args.append('--')
1711 args.append(pattern)
1712 args.append(index)
1713 if stopindex: args.append(stopindex)
Guido van Rossumf9756991998-04-29 21:57:08 +00001714 return self.tk.call(tuple(args))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001715 def see(self, index):
1716 self.tk.call(self._w, 'see', index)
Guido van Rossumfa8c3ea1999-06-01 13:57:15 +00001717 def tag_add(self, tagName, index1, *args):
Guido van Rossum18468821994-06-20 07:49:28 +00001718 self.tk.call(
Guido van Rossumfa8c3ea1999-06-01 13:57:15 +00001719 (self._w, 'tag', 'add', tagName, index1) + args)
Guido van Rossum117a5a81998-03-27 21:26:51 +00001720 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001721 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001722 if funcid:
1723 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001724 def tag_bind(self, tagName, sequence, func, add=None):
1725 return self._bind((self._w, 'tag', 'bind', tagName),
1726 sequence, func, add)
1727 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001728 if option[:1] != '-':
1729 option = '-' + option
1730 if option[-1:] == '_':
1731 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001732 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001733 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001734 if type(cnf) == StringType:
1735 x = self.tk.split(self.tk.call(
1736 self._w, 'tag', 'configure', tagName, '-'+cnf))
1737 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001738 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001739 (self._w, 'tag', 'configure', tagName)
1740 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001741 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001742 def tag_delete(self, *tagNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001743 self.tk.call((self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001744 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001745 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001746 def tag_names(self, index=None):
1747 return self.tk.splitlist(
1748 self.tk.call(self._w, 'tag', 'names', index))
1749 def tag_nextrange(self, tagName, index1, index2=None):
1750 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001751 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001752 def tag_prevrange(self, tagName, index1, index2=None):
1753 return self.tk.splitlist(self.tk.call(
1754 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001755 def tag_raise(self, tagName, aboveThis=None):
1756 self.tk.call(
1757 self._w, 'tag', 'raise', tagName, aboveThis)
1758 def tag_ranges(self, tagName):
1759 return self.tk.splitlist(self.tk.call(
1760 self._w, 'tag', 'ranges', tagName))
1761 def tag_remove(self, tagName, index1, index2=None):
1762 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001763 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001764 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001765 if option[:1] != '-':
1766 option = '-' + option
1767 if option[-1:] == '_':
1768 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001769 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001770 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001771 if type(cnf) == StringType:
1772 x = self.tk.split(self.tk.call(
1773 self._w, 'window', 'configure',
1774 index, '-'+cnf))
1775 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001776 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001777 (self._w, 'window', 'configure', index)
1778 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001779 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001780 def window_create(self, index, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001781 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001782 (self._w, 'window', 'create', index)
1783 + self._options(cnf, kw))
1784 def window_names(self):
1785 return self.tk.splitlist(
1786 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001787 def xview(self, *what):
1788 if not what:
1789 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001790 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001791 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001792 if not what:
1793 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001794 self.tk.call((self._w, 'yview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001795 def yview_pickplace(self, *what):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001796 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00001797
Guido van Rossum28574b51996-10-21 15:16:51 +00001798class _setit:
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001799 def __init__(self, var, value, callback=None):
Guido van Rossum28574b51996-10-21 15:16:51 +00001800 self.__value = value
1801 self.__var = var
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001802 self.__callback = callback
Guido van Rossum28574b51996-10-21 15:16:51 +00001803 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001804 self.__var.set(self.__value)
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001805 if self.__callback:
1806 apply(self.__callback, (self.__value,)+args)
Guido van Rossum28574b51996-10-21 15:16:51 +00001807
1808class OptionMenu(Menubutton):
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001809 def __init__(self, master, variable, value, *values, **kwargs):
Guido van Rossum28574b51996-10-21 15:16:51 +00001810 kw = {"borderwidth": 2, "textvariable": variable,
1811 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1812 "highlightthickness": 2}
1813 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001814 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001815 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1816 self.menuname = menu._w
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001817 # 'command' is the only supported keyword
1818 callback = kwargs.get('command')
Guido van Rossum0ba33002000-02-27 15:35:47 +00001819 if kwargs.has_key('command'):
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001820 del kwargs['command']
1821 if kwargs:
1822 raise TclError, 'unknown option -'+kwargs.keys()[0]
1823 menu.add_command(label=value,
1824 command=_setit(variable, value, callback))
Guido van Rossum28574b51996-10-21 15:16:51 +00001825 for v in values:
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00001826 menu.add_command(label=v,
1827 command=_setit(variable, v, callback))
Guido van Rossum28574b51996-10-21 15:16:51 +00001828 self["menu"] = menu
1829
1830 def __getitem__(self, name):
1831 if name == 'menu':
1832 return self.__menu
1833 return Widget.__getitem__(self, name)
1834
1835 def destroy(self):
1836 Menubutton.destroy(self)
1837 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001838
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001839class Image:
Guido van Rossumc4570481998-03-20 20:45:49 +00001840 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001841 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00001842 if not master:
1843 master = _default_root
1844 if not master:
1845 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001846 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001847 if not name:
1848 name = `id(self)`
1849 # The following is needed for systems where id(x)
1850 # can return a negative number, such as Linux/m68k:
1851 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001852 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1853 elif kw: cnf = kw
1854 options = ()
1855 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001856 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001857 v = self._register(v)
1858 options = options + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001859 self.tk.call(('image', 'create', imgtype, name,) + options)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001860 self.name = name
1861 def __str__(self): return self.name
1862 def __del__(self):
1863 if self.name:
Guido van Rossumc55b0ca1999-02-08 15:26:49 +00001864 try:
1865 self.tk.call('image', 'delete', self.name)
1866 except TclError:
1867 # May happen if the root was destroyed
1868 pass
Guido van Rossum71b1a901995-09-18 21:54:35 +00001869 def __setitem__(self, key, value):
1870 self.tk.call(self.name, 'configure', '-'+key, value)
1871 def __getitem__(self, key):
1872 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001873 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001874 res = ()
1875 for k, v in _cnfmerge(kw).items():
1876 if v is not None:
1877 if k[-1] == '_': k = k[:-1]
1878 if callable(v):
1879 v = self._register(v)
1880 res = res + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001881 self.tk.call((self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001882 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001883 def height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001884 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001885 self.tk.call('image', 'height', self.name))
1886 def type(self):
1887 return self.tk.call('image', 'type', self.name)
1888 def width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001889 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001890 self.tk.call('image', 'width', self.name))
1891
1892class PhotoImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001893 def __init__(self, name=None, cnf={}, master=None, **kw):
1894 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001895 def blank(self):
1896 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001897 def cget(self, option):
1898 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001899 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001900 def __getitem__(self, key):
1901 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001902 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001903 def copy(self):
1904 destImage = PhotoImage()
1905 self.tk.call(destImage, 'copy', self.name)
1906 return destImage
1907 def zoom(self,x,y=''):
1908 destImage = PhotoImage()
1909 if y=='': y=x
1910 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1911 return destImage
1912 def subsample(self,x,y=''):
1913 destImage = PhotoImage()
1914 if y=='': y=x
1915 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1916 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001917 def get(self, x, y):
1918 return self.tk.call(self.name, 'get', x, y)
1919 def put(self, data, to=None):
1920 args = (self.name, 'put', data)
1921 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001922 if to[0] == '-to':
1923 to = to[1:]
1924 args = args + ('-to',) + tuple(to)
Guido van Rossumf9756991998-04-29 21:57:08 +00001925 self.tk.call(args)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001926 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001927 def write(self, filename, format=None, from_coords=None):
1928 args = (self.name, 'write', filename)
1929 if format:
1930 args = args + ('-format', format)
1931 if from_coords:
1932 args = args + ('-from',) + tuple(from_coords)
Guido van Rossumf9756991998-04-29 21:57:08 +00001933 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001934
1935class BitmapImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001936 def __init__(self, name=None, cnf={}, master=None, **kw):
1937 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001938
1939def image_names(): return _default_root.tk.call('image', 'names')
1940def image_types(): return _default_root.tk.call('image', 'types')
1941
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001942######################################################################
1943# Extensions:
1944
1945class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001946 def __init__(self, master=None, cnf={}, **kw):
1947 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001948 self.bind('<Any-Enter>', self.tkButtonEnter)
1949 self.bind('<Any-Leave>', self.tkButtonLeave)
1950 self.bind('<1>', self.tkButtonDown)
1951 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001952
1953class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001954 def __init__(self, master=None, cnf={}, **kw):
1955 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001956 self.bind('<Any-Enter>', self.tkButtonEnter)
1957 self.bind('<Any-Leave>', self.tkButtonLeave)
1958 self.bind('<1>', self.tkButtonDown)
1959 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1960 self['fg'] = self['bg']
1961 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001962
Guido van Rossumc417ef81996-08-21 23:38:59 +00001963######################################################################
1964# Test:
1965
1966def _test():
1967 root = Tk()
Guido van Rossum2ab90822000-03-30 23:19:44 +00001968 text = "This is Tcl/Tk version %s" % TclVersion
1969 if TclVersion >= 8.1:
1970 text = text + u"\nThis should be a cedilla: \347"
1971 label = Label(root, text=text)
Guido van Rossumc417ef81996-08-21 23:38:59 +00001972 label.pack()
1973 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001974 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001975 text="[%s]" % root.test['text']))
1976 test.pack()
1977 root.test = test
1978 quit = Button(root, text="QUIT", command=root.destroy)
1979 quit.pack()
Guido van Rossum268824e1998-06-19 04:34:19 +00001980 # The following three commands are needed so the window pops
1981 # up on top on Windows...
1982 root.iconify()
1983 root.update()
1984 root.deiconify()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001985 root.mainloop()
1986
1987if __name__ == '__main__':
1988 _test()