blob: 30cc982552cb003d65dd63a3773a09d267708535 [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))
Guido van Rossumf8d8e071999-10-18 22:06:38 +0000426 return map(self.__winfo_parseitem, data)
427 def __winfo_parseitem(self, t):
428 return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
429 def __winfo_getint(self, x):
430 return _string.atoi(x, 0)
Guido van Rossum18468821994-06-20 07:49:28 +0000431 def winfo_vrootheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000432 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000433 self.tk.call('winfo', 'vrootheight', self._w))
434 def winfo_vrootwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000435 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000436 self.tk.call('winfo', 'vrootwidth', self._w))
437 def winfo_vrootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000438 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000439 self.tk.call('winfo', 'vrootx', self._w))
440 def winfo_vrooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000441 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000442 self.tk.call('winfo', 'vrooty', self._w))
443 def winfo_width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000444 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000445 self.tk.call('winfo', 'width', self._w))
446 def winfo_x(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000447 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000448 self.tk.call('winfo', 'x', self._w))
449 def winfo_y(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000450 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000451 self.tk.call('winfo', 'y', self._w))
452 def update(self):
453 self.tk.call('update')
454 def update_idletasks(self):
455 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000456 def bindtags(self, tagList=None):
457 if tagList is None:
458 return self.tk.splitlist(
459 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000460 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000461 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000462 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum88b63b81998-06-25 18:54:49 +0000463 if type(func) is StringType:
464 self.tk.call(what + (sequence, func))
465 elif func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000466 funcid = self._register(func, self._substitute,
467 needcleanup)
Guido van Rossumdc593401998-04-29 22:16:57 +0000468 cmd = ('%sif {"[%s %s]" == "break"} break\n'
469 %
470 (add and '+' or '',
471 funcid,
472 _string.join(self._subst_format)))
Guido van Rossumf9756991998-04-29 21:57:08 +0000473 self.tk.call(what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000474 return funcid
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000475 elif sequence:
Guido van Rossumf9756991998-04-29 21:57:08 +0000476 return self.tk.call(what + (sequence,))
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000477 else:
478 return self.tk.splitlist(self.tk.call(what))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000479 def bind(self, sequence=None, func=None, add=None):
480 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000481 def unbind(self, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +0000482 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000483 if funcid:
484 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000485 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000486 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000487 def unbind_all(self, sequence):
488 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000489 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000490 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000491 def unbind_class(self, className, sequence):
492 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000493 def mainloop(self, n=0):
494 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000495 def quit(self):
496 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000497 def _getints(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000498 if string:
499 return tuple(map(getint, self.tk.splitlist(string)))
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000500 def _getdoubles(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000501 if string:
502 return tuple(map(getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000503 def _getboolean(self, string):
504 if string:
505 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000506 def _displayof(self, displayof):
507 if displayof:
508 return ('-displayof', displayof)
509 if displayof is None:
510 return ('-displayof', self._w)
511 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000512 def _options(self, cnf, kw = None):
513 if kw:
514 cnf = _cnfmerge((cnf, kw))
515 else:
516 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000517 res = ()
518 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000519 if v is not None:
520 if k[-1] == '_': k = k[:-1]
521 if callable(v):
522 v = self._register(v)
523 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000524 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000525 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000526 w = self
527 if name[0] == '.':
528 w = w._root()
529 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000530 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000531 while name:
532 i = find(name, '.')
533 if i >= 0:
534 name, tail = name[:i], name[i+1:]
535 else:
536 tail = ''
537 w = w.children[name]
538 name = tail
539 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000540 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000541 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000542 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000543 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000544 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000545 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000546 except AttributeError:
547 pass
548 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000549 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000550 except AttributeError:
551 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000552 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000553 if needcleanup:
554 if self._tclCommands is None:
555 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +0000556 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000557 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000558 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000559 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000560 def _root(self):
561 w = self
562 while w.master: w = w.master
563 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000564 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000565 '%s', '%t', '%w', '%x', '%y',
566 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
567 def _substitute(self, *args):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000568 if len(args) != len(self._subst_format): return args
Guido van Rossum0132f691998-04-30 17:50:36 +0000569 getboolean = self.tk.getboolean
570 getint = int
Guido van Rossum45853db1994-06-20 12:19:19 +0000571 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
572 # Missing: (a, c, d, m, o, v, B, R)
573 e = Event()
Guido van Rossum0132f691998-04-30 17:50:36 +0000574 e.serial = getint(nsign)
575 e.num = getint(b)
576 try: e.focus = getboolean(f)
Guido van Rossum45853db1994-06-20 12:19:19 +0000577 except TclError: pass
Guido van Rossum0132f691998-04-30 17:50:36 +0000578 e.height = getint(h)
579 e.keycode = getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000580 # For Visibility events, event state is a string and
581 # not an integer:
582 try:
Guido van Rossum0132f691998-04-30 17:50:36 +0000583 e.state = getint(s)
Guido van Rossumfe02efd1998-06-09 02:37:45 +0000584 except ValueError:
Guido van Rossum36269991996-05-16 17:11:27 +0000585 e.state = s
Guido van Rossum0132f691998-04-30 17:50:36 +0000586 e.time = getint(t)
587 e.width = getint(w)
588 e.x = getint(x)
589 e.y = getint(y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000590 e.char = A
Guido van Rossum0132f691998-04-30 17:50:36 +0000591 try: e.send_event = getboolean(E)
Guido van Rossum45853db1994-06-20 12:19:19 +0000592 except TclError: pass
593 e.keysym = K
Guido van Rossum0132f691998-04-30 17:50:36 +0000594 e.keysym_num = getint(N)
Guido van Rossum45853db1994-06-20 12:19:19 +0000595 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +0000596 try:
597 e.widget = self._nametowidget(W)
598 except KeyError:
599 e.widget = W
Guido van Rossum0132f691998-04-30 17:50:36 +0000600 e.x_root = getint(X)
601 e.y_root = getint(Y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000602 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000603 def _report_exception(self):
604 import sys
605 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
606 root = self._root()
607 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000608 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000609 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000610 # XXX ought to generalize this so tag_config etc. can use it
611 if kw:
612 cnf = _cnfmerge((cnf, kw))
613 elif cnf:
614 cnf = _cnfmerge(cnf)
615 if cnf is None:
616 cnf = {}
617 for x in self.tk.split(
618 self.tk.call(self._w, 'configure')):
619 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
620 return cnf
621 if type(cnf) is StringType:
622 x = self.tk.split(self.tk.call(
623 self._w, 'configure', '-'+cnf))
624 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +0000625 self.tk.call((self._w, 'configure')
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000626 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000627 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000628 def cget(self, key):
629 return self.tk.call(self._w, 'cget', '-' + key)
630 __getitem__ = cget
631 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000632 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000633 def keys(self):
634 return map(lambda x: x[0][1:],
635 self.tk.split(self.tk.call(self._w, 'configure')))
636 def __str__(self):
637 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000638 # Pack methods that apply to the master
639 _noarg_ = ['_noarg_']
640 def pack_propagate(self, flag=_noarg_):
641 if flag is Misc._noarg_:
642 return self._getboolean(self.tk.call(
643 'pack', 'propagate', self._w))
644 else:
645 self.tk.call('pack', 'propagate', self._w, flag)
646 propagate = pack_propagate
647 def pack_slaves(self):
648 return map(self._nametowidget,
649 self.tk.splitlist(
650 self.tk.call('pack', 'slaves', self._w)))
651 slaves = pack_slaves
652 # Place method that applies to the master
653 def place_slaves(self):
654 return map(self._nametowidget,
655 self.tk.splitlist(
656 self.tk.call(
657 'place', 'slaves', self._w)))
658 # Grid methods that apply to the master
Barry Warsaw107e6231998-12-15 00:44:15 +0000659 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
660 args = ('grid', 'bbox', self._w)
661 if column is not None and row is not None:
662 args = args + (column, row)
663 if col2 is not None and row2 is not None:
664 args = args + (col2, row2)
665 return self._getints(apply(self.tk.call, args)) or None
666
Guido van Rossum368e06b1997-11-07 20:38:49 +0000667 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000668 def _grid_configure(self, command, index, cnf, kw):
669 if type(cnf) is StringType and not kw:
670 if cnf[-1:] == '_':
671 cnf = cnf[:-1]
672 if cnf[:1] != '-':
673 cnf = '-'+cnf
674 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000675 else:
676 options = self._options(cnf, kw)
677 if not options:
678 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000679 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000680 words = self.tk.splitlist(res)
681 dict = {}
682 for i in range(0, len(words), 2):
683 key = words[i][1:]
684 value = words[i+1]
685 if not value:
686 value = None
687 elif '.' in value:
Guido van Rossum0132f691998-04-30 17:50:36 +0000688 value = getdouble(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000689 else:
Guido van Rossum0132f691998-04-30 17:50:36 +0000690 value = getint(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000691 dict[key] = value
692 return dict
Guido van Rossumf9756991998-04-29 21:57:08 +0000693 res = self.tk.call(
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000694 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000695 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000696 if len(options) == 1:
697 if not res: return None
698 # In Tk 7.5, -width can be a float
Guido van Rossum0132f691998-04-30 17:50:36 +0000699 if '.' in res: return getdouble(res)
700 return getint(res)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000701 def grid_columnconfigure(self, index, cnf={}, **kw):
702 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000703 columnconfigure = grid_columnconfigure
704 def grid_propagate(self, flag=_noarg_):
705 if flag is Misc._noarg_:
706 return self._getboolean(self.tk.call(
707 'grid', 'propagate', self._w))
708 else:
709 self.tk.call('grid', 'propagate', self._w, flag)
710 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000711 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000712 rowconfigure = grid_rowconfigure
713 def grid_size(self):
714 return self._getints(
715 self.tk.call('grid', 'size', self._w)) or None
716 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000717 def grid_slaves(self, row=None, column=None):
718 args = ()
Guido van Rossumeb354b31999-03-16 21:54:50 +0000719 if row is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000720 args = args + ('-row', row)
Guido van Rossumeb354b31999-03-16 21:54:50 +0000721 if column is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000722 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000723 return map(self._nametowidget,
Guido van Rossumf9756991998-04-29 21:57:08 +0000724 self.tk.splitlist(self.tk.call(
725 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000726
Guido van Rossum80f8be81997-12-02 19:51:39 +0000727 # Support for the "event" command, new in Tk 4.2.
728 # By Case Roole.
729
Guido van Rossum56c04b81998-04-06 03:10:03 +0000730 def event_add(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000731 args = ('event', 'add', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000732 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000733
Guido van Rossum56c04b81998-04-06 03:10:03 +0000734 def event_delete(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000735 args = ('event', 'delete', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000736 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000737
738 def event_generate(self, sequence, **kw):
739 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +0000740 for k, v in kw.items():
741 args = args + ('-%s' % k, str(v))
Guido van Rossumf9756991998-04-29 21:57:08 +0000742 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000743
Guido van Rossum56c04b81998-04-06 03:10:03 +0000744 def event_info(self, virtual=None):
745 return self.tk.splitlist(
746 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +0000747
Guido van Rossumc2966511998-04-10 19:16:10 +0000748 # Image related commands
749
750 def image_names(self):
751 return self.tk.call('image', 'names')
752
753 def image_types(self):
754 return self.tk.call('image', 'types')
755
Guido van Rossum80f8be81997-12-02 19:51:39 +0000756
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000757class CallWrapper:
758 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000759 self.func = func
760 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000761 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000762 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000763 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000764 if self.subst:
765 args = apply(self.subst, args)
766 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000767 except SystemExit, msg:
768 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000769 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000770 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000771
Guido van Rossume365a591998-05-01 19:48:20 +0000772
Guido van Rossum18468821994-06-20 07:49:28 +0000773class Wm:
Guido van Rossume365a591998-05-01 19:48:20 +0000774 def wm_aspect(self,
Guido van Rossum18468821994-06-20 07:49:28 +0000775 minNumer=None, minDenom=None,
776 maxNumer=None, maxDenom=None):
777 return self._getints(
778 self.tk.call('wm', 'aspect', self._w,
779 minNumer, minDenom,
780 maxNumer, maxDenom))
Guido van Rossume365a591998-05-01 19:48:20 +0000781 aspect = wm_aspect
782 def wm_client(self, name=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000783 return self.tk.call('wm', 'client', self._w, name)
Guido van Rossume365a591998-05-01 19:48:20 +0000784 client = wm_client
785 def wm_colormapwindows(self, *wlist):
Guido van Rossum8fa42af1999-10-20 12:29:56 +0000786 if len(wlist) > 1:
787 wlist = (wlist,) # Tk needs a list of windows here
788 args = ('wm', 'colormapwindows', self._w) + wlist
Guido van Rossumf9756991998-04-29 21:57:08 +0000789 return map(self._nametowidget, self.tk.call(args))
Guido van Rossume365a591998-05-01 19:48:20 +0000790 colormapwindows = wm_colormapwindows
791 def wm_command(self, value=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000792 return self.tk.call('wm', 'command', self._w, value)
Guido van Rossume365a591998-05-01 19:48:20 +0000793 command = wm_command
794 def wm_deiconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000795 return self.tk.call('wm', 'deiconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000796 deiconify = wm_deiconify
797 def wm_focusmodel(self, model=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000798 return self.tk.call('wm', 'focusmodel', self._w, model)
Guido van Rossume365a591998-05-01 19:48:20 +0000799 focusmodel = wm_focusmodel
800 def wm_frame(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000801 return self.tk.call('wm', 'frame', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000802 frame = wm_frame
803 def wm_geometry(self, newGeometry=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000804 return self.tk.call('wm', 'geometry', self._w, newGeometry)
Guido van Rossume365a591998-05-01 19:48:20 +0000805 geometry = wm_geometry
806 def wm_grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000807 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000808 widthInc=None, heightInc=None):
809 return self._getints(self.tk.call(
810 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000811 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossume365a591998-05-01 19:48:20 +0000812 grid = wm_grid
813 def wm_group(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000814 return self.tk.call('wm', 'group', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000815 group = wm_group
816 def wm_iconbitmap(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000817 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000818 iconbitmap = wm_iconbitmap
819 def wm_iconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000820 return self.tk.call('wm', 'iconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000821 iconify = wm_iconify
822 def wm_iconmask(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000823 return self.tk.call('wm', 'iconmask', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000824 iconmask = wm_iconmask
825 def wm_iconname(self, newName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000826 return self.tk.call('wm', 'iconname', self._w, newName)
Guido van Rossume365a591998-05-01 19:48:20 +0000827 iconname = wm_iconname
828 def wm_iconposition(self, x=None, y=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000829 return self._getints(self.tk.call(
830 'wm', 'iconposition', self._w, x, y))
Guido van Rossume365a591998-05-01 19:48:20 +0000831 iconposition = wm_iconposition
832 def wm_iconwindow(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000833 return self.tk.call('wm', 'iconwindow', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000834 iconwindow = wm_iconwindow
835 def wm_maxsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000836 return self._getints(self.tk.call(
837 'wm', 'maxsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000838 maxsize = wm_maxsize
839 def wm_minsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000840 return self._getints(self.tk.call(
841 'wm', 'minsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000842 minsize = wm_minsize
843 def wm_overrideredirect(self, boolean=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000844 return self._getboolean(self.tk.call(
845 'wm', 'overrideredirect', self._w, boolean))
Guido van Rossume365a591998-05-01 19:48:20 +0000846 overrideredirect = wm_overrideredirect
847 def wm_positionfrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000848 return self.tk.call('wm', 'positionfrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000849 positionfrom = wm_positionfrom
850 def wm_protocol(self, name=None, func=None):
Guido van Rossumc4570481998-03-20 20:45:49 +0000851 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000852 command = self._register(func)
853 else:
854 command = func
855 return self.tk.call(
856 'wm', 'protocol', self._w, name, command)
Guido van Rossume365a591998-05-01 19:48:20 +0000857 protocol = wm_protocol
858 def wm_resizable(self, width=None, height=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000859 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossume365a591998-05-01 19:48:20 +0000860 resizable = wm_resizable
861 def wm_sizefrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000862 return self.tk.call('wm', 'sizefrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000863 sizefrom = wm_sizefrom
864 def wm_state(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000865 return self.tk.call('wm', 'state', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000866 state = wm_state
867 def wm_title(self, string=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000868 return self.tk.call('wm', 'title', self._w, string)
Guido van Rossume365a591998-05-01 19:48:20 +0000869 title = wm_title
870 def wm_transient(self, master=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000871 return self.tk.call('wm', 'transient', self._w, master)
Guido van Rossume365a591998-05-01 19:48:20 +0000872 transient = wm_transient
873 def wm_withdraw(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000874 return self.tk.call('wm', 'withdraw', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000875 withdraw = wm_withdraw
876
Guido van Rossum18468821994-06-20 07:49:28 +0000877
878class Tk(Misc, Wm):
879 _w = '.'
880 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000881 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000882 self.master = None
883 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000884 if baseName is None:
885 import sys, os
886 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000887 baseName, ext = os.path.splitext(baseName)
Fred Drake182c5901998-07-15 04:36:56 +0000888 if ext not in ('.py', '.pyc', '.pyo'):
889 baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000890 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000891 if _MacOS:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000892 # Disable event scanning except for Command-Period
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000893 _MacOS.SchedParams(1, 0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000894 # Work around nasty MacTk bug
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000895 # XXX Is this one still needed?
Guido van Rossum37dcab11996-05-16 16:00:19 +0000896 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000897 # Version sanity checks
898 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000899 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000900 raise RuntimeError, \
901 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000902 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000903 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000904 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000905 raise RuntimeError, \
906 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000907 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000908 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000909 raise RuntimeError, \
910 "Tk 4.0 or higher is required; found Tk %s" \
911 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000912 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000913 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000914 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +0000915 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000916 _default_root = self
Guido van Rossume61e98d1999-08-20 18:26:06 +0000917 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum45853db1994-06-20 12:19:19 +0000918 def destroy(self):
919 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000920 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000921 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000922 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000923 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000924 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000925 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000926 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000927 if os.environ.has_key('HOME'): home = os.environ['HOME']
928 else: home = os.curdir
929 class_tcl = os.path.join(home, '.%s.tcl' % className)
930 class_py = os.path.join(home, '.%s.py' % className)
931 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
932 base_py = os.path.join(home, '.%s.py' % baseName)
933 dir = {'self': self}
934 exec 'from Tkinter import *' in dir
935 if os.path.isfile(class_tcl):
936 print 'source', `class_tcl`
937 self.tk.call('source', class_tcl)
938 if os.path.isfile(class_py):
939 print 'execfile', `class_py`
940 execfile(class_py, dir)
941 if os.path.isfile(base_tcl):
942 print 'source', `base_tcl`
943 self.tk.call('source', base_tcl)
944 if os.path.isfile(base_py):
945 print 'execfile', `base_py`
946 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000947 def report_callback_exception(self, exc, val, tb):
Guido van Rossumda654501998-10-06 19:06:27 +0000948 import traceback, sys
949 sys.stderr.write("Exception in Tkinter callback\n")
Guido van Rossum9f1292d1998-10-13 20:02:39 +0000950 sys.last_type = exc
951 sys.last_value = val
952 sys.last_traceback = tb
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000953 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000954
Guido van Rossum368e06b1997-11-07 20:38:49 +0000955# Ideally, the classes Pack, Place and Grid disappear, the
956# pack/place/grid methods are defined on the Widget class, and
957# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
958# ...), with pack(), place() and grid() being short for
959# pack_configure(), place_configure() and grid_columnconfigure(), and
960# forget() being short for pack_forget(). As a practical matter, I'm
961# afraid that there is too much code out there that may be using the
962# Pack, Place or Grid class, so I leave them intact -- but only as
963# backwards compatibility features. Also note that those methods that
964# take a master as argument (e.g. pack_propagate) have been moved to
965# the Misc class (which now incorporates all methods common between
966# toplevel and interior widgets). Again, for compatibility, these are
967# copied into the Pack, Place or Grid class.
968
Guido van Rossum18468821994-06-20 07:49:28 +0000969class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000970 def pack_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000971 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000972 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000973 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000974 pack = configure = config = pack_configure
975 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000976 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000977 forget = pack_forget
978 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000979 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000980 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000981 dict = {}
982 for i in range(0, len(words), 2):
983 key = words[i][1:]
984 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000985 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000986 value = self._nametowidget(value)
987 dict[key] = value
988 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000989 info = pack_info
990 propagate = pack_propagate = Misc.pack_propagate
991 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000992
993class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000994 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000995 for k in ['in_']:
996 if kw.has_key(k):
997 kw[k[:-1]] = kw[k]
998 del kw[k]
Guido van Rossumf9756991998-04-29 21:57:08 +0000999 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001000 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001001 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001002 place = configure = config = place_configure
1003 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +00001004 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001005 forget = place_forget
1006 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +00001007 words = self.tk.splitlist(
1008 self.tk.call('place', 'info', self._w))
1009 dict = {}
1010 for i in range(0, len(words), 2):
1011 key = words[i][1:]
1012 value = words[i+1]
1013 if value[:1] == '.':
1014 value = self._nametowidget(value)
1015 dict[key] = value
1016 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001017 info = place_info
1018 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001019
Guido van Rossum37dcab11996-05-16 16:00:19 +00001020class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001021 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001022 def grid_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001023 self.tk.call(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001024 ('grid', 'configure', self._w)
1025 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001026 grid = configure = config = grid_configure
1027 bbox = grid_bbox = Misc.grid_bbox
1028 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1029 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001030 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001031 forget = grid_forget
Guido van Rossum8e407591999-05-05 23:03:48 +00001032 def grid_remove(self):
1033 self.tk.call('grid', 'remove', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001034 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001035 words = self.tk.splitlist(
1036 self.tk.call('grid', 'info', self._w))
1037 dict = {}
1038 for i in range(0, len(words), 2):
1039 key = words[i][1:]
1040 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001041 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001042 value = self._nametowidget(value)
1043 dict[key] = value
1044 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001045 info = grid_info
1046 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001047 return self._getints(
1048 self.tk.call(
1049 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001050 location = grid_location
1051 propagate = grid_propagate = Misc.grid_propagate
1052 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1053 size = grid_size = Misc.grid_size
1054 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001055
Guido van Rossum368e06b1997-11-07 20:38:49 +00001056class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001057 def _setup(self, master, cnf):
Guido van Rossumc4570481998-03-20 20:45:49 +00001058 if _support_default_root:
1059 global _default_root
1060 if not master:
1061 if not _default_root:
1062 _default_root = Tk()
1063 master = _default_root
Guido van Rossum18468821994-06-20 07:49:28 +00001064 self.master = master
1065 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001066 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001067 if cnf.has_key('name'):
1068 name = cnf['name']
1069 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001070 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001071 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001072 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001073 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001074 self._w = '.' + name
1075 else:
1076 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001077 self.children = {}
1078 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001079 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001080 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001081 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1082 if kw:
1083 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001084 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001085 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001086 classes = []
1087 for k in cnf.keys():
1088 if type(k) is ClassType:
1089 classes.append((k, cnf[k]))
1090 del cnf[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001091 self.tk.call(
1092 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001093 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001094 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001095 def destroy(self):
1096 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001097 if self.master.children.has_key(self._name):
1098 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001099 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001100 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001101 def _do(self, name, args=()):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001102 # XXX Obsolete -- better use self.tk.call directly!
Guido van Rossumf9756991998-04-29 21:57:08 +00001103 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001104
Guido van Rossum368e06b1997-11-07 20:38:49 +00001105class Widget(BaseWidget, Pack, Place, Grid):
1106 pass
1107
1108class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001109 def __init__(self, master=None, cnf={}, **kw):
1110 if kw:
1111 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001112 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001113 for wmkey in ['screen', 'class_', 'class', 'visual',
1114 'colormap']:
1115 if cnf.has_key(wmkey):
1116 val = cnf[wmkey]
1117 # TBD: a hack needed because some keys
1118 # are not valid as keyword arguments
1119 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1120 else: opt = '-'+wmkey
1121 extra = extra + (opt, val)
1122 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001123 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001124 root = self._root()
1125 self.iconname(root.iconname())
1126 self.title(root.title())
Guido van Rossume61e98d1999-08-20 18:26:06 +00001127 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum18468821994-06-20 07:49:28 +00001128
1129class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001130 def __init__(self, master=None, cnf={}, **kw):
1131 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001132 def tkButtonEnter(self, *dummy):
1133 self.tk.call('tkButtonEnter', self._w)
1134 def tkButtonLeave(self, *dummy):
1135 self.tk.call('tkButtonLeave', self._w)
1136 def tkButtonDown(self, *dummy):
1137 self.tk.call('tkButtonDown', self._w)
1138 def tkButtonUp(self, *dummy):
1139 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001140 def tkButtonInvoke(self, *dummy):
1141 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001142 def flash(self):
1143 self.tk.call(self._w, 'flash')
1144 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001145 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001146
1147# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001148# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001149def AtEnd():
1150 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001151def AtInsert(*args):
1152 s = 'insert'
1153 for a in args:
1154 if a: s = s + (' ' + a)
1155 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001156def AtSelFirst():
1157 return 'sel.first'
1158def AtSelLast():
1159 return 'sel.last'
1160def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001161 if y is None:
1162 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001163 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001164 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001165
1166class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001167 def __init__(self, master=None, cnf={}, **kw):
1168 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001169 def addtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001170 self.tk.call((self._w, 'addtag') + args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001171 def addtag_above(self, newtag, tagOrId):
1172 self.addtag(newtag, 'above', tagOrId)
1173 def addtag_all(self, newtag):
1174 self.addtag(newtag, 'all')
1175 def addtag_below(self, newtag, tagOrId):
1176 self.addtag(newtag, 'below', tagOrId)
1177 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1178 self.addtag(newtag, 'closest', x, y, halo, start)
1179 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1180 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1181 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1182 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1183 def addtag_withtag(self, newtag, tagOrId):
1184 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001185 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001186 return self._getints(
1187 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001188 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001189 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001190 if funcid:
1191 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001192 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum42b334d1999-07-30 12:22:12 +00001193 return self._bind((self._w, 'bind', tagOrId),
1194 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001195 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001196 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001197 self._w, 'canvasx', screenx, gridspacing))
1198 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001199 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001200 self._w, 'canvasy', screeny, gridspacing))
1201 def coords(self, *args):
Guido van Rossum0001a111998-02-19 21:20:30 +00001202 # XXX Should use _flatten on args
Guido van Rossum0132f691998-04-30 17:50:36 +00001203 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00001204 self.tk.splitlist(
1205 self.tk.call((self._w, 'coords') + args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001206 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001207 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001208 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001209 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001210 args = args[:-1]
1211 else:
1212 cnf = {}
Guido van Rossum0132f691998-04-30 17:50:36 +00001213 return getint(apply(
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001214 self.tk.call,
1215 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001216 + args + self._options(cnf, kw)))
1217 def create_arc(self, *args, **kw):
1218 return self._create('arc', args, kw)
1219 def create_bitmap(self, *args, **kw):
1220 return self._create('bitmap', args, kw)
1221 def create_image(self, *args, **kw):
1222 return self._create('image', args, kw)
1223 def create_line(self, *args, **kw):
1224 return self._create('line', args, kw)
1225 def create_oval(self, *args, **kw):
1226 return self._create('oval', args, kw)
1227 def create_polygon(self, *args, **kw):
1228 return self._create('polygon', args, kw)
1229 def create_rectangle(self, *args, **kw):
1230 return self._create('rectangle', args, kw)
1231 def create_text(self, *args, **kw):
1232 return self._create('text', args, kw)
1233 def create_window(self, *args, **kw):
1234 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001235 def dchars(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001236 self.tk.call((self._w, 'dchars') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001237 def delete(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001238 self.tk.call((self._w, 'delete') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001239 def dtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001240 self.tk.call((self._w, 'dtag') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001241 def find(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001242 return self._getints(
1243 self.tk.call((self._w, 'find') + args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001244 def find_above(self, tagOrId):
1245 return self.find('above', tagOrId)
1246 def find_all(self):
1247 return self.find('all')
1248 def find_below(self, tagOrId):
1249 return self.find('below', tagOrId)
1250 def find_closest(self, x, y, halo=None, start=None):
1251 return self.find('closest', x, y, halo, start)
1252 def find_enclosed(self, x1, y1, x2, y2):
1253 return self.find('enclosed', x1, y1, x2, y2)
1254 def find_overlapping(self, x1, y1, x2, y2):
1255 return self.find('overlapping', x1, y1, x2, y2)
1256 def find_withtag(self, tagOrId):
1257 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001258 def focus(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001259 return self.tk.call((self._w, 'focus') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001260 def gettags(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001261 return self.tk.splitlist(
1262 self.tk.call((self._w, 'gettags') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001263 def icursor(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001264 self.tk.call((self._w, 'icursor') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001265 def index(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001266 return getint(self.tk.call((self._w, 'index') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001267 def insert(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001268 self.tk.call((self._w, 'insert') + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001269 def itemcget(self, tagOrId, option):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001270 return self.tk.call(
1271 (self._w, 'itemcget') + (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001272 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001273 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001274 cnf = {}
1275 for x in self.tk.split(
Guido van Rossum0bd54331998-05-19 21:18:13 +00001276 self.tk.call(self._w,
1277 'itemconfigure', tagOrId)):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001278 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1279 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001280 if type(cnf) == StringType and not kw:
Guido van Rossum0bd54331998-05-19 21:18:13 +00001281 x = self.tk.split(self.tk.call(
1282 self._w, 'itemconfigure', tagOrId, '-'+cnf))
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001283 return (x[0][1:],) + x[1:]
Guido van Rossum0bd54331998-05-19 21:18:13 +00001284 self.tk.call((self._w, 'itemconfigure', tagOrId) +
1285 self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001286 itemconfig = itemconfigure
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001287 # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
1288 # so the preferred name for them is tag_lower, tag_raise
1289 # (similar to tag_bind, and similar to the Text widget);
1290 # unfortunately can't delete the old ones yet (maybe in 1.6)
1291 def tag_lower(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001292 self.tk.call((self._w, 'lower') + args)
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001293 lower = tag_lower
Guido van Rossum18468821994-06-20 07:49:28 +00001294 def move(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001295 self.tk.call((self._w, 'move') + args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001296 def postscript(self, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001297 return self.tk.call((self._w, 'postscript') +
1298 self._options(cnf, kw))
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001299 def tag_raise(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001300 self.tk.call((self._w, 'raise') + args)
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00001301 lift = tkraise = tag_raise
Guido van Rossum18468821994-06-20 07:49:28 +00001302 def scale(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001303 self.tk.call((self._w, 'scale') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001304 def scan_mark(self, x, y):
1305 self.tk.call(self._w, 'scan', 'mark', x, y)
1306 def scan_dragto(self, x, y):
1307 self.tk.call(self._w, 'scan', 'dragto', x, y)
1308 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001309 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001310 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001311 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001312 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001313 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001314 def select_item(self):
1315 self.tk.call(self._w, 'select', 'item')
1316 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001317 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001318 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001319 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001320 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001321 if not args:
1322 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001323 self.tk.call((self._w, 'xview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001324 def xview_moveto(self, fraction):
1325 self.tk.call(self._w, 'xview', 'moveto', fraction)
1326 def xview_scroll(self, number, what):
1327 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001328 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001329 if not args:
1330 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001331 self.tk.call((self._w, 'yview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001332 def yview_moveto(self, fraction):
1333 self.tk.call(self._w, 'yview', 'moveto', fraction)
1334 def yview_scroll(self, number, what):
1335 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001336
1337class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001338 def __init__(self, master=None, cnf={}, **kw):
1339 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001340 def deselect(self):
1341 self.tk.call(self._w, 'deselect')
1342 def flash(self):
1343 self.tk.call(self._w, 'flash')
1344 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001345 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001346 def select(self):
1347 self.tk.call(self._w, 'select')
1348 def toggle(self):
1349 self.tk.call(self._w, 'toggle')
1350
1351class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001352 def __init__(self, master=None, cnf={}, **kw):
1353 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001354 def delete(self, first, last=None):
1355 self.tk.call(self._w, 'delete', first, last)
1356 def get(self):
1357 return self.tk.call(self._w, 'get')
1358 def icursor(self, index):
1359 self.tk.call(self._w, 'icursor', index)
1360 def index(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001361 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001362 self._w, 'index', index))
1363 def insert(self, index, string):
1364 self.tk.call(self._w, 'insert', index, string)
1365 def scan_mark(self, x):
1366 self.tk.call(self._w, 'scan', 'mark', x)
1367 def scan_dragto(self, x):
1368 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001369 def selection_adjust(self, index):
1370 self.tk.call(self._w, 'selection', 'adjust', index)
1371 select_adjust = selection_adjust
1372 def selection_clear(self):
1373 self.tk.call(self._w, 'selection', 'clear')
1374 select_clear = selection_clear
1375 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001376 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001377 select_from = selection_from
1378 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001379 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001380 self.tk.call(self._w, 'selection', 'present'))
1381 select_present = selection_present
1382 def selection_range(self, start, end):
1383 self.tk.call(self._w, 'selection', 'range', start, end)
1384 select_range = selection_range
1385 def selection_to(self, index):
1386 self.tk.call(self._w, 'selection', 'to', index)
1387 select_to = selection_to
1388 def xview(self, index):
1389 self.tk.call(self._w, 'xview', index)
1390 def xview_moveto(self, fraction):
1391 self.tk.call(self._w, 'xview', 'moveto', fraction)
1392 def xview_scroll(self, number, what):
1393 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001394
1395class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001396 def __init__(self, master=None, cnf={}, **kw):
1397 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001398 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001399 if cnf.has_key('class_'):
1400 extra = ('-class', cnf['class_'])
1401 del cnf['class_']
1402 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001403 extra = ('-class', cnf['class'])
1404 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001405 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001406
1407class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001408 def __init__(self, master=None, cnf={}, **kw):
1409 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001410
Guido van Rossum18468821994-06-20 07:49:28 +00001411class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001412 def __init__(self, master=None, cnf={}, **kw):
1413 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001414 def activate(self, index):
1415 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001416 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001417 return self._getints(
1418 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001419 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001420 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001421 return self.tk.splitlist(self.tk.call(
1422 self._w, 'curselection'))
1423 def delete(self, first, last=None):
1424 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001425 def get(self, first, last=None):
1426 if last:
1427 return self.tk.splitlist(self.tk.call(
1428 self._w, 'get', first, last))
1429 else:
1430 return self.tk.call(self._w, 'get', first)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001431 def index(self, index):
1432 i = self.tk.call(self._w, 'index', index)
1433 if i == 'none': return None
1434 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001435 def insert(self, index, *elements):
Guido van Rossumf9756991998-04-29 21:57:08 +00001436 self.tk.call((self._w, 'insert', index) + elements)
Guido van Rossum18468821994-06-20 07:49:28 +00001437 def nearest(self, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001438 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001439 self._w, 'nearest', y))
1440 def scan_mark(self, x, y):
1441 self.tk.call(self._w, 'scan', 'mark', x, y)
1442 def scan_dragto(self, x, y):
1443 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001444 def see(self, index):
1445 self.tk.call(self._w, 'see', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001446 def selection_anchor(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001447 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001448 select_anchor = selection_anchor
1449 def selection_clear(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001450 self.tk.call(self._w,
1451 'selection', 'clear', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001452 select_clear = selection_clear
1453 def selection_includes(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001454 return self.tk.getboolean(self.tk.call(
1455 self._w, 'selection', 'includes', index))
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001456 select_includes = selection_includes
1457 def selection_set(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001458 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001459 select_set = selection_set
Guido van Rossum18468821994-06-20 07:49:28 +00001460 def size(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001461 return getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001462 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001463 if not what:
1464 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001465 self.tk.call((self._w, 'xview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001466 def xview_moveto(self, fraction):
1467 self.tk.call(self._w, 'xview', 'moveto', fraction)
1468 def xview_scroll(self, number, what):
1469 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001470 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001471 if not what:
1472 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001473 self.tk.call((self._w, 'yview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001474 def yview_moveto(self, fraction):
1475 self.tk.call(self._w, 'yview', 'moveto', fraction)
1476 def yview_scroll(self, number, what):
1477 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001478
1479class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001480 def __init__(self, master=None, cnf={}, **kw):
1481 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001482 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001483 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001484 def tk_mbPost(self):
1485 self.tk.call('tk_mbPost', self._w)
1486 def tk_mbUnpost(self):
1487 self.tk.call('tk_mbUnpost')
1488 def tk_traverseToMenu(self, char):
1489 self.tk.call('tk_traverseToMenu', self._w, char)
1490 def tk_traverseWithinMenu(self, char):
1491 self.tk.call('tk_traverseWithinMenu', self._w, char)
1492 def tk_getMenuButtons(self):
1493 return self.tk.call('tk_getMenuButtons', self._w)
1494 def tk_nextMenu(self, count):
1495 self.tk.call('tk_nextMenu', count)
1496 def tk_nextMenuEntry(self, count):
1497 self.tk.call('tk_nextMenuEntry', count)
1498 def tk_invokeMenu(self):
1499 self.tk.call('tk_invokeMenu', self._w)
1500 def tk_firstMenu(self):
1501 self.tk.call('tk_firstMenu', self._w)
1502 def tk_mbButtonDown(self):
1503 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001504 def tk_popup(self, x, y, entry=""):
1505 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001506 def activate(self, index):
1507 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001508 def add(self, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001509 self.tk.call((self._w, 'add', itemType) +
1510 self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001511 def add_cascade(self, cnf={}, **kw):
1512 self.add('cascade', cnf or kw)
1513 def add_checkbutton(self, cnf={}, **kw):
1514 self.add('checkbutton', cnf or kw)
1515 def add_command(self, cnf={}, **kw):
1516 self.add('command', cnf or kw)
1517 def add_radiobutton(self, cnf={}, **kw):
1518 self.add('radiobutton', cnf or kw)
1519 def add_separator(self, cnf={}, **kw):
1520 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001521 def insert(self, index, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001522 self.tk.call((self._w, 'insert', index, itemType) +
1523 self._options(cnf, kw))
Guido van Rossum2caac731996-09-05 16:46:31 +00001524 def insert_cascade(self, index, cnf={}, **kw):
1525 self.insert(index, 'cascade', cnf or kw)
1526 def insert_checkbutton(self, index, cnf={}, **kw):
1527 self.insert(index, 'checkbutton', cnf or kw)
1528 def insert_command(self, index, cnf={}, **kw):
1529 self.insert(index, 'command', cnf or kw)
1530 def insert_radiobutton(self, index, cnf={}, **kw):
1531 self.insert(index, 'radiobutton', cnf or kw)
1532 def insert_separator(self, index, cnf={}, **kw):
1533 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001534 def delete(self, index1, index2=None):
1535 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001536 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001537 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001538 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001539 if cnf is None and not kw:
1540 cnf = {}
Guido van Rossumf9756991998-04-29 21:57:08 +00001541 for x in self.tk.split(self.tk.call(
1542 (self._w, 'entryconfigure', index))):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001543 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1544 return cnf
1545 if type(cnf) == StringType and not kw:
Guido van Rossumf9756991998-04-29 21:57:08 +00001546 x = self.tk.split(self.tk.call(
1547 (self._w, 'entryconfigure', index, '-'+cnf)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001548 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001549 self.tk.call((self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001550 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001551 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001552 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001553 i = self.tk.call(self._w, 'index', index)
1554 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00001555 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001556 def invoke(self, index):
1557 return self.tk.call(self._w, 'invoke', index)
1558 def post(self, x, y):
1559 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001560 def type(self, index):
1561 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001562 def unpost(self):
1563 self.tk.call(self._w, 'unpost')
1564 def yposition(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001565 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001566 self._w, 'yposition', index))
1567
1568class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001569 def __init__(self, master=None, cnf={}, **kw):
1570 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001571
1572class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001573 def __init__(self, master=None, cnf={}, **kw):
1574 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001575
1576class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001577 def __init__(self, master=None, cnf={}, **kw):
1578 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001579 def deselect(self):
1580 self.tk.call(self._w, 'deselect')
1581 def flash(self):
1582 self.tk.call(self._w, 'flash')
1583 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001584 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001585 def select(self):
1586 self.tk.call(self._w, 'select')
1587
1588class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001589 def __init__(self, master=None, cnf={}, **kw):
1590 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001591 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001592 value = self.tk.call(self._w, 'get')
1593 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00001594 return getint(value)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00001595 except ValueError:
Guido van Rossum0132f691998-04-30 17:50:36 +00001596 return getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001597 def set(self, value):
1598 self.tk.call(self._w, 'set', value)
Guido van Rossumb4750db1998-08-11 19:07:14 +00001599 def coords(self, value=None):
1600 return self._getints(self.tk.call(self._w, 'coords', value))
1601 def identify(self, x, y):
1602 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001603
1604class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001605 def __init__(self, master=None, cnf={}, **kw):
1606 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001607 def activate(self, index):
1608 self.tk.call(self._w, 'activate', index)
1609 def delta(self, deltax, deltay):
Guido van Rossum0132f691998-04-30 17:50:36 +00001610 return getdouble(
1611 self.tk.call(self._w, 'delta', deltax, deltay))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001612 def fraction(self, x, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001613 return getdouble(self.tk.call(self._w, 'fraction', x, y))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001614 def identify(self, x, y):
1615 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001616 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001617 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001618 def set(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001619 self.tk.call((self._w, 'set') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001620
1621class Text(Widget):
Guido van Rossumdfaac4d1998-12-21 18:25:03 +00001622 # XXX Add dump()
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001623 def __init__(self, master=None, cnf={}, **kw):
1624 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001625 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001626 return self._getints(
1627 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001628 def tk_textSelectTo(self, index):
1629 self.tk.call('tk_textSelectTo', self._w, index)
1630 def tk_textBackspace(self):
1631 self.tk.call('tk_textBackspace', self._w)
1632 def tk_textIndexCloser(self, a, b, c):
1633 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1634 def tk_textResetAnchor(self, index):
1635 self.tk.call('tk_textResetAnchor', self._w, index)
1636 def compare(self, index1, op, index2):
1637 return self.tk.getboolean(self.tk.call(
1638 self._w, 'compare', index1, op, index2))
1639 def debug(self, boolean=None):
1640 return self.tk.getboolean(self.tk.call(
1641 self._w, 'debug', boolean))
1642 def delete(self, index1, index2=None):
1643 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001644 def dlineinfo(self, index):
1645 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001646 def get(self, index1, index2=None):
1647 return self.tk.call(self._w, 'get', index1, index2)
Guido van Rossumc86b7c61998-08-31 16:54:33 +00001648 # (Image commands are new in 8.0)
1649 def image_cget(self, index, option):
1650 if option[:1] != "-":
1651 option = "-" + option
1652 if option[-1:] == "_":
1653 option = option[:-1]
1654 return self.tk.call(self._w, "image", "cget", index, option)
1655 def image_configure(self, index, cnf={}, **kw):
1656 if not cnf and not kw:
1657 cnf = {}
1658 for x in self.tk.split(
1659 self.tk.call(
1660 self._w, "image", "configure", index)):
1661 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1662 return cnf
1663 apply(self.tk.call,
1664 (self._w, "image", "configure", index)
1665 + self._options(cnf, kw))
1666 def image_create(self, index, cnf={}, **kw):
1667 return apply(self.tk.call,
1668 (self._w, "image", "create", index)
1669 + self._options(cnf, kw))
1670 def image_names(self):
1671 return self.tk.call(self._w, "image", "names")
Guido van Rossum18468821994-06-20 07:49:28 +00001672 def index(self, index):
1673 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001674 def insert(self, index, chars, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001675 self.tk.call((self._w, 'insert', index, chars) + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001676 def mark_gravity(self, markName, direction=None):
Guido van Rossumf9756991998-04-29 21:57:08 +00001677 return self.tk.call(
1678 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001679 def mark_names(self):
1680 return self.tk.splitlist(self.tk.call(
1681 self._w, 'mark', 'names'))
1682 def mark_set(self, markName, index):
1683 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001684 def mark_unset(self, *markNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001685 self.tk.call((self._w, 'mark', 'unset') + markNames)
Guido van Rossum1a03cf51999-06-21 14:13:30 +00001686 def mark_next(self, index):
1687 return self.tk.call(self._w, 'mark', 'next', index) or None
1688 def mark_previous(self, index):
1689 return self.tk.call(self._w, 'mark', 'previous', index) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +00001690 def scan_mark(self, x, y):
1691 self.tk.call(self._w, 'scan', 'mark', x, y)
1692 def scan_dragto(self, x, y):
1693 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001694 def search(self, pattern, index, stopindex=None,
1695 forwards=None, backwards=None, exact=None,
1696 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001697 args = [self._w, 'search']
1698 if forwards: args.append('-forwards')
1699 if backwards: args.append('-backwards')
1700 if exact: args.append('-exact')
1701 if regexp: args.append('-regexp')
1702 if nocase: args.append('-nocase')
1703 if count: args.append('-count'); args.append(count)
1704 if pattern[0] == '-': args.append('--')
1705 args.append(pattern)
1706 args.append(index)
1707 if stopindex: args.append(stopindex)
Guido van Rossumf9756991998-04-29 21:57:08 +00001708 return self.tk.call(tuple(args))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001709 def see(self, index):
1710 self.tk.call(self._w, 'see', index)
Guido van Rossumfa8c3ea1999-06-01 13:57:15 +00001711 def tag_add(self, tagName, index1, *args):
Guido van Rossum18468821994-06-20 07:49:28 +00001712 self.tk.call(
Guido van Rossumfa8c3ea1999-06-01 13:57:15 +00001713 (self._w, 'tag', 'add', tagName, index1) + args)
Guido van Rossum117a5a81998-03-27 21:26:51 +00001714 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001715 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001716 if funcid:
1717 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001718 def tag_bind(self, tagName, sequence, func, add=None):
1719 return self._bind((self._w, 'tag', 'bind', tagName),
1720 sequence, func, add)
1721 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001722 if option[:1] != '-':
1723 option = '-' + option
1724 if option[-1:] == '_':
1725 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001726 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001727 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001728 if type(cnf) == StringType:
1729 x = self.tk.split(self.tk.call(
1730 self._w, 'tag', 'configure', tagName, '-'+cnf))
1731 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001732 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001733 (self._w, 'tag', 'configure', tagName)
1734 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001735 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001736 def tag_delete(self, *tagNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001737 self.tk.call((self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001738 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001739 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001740 def tag_names(self, index=None):
1741 return self.tk.splitlist(
1742 self.tk.call(self._w, 'tag', 'names', index))
1743 def tag_nextrange(self, tagName, index1, index2=None):
1744 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001745 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001746 def tag_prevrange(self, tagName, index1, index2=None):
1747 return self.tk.splitlist(self.tk.call(
1748 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001749 def tag_raise(self, tagName, aboveThis=None):
1750 self.tk.call(
1751 self._w, 'tag', 'raise', tagName, aboveThis)
1752 def tag_ranges(self, tagName):
1753 return self.tk.splitlist(self.tk.call(
1754 self._w, 'tag', 'ranges', tagName))
1755 def tag_remove(self, tagName, index1, index2=None):
1756 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001757 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001758 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001759 if option[:1] != '-':
1760 option = '-' + option
1761 if option[-1:] == '_':
1762 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001763 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001764 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001765 if type(cnf) == StringType:
1766 x = self.tk.split(self.tk.call(
1767 self._w, 'window', 'configure',
1768 index, '-'+cnf))
1769 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001770 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001771 (self._w, 'window', 'configure', index)
1772 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001773 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001774 def window_create(self, index, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001775 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001776 (self._w, 'window', 'create', index)
1777 + self._options(cnf, kw))
1778 def window_names(self):
1779 return self.tk.splitlist(
1780 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001781 def xview(self, *what):
1782 if not what:
1783 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001784 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001785 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001786 if not what:
1787 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001788 self.tk.call((self._w, 'yview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001789 def yview_pickplace(self, *what):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001790 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00001791
Guido van Rossum28574b51996-10-21 15:16:51 +00001792class _setit:
1793 def __init__(self, var, value):
1794 self.__value = value
1795 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001796 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001797 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001798
1799class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001800 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001801 kw = {"borderwidth": 2, "textvariable": variable,
1802 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1803 "highlightthickness": 2}
1804 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001805 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001806 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1807 self.menuname = menu._w
1808 menu.add_command(label=value, command=_setit(variable, value))
1809 for v in values:
1810 menu.add_command(label=v, command=_setit(variable, v))
1811 self["menu"] = menu
1812
1813 def __getitem__(self, name):
1814 if name == 'menu':
1815 return self.__menu
1816 return Widget.__getitem__(self, name)
1817
1818 def destroy(self):
1819 Menubutton.destroy(self)
1820 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001821
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001822class Image:
Guido van Rossumc4570481998-03-20 20:45:49 +00001823 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001824 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00001825 if not master:
1826 master = _default_root
1827 if not master:
1828 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001829 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001830 if not name:
1831 name = `id(self)`
1832 # The following is needed for systems where id(x)
1833 # can return a negative number, such as Linux/m68k:
1834 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001835 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1836 elif kw: cnf = kw
1837 options = ()
1838 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001839 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001840 v = self._register(v)
1841 options = options + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001842 self.tk.call(('image', 'create', imgtype, name,) + options)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001843 self.name = name
1844 def __str__(self): return self.name
1845 def __del__(self):
1846 if self.name:
Guido van Rossumc55b0ca1999-02-08 15:26:49 +00001847 try:
1848 self.tk.call('image', 'delete', self.name)
1849 except TclError:
1850 # May happen if the root was destroyed
1851 pass
Guido van Rossum71b1a901995-09-18 21:54:35 +00001852 def __setitem__(self, key, value):
1853 self.tk.call(self.name, 'configure', '-'+key, value)
1854 def __getitem__(self, key):
1855 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001856 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001857 res = ()
1858 for k, v in _cnfmerge(kw).items():
1859 if v is not None:
1860 if k[-1] == '_': k = k[:-1]
1861 if callable(v):
1862 v = self._register(v)
1863 res = res + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001864 self.tk.call((self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001865 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001866 def height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001867 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001868 self.tk.call('image', 'height', self.name))
1869 def type(self):
1870 return self.tk.call('image', 'type', self.name)
1871 def width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001872 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001873 self.tk.call('image', 'width', self.name))
1874
1875class PhotoImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001876 def __init__(self, name=None, cnf={}, master=None, **kw):
1877 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001878 def blank(self):
1879 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001880 def cget(self, option):
1881 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001882 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001883 def __getitem__(self, key):
1884 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001885 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001886 def copy(self):
1887 destImage = PhotoImage()
1888 self.tk.call(destImage, 'copy', self.name)
1889 return destImage
1890 def zoom(self,x,y=''):
1891 destImage = PhotoImage()
1892 if y=='': y=x
1893 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1894 return destImage
1895 def subsample(self,x,y=''):
1896 destImage = PhotoImage()
1897 if y=='': y=x
1898 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1899 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001900 def get(self, x, y):
1901 return self.tk.call(self.name, 'get', x, y)
1902 def put(self, data, to=None):
1903 args = (self.name, 'put', data)
1904 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001905 if to[0] == '-to':
1906 to = to[1:]
1907 args = args + ('-to',) + tuple(to)
Guido van Rossumf9756991998-04-29 21:57:08 +00001908 self.tk.call(args)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001909 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001910 def write(self, filename, format=None, from_coords=None):
1911 args = (self.name, 'write', filename)
1912 if format:
1913 args = args + ('-format', format)
1914 if from_coords:
1915 args = args + ('-from',) + tuple(from_coords)
Guido van Rossumf9756991998-04-29 21:57:08 +00001916 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001917
1918class BitmapImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001919 def __init__(self, name=None, cnf={}, master=None, **kw):
1920 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001921
1922def image_names(): return _default_root.tk.call('image', 'names')
1923def image_types(): return _default_root.tk.call('image', 'types')
1924
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001925######################################################################
1926# Extensions:
1927
1928class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001929 def __init__(self, master=None, cnf={}, **kw):
1930 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001931 self.bind('<Any-Enter>', self.tkButtonEnter)
1932 self.bind('<Any-Leave>', self.tkButtonLeave)
1933 self.bind('<1>', self.tkButtonDown)
1934 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001935
1936class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001937 def __init__(self, master=None, cnf={}, **kw):
1938 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001939 self.bind('<Any-Enter>', self.tkButtonEnter)
1940 self.bind('<Any-Leave>', self.tkButtonLeave)
1941 self.bind('<1>', self.tkButtonDown)
1942 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1943 self['fg'] = self['bg']
1944 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001945
Guido van Rossumc417ef81996-08-21 23:38:59 +00001946######################################################################
1947# Test:
1948
1949def _test():
1950 root = Tk()
1951 label = Label(root, text="Proof-of-existence test for Tk")
1952 label.pack()
1953 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001954 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001955 text="[%s]" % root.test['text']))
1956 test.pack()
1957 root.test = test
1958 quit = Button(root, text="QUIT", command=root.destroy)
1959 quit.pack()
Guido van Rossum268824e1998-06-19 04:34:19 +00001960 # The following three commands are needed so the window pops
1961 # up on top on Windows...
1962 root.iconify()
1963 root.update()
1964 root.deiconify()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001965 root.mainloop()
1966
1967if __name__ == '__main__':
1968 _test()