blob: e50a412ae9ace23317612da44a87cdda9a75cae9 [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
42def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000043 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000044 return cnfs
45 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000046 return cnfs
47 else:
48 cnf = {}
49 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000050 try:
51 cnf.update(c)
52 except (AttributeError, TypeError), msg:
53 print "_cnfmerge: fallback due to:", msg
54 for k, v in c.items():
55 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000056 return cnf
57
58class Event:
59 pass
60
Guido van Rossumc4570481998-03-20 20:45:49 +000061_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +000062_default_root = None
63
Guido van Rossumc4570481998-03-20 20:45:49 +000064def NoDefaultRoot():
65 global _support_default_root
66 _support_default_root = 0
Guido van Rossumda654501998-10-06 19:06:27 +000067 global _default_root
68 _default_root = None
Guido van Rossumc4570481998-03-20 20:45:49 +000069 del _default_root
70
Guido van Rossum45853db1994-06-20 12:19:19 +000071def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000072 pass
73
Guido van Rossum97aeca11994-07-07 13:12:12 +000074def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000075 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000076
Guido van Rossumaec5dc91994-06-27 07:55:12 +000077_varnum = 0
78class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000079 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000080 def __init__(self, master=None):
Guido van Rossumaec5dc91994-06-27 07:55:12 +000081 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +000082 if not master:
83 master = _default_root
84 self._master = master
85 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +000086 self._name = 'PY_VAR' + `_varnum`
87 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000088 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000089 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000090 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000091 def __str__(self):
92 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000093 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000094 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +000095 def trace_variable(self, mode, callback):
96 cbname = self._master._register(callback)
97 self._tk.call("trace", "variable", self._name, mode, cbname)
98 return cbname
99 trace = trace_variable
100 def trace_vdelete(self, mode, cbname):
101 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Guido van Rossum0001a111998-02-19 21:20:30 +0000102 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +0000103 def trace_vinfo(self):
104 return map(self._tk.split, self._tk.splitlist(
105 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000106
107class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000108 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000109 def __init__(self, master=None):
110 Variable.__init__(self, master)
111 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000112 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000113
114class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000115 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000116 def __init__(self, master=None):
117 Variable.__init__(self, master)
118 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000119 return getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000120
121class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000122 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000123 def __init__(self, master=None):
124 Variable.__init__(self, master)
125 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000126 return getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000127
128class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000129 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000130 def __init__(self, master=None):
131 Variable.__init__(self, master)
132 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000133 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000134
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000135def mainloop(n=0):
136 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000137
Guido van Rossum0132f691998-04-30 17:50:36 +0000138getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000139
Guido van Rossum0132f691998-04-30 17:50:36 +0000140getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000141
142def getboolean(s):
143 return _default_root.tk.getboolean(s)
144
Guido van Rossum368e06b1997-11-07 20:38:49 +0000145# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000146class Misc:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000147 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000148 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000149 def destroy(self):
150 if self._tclCommands is not None:
151 for name in self._tclCommands:
152 #print '- Tkinter: deleted command', name
153 self.tk.deletecommand(name)
154 self._tclCommands = None
155 def deletecommand(self, name):
156 #print '- Tkinter: deleted command', name
157 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000158 try:
159 self._tclCommands.remove(name)
160 except ValueError:
161 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000162 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000163 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000164 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000165 def tk_bisque(self):
166 self.tk.call('tk_bisque')
167 def tk_setPalette(self, *args, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000168 self.tk.call(('tk_setPalette',)
Fred Drake3faf9b41996-10-04 19:23:04 +0000169 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000170 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000171 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000172 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000173 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000174 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000175 def wait_window(self, window=None):
176 if window == None:
177 window = self
178 self.tk.call('tkwait', 'window', window._w)
179 def wait_visibility(self, window=None):
180 if window == None:
181 window = self
182 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000183 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000184 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000185 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000186 return self.tk.getvar(name)
Guido van Rossum0132f691998-04-30 17:50:36 +0000187 getint = int
188 getdouble = float
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000189 def getboolean(self, s):
190 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000191 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000192 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000193 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000194 def focus_force(self):
195 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000196 def focus_get(self):
197 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000198 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000199 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000200 def focus_displayof(self):
201 name = self.tk.call('focus', '-displayof', self._w)
202 if name == 'none' or not name: return None
203 return self._nametowidget(name)
204 def focus_lastfor(self):
205 name = self.tk.call('focus', '-lastfor', self._w)
206 if name == 'none' or not name: return None
207 return self._nametowidget(name)
208 def tk_focusFollowsMouse(self):
209 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000210 def tk_focusNext(self):
211 name = self.tk.call('tk_focusNext', self._w)
212 if not name: return None
213 return self._nametowidget(name)
214 def tk_focusPrev(self):
215 name = self.tk.call('tk_focusPrev', self._w)
216 if not name: return None
217 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000218 def after(self, ms, func=None, *args):
219 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000220 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000221 self.tk.call('after', ms)
222 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000223 # XXX Disgusting hack to clean up after calling func
224 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000225 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000226 try:
227 apply(func, args)
228 finally:
Guido van Rossum0c920001998-09-14 19:06:39 +0000229 try:
230 self.deletecommand(tmp[0])
231 except TclError:
232 pass
Guido van Rossum08a40381994-06-21 11:44:21 +0000233 name = self._register(callit)
234 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000235 return self.tk.call('after', ms, name)
236 def after_idle(self, func, *args):
237 return apply(self.after, ('idle', func) + args)
238 def after_cancel(self, id):
239 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000240 def bell(self, displayof=0):
Guido van Rossumf9756991998-04-29 21:57:08 +0000241 self.tk.call(('bell',) + self._displayof(displayof))
Fred Drake3c602d71996-09-27 14:06:54 +0000242 # Clipboard handling:
243 def clipboard_clear(self, **kw):
244 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000245 self.tk.call(('clipboard', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000246 def clipboard_append(self, string, **kw):
247 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000248 self.tk.call(('clipboard', 'append') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000249 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000250 # XXX grab current w/o window argument
251 def grab_current(self):
252 name = self.tk.call('grab', 'current', self._w)
253 if not name: return None
254 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000255 def grab_release(self):
256 self.tk.call('grab', 'release', self._w)
257 def grab_set(self):
258 self.tk.call('grab', 'set', self._w)
259 def grab_set_global(self):
260 self.tk.call('grab', 'set', '-global', self._w)
261 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000262 status = self.tk.call('grab', 'status', self._w)
263 if status == 'none': status = None
264 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000265 def lower(self, belowThis=None):
266 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000267 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000268 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000269 def option_clear(self):
270 self.tk.call('option', 'clear')
271 def option_get(self, name, className):
272 return self.tk.call('option', 'get', self._w, name, className)
273 def option_readfile(self, fileName, priority = None):
274 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000275 def selection_clear(self, **kw):
276 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000277 self.tk.call(('selection', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000278 def selection_get(self, **kw):
279 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000280 return self.tk.call(('selection', 'get') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000281 def selection_handle(self, command, **kw):
282 name = self._register(command)
Guido van Rossumf9756991998-04-29 21:57:08 +0000283 self.tk.call(('selection', 'handle') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000284 + (self._w, name))
285 def selection_own(self, **kw):
286 "Become owner of X selection."
Guido van Rossumf9756991998-04-29 21:57:08 +0000287 self.tk.call(('selection', 'own') +
288 self._options(kw) + (self._w,))
Fred Drake3c602d71996-09-27 14:06:54 +0000289 def selection_own_get(self, **kw):
290 "Find owner of X selection."
291 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000292 name = self.tk.call(('selection', 'own') + self._options(kw))
Guido van Rossum76f587b1997-01-21 23:22:03 +0000293 if not name: return None
294 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000295 def send(self, interp, cmd, *args):
Guido van Rossumf9756991998-04-29 21:57:08 +0000296 return self.tk.call(('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000297 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000298 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000299 def tkraise(self, aboveThis=None):
300 self.tk.call('raise', self._w, aboveThis)
301 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000302 def colormodel(self, value=None):
303 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000304 def winfo_atom(self, name, displayof=0):
305 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Guido van Rossum0132f691998-04-30 17:50:36 +0000306 return getint(self.tk.call(args))
Fred Drake3c602d71996-09-27 14:06:54 +0000307 def winfo_atomname(self, id, displayof=0):
308 args = ('winfo', 'atomname') \
309 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000310 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000311 def winfo_cells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000312 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000313 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000314 def winfo_children(self):
315 return map(self._nametowidget,
316 self.tk.splitlist(self.tk.call(
317 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000318 def winfo_class(self):
319 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000320 def winfo_colormapfull(self):
321 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000322 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000323 def winfo_containing(self, rootX, rootY, displayof=0):
324 args = ('winfo', 'containing') \
325 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumf9756991998-04-29 21:57:08 +0000326 name = self.tk.call(args)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000327 if not name: return None
328 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000329 def winfo_depth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000330 return getint(self.tk.call('winfo', 'depth', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000331 def winfo_exists(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000332 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000333 self.tk.call('winfo', 'exists', self._w))
334 def winfo_fpixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000335 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000336 'winfo', 'fpixels', self._w, number))
337 def winfo_geometry(self):
338 return self.tk.call('winfo', 'geometry', self._w)
339 def winfo_height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000340 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000341 self.tk.call('winfo', 'height', self._w))
342 def winfo_id(self):
Guido van Rossumcef4c841998-06-19 04:35:45 +0000343 return self.tk.getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000344 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000345 def winfo_interps(self, displayof=0):
346 args = ('winfo', 'interps') + self._displayof(displayof)
Guido van Rossumf9756991998-04-29 21:57:08 +0000347 return self.tk.splitlist(self.tk.call(args))
Guido van Rossum18468821994-06-20 07:49:28 +0000348 def winfo_ismapped(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000349 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000350 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000351 def winfo_manager(self):
352 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000353 def winfo_name(self):
354 return self.tk.call('winfo', 'name', self._w)
355 def winfo_parent(self):
356 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000357 def winfo_pathname(self, id, displayof=0):
358 args = ('winfo', 'pathname') \
359 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000360 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000361 def winfo_pixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000362 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000363 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000364 def winfo_pointerx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000365 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000366 self.tk.call('winfo', 'pointerx', self._w))
367 def winfo_pointerxy(self):
368 return self._getints(
369 self.tk.call('winfo', 'pointerxy', self._w))
370 def winfo_pointery(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', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000373 def winfo_reqheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000374 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000375 self.tk.call('winfo', 'reqheight', self._w))
376 def winfo_reqwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000377 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000378 self.tk.call('winfo', 'reqwidth', self._w))
379 def winfo_rgb(self, color):
380 return self._getints(
381 self.tk.call('winfo', 'rgb', self._w, color))
382 def winfo_rootx(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', 'rootx', self._w))
385 def winfo_rooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000386 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000387 self.tk.call('winfo', 'rooty', self._w))
388 def winfo_screen(self):
389 return self.tk.call('winfo', 'screen', self._w)
390 def winfo_screencells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000391 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000392 self.tk.call('winfo', 'screencells', self._w))
393 def winfo_screendepth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000394 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000395 self.tk.call('winfo', 'screendepth', self._w))
396 def winfo_screenheight(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', 'screenheight', self._w))
399 def winfo_screenmmheight(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', 'screenmmheight', self._w))
402 def winfo_screenmmwidth(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', 'screenmmwidth', self._w))
405 def winfo_screenvisual(self):
406 return self.tk.call('winfo', 'screenvisual', self._w)
407 def winfo_screenwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000408 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000409 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000410 def winfo_server(self):
411 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000412 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000413 return self._nametowidget(self.tk.call(
414 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000415 def winfo_viewable(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000416 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000417 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000418 def winfo_visual(self):
419 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000420 def winfo_visualid(self):
421 return self.tk.call('winfo', 'visualid', self._w)
422 def winfo_visualsavailable(self, includeids=0):
423 data = self.tk.split(
424 self.tk.call('winfo', 'visualsavailable', self._w,
425 includeids and 'includeids' or None))
426 def parseitem(x, self=self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000427 return x[:1] + tuple(map(getint, x[1:]))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000428 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000429 def winfo_vrootheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000430 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000431 self.tk.call('winfo', 'vrootheight', self._w))
432 def winfo_vrootwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000433 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000434 self.tk.call('winfo', 'vrootwidth', self._w))
435 def winfo_vrootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000436 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000437 self.tk.call('winfo', 'vrootx', self._w))
438 def winfo_vrooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000439 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000440 self.tk.call('winfo', 'vrooty', self._w))
441 def winfo_width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000442 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000443 self.tk.call('winfo', 'width', self._w))
444 def winfo_x(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000445 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000446 self.tk.call('winfo', 'x', self._w))
447 def winfo_y(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000448 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000449 self.tk.call('winfo', 'y', self._w))
450 def update(self):
451 self.tk.call('update')
452 def update_idletasks(self):
453 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000454 def bindtags(self, tagList=None):
455 if tagList is None:
456 return self.tk.splitlist(
457 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000458 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000459 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000460 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum88b63b81998-06-25 18:54:49 +0000461 if type(func) is StringType:
462 self.tk.call(what + (sequence, func))
463 elif func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000464 funcid = self._register(func, self._substitute,
465 needcleanup)
Guido van Rossumdc593401998-04-29 22:16:57 +0000466 cmd = ('%sif {"[%s %s]" == "break"} break\n'
467 %
468 (add and '+' or '',
469 funcid,
470 _string.join(self._subst_format)))
Guido van Rossumf9756991998-04-29 21:57:08 +0000471 self.tk.call(what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000472 return funcid
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000473 elif sequence:
Guido van Rossumf9756991998-04-29 21:57:08 +0000474 return self.tk.call(what + (sequence,))
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000475 else:
476 return self.tk.splitlist(self.tk.call(what))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000477 def bind(self, sequence=None, func=None, add=None):
478 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000479 def unbind(self, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +0000480 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000481 if funcid:
482 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000483 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000484 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000485 def unbind_all(self, sequence):
486 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000487 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000488 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000489 def unbind_class(self, className, sequence):
490 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000491 def mainloop(self, n=0):
492 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000493 def quit(self):
494 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000495 def _getints(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000496 if string:
497 return tuple(map(getint, self.tk.splitlist(string)))
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000498 def _getdoubles(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000499 if string:
500 return tuple(map(getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000501 def _getboolean(self, string):
502 if string:
503 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000504 def _displayof(self, displayof):
505 if displayof:
506 return ('-displayof', displayof)
507 if displayof is None:
508 return ('-displayof', self._w)
509 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000510 def _options(self, cnf, kw = None):
511 if kw:
512 cnf = _cnfmerge((cnf, kw))
513 else:
514 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000515 res = ()
516 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000517 if v is not None:
518 if k[-1] == '_': k = k[:-1]
519 if callable(v):
520 v = self._register(v)
521 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000522 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000523 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000524 w = self
525 if name[0] == '.':
526 w = w._root()
527 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000528 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000529 while name:
530 i = find(name, '.')
531 if i >= 0:
532 name, tail = name[:i], name[i+1:]
533 else:
534 tail = ''
535 w = w.children[name]
536 name = tail
537 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000538 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000539 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000540 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000541 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000542 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000543 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000544 except AttributeError:
545 pass
546 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000547 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000548 except AttributeError:
549 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000550 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000551 if needcleanup:
552 if self._tclCommands is None:
553 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +0000554 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000555 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000556 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000557 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000558 def _root(self):
559 w = self
560 while w.master: w = w.master
561 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000562 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000563 '%s', '%t', '%w', '%x', '%y',
564 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
565 def _substitute(self, *args):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000566 if len(args) != len(self._subst_format): return args
Guido van Rossum0132f691998-04-30 17:50:36 +0000567 getboolean = self.tk.getboolean
568 getint = int
Guido van Rossum45853db1994-06-20 12:19:19 +0000569 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
570 # Missing: (a, c, d, m, o, v, B, R)
571 e = Event()
Guido van Rossum0132f691998-04-30 17:50:36 +0000572 e.serial = getint(nsign)
573 e.num = getint(b)
574 try: e.focus = getboolean(f)
Guido van Rossum45853db1994-06-20 12:19:19 +0000575 except TclError: pass
Guido van Rossum0132f691998-04-30 17:50:36 +0000576 e.height = getint(h)
577 e.keycode = getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000578 # For Visibility events, event state is a string and
579 # not an integer:
580 try:
Guido van Rossum0132f691998-04-30 17:50:36 +0000581 e.state = getint(s)
Guido van Rossumfe02efd1998-06-09 02:37:45 +0000582 except ValueError:
Guido van Rossum36269991996-05-16 17:11:27 +0000583 e.state = s
Guido van Rossum0132f691998-04-30 17:50:36 +0000584 e.time = getint(t)
585 e.width = getint(w)
586 e.x = getint(x)
587 e.y = getint(y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000588 e.char = A
Guido van Rossum0132f691998-04-30 17:50:36 +0000589 try: e.send_event = getboolean(E)
Guido van Rossum45853db1994-06-20 12:19:19 +0000590 except TclError: pass
591 e.keysym = K
Guido van Rossum0132f691998-04-30 17:50:36 +0000592 e.keysym_num = getint(N)
Guido van Rossum45853db1994-06-20 12:19:19 +0000593 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +0000594 try:
595 e.widget = self._nametowidget(W)
596 except KeyError:
597 e.widget = W
Guido van Rossum0132f691998-04-30 17:50:36 +0000598 e.x_root = getint(X)
599 e.y_root = getint(Y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000600 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000601 def _report_exception(self):
602 import sys
603 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
604 root = self._root()
605 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000606 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000607 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000608 # XXX ought to generalize this so tag_config etc. can use it
609 if kw:
610 cnf = _cnfmerge((cnf, kw))
611 elif cnf:
612 cnf = _cnfmerge(cnf)
613 if cnf is None:
614 cnf = {}
615 for x in self.tk.split(
616 self.tk.call(self._w, 'configure')):
617 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
618 return cnf
619 if type(cnf) is StringType:
620 x = self.tk.split(self.tk.call(
621 self._w, 'configure', '-'+cnf))
622 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +0000623 self.tk.call((self._w, 'configure')
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000624 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000625 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000626 def cget(self, key):
627 return self.tk.call(self._w, 'cget', '-' + key)
628 __getitem__ = cget
629 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000630 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000631 def keys(self):
632 return map(lambda x: x[0][1:],
633 self.tk.split(self.tk.call(self._w, 'configure')))
634 def __str__(self):
635 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000636 # Pack methods that apply to the master
637 _noarg_ = ['_noarg_']
638 def pack_propagate(self, flag=_noarg_):
639 if flag is Misc._noarg_:
640 return self._getboolean(self.tk.call(
641 'pack', 'propagate', self._w))
642 else:
643 self.tk.call('pack', 'propagate', self._w, flag)
644 propagate = pack_propagate
645 def pack_slaves(self):
646 return map(self._nametowidget,
647 self.tk.splitlist(
648 self.tk.call('pack', 'slaves', self._w)))
649 slaves = pack_slaves
650 # Place method that applies to the master
651 def place_slaves(self):
652 return map(self._nametowidget,
653 self.tk.splitlist(
654 self.tk.call(
655 'place', 'slaves', self._w)))
656 # Grid methods that apply to the master
Barry Warsaw107e6231998-12-15 00:44:15 +0000657 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
658 args = ('grid', 'bbox', self._w)
659 if column is not None and row is not None:
660 args = args + (column, row)
661 if col2 is not None and row2 is not None:
662 args = args + (col2, row2)
663 return self._getints(apply(self.tk.call, args)) or None
664
Guido van Rossum368e06b1997-11-07 20:38:49 +0000665 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000666 def _grid_configure(self, command, index, cnf, kw):
667 if type(cnf) is StringType and not kw:
668 if cnf[-1:] == '_':
669 cnf = cnf[:-1]
670 if cnf[:1] != '-':
671 cnf = '-'+cnf
672 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000673 else:
674 options = self._options(cnf, kw)
675 if not options:
676 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000677 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000678 words = self.tk.splitlist(res)
679 dict = {}
680 for i in range(0, len(words), 2):
681 key = words[i][1:]
682 value = words[i+1]
683 if not value:
684 value = None
685 elif '.' in value:
Guido van Rossum0132f691998-04-30 17:50:36 +0000686 value = getdouble(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000687 else:
Guido van Rossum0132f691998-04-30 17:50:36 +0000688 value = getint(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000689 dict[key] = value
690 return dict
Guido van Rossumf9756991998-04-29 21:57:08 +0000691 res = self.tk.call(
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000692 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000693 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000694 if len(options) == 1:
695 if not res: return None
696 # In Tk 7.5, -width can be a float
Guido van Rossum0132f691998-04-30 17:50:36 +0000697 if '.' in res: return getdouble(res)
698 return getint(res)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000699 def grid_columnconfigure(self, index, cnf={}, **kw):
700 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000701 columnconfigure = grid_columnconfigure
702 def grid_propagate(self, flag=_noarg_):
703 if flag is Misc._noarg_:
704 return self._getboolean(self.tk.call(
705 'grid', 'propagate', self._w))
706 else:
707 self.tk.call('grid', 'propagate', self._w, flag)
708 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000709 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000710 rowconfigure = grid_rowconfigure
711 def grid_size(self):
712 return self._getints(
713 self.tk.call('grid', 'size', self._w)) or None
714 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000715 def grid_slaves(self, row=None, column=None):
716 args = ()
Guido van Rossumeb354b31999-03-16 21:54:50 +0000717 if row is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000718 args = args + ('-row', row)
Guido van Rossumeb354b31999-03-16 21:54:50 +0000719 if column is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000720 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000721 return map(self._nametowidget,
Guido van Rossumf9756991998-04-29 21:57:08 +0000722 self.tk.splitlist(self.tk.call(
723 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000724
Guido van Rossum80f8be81997-12-02 19:51:39 +0000725 # Support for the "event" command, new in Tk 4.2.
726 # By Case Roole.
727
Guido van Rossum56c04b81998-04-06 03:10:03 +0000728 def event_add(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000729 args = ('event', 'add', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000730 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000731
Guido van Rossum56c04b81998-04-06 03:10:03 +0000732 def event_delete(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000733 args = ('event', 'delete', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000734 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000735
736 def event_generate(self, sequence, **kw):
737 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +0000738 for k, v in kw.items():
739 args = args + ('-%s' % k, str(v))
Guido van Rossumf9756991998-04-29 21:57:08 +0000740 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000741
Guido van Rossum56c04b81998-04-06 03:10:03 +0000742 def event_info(self, virtual=None):
743 return self.tk.splitlist(
744 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +0000745
Guido van Rossumc2966511998-04-10 19:16:10 +0000746 # Image related commands
747
748 def image_names(self):
749 return self.tk.call('image', 'names')
750
751 def image_types(self):
752 return self.tk.call('image', 'types')
753
Guido van Rossum80f8be81997-12-02 19:51:39 +0000754
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000755class CallWrapper:
756 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000757 self.func = func
758 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000759 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000760 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000761 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000762 if self.subst:
763 args = apply(self.subst, args)
764 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000765 except SystemExit, msg:
766 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000767 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000768 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000769
Guido van Rossume365a591998-05-01 19:48:20 +0000770
Guido van Rossum18468821994-06-20 07:49:28 +0000771class Wm:
Guido van Rossume365a591998-05-01 19:48:20 +0000772 def wm_aspect(self,
Guido van Rossum18468821994-06-20 07:49:28 +0000773 minNumer=None, minDenom=None,
774 maxNumer=None, maxDenom=None):
775 return self._getints(
776 self.tk.call('wm', 'aspect', self._w,
777 minNumer, minDenom,
778 maxNumer, maxDenom))
Guido van Rossume365a591998-05-01 19:48:20 +0000779 aspect = wm_aspect
780 def wm_client(self, name=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000781 return self.tk.call('wm', 'client', self._w, name)
Guido van Rossume365a591998-05-01 19:48:20 +0000782 client = wm_client
783 def wm_colormapwindows(self, *wlist):
Fred Drake3c602d71996-09-27 14:06:54 +0000784 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
Guido van Rossumf9756991998-04-29 21:57:08 +0000785 return map(self._nametowidget, self.tk.call(args))
Guido van Rossume365a591998-05-01 19:48:20 +0000786 colormapwindows = wm_colormapwindows
787 def wm_command(self, value=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000788 return self.tk.call('wm', 'command', self._w, value)
Guido van Rossume365a591998-05-01 19:48:20 +0000789 command = wm_command
790 def wm_deiconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000791 return self.tk.call('wm', 'deiconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000792 deiconify = wm_deiconify
793 def wm_focusmodel(self, model=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000794 return self.tk.call('wm', 'focusmodel', self._w, model)
Guido van Rossume365a591998-05-01 19:48:20 +0000795 focusmodel = wm_focusmodel
796 def wm_frame(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000797 return self.tk.call('wm', 'frame', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000798 frame = wm_frame
799 def wm_geometry(self, newGeometry=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000800 return self.tk.call('wm', 'geometry', self._w, newGeometry)
Guido van Rossume365a591998-05-01 19:48:20 +0000801 geometry = wm_geometry
802 def wm_grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000803 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000804 widthInc=None, heightInc=None):
805 return self._getints(self.tk.call(
806 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000807 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossume365a591998-05-01 19:48:20 +0000808 grid = wm_grid
809 def wm_group(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000810 return self.tk.call('wm', 'group', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000811 group = wm_group
812 def wm_iconbitmap(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000813 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000814 iconbitmap = wm_iconbitmap
815 def wm_iconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000816 return self.tk.call('wm', 'iconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000817 iconify = wm_iconify
818 def wm_iconmask(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000819 return self.tk.call('wm', 'iconmask', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000820 iconmask = wm_iconmask
821 def wm_iconname(self, newName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000822 return self.tk.call('wm', 'iconname', self._w, newName)
Guido van Rossume365a591998-05-01 19:48:20 +0000823 iconname = wm_iconname
824 def wm_iconposition(self, x=None, y=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000825 return self._getints(self.tk.call(
826 'wm', 'iconposition', self._w, x, y))
Guido van Rossume365a591998-05-01 19:48:20 +0000827 iconposition = wm_iconposition
828 def wm_iconwindow(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000829 return self.tk.call('wm', 'iconwindow', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000830 iconwindow = wm_iconwindow
831 def wm_maxsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000832 return self._getints(self.tk.call(
833 'wm', 'maxsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000834 maxsize = wm_maxsize
835 def wm_minsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000836 return self._getints(self.tk.call(
837 'wm', 'minsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000838 minsize = wm_minsize
839 def wm_overrideredirect(self, boolean=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000840 return self._getboolean(self.tk.call(
841 'wm', 'overrideredirect', self._w, boolean))
Guido van Rossume365a591998-05-01 19:48:20 +0000842 overrideredirect = wm_overrideredirect
843 def wm_positionfrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000844 return self.tk.call('wm', 'positionfrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000845 positionfrom = wm_positionfrom
846 def wm_protocol(self, name=None, func=None):
Guido van Rossumc4570481998-03-20 20:45:49 +0000847 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000848 command = self._register(func)
849 else:
850 command = func
851 return self.tk.call(
852 'wm', 'protocol', self._w, name, command)
Guido van Rossume365a591998-05-01 19:48:20 +0000853 protocol = wm_protocol
854 def wm_resizable(self, width=None, height=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000855 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossume365a591998-05-01 19:48:20 +0000856 resizable = wm_resizable
857 def wm_sizefrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000858 return self.tk.call('wm', 'sizefrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000859 sizefrom = wm_sizefrom
860 def wm_state(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000861 return self.tk.call('wm', 'state', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000862 state = wm_state
863 def wm_title(self, string=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000864 return self.tk.call('wm', 'title', self._w, string)
Guido van Rossume365a591998-05-01 19:48:20 +0000865 title = wm_title
866 def wm_transient(self, master=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000867 return self.tk.call('wm', 'transient', self._w, master)
Guido van Rossume365a591998-05-01 19:48:20 +0000868 transient = wm_transient
869 def wm_withdraw(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000870 return self.tk.call('wm', 'withdraw', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000871 withdraw = wm_withdraw
872
Guido van Rossum18468821994-06-20 07:49:28 +0000873
874class Tk(Misc, Wm):
875 _w = '.'
876 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000877 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000878 self.master = None
879 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000880 if baseName is None:
881 import sys, os
882 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000883 baseName, ext = os.path.splitext(baseName)
Fred Drake182c5901998-07-15 04:36:56 +0000884 if ext not in ('.py', '.pyc', '.pyo'):
885 baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000886 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000887 if _MacOS:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000888 # Disable event scanning except for Command-Period
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000889 _MacOS.SchedParams(1, 0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000890 # Work around nasty MacTk bug
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000891 # XXX Is this one still needed?
Guido van Rossum37dcab11996-05-16 16:00:19 +0000892 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000893 # Version sanity checks
894 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000895 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000896 raise RuntimeError, \
897 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000898 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000899 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000900 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000901 raise RuntimeError, \
902 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000903 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000904 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000905 raise RuntimeError, \
906 "Tk 4.0 or higher is required; found Tk %s" \
907 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000908 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000909 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000910 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +0000911 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000912 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000913 def destroy(self):
914 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000915 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000916 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000917 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000918 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000919 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000920 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000921 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000922 if os.environ.has_key('HOME'): home = os.environ['HOME']
923 else: home = os.curdir
924 class_tcl = os.path.join(home, '.%s.tcl' % className)
925 class_py = os.path.join(home, '.%s.py' % className)
926 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
927 base_py = os.path.join(home, '.%s.py' % baseName)
928 dir = {'self': self}
929 exec 'from Tkinter import *' in dir
930 if os.path.isfile(class_tcl):
931 print 'source', `class_tcl`
932 self.tk.call('source', class_tcl)
933 if os.path.isfile(class_py):
934 print 'execfile', `class_py`
935 execfile(class_py, dir)
936 if os.path.isfile(base_tcl):
937 print 'source', `base_tcl`
938 self.tk.call('source', base_tcl)
939 if os.path.isfile(base_py):
940 print 'execfile', `base_py`
941 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000942 def report_callback_exception(self, exc, val, tb):
Guido van Rossumda654501998-10-06 19:06:27 +0000943 import traceback, sys
944 sys.stderr.write("Exception in Tkinter callback\n")
Guido van Rossum9f1292d1998-10-13 20:02:39 +0000945 sys.last_type = exc
946 sys.last_value = val
947 sys.last_traceback = tb
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000948 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000949
Guido van Rossum368e06b1997-11-07 20:38:49 +0000950# Ideally, the classes Pack, Place and Grid disappear, the
951# pack/place/grid methods are defined on the Widget class, and
952# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
953# ...), with pack(), place() and grid() being short for
954# pack_configure(), place_configure() and grid_columnconfigure(), and
955# forget() being short for pack_forget(). As a practical matter, I'm
956# afraid that there is too much code out there that may be using the
957# Pack, Place or Grid class, so I leave them intact -- but only as
958# backwards compatibility features. Also note that those methods that
959# take a master as argument (e.g. pack_propagate) have been moved to
960# the Misc class (which now incorporates all methods common between
961# toplevel and interior widgets). Again, for compatibility, these are
962# copied into the Pack, Place or Grid class.
963
Guido van Rossum18468821994-06-20 07:49:28 +0000964class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000965 def pack_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000966 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000967 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000968 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000969 pack = configure = config = pack_configure
970 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000971 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000972 forget = pack_forget
973 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000974 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000975 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000976 dict = {}
977 for i in range(0, len(words), 2):
978 key = words[i][1:]
979 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000980 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000981 value = self._nametowidget(value)
982 dict[key] = value
983 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000984 info = pack_info
985 propagate = pack_propagate = Misc.pack_propagate
986 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000987
988class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000989 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000990 for k in ['in_']:
991 if kw.has_key(k):
992 kw[k[:-1]] = kw[k]
993 del kw[k]
Guido van Rossumf9756991998-04-29 21:57:08 +0000994 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000995 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000996 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000997 place = configure = config = place_configure
998 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000999 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001000 forget = place_forget
1001 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +00001002 words = self.tk.splitlist(
1003 self.tk.call('place', 'info', self._w))
1004 dict = {}
1005 for i in range(0, len(words), 2):
1006 key = words[i][1:]
1007 value = words[i+1]
1008 if value[:1] == '.':
1009 value = self._nametowidget(value)
1010 dict[key] = value
1011 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001012 info = place_info
1013 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001014
Guido van Rossum37dcab11996-05-16 16:00:19 +00001015class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001016 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001017 def grid_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001018 self.tk.call(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001019 ('grid', 'configure', self._w)
1020 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001021 grid = configure = config = grid_configure
1022 bbox = grid_bbox = Misc.grid_bbox
1023 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1024 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001025 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001026 forget = grid_forget
1027 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001028 words = self.tk.splitlist(
1029 self.tk.call('grid', 'info', self._w))
1030 dict = {}
1031 for i in range(0, len(words), 2):
1032 key = words[i][1:]
1033 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001034 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001035 value = self._nametowidget(value)
1036 dict[key] = value
1037 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001038 info = grid_info
1039 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001040 return self._getints(
1041 self.tk.call(
1042 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001043 location = grid_location
1044 propagate = grid_propagate = Misc.grid_propagate
1045 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1046 size = grid_size = Misc.grid_size
1047 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001048
Guido van Rossum368e06b1997-11-07 20:38:49 +00001049class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001050 def _setup(self, master, cnf):
Guido van Rossumc4570481998-03-20 20:45:49 +00001051 if _support_default_root:
1052 global _default_root
1053 if not master:
1054 if not _default_root:
1055 _default_root = Tk()
1056 master = _default_root
Guido van Rossum18468821994-06-20 07:49:28 +00001057 self.master = master
1058 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001059 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001060 if cnf.has_key('name'):
1061 name = cnf['name']
1062 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001063 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001064 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001065 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001066 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001067 self._w = '.' + name
1068 else:
1069 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001070 self.children = {}
1071 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001072 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001073 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001074 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1075 if kw:
1076 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001077 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001078 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001079 classes = []
1080 for k in cnf.keys():
1081 if type(k) is ClassType:
1082 classes.append((k, cnf[k]))
1083 del cnf[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001084 self.tk.call(
1085 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001086 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001087 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001088 def destroy(self):
1089 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001090 if self.master.children.has_key(self._name):
1091 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001092 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001093 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001094 def _do(self, name, args=()):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001095 # XXX Obsolete -- better use self.tk.call directly!
Guido van Rossumf9756991998-04-29 21:57:08 +00001096 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001097
Guido van Rossum368e06b1997-11-07 20:38:49 +00001098class Widget(BaseWidget, Pack, Place, Grid):
1099 pass
1100
1101class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001102 def __init__(self, master=None, cnf={}, **kw):
1103 if kw:
1104 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001105 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001106 for wmkey in ['screen', 'class_', 'class', 'visual',
1107 'colormap']:
1108 if cnf.has_key(wmkey):
1109 val = cnf[wmkey]
1110 # TBD: a hack needed because some keys
1111 # are not valid as keyword arguments
1112 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1113 else: opt = '-'+wmkey
1114 extra = extra + (opt, val)
1115 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001116 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001117 root = self._root()
1118 self.iconname(root.iconname())
1119 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001120
1121class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001122 def __init__(self, master=None, cnf={}, **kw):
1123 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001124 def tkButtonEnter(self, *dummy):
1125 self.tk.call('tkButtonEnter', self._w)
1126 def tkButtonLeave(self, *dummy):
1127 self.tk.call('tkButtonLeave', self._w)
1128 def tkButtonDown(self, *dummy):
1129 self.tk.call('tkButtonDown', self._w)
1130 def tkButtonUp(self, *dummy):
1131 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001132 def tkButtonInvoke(self, *dummy):
1133 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001134 def flash(self):
1135 self.tk.call(self._w, 'flash')
1136 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001137 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001138
1139# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001140# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001141def AtEnd():
1142 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001143def AtInsert(*args):
1144 s = 'insert'
1145 for a in args:
1146 if a: s = s + (' ' + a)
1147 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001148def AtSelFirst():
1149 return 'sel.first'
1150def AtSelLast():
1151 return 'sel.last'
1152def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001153 if y is None:
1154 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001155 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001156 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001157
1158class Canvas(Widget):
Guido van Rossumeb354b31999-03-16 21:54:50 +00001159 _tagcommands = None
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001160 def __init__(self, master=None, cnf={}, **kw):
1161 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001162 def addtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001163 self.tk.call((self._w, 'addtag') + args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001164 def addtag_above(self, newtag, tagOrId):
1165 self.addtag(newtag, 'above', tagOrId)
1166 def addtag_all(self, newtag):
1167 self.addtag(newtag, 'all')
1168 def addtag_below(self, newtag, tagOrId):
1169 self.addtag(newtag, 'below', tagOrId)
1170 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1171 self.addtag(newtag, 'closest', x, y, halo, start)
1172 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1173 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1174 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1175 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1176 def addtag_withtag(self, newtag, tagOrId):
1177 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001178 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001179 return self._getints(
1180 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001181 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001182 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001183 if funcid:
1184 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001185 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossumeb354b31999-03-16 21:54:50 +00001186 res = self._bind((self._w, 'bind', tagOrId),
1187 sequence, func, add)
1188 if sequence and func and res:
1189 # remember the funcid for later
1190 if self._tagcommands is None:
1191 self._tagcommands = {}
1192 list = self._tagcommands.get(tagOrId) or []
1193 self._tagcommands[tagOrId] = list
1194 list.append(res)
1195 return res
Guido van Rossum18468821994-06-20 07:49:28 +00001196 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001197 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001198 self._w, 'canvasx', screenx, gridspacing))
1199 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001200 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001201 self._w, 'canvasy', screeny, gridspacing))
1202 def coords(self, *args):
Guido van Rossum0001a111998-02-19 21:20:30 +00001203 # XXX Should use _flatten on args
Guido van Rossum0132f691998-04-30 17:50:36 +00001204 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00001205 self.tk.splitlist(
1206 self.tk.call((self._w, 'coords') + args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001207 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001208 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001209 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001210 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001211 args = args[:-1]
1212 else:
1213 cnf = {}
Guido van Rossum0132f691998-04-30 17:50:36 +00001214 return getint(apply(
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001215 self.tk.call,
1216 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001217 + args + self._options(cnf, kw)))
1218 def create_arc(self, *args, **kw):
1219 return self._create('arc', args, kw)
1220 def create_bitmap(self, *args, **kw):
1221 return self._create('bitmap', args, kw)
1222 def create_image(self, *args, **kw):
1223 return self._create('image', args, kw)
1224 def create_line(self, *args, **kw):
1225 return self._create('line', args, kw)
1226 def create_oval(self, *args, **kw):
1227 return self._create('oval', args, kw)
1228 def create_polygon(self, *args, **kw):
1229 return self._create('polygon', args, kw)
1230 def create_rectangle(self, *args, **kw):
1231 return self._create('rectangle', args, kw)
1232 def create_text(self, *args, **kw):
1233 return self._create('text', args, kw)
1234 def create_window(self, *args, **kw):
1235 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001236 def dchars(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001237 self.tk.call((self._w, 'dchars') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001238 def delete(self, *args):
Guido van Rossumeb354b31999-03-16 21:54:50 +00001239 self._delete_bindings(args)
Guido van Rossum0bd54331998-05-19 21:18:13 +00001240 self.tk.call((self._w, 'delete') + args)
Guido van Rossumeb354b31999-03-16 21:54:50 +00001241 def _delete_bindings(self, args):
1242 for tag in args:
1243 for a in self.tag_bind(tag):
1244 b = self.tag_bind(tag, a)
1245 c = _string.split(b, '[')[1]
1246 d = _string.split(c)[0]
1247 print "deletecommand(%s)" % `d`
1248 self.deletecommand(d)
Guido van Rossum18468821994-06-20 07:49:28 +00001249 def dtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001250 self.tk.call((self._w, 'dtag') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001251 def find(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001252 return self._getints(
1253 self.tk.call((self._w, 'find') + args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001254 def find_above(self, tagOrId):
1255 return self.find('above', tagOrId)
1256 def find_all(self):
1257 return self.find('all')
1258 def find_below(self, tagOrId):
1259 return self.find('below', tagOrId)
1260 def find_closest(self, x, y, halo=None, start=None):
1261 return self.find('closest', x, y, halo, start)
1262 def find_enclosed(self, x1, y1, x2, y2):
1263 return self.find('enclosed', x1, y1, x2, y2)
1264 def find_overlapping(self, x1, y1, x2, y2):
1265 return self.find('overlapping', x1, y1, x2, y2)
1266 def find_withtag(self, tagOrId):
1267 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001268 def focus(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001269 return self.tk.call((self._w, 'focus') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001270 def gettags(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001271 return self.tk.splitlist(
1272 self.tk.call((self._w, 'gettags') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001273 def icursor(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001274 self.tk.call((self._w, 'icursor') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001275 def index(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001276 return getint(self.tk.call((self._w, 'index') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001277 def insert(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001278 self.tk.call((self._w, 'insert') + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001279 def itemcget(self, tagOrId, option):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001280 return self.tk.call(
1281 (self._w, 'itemcget') + (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001282 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001283 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001284 cnf = {}
1285 for x in self.tk.split(
Guido van Rossum0bd54331998-05-19 21:18:13 +00001286 self.tk.call(self._w,
1287 'itemconfigure', tagOrId)):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001288 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1289 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001290 if type(cnf) == StringType and not kw:
Guido van Rossum0bd54331998-05-19 21:18:13 +00001291 x = self.tk.split(self.tk.call(
1292 self._w, 'itemconfigure', tagOrId, '-'+cnf))
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001293 return (x[0][1:],) + x[1:]
Guido van Rossum0bd54331998-05-19 21:18:13 +00001294 self.tk.call((self._w, 'itemconfigure', tagOrId) +
1295 self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001296 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001297 def lower(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001298 self.tk.call((self._w, 'lower') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001299 def move(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001300 self.tk.call((self._w, 'move') + args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001301 def postscript(self, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001302 return self.tk.call((self._w, 'postscript') +
1303 self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001304 def tkraise(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001305 self.tk.call((self._w, 'raise') + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001306 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001307 def scale(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001308 self.tk.call((self._w, 'scale') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001309 def scan_mark(self, x, y):
1310 self.tk.call(self._w, 'scan', 'mark', x, y)
1311 def scan_dragto(self, x, y):
1312 self.tk.call(self._w, 'scan', 'dragto', x, y)
1313 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001314 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001315 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001316 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001317 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001318 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001319 def select_item(self):
1320 self.tk.call(self._w, 'select', 'item')
1321 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001322 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001323 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001324 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001325 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001326 if not args:
1327 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001328 self.tk.call((self._w, 'xview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001329 def xview_moveto(self, fraction):
1330 self.tk.call(self._w, 'xview', 'moveto', fraction)
1331 def xview_scroll(self, number, what):
1332 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001333 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001334 if not args:
1335 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001336 self.tk.call((self._w, 'yview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001337 def yview_moveto(self, fraction):
1338 self.tk.call(self._w, 'yview', 'moveto', fraction)
1339 def yview_scroll(self, number, what):
1340 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001341
1342class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001343 def __init__(self, master=None, cnf={}, **kw):
1344 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001345 def deselect(self):
1346 self.tk.call(self._w, 'deselect')
1347 def flash(self):
1348 self.tk.call(self._w, 'flash')
1349 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001350 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001351 def select(self):
1352 self.tk.call(self._w, 'select')
1353 def toggle(self):
1354 self.tk.call(self._w, 'toggle')
1355
1356class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001357 def __init__(self, master=None, cnf={}, **kw):
1358 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001359 def delete(self, first, last=None):
1360 self.tk.call(self._w, 'delete', first, last)
1361 def get(self):
1362 return self.tk.call(self._w, 'get')
1363 def icursor(self, index):
1364 self.tk.call(self._w, 'icursor', index)
1365 def index(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001366 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001367 self._w, 'index', index))
1368 def insert(self, index, string):
1369 self.tk.call(self._w, 'insert', index, string)
1370 def scan_mark(self, x):
1371 self.tk.call(self._w, 'scan', 'mark', x)
1372 def scan_dragto(self, x):
1373 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001374 def selection_adjust(self, index):
1375 self.tk.call(self._w, 'selection', 'adjust', index)
1376 select_adjust = selection_adjust
1377 def selection_clear(self):
1378 self.tk.call(self._w, 'selection', 'clear')
1379 select_clear = selection_clear
1380 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001381 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001382 select_from = selection_from
1383 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001384 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001385 self.tk.call(self._w, 'selection', 'present'))
1386 select_present = selection_present
1387 def selection_range(self, start, end):
1388 self.tk.call(self._w, 'selection', 'range', start, end)
1389 select_range = selection_range
1390 def selection_to(self, index):
1391 self.tk.call(self._w, 'selection', 'to', index)
1392 select_to = selection_to
1393 def xview(self, index):
1394 self.tk.call(self._w, 'xview', index)
1395 def xview_moveto(self, fraction):
1396 self.tk.call(self._w, 'xview', 'moveto', fraction)
1397 def xview_scroll(self, number, what):
1398 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001399
1400class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001401 def __init__(self, master=None, cnf={}, **kw):
1402 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001403 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001404 if cnf.has_key('class_'):
1405 extra = ('-class', cnf['class_'])
1406 del cnf['class_']
1407 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001408 extra = ('-class', cnf['class'])
1409 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001410 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001411
1412class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001413 def __init__(self, master=None, cnf={}, **kw):
1414 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001415
Guido van Rossum18468821994-06-20 07:49:28 +00001416class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001417 def __init__(self, master=None, cnf={}, **kw):
1418 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001419 def activate(self, index):
1420 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001421 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001422 return self._getints(
1423 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001424 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001425 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001426 return self.tk.splitlist(self.tk.call(
1427 self._w, 'curselection'))
1428 def delete(self, first, last=None):
1429 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001430 def get(self, first, last=None):
1431 if last:
1432 return self.tk.splitlist(self.tk.call(
1433 self._w, 'get', first, last))
1434 else:
1435 return self.tk.call(self._w, 'get', first)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001436 def index(self, index):
1437 i = self.tk.call(self._w, 'index', index)
1438 if i == 'none': return None
1439 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001440 def insert(self, index, *elements):
Guido van Rossumf9756991998-04-29 21:57:08 +00001441 self.tk.call((self._w, 'insert', index) + elements)
Guido van Rossum18468821994-06-20 07:49:28 +00001442 def nearest(self, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001443 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001444 self._w, 'nearest', y))
1445 def scan_mark(self, x, y):
1446 self.tk.call(self._w, 'scan', 'mark', x, y)
1447 def scan_dragto(self, x, y):
1448 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001449 def see(self, index):
1450 self.tk.call(self._w, 'see', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001451 def selection_anchor(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001452 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001453 select_anchor = selection_anchor
1454 def selection_clear(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001455 self.tk.call(self._w,
1456 'selection', 'clear', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001457 select_clear = selection_clear
1458 def selection_includes(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001459 return self.tk.getboolean(self.tk.call(
1460 self._w, 'selection', 'includes', index))
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001461 select_includes = selection_includes
1462 def selection_set(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001463 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001464 select_set = selection_set
Guido van Rossum18468821994-06-20 07:49:28 +00001465 def size(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001466 return getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001467 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001468 if not what:
1469 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001470 self.tk.call((self._w, 'xview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001471 def xview_moveto(self, fraction):
1472 self.tk.call(self._w, 'xview', 'moveto', fraction)
1473 def xview_scroll(self, number, what):
1474 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001475 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001476 if not what:
1477 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001478 self.tk.call((self._w, 'yview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001479 def yview_moveto(self, fraction):
1480 self.tk.call(self._w, 'yview', 'moveto', fraction)
1481 def yview_scroll(self, number, what):
1482 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001483
1484class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001485 def __init__(self, master=None, cnf={}, **kw):
1486 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001487 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001488 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001489 def tk_mbPost(self):
1490 self.tk.call('tk_mbPost', self._w)
1491 def tk_mbUnpost(self):
1492 self.tk.call('tk_mbUnpost')
1493 def tk_traverseToMenu(self, char):
1494 self.tk.call('tk_traverseToMenu', self._w, char)
1495 def tk_traverseWithinMenu(self, char):
1496 self.tk.call('tk_traverseWithinMenu', self._w, char)
1497 def tk_getMenuButtons(self):
1498 return self.tk.call('tk_getMenuButtons', self._w)
1499 def tk_nextMenu(self, count):
1500 self.tk.call('tk_nextMenu', count)
1501 def tk_nextMenuEntry(self, count):
1502 self.tk.call('tk_nextMenuEntry', count)
1503 def tk_invokeMenu(self):
1504 self.tk.call('tk_invokeMenu', self._w)
1505 def tk_firstMenu(self):
1506 self.tk.call('tk_firstMenu', self._w)
1507 def tk_mbButtonDown(self):
1508 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001509 def tk_popup(self, x, y, entry=""):
1510 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001511 def activate(self, index):
1512 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001513 def add(self, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001514 self.tk.call((self._w, 'add', itemType) +
1515 self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001516 def add_cascade(self, cnf={}, **kw):
1517 self.add('cascade', cnf or kw)
1518 def add_checkbutton(self, cnf={}, **kw):
1519 self.add('checkbutton', cnf or kw)
1520 def add_command(self, cnf={}, **kw):
1521 self.add('command', cnf or kw)
1522 def add_radiobutton(self, cnf={}, **kw):
1523 self.add('radiobutton', cnf or kw)
1524 def add_separator(self, cnf={}, **kw):
1525 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001526 def insert(self, index, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001527 self.tk.call((self._w, 'insert', index, itemType) +
1528 self._options(cnf, kw))
Guido van Rossum2caac731996-09-05 16:46:31 +00001529 def insert_cascade(self, index, cnf={}, **kw):
1530 self.insert(index, 'cascade', cnf or kw)
1531 def insert_checkbutton(self, index, cnf={}, **kw):
1532 self.insert(index, 'checkbutton', cnf or kw)
1533 def insert_command(self, index, cnf={}, **kw):
1534 self.insert(index, 'command', cnf or kw)
1535 def insert_radiobutton(self, index, cnf={}, **kw):
1536 self.insert(index, 'radiobutton', cnf or kw)
1537 def insert_separator(self, index, cnf={}, **kw):
1538 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001539 def delete(self, index1, index2=None):
1540 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001541 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001542 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001543 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001544 if cnf is None and not kw:
1545 cnf = {}
Guido van Rossumf9756991998-04-29 21:57:08 +00001546 for x in self.tk.split(self.tk.call(
1547 (self._w, 'entryconfigure', index))):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001548 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1549 return cnf
1550 if type(cnf) == StringType and not kw:
Guido van Rossumf9756991998-04-29 21:57:08 +00001551 x = self.tk.split(self.tk.call(
1552 (self._w, 'entryconfigure', index, '-'+cnf)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001553 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001554 self.tk.call((self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001555 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001556 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001557 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001558 i = self.tk.call(self._w, 'index', index)
1559 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00001560 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001561 def invoke(self, index):
1562 return self.tk.call(self._w, 'invoke', index)
1563 def post(self, x, y):
1564 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001565 def type(self, index):
1566 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001567 def unpost(self):
1568 self.tk.call(self._w, 'unpost')
1569 def yposition(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001570 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001571 self._w, 'yposition', index))
1572
1573class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001574 def __init__(self, master=None, cnf={}, **kw):
1575 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001576
1577class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001578 def __init__(self, master=None, cnf={}, **kw):
1579 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001580
1581class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001582 def __init__(self, master=None, cnf={}, **kw):
1583 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001584 def deselect(self):
1585 self.tk.call(self._w, 'deselect')
1586 def flash(self):
1587 self.tk.call(self._w, 'flash')
1588 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001589 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001590 def select(self):
1591 self.tk.call(self._w, 'select')
1592
1593class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001594 def __init__(self, master=None, cnf={}, **kw):
1595 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001596 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001597 value = self.tk.call(self._w, 'get')
1598 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00001599 return getint(value)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00001600 except ValueError:
Guido van Rossum0132f691998-04-30 17:50:36 +00001601 return getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001602 def set(self, value):
1603 self.tk.call(self._w, 'set', value)
Guido van Rossumb4750db1998-08-11 19:07:14 +00001604 def coords(self, value=None):
1605 return self._getints(self.tk.call(self._w, 'coords', value))
1606 def identify(self, x, y):
1607 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001608
1609class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001610 def __init__(self, master=None, cnf={}, **kw):
1611 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001612 def activate(self, index):
1613 self.tk.call(self._w, 'activate', index)
1614 def delta(self, deltax, deltay):
Guido van Rossum0132f691998-04-30 17:50:36 +00001615 return getdouble(
1616 self.tk.call(self._w, 'delta', deltax, deltay))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001617 def fraction(self, x, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001618 return getdouble(self.tk.call(self._w, 'fraction', x, y))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001619 def identify(self, x, y):
1620 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001621 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001622 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001623 def set(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001624 self.tk.call((self._w, 'set') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001625
1626class Text(Widget):
Guido van Rossumdfaac4d1998-12-21 18:25:03 +00001627 # XXX Add dump()
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001628 def __init__(self, master=None, cnf={}, **kw):
1629 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001630 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001631 return self._getints(
1632 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001633 def tk_textSelectTo(self, index):
1634 self.tk.call('tk_textSelectTo', self._w, index)
1635 def tk_textBackspace(self):
1636 self.tk.call('tk_textBackspace', self._w)
1637 def tk_textIndexCloser(self, a, b, c):
1638 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1639 def tk_textResetAnchor(self, index):
1640 self.tk.call('tk_textResetAnchor', self._w, index)
1641 def compare(self, index1, op, index2):
1642 return self.tk.getboolean(self.tk.call(
1643 self._w, 'compare', index1, op, index2))
1644 def debug(self, boolean=None):
1645 return self.tk.getboolean(self.tk.call(
1646 self._w, 'debug', boolean))
1647 def delete(self, index1, index2=None):
1648 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001649 def dlineinfo(self, index):
1650 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001651 def get(self, index1, index2=None):
1652 return self.tk.call(self._w, 'get', index1, index2)
Guido van Rossumc86b7c61998-08-31 16:54:33 +00001653 # (Image commands are new in 8.0)
1654 def image_cget(self, index, option):
1655 if option[:1] != "-":
1656 option = "-" + option
1657 if option[-1:] == "_":
1658 option = option[:-1]
1659 return self.tk.call(self._w, "image", "cget", index, option)
1660 def image_configure(self, index, cnf={}, **kw):
1661 if not cnf and not kw:
1662 cnf = {}
1663 for x in self.tk.split(
1664 self.tk.call(
1665 self._w, "image", "configure", index)):
1666 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1667 return cnf
1668 apply(self.tk.call,
1669 (self._w, "image", "configure", index)
1670 + self._options(cnf, kw))
1671 def image_create(self, index, cnf={}, **kw):
1672 return apply(self.tk.call,
1673 (self._w, "image", "create", index)
1674 + self._options(cnf, kw))
1675 def image_names(self):
1676 return self.tk.call(self._w, "image", "names")
Guido van Rossum18468821994-06-20 07:49:28 +00001677 def index(self, index):
1678 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001679 def insert(self, index, chars, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001680 self.tk.call((self._w, 'insert', index, chars) + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001681 def mark_gravity(self, markName, direction=None):
Guido van Rossumf9756991998-04-29 21:57:08 +00001682 return self.tk.call(
1683 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001684 def mark_names(self):
1685 return self.tk.splitlist(self.tk.call(
1686 self._w, 'mark', 'names'))
1687 def mark_set(self, markName, index):
1688 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001689 def mark_unset(self, *markNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001690 self.tk.call((self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001691 def scan_mark(self, x, y):
1692 self.tk.call(self._w, 'scan', 'mark', x, y)
1693 def scan_dragto(self, x, y):
1694 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001695 def search(self, pattern, index, stopindex=None,
1696 forwards=None, backwards=None, exact=None,
1697 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001698 args = [self._w, 'search']
1699 if forwards: args.append('-forwards')
1700 if backwards: args.append('-backwards')
1701 if exact: args.append('-exact')
1702 if regexp: args.append('-regexp')
1703 if nocase: args.append('-nocase')
1704 if count: args.append('-count'); args.append(count)
1705 if pattern[0] == '-': args.append('--')
1706 args.append(pattern)
1707 args.append(index)
1708 if stopindex: args.append(stopindex)
Guido van Rossumf9756991998-04-29 21:57:08 +00001709 return self.tk.call(tuple(args))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001710 def see(self, index):
1711 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001712 def tag_add(self, tagName, index1, index2=None):
1713 self.tk.call(
1714 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossum117a5a81998-03-27 21:26:51 +00001715 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001716 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001717 if funcid:
1718 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001719 def tag_bind(self, tagName, sequence, func, add=None):
1720 return self._bind((self._w, 'tag', 'bind', tagName),
1721 sequence, func, add)
1722 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001723 if option[:1] != '-':
1724 option = '-' + option
1725 if option[-1:] == '_':
1726 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001727 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001728 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001729 if type(cnf) == StringType:
1730 x = self.tk.split(self.tk.call(
1731 self._w, 'tag', 'configure', tagName, '-'+cnf))
1732 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001733 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001734 (self._w, 'tag', 'configure', tagName)
1735 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001736 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001737 def tag_delete(self, *tagNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001738 self.tk.call((self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001739 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001740 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001741 def tag_names(self, index=None):
1742 return self.tk.splitlist(
1743 self.tk.call(self._w, 'tag', 'names', index))
1744 def tag_nextrange(self, tagName, index1, index2=None):
1745 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001746 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001747 def tag_prevrange(self, tagName, index1, index2=None):
1748 return self.tk.splitlist(self.tk.call(
1749 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001750 def tag_raise(self, tagName, aboveThis=None):
1751 self.tk.call(
1752 self._w, 'tag', 'raise', tagName, aboveThis)
1753 def tag_ranges(self, tagName):
1754 return self.tk.splitlist(self.tk.call(
1755 self._w, 'tag', 'ranges', tagName))
1756 def tag_remove(self, tagName, index1, index2=None):
1757 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001758 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001759 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001760 if option[:1] != '-':
1761 option = '-' + option
1762 if option[-1:] == '_':
1763 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001764 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001765 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001766 if type(cnf) == StringType:
1767 x = self.tk.split(self.tk.call(
1768 self._w, 'window', 'configure',
1769 index, '-'+cnf))
1770 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001771 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001772 (self._w, 'window', 'configure', index)
1773 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001774 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001775 def window_create(self, index, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001776 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001777 (self._w, 'window', 'create', index)
1778 + self._options(cnf, kw))
1779 def window_names(self):
1780 return self.tk.splitlist(
1781 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001782 def xview(self, *what):
1783 if not what:
1784 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001785 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001786 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001787 if not what:
1788 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001789 self.tk.call((self._w, 'yview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001790 def yview_pickplace(self, *what):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001791 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00001792
Guido van Rossum28574b51996-10-21 15:16:51 +00001793class _setit:
1794 def __init__(self, var, value):
1795 self.__value = value
1796 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001797 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001798 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001799
1800class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001801 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001802 kw = {"borderwidth": 2, "textvariable": variable,
1803 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1804 "highlightthickness": 2}
1805 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001806 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001807 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1808 self.menuname = menu._w
1809 menu.add_command(label=value, command=_setit(variable, value))
1810 for v in values:
1811 menu.add_command(label=v, command=_setit(variable, v))
1812 self["menu"] = menu
1813
1814 def __getitem__(self, name):
1815 if name == 'menu':
1816 return self.__menu
1817 return Widget.__getitem__(self, name)
1818
1819 def destroy(self):
1820 Menubutton.destroy(self)
1821 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001822
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001823class Image:
Guido van Rossumc4570481998-03-20 20:45:49 +00001824 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001825 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00001826 if not master:
1827 master = _default_root
1828 if not master:
1829 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001830 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001831 if not name:
1832 name = `id(self)`
1833 # The following is needed for systems where id(x)
1834 # can return a negative number, such as Linux/m68k:
1835 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001836 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1837 elif kw: cnf = kw
1838 options = ()
1839 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001840 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001841 v = self._register(v)
1842 options = options + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001843 self.tk.call(('image', 'create', imgtype, name,) + options)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001844 self.name = name
1845 def __str__(self): return self.name
1846 def __del__(self):
1847 if self.name:
Guido van Rossumc55b0ca1999-02-08 15:26:49 +00001848 try:
1849 self.tk.call('image', 'delete', self.name)
1850 except TclError:
1851 # May happen if the root was destroyed
1852 pass
Guido van Rossum71b1a901995-09-18 21:54:35 +00001853 def __setitem__(self, key, value):
1854 self.tk.call(self.name, 'configure', '-'+key, value)
1855 def __getitem__(self, key):
1856 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001857 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001858 res = ()
1859 for k, v in _cnfmerge(kw).items():
1860 if v is not None:
1861 if k[-1] == '_': k = k[:-1]
1862 if callable(v):
1863 v = self._register(v)
1864 res = res + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001865 self.tk.call((self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001866 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001867 def height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001868 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001869 self.tk.call('image', 'height', self.name))
1870 def type(self):
1871 return self.tk.call('image', 'type', self.name)
1872 def width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001873 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001874 self.tk.call('image', 'width', self.name))
1875
1876class PhotoImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001877 def __init__(self, name=None, cnf={}, master=None, **kw):
1878 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001879 def blank(self):
1880 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001881 def cget(self, option):
1882 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001883 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001884 def __getitem__(self, key):
1885 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001886 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001887 def copy(self):
1888 destImage = PhotoImage()
1889 self.tk.call(destImage, 'copy', self.name)
1890 return destImage
1891 def zoom(self,x,y=''):
1892 destImage = PhotoImage()
1893 if y=='': y=x
1894 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1895 return destImage
1896 def subsample(self,x,y=''):
1897 destImage = PhotoImage()
1898 if y=='': y=x
1899 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1900 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001901 def get(self, x, y):
1902 return self.tk.call(self.name, 'get', x, y)
1903 def put(self, data, to=None):
1904 args = (self.name, 'put', data)
1905 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001906 if to[0] == '-to':
1907 to = to[1:]
1908 args = args + ('-to',) + tuple(to)
Guido van Rossumf9756991998-04-29 21:57:08 +00001909 self.tk.call(args)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001910 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001911 def write(self, filename, format=None, from_coords=None):
1912 args = (self.name, 'write', filename)
1913 if format:
1914 args = args + ('-format', format)
1915 if from_coords:
1916 args = args + ('-from',) + tuple(from_coords)
Guido van Rossumf9756991998-04-29 21:57:08 +00001917 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001918
1919class BitmapImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001920 def __init__(self, name=None, cnf={}, master=None, **kw):
1921 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001922
1923def image_names(): return _default_root.tk.call('image', 'names')
1924def image_types(): return _default_root.tk.call('image', 'types')
1925
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001926######################################################################
1927# Extensions:
1928
1929class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001930 def __init__(self, master=None, cnf={}, **kw):
1931 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001932 self.bind('<Any-Enter>', self.tkButtonEnter)
1933 self.bind('<Any-Leave>', self.tkButtonLeave)
1934 self.bind('<1>', self.tkButtonDown)
1935 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001936
1937class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001938 def __init__(self, master=None, cnf={}, **kw):
1939 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001940 self.bind('<Any-Enter>', self.tkButtonEnter)
1941 self.bind('<Any-Leave>', self.tkButtonLeave)
1942 self.bind('<1>', self.tkButtonDown)
1943 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1944 self['fg'] = self['bg']
1945 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001946
Guido van Rossumc417ef81996-08-21 23:38:59 +00001947######################################################################
1948# Test:
1949
1950def _test():
1951 root = Tk()
1952 label = Label(root, text="Proof-of-existence test for Tk")
1953 label.pack()
1954 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001955 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001956 text="[%s]" % root.test['text']))
1957 test.pack()
1958 root.test = test
1959 quit = Button(root, text="QUIT", command=root.destroy)
1960 quit.pack()
Guido van Rossum268824e1998-06-19 04:34:19 +00001961 # The following three commands are needed so the window pops
1962 # up on top on Windows...
1963 root.iconify()
1964 root.update()
1965 root.deiconify()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001966 root.mainloop()
1967
1968if __name__ == '__main__':
1969 _test()