blob: 0f4c7135337c09a8e5532afda2d78f346a2d0610 [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum37dcab11996-05-16 16:00:19 +00003__version__ = "$Revision$"
4
Guido van Rossum95806091997-02-15 18:33:24 +00005import _tkinter # If this fails your Python is not configured for Tk
6tkinter = _tkinter # b/w compat for export
7TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00008from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +00009from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000010import string; _string = string; del string
Guido van Rossumf0c891a1998-04-29 21:43:36 +000011try:
12 import MacOS; _MacOS = MacOS; del MacOS
13except ImportError:
14 _MacOS = None
Guido van Rossum18468821994-06-20 07:49:28 +000015
Guido van Rossum95806091997-02-15 18:33:24 +000016TkVersion = _string.atof(_tkinter.TK_VERSION)
17TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000018
Guido van Rossumd6615ab1997-08-05 02:35:01 +000019READABLE = _tkinter.READABLE
20WRITABLE = _tkinter.WRITABLE
21EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000022
23# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
24try: _tkinter.createfilehandler
25except AttributeError: _tkinter.createfilehandler = None
26try: _tkinter.deletefilehandler
27except AttributeError: _tkinter.deletefilehandler = None
Guido van Rossum36269991996-05-16 17:11:27 +000028
29
Guido van Rossum2dcf5291994-07-06 09:23:20 +000030def _flatten(tuple):
31 res = ()
32 for item in tuple:
33 if type(item) in (TupleType, ListType):
34 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000035 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000036 res = res + (item,)
37 return res
38
39def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000040 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000041 return cnfs
42 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000043 return cnfs
44 else:
45 cnf = {}
46 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000047 try:
48 cnf.update(c)
49 except (AttributeError, TypeError), msg:
50 print "_cnfmerge: fallback due to:", msg
51 for k, v in c.items():
52 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000053 return cnf
54
55class Event:
56 pass
57
Guido van Rossumc4570481998-03-20 20:45:49 +000058_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +000059_default_root = None
60
Guido van Rossumc4570481998-03-20 20:45:49 +000061def NoDefaultRoot():
62 global _support_default_root
63 _support_default_root = 0
Guido van Rossumda654501998-10-06 19:06:27 +000064 global _default_root
65 _default_root = None
Guido van Rossumc4570481998-03-20 20:45:49 +000066 del _default_root
67
Guido van Rossum45853db1994-06-20 12:19:19 +000068def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000069 pass
70
Guido van Rossum97aeca11994-07-07 13:12:12 +000071def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000072 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000073
Guido van Rossumaec5dc91994-06-27 07:55:12 +000074_varnum = 0
75class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000076 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000077 def __init__(self, master=None):
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +000079 if not master:
80 master = _default_root
81 self._master = master
82 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +000083 self._name = 'PY_VAR' + `_varnum`
84 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000085 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000086 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000087 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000088 def __str__(self):
89 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000090 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000091 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +000092 def trace_variable(self, mode, callback):
93 cbname = self._master._register(callback)
94 self._tk.call("trace", "variable", self._name, mode, cbname)
95 return cbname
96 trace = trace_variable
97 def trace_vdelete(self, mode, cbname):
98 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Guido van Rossum0001a111998-02-19 21:20:30 +000099 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +0000100 def trace_vinfo(self):
101 return map(self._tk.split, self._tk.splitlist(
102 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000103
104class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000105 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000106 def __init__(self, master=None):
107 Variable.__init__(self, master)
108 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000109 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000110
111class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000112 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000113 def __init__(self, master=None):
114 Variable.__init__(self, master)
115 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000116 return getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000117
118class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000119 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000120 def __init__(self, master=None):
121 Variable.__init__(self, master)
122 def get(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000123 return getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000124
125class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000126 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000127 def __init__(self, master=None):
128 Variable.__init__(self, master)
129 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000130 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000131
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000132def mainloop(n=0):
133 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000134
Guido van Rossum0132f691998-04-30 17:50:36 +0000135getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000136
Guido van Rossum0132f691998-04-30 17:50:36 +0000137getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000138
139def getboolean(s):
140 return _default_root.tk.getboolean(s)
141
Guido van Rossum368e06b1997-11-07 20:38:49 +0000142# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000143class Misc:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000144 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000145 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000146 def destroy(self):
147 if self._tclCommands is not None:
148 for name in self._tclCommands:
149 #print '- Tkinter: deleted command', name
150 self.tk.deletecommand(name)
151 self._tclCommands = None
152 def deletecommand(self, name):
153 #print '- Tkinter: deleted command', name
154 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000155 try:
156 self._tclCommands.remove(name)
157 except ValueError:
158 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000159 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000160 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000161 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000162 def tk_bisque(self):
163 self.tk.call('tk_bisque')
164 def tk_setPalette(self, *args, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000165 self.tk.call(('tk_setPalette',)
Fred Drake3faf9b41996-10-04 19:23:04 +0000166 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000167 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000168 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000169 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000170 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000171 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000172 def wait_window(self, window=None):
173 if window == None:
174 window = self
175 self.tk.call('tkwait', 'window', window._w)
176 def wait_visibility(self, window=None):
177 if window == None:
178 window = self
179 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000180 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000181 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000182 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000183 return self.tk.getvar(name)
Guido van Rossum0132f691998-04-30 17:50:36 +0000184 getint = int
185 getdouble = float
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000186 def getboolean(self, s):
187 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000188 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000189 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000190 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000191 def focus_force(self):
192 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000193 def focus_get(self):
194 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000195 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000196 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000197 def focus_displayof(self):
198 name = self.tk.call('focus', '-displayof', self._w)
199 if name == 'none' or not name: return None
200 return self._nametowidget(name)
201 def focus_lastfor(self):
202 name = self.tk.call('focus', '-lastfor', self._w)
203 if name == 'none' or not name: return None
204 return self._nametowidget(name)
205 def tk_focusFollowsMouse(self):
206 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000207 def tk_focusNext(self):
208 name = self.tk.call('tk_focusNext', self._w)
209 if not name: return None
210 return self._nametowidget(name)
211 def tk_focusPrev(self):
212 name = self.tk.call('tk_focusPrev', self._w)
213 if not name: return None
214 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000215 def after(self, ms, func=None, *args):
216 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000217 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000218 self.tk.call('after', ms)
219 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000220 # XXX Disgusting hack to clean up after calling func
221 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000222 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000223 try:
224 apply(func, args)
225 finally:
Guido van Rossum0c920001998-09-14 19:06:39 +0000226 try:
227 self.deletecommand(tmp[0])
228 except TclError:
229 pass
Guido van Rossum08a40381994-06-21 11:44:21 +0000230 name = self._register(callit)
231 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000232 return self.tk.call('after', ms, name)
233 def after_idle(self, func, *args):
234 return apply(self.after, ('idle', func) + args)
235 def after_cancel(self, id):
236 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000237 def bell(self, displayof=0):
Guido van Rossumf9756991998-04-29 21:57:08 +0000238 self.tk.call(('bell',) + self._displayof(displayof))
Fred Drake3c602d71996-09-27 14:06:54 +0000239 # Clipboard handling:
240 def clipboard_clear(self, **kw):
241 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000242 self.tk.call(('clipboard', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000243 def clipboard_append(self, string, **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', 'append') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000246 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000247 # XXX grab current w/o window argument
248 def grab_current(self):
249 name = self.tk.call('grab', 'current', self._w)
250 if not name: return None
251 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000252 def grab_release(self):
253 self.tk.call('grab', 'release', self._w)
254 def grab_set(self):
255 self.tk.call('grab', 'set', self._w)
256 def grab_set_global(self):
257 self.tk.call('grab', 'set', '-global', self._w)
258 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000259 status = self.tk.call('grab', 'status', self._w)
260 if status == 'none': status = None
261 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000262 def lower(self, belowThis=None):
263 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000264 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000265 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000266 def option_clear(self):
267 self.tk.call('option', 'clear')
268 def option_get(self, name, className):
269 return self.tk.call('option', 'get', self._w, name, className)
270 def option_readfile(self, fileName, priority = None):
271 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000272 def selection_clear(self, **kw):
273 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000274 self.tk.call(('selection', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000275 def selection_get(self, **kw):
276 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000277 return self.tk.call(('selection', 'get') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000278 def selection_handle(self, command, **kw):
279 name = self._register(command)
Guido van Rossumf9756991998-04-29 21:57:08 +0000280 self.tk.call(('selection', 'handle') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000281 + (self._w, name))
282 def selection_own(self, **kw):
283 "Become owner of X selection."
Guido van Rossumf9756991998-04-29 21:57:08 +0000284 self.tk.call(('selection', 'own') +
285 self._options(kw) + (self._w,))
Fred Drake3c602d71996-09-27 14:06:54 +0000286 def selection_own_get(self, **kw):
287 "Find owner of X selection."
288 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000289 name = self.tk.call(('selection', 'own') + self._options(kw))
Guido van Rossum76f587b1997-01-21 23:22:03 +0000290 if not name: return None
291 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000292 def send(self, interp, cmd, *args):
Guido van Rossumf9756991998-04-29 21:57:08 +0000293 return self.tk.call(('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000294 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000295 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000296 def tkraise(self, aboveThis=None):
297 self.tk.call('raise', self._w, aboveThis)
298 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000299 def colormodel(self, value=None):
300 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000301 def winfo_atom(self, name, displayof=0):
302 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Guido van Rossum0132f691998-04-30 17:50:36 +0000303 return getint(self.tk.call(args))
Fred Drake3c602d71996-09-27 14:06:54 +0000304 def winfo_atomname(self, id, displayof=0):
305 args = ('winfo', 'atomname') \
306 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000307 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000308 def winfo_cells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000309 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000310 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000311 def winfo_children(self):
312 return map(self._nametowidget,
313 self.tk.splitlist(self.tk.call(
314 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000315 def winfo_class(self):
316 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000317 def winfo_colormapfull(self):
318 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000319 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000320 def winfo_containing(self, rootX, rootY, displayof=0):
321 args = ('winfo', 'containing') \
322 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumf9756991998-04-29 21:57:08 +0000323 name = self.tk.call(args)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000324 if not name: return None
325 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000326 def winfo_depth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000327 return getint(self.tk.call('winfo', 'depth', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000328 def winfo_exists(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000329 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000330 self.tk.call('winfo', 'exists', self._w))
331 def winfo_fpixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000332 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000333 'winfo', 'fpixels', self._w, number))
334 def winfo_geometry(self):
335 return self.tk.call('winfo', 'geometry', self._w)
336 def winfo_height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000337 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000338 self.tk.call('winfo', 'height', self._w))
339 def winfo_id(self):
Guido van Rossumcef4c841998-06-19 04:35:45 +0000340 return self.tk.getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000341 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000342 def winfo_interps(self, displayof=0):
343 args = ('winfo', 'interps') + self._displayof(displayof)
Guido van Rossumf9756991998-04-29 21:57:08 +0000344 return self.tk.splitlist(self.tk.call(args))
Guido van Rossum18468821994-06-20 07:49:28 +0000345 def winfo_ismapped(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000346 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000347 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000348 def winfo_manager(self):
349 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000350 def winfo_name(self):
351 return self.tk.call('winfo', 'name', self._w)
352 def winfo_parent(self):
353 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000354 def winfo_pathname(self, id, displayof=0):
355 args = ('winfo', 'pathname') \
356 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000357 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000358 def winfo_pixels(self, number):
Guido van Rossum0132f691998-04-30 17:50:36 +0000359 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000360 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000361 def winfo_pointerx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000362 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000363 self.tk.call('winfo', 'pointerx', self._w))
364 def winfo_pointerxy(self):
365 return self._getints(
366 self.tk.call('winfo', 'pointerxy', self._w))
367 def winfo_pointery(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000368 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000369 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000370 def winfo_reqheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000371 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000372 self.tk.call('winfo', 'reqheight', self._w))
373 def winfo_reqwidth(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', 'reqwidth', self._w))
376 def winfo_rgb(self, color):
377 return self._getints(
378 self.tk.call('winfo', 'rgb', self._w, color))
379 def winfo_rootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000380 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000381 self.tk.call('winfo', 'rootx', self._w))
382 def winfo_rooty(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', 'rooty', self._w))
385 def winfo_screen(self):
386 return self.tk.call('winfo', 'screen', self._w)
387 def winfo_screencells(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000388 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000389 self.tk.call('winfo', 'screencells', self._w))
390 def winfo_screendepth(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', 'screendepth', self._w))
393 def winfo_screenheight(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', 'screenheight', self._w))
396 def winfo_screenmmheight(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', 'screenmmheight', self._w))
399 def winfo_screenmmwidth(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', 'screenmmwidth', self._w))
402 def winfo_screenvisual(self):
403 return self.tk.call('winfo', 'screenvisual', self._w)
404 def winfo_screenwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000405 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000406 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000407 def winfo_server(self):
408 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000409 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000410 return self._nametowidget(self.tk.call(
411 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000412 def winfo_viewable(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000413 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000414 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000415 def winfo_visual(self):
416 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000417 def winfo_visualid(self):
418 return self.tk.call('winfo', 'visualid', self._w)
419 def winfo_visualsavailable(self, includeids=0):
420 data = self.tk.split(
421 self.tk.call('winfo', 'visualsavailable', self._w,
422 includeids and 'includeids' or None))
423 def parseitem(x, self=self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000424 return x[:1] + tuple(map(getint, x[1:]))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000425 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000426 def winfo_vrootheight(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000427 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000428 self.tk.call('winfo', 'vrootheight', self._w))
429 def winfo_vrootwidth(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000430 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000431 self.tk.call('winfo', 'vrootwidth', self._w))
432 def winfo_vrootx(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000433 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000434 self.tk.call('winfo', 'vrootx', self._w))
435 def winfo_vrooty(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000436 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000437 self.tk.call('winfo', 'vrooty', self._w))
438 def winfo_width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000439 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000440 self.tk.call('winfo', 'width', self._w))
441 def winfo_x(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000442 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000443 self.tk.call('winfo', 'x', self._w))
444 def winfo_y(self):
Guido van Rossum0132f691998-04-30 17:50:36 +0000445 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000446 self.tk.call('winfo', 'y', self._w))
447 def update(self):
448 self.tk.call('update')
449 def update_idletasks(self):
450 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000451 def bindtags(self, tagList=None):
452 if tagList is None:
453 return self.tk.splitlist(
454 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000455 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000456 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000457 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum88b63b81998-06-25 18:54:49 +0000458 if type(func) is StringType:
459 self.tk.call(what + (sequence, func))
460 elif func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000461 funcid = self._register(func, self._substitute,
462 needcleanup)
Guido van Rossumdc593401998-04-29 22:16:57 +0000463 cmd = ('%sif {"[%s %s]" == "break"} break\n'
464 %
465 (add and '+' or '',
466 funcid,
467 _string.join(self._subst_format)))
Guido van Rossumf9756991998-04-29 21:57:08 +0000468 self.tk.call(what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000469 return funcid
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000470 elif sequence:
Guido van Rossumf9756991998-04-29 21:57:08 +0000471 return self.tk.call(what + (sequence,))
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000472 else:
473 return self.tk.splitlist(self.tk.call(what))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000474 def bind(self, sequence=None, func=None, add=None):
475 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000476 def unbind(self, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +0000477 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000478 if funcid:
479 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000480 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000481 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000482 def unbind_all(self, sequence):
483 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000484 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000485 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000486 def unbind_class(self, className, sequence):
487 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000488 def mainloop(self, n=0):
489 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000490 def quit(self):
491 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000492 def _getints(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000493 if string:
494 return tuple(map(getint, self.tk.splitlist(string)))
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000495 def _getdoubles(self, string):
Guido van Rossum0132f691998-04-30 17:50:36 +0000496 if string:
497 return tuple(map(getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000498 def _getboolean(self, string):
499 if string:
500 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000501 def _displayof(self, displayof):
502 if displayof:
503 return ('-displayof', displayof)
504 if displayof is None:
505 return ('-displayof', self._w)
506 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000507 def _options(self, cnf, kw = None):
508 if kw:
509 cnf = _cnfmerge((cnf, kw))
510 else:
511 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000512 res = ()
513 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000514 if v is not None:
515 if k[-1] == '_': k = k[:-1]
516 if callable(v):
517 v = self._register(v)
518 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000519 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000520 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000521 w = self
522 if name[0] == '.':
523 w = w._root()
524 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000525 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000526 while name:
527 i = find(name, '.')
528 if i >= 0:
529 name, tail = name[:i], name[i+1:]
530 else:
531 tail = ''
532 w = w.children[name]
533 name = tail
534 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000535 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000536 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000537 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000538 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000539 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000540 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000541 except AttributeError:
542 pass
543 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000544 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000545 except AttributeError:
546 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000547 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000548 if needcleanup:
549 if self._tclCommands is None:
550 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +0000551 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000552 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000553 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000554 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000555 def _root(self):
556 w = self
557 while w.master: w = w.master
558 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000559 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000560 '%s', '%t', '%w', '%x', '%y',
561 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
562 def _substitute(self, *args):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000563 if len(args) != len(self._subst_format): return args
Guido van Rossum0132f691998-04-30 17:50:36 +0000564 getboolean = self.tk.getboolean
565 getint = int
Guido van Rossum45853db1994-06-20 12:19:19 +0000566 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
567 # Missing: (a, c, d, m, o, v, B, R)
568 e = Event()
Guido van Rossum0132f691998-04-30 17:50:36 +0000569 e.serial = getint(nsign)
570 e.num = getint(b)
571 try: e.focus = getboolean(f)
Guido van Rossum45853db1994-06-20 12:19:19 +0000572 except TclError: pass
Guido van Rossum0132f691998-04-30 17:50:36 +0000573 e.height = getint(h)
574 e.keycode = getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000575 # For Visibility events, event state is a string and
576 # not an integer:
577 try:
Guido van Rossum0132f691998-04-30 17:50:36 +0000578 e.state = getint(s)
Guido van Rossumfe02efd1998-06-09 02:37:45 +0000579 except ValueError:
Guido van Rossum36269991996-05-16 17:11:27 +0000580 e.state = s
Guido van Rossum0132f691998-04-30 17:50:36 +0000581 e.time = getint(t)
582 e.width = getint(w)
583 e.x = getint(x)
584 e.y = getint(y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000585 e.char = A
Guido van Rossum0132f691998-04-30 17:50:36 +0000586 try: e.send_event = getboolean(E)
Guido van Rossum45853db1994-06-20 12:19:19 +0000587 except TclError: pass
588 e.keysym = K
Guido van Rossum0132f691998-04-30 17:50:36 +0000589 e.keysym_num = getint(N)
Guido van Rossum45853db1994-06-20 12:19:19 +0000590 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +0000591 try:
592 e.widget = self._nametowidget(W)
593 except KeyError:
594 e.widget = W
Guido van Rossum0132f691998-04-30 17:50:36 +0000595 e.x_root = getint(X)
596 e.y_root = getint(Y)
Guido van Rossum45853db1994-06-20 12:19:19 +0000597 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000598 def _report_exception(self):
599 import sys
600 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
601 root = self._root()
602 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000603 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000604 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000605 # XXX ought to generalize this so tag_config etc. can use it
606 if kw:
607 cnf = _cnfmerge((cnf, kw))
608 elif cnf:
609 cnf = _cnfmerge(cnf)
610 if cnf is None:
611 cnf = {}
612 for x in self.tk.split(
613 self.tk.call(self._w, 'configure')):
614 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
615 return cnf
616 if type(cnf) is StringType:
617 x = self.tk.split(self.tk.call(
618 self._w, 'configure', '-'+cnf))
619 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +0000620 self.tk.call((self._w, 'configure')
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000621 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000622 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000623 def cget(self, key):
624 return self.tk.call(self._w, 'cget', '-' + key)
625 __getitem__ = cget
626 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000627 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000628 def keys(self):
629 return map(lambda x: x[0][1:],
630 self.tk.split(self.tk.call(self._w, 'configure')))
631 def __str__(self):
632 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000633 # Pack methods that apply to the master
634 _noarg_ = ['_noarg_']
635 def pack_propagate(self, flag=_noarg_):
636 if flag is Misc._noarg_:
637 return self._getboolean(self.tk.call(
638 'pack', 'propagate', self._w))
639 else:
640 self.tk.call('pack', 'propagate', self._w, flag)
641 propagate = pack_propagate
642 def pack_slaves(self):
643 return map(self._nametowidget,
644 self.tk.splitlist(
645 self.tk.call('pack', 'slaves', self._w)))
646 slaves = pack_slaves
647 # Place method that applies to the master
648 def place_slaves(self):
649 return map(self._nametowidget,
650 self.tk.splitlist(
651 self.tk.call(
652 'place', 'slaves', self._w)))
653 # Grid methods that apply to the master
654 def grid_bbox(self, column, row):
655 return self._getints(
656 self.tk.call(
657 'grid', 'bbox', self._w, column, row)) or None
658 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000659 def _grid_configure(self, command, index, cnf, kw):
660 if type(cnf) is StringType and not kw:
661 if cnf[-1:] == '_':
662 cnf = cnf[:-1]
663 if cnf[:1] != '-':
664 cnf = '-'+cnf
665 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000666 else:
667 options = self._options(cnf, kw)
668 if not options:
669 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000670 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000671 words = self.tk.splitlist(res)
672 dict = {}
673 for i in range(0, len(words), 2):
674 key = words[i][1:]
675 value = words[i+1]
676 if not value:
677 value = None
678 elif '.' in value:
Guido van Rossum0132f691998-04-30 17:50:36 +0000679 value = getdouble(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000680 else:
Guido van Rossum0132f691998-04-30 17:50:36 +0000681 value = getint(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000682 dict[key] = value
683 return dict
Guido van Rossumf9756991998-04-29 21:57:08 +0000684 res = self.tk.call(
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000685 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000686 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000687 if len(options) == 1:
688 if not res: return None
689 # In Tk 7.5, -width can be a float
Guido van Rossum0132f691998-04-30 17:50:36 +0000690 if '.' in res: return getdouble(res)
691 return getint(res)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000692 def grid_columnconfigure(self, index, cnf={}, **kw):
693 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000694 columnconfigure = grid_columnconfigure
695 def grid_propagate(self, flag=_noarg_):
696 if flag is Misc._noarg_:
697 return self._getboolean(self.tk.call(
698 'grid', 'propagate', self._w))
699 else:
700 self.tk.call('grid', 'propagate', self._w, flag)
701 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000702 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000703 rowconfigure = grid_rowconfigure
704 def grid_size(self):
705 return self._getints(
706 self.tk.call('grid', 'size', self._w)) or None
707 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000708 def grid_slaves(self, row=None, column=None):
709 args = ()
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000710 if row:
711 args = args + ('-row', row)
712 if column:
713 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000714 return map(self._nametowidget,
Guido van Rossumf9756991998-04-29 21:57:08 +0000715 self.tk.splitlist(self.tk.call(
716 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000717
Guido van Rossum80f8be81997-12-02 19:51:39 +0000718 # Support for the "event" command, new in Tk 4.2.
719 # By Case Roole.
720
Guido van Rossum56c04b81998-04-06 03:10:03 +0000721 def event_add(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000722 args = ('event', 'add', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000723 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000724
Guido van Rossum56c04b81998-04-06 03:10:03 +0000725 def event_delete(self, virtual, *sequences):
Guido van Rossum80f8be81997-12-02 19:51:39 +0000726 args = ('event', 'delete', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +0000727 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000728
729 def event_generate(self, sequence, **kw):
730 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +0000731 for k, v in kw.items():
732 args = args + ('-%s' % k, str(v))
Guido van Rossumf9756991998-04-29 21:57:08 +0000733 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +0000734
Guido van Rossum56c04b81998-04-06 03:10:03 +0000735 def event_info(self, virtual=None):
736 return self.tk.splitlist(
737 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +0000738
Guido van Rossumc2966511998-04-10 19:16:10 +0000739 # Image related commands
740
741 def image_names(self):
742 return self.tk.call('image', 'names')
743
744 def image_types(self):
745 return self.tk.call('image', 'types')
746
Guido van Rossum80f8be81997-12-02 19:51:39 +0000747
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000748class CallWrapper:
749 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000750 self.func = func
751 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000752 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000753 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000754 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000755 if self.subst:
756 args = apply(self.subst, args)
757 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000758 except SystemExit, msg:
759 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000760 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000761 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000762
Guido van Rossume365a591998-05-01 19:48:20 +0000763
Guido van Rossum18468821994-06-20 07:49:28 +0000764class Wm:
Guido van Rossume365a591998-05-01 19:48:20 +0000765 def wm_aspect(self,
Guido van Rossum18468821994-06-20 07:49:28 +0000766 minNumer=None, minDenom=None,
767 maxNumer=None, maxDenom=None):
768 return self._getints(
769 self.tk.call('wm', 'aspect', self._w,
770 minNumer, minDenom,
771 maxNumer, maxDenom))
Guido van Rossume365a591998-05-01 19:48:20 +0000772 aspect = wm_aspect
773 def wm_client(self, name=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000774 return self.tk.call('wm', 'client', self._w, name)
Guido van Rossume365a591998-05-01 19:48:20 +0000775 client = wm_client
776 def wm_colormapwindows(self, *wlist):
Fred Drake3c602d71996-09-27 14:06:54 +0000777 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
Guido van Rossumf9756991998-04-29 21:57:08 +0000778 return map(self._nametowidget, self.tk.call(args))
Guido van Rossume365a591998-05-01 19:48:20 +0000779 colormapwindows = wm_colormapwindows
780 def wm_command(self, value=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000781 return self.tk.call('wm', 'command', self._w, value)
Guido van Rossume365a591998-05-01 19:48:20 +0000782 command = wm_command
783 def wm_deiconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000784 return self.tk.call('wm', 'deiconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000785 deiconify = wm_deiconify
786 def wm_focusmodel(self, model=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000787 return self.tk.call('wm', 'focusmodel', self._w, model)
Guido van Rossume365a591998-05-01 19:48:20 +0000788 focusmodel = wm_focusmodel
789 def wm_frame(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000790 return self.tk.call('wm', 'frame', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000791 frame = wm_frame
792 def wm_geometry(self, newGeometry=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000793 return self.tk.call('wm', 'geometry', self._w, newGeometry)
Guido van Rossume365a591998-05-01 19:48:20 +0000794 geometry = wm_geometry
795 def wm_grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000796 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000797 widthInc=None, heightInc=None):
798 return self._getints(self.tk.call(
799 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000800 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossume365a591998-05-01 19:48:20 +0000801 grid = wm_grid
802 def wm_group(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000803 return self.tk.call('wm', 'group', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000804 group = wm_group
805 def wm_iconbitmap(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000806 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000807 iconbitmap = wm_iconbitmap
808 def wm_iconify(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000809 return self.tk.call('wm', 'iconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000810 iconify = wm_iconify
811 def wm_iconmask(self, bitmap=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000812 return self.tk.call('wm', 'iconmask', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +0000813 iconmask = wm_iconmask
814 def wm_iconname(self, newName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000815 return self.tk.call('wm', 'iconname', self._w, newName)
Guido van Rossume365a591998-05-01 19:48:20 +0000816 iconname = wm_iconname
817 def wm_iconposition(self, x=None, y=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000818 return self._getints(self.tk.call(
819 'wm', 'iconposition', self._w, x, y))
Guido van Rossume365a591998-05-01 19:48:20 +0000820 iconposition = wm_iconposition
821 def wm_iconwindow(self, pathName=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000822 return self.tk.call('wm', 'iconwindow', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +0000823 iconwindow = wm_iconwindow
824 def wm_maxsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000825 return self._getints(self.tk.call(
826 'wm', 'maxsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000827 maxsize = wm_maxsize
828 def wm_minsize(self, width=None, height=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000829 return self._getints(self.tk.call(
830 'wm', 'minsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +0000831 minsize = wm_minsize
832 def wm_overrideredirect(self, boolean=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000833 return self._getboolean(self.tk.call(
834 'wm', 'overrideredirect', self._w, boolean))
Guido van Rossume365a591998-05-01 19:48:20 +0000835 overrideredirect = wm_overrideredirect
836 def wm_positionfrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000837 return self.tk.call('wm', 'positionfrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000838 positionfrom = wm_positionfrom
839 def wm_protocol(self, name=None, func=None):
Guido van Rossumc4570481998-03-20 20:45:49 +0000840 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000841 command = self._register(func)
842 else:
843 command = func
844 return self.tk.call(
845 'wm', 'protocol', self._w, name, command)
Guido van Rossume365a591998-05-01 19:48:20 +0000846 protocol = wm_protocol
847 def wm_resizable(self, width=None, height=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000848 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossume365a591998-05-01 19:48:20 +0000849 resizable = wm_resizable
850 def wm_sizefrom(self, who=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000851 return self.tk.call('wm', 'sizefrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +0000852 sizefrom = wm_sizefrom
853 def wm_state(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000854 return self.tk.call('wm', 'state', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000855 state = wm_state
856 def wm_title(self, string=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000857 return self.tk.call('wm', 'title', self._w, string)
Guido van Rossume365a591998-05-01 19:48:20 +0000858 title = wm_title
859 def wm_transient(self, master=None):
Guido van Rossum18468821994-06-20 07:49:28 +0000860 return self.tk.call('wm', 'transient', self._w, master)
Guido van Rossume365a591998-05-01 19:48:20 +0000861 transient = wm_transient
862 def wm_withdraw(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000863 return self.tk.call('wm', 'withdraw', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +0000864 withdraw = wm_withdraw
865
Guido van Rossum18468821994-06-20 07:49:28 +0000866
867class Tk(Misc, Wm):
868 _w = '.'
869 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000870 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000871 self.master = None
872 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000873 if baseName is None:
874 import sys, os
875 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000876 baseName, ext = os.path.splitext(baseName)
Fred Drake182c5901998-07-15 04:36:56 +0000877 if ext not in ('.py', '.pyc', '.pyo'):
878 baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000879 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000880 if _MacOS:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000881 # Disable event scanning except for Command-Period
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000882 _MacOS.SchedParams(1, 0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000883 # Work around nasty MacTk bug
Guido van Rossumf0c891a1998-04-29 21:43:36 +0000884 # XXX Is this one still needed?
Guido van Rossum37dcab11996-05-16 16:00:19 +0000885 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000886 # Version sanity checks
887 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000888 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000889 raise RuntimeError, \
890 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000891 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000892 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000893 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000894 raise RuntimeError, \
895 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000896 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000897 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000898 raise RuntimeError, \
899 "Tk 4.0 or higher is required; found Tk %s" \
900 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000901 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000902 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000903 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +0000904 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000905 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000906 def destroy(self):
907 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000908 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000909 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000910 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000911 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000912 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000913 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000914 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000915 if os.environ.has_key('HOME'): home = os.environ['HOME']
916 else: home = os.curdir
917 class_tcl = os.path.join(home, '.%s.tcl' % className)
918 class_py = os.path.join(home, '.%s.py' % className)
919 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
920 base_py = os.path.join(home, '.%s.py' % baseName)
921 dir = {'self': self}
922 exec 'from Tkinter import *' in dir
923 if os.path.isfile(class_tcl):
924 print 'source', `class_tcl`
925 self.tk.call('source', class_tcl)
926 if os.path.isfile(class_py):
927 print 'execfile', `class_py`
928 execfile(class_py, dir)
929 if os.path.isfile(base_tcl):
930 print 'source', `base_tcl`
931 self.tk.call('source', base_tcl)
932 if os.path.isfile(base_py):
933 print 'execfile', `base_py`
934 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000935 def report_callback_exception(self, exc, val, tb):
Guido van Rossumda654501998-10-06 19:06:27 +0000936 import traceback, sys
937 sys.stderr.write("Exception in Tkinter callback\n")
Guido van Rossum9f1292d1998-10-13 20:02:39 +0000938 sys.last_type = exc
939 sys.last_value = val
940 sys.last_traceback = tb
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000941 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000942
Guido van Rossum368e06b1997-11-07 20:38:49 +0000943# Ideally, the classes Pack, Place and Grid disappear, the
944# pack/place/grid methods are defined on the Widget class, and
945# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
946# ...), with pack(), place() and grid() being short for
947# pack_configure(), place_configure() and grid_columnconfigure(), and
948# forget() being short for pack_forget(). As a practical matter, I'm
949# afraid that there is too much code out there that may be using the
950# Pack, Place or Grid class, so I leave them intact -- but only as
951# backwards compatibility features. Also note that those methods that
952# take a master as argument (e.g. pack_propagate) have been moved to
953# the Misc class (which now incorporates all methods common between
954# toplevel and interior widgets). Again, for compatibility, these are
955# copied into the Pack, Place or Grid class.
956
Guido van Rossum18468821994-06-20 07:49:28 +0000957class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000958 def pack_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +0000959 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000960 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000961 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000962 pack = configure = config = pack_configure
963 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000964 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000965 forget = pack_forget
966 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000967 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000968 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000969 dict = {}
970 for i in range(0, len(words), 2):
971 key = words[i][1:]
972 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000973 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000974 value = self._nametowidget(value)
975 dict[key] = value
976 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000977 info = pack_info
978 propagate = pack_propagate = Misc.pack_propagate
979 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000980
981class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000982 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000983 for k in ['in_']:
984 if kw.has_key(k):
985 kw[k[:-1]] = kw[k]
986 del kw[k]
Guido van Rossumf9756991998-04-29 21:57:08 +0000987 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000988 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000989 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000990 place = configure = config = place_configure
991 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000992 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000993 forget = place_forget
994 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000995 words = self.tk.splitlist(
996 self.tk.call('place', 'info', self._w))
997 dict = {}
998 for i in range(0, len(words), 2):
999 key = words[i][1:]
1000 value = words[i+1]
1001 if value[:1] == '.':
1002 value = self._nametowidget(value)
1003 dict[key] = value
1004 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001005 info = place_info
1006 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001007
Guido van Rossum37dcab11996-05-16 16:00:19 +00001008class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001009 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001010 def grid_configure(self, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001011 self.tk.call(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001012 ('grid', 'configure', self._w)
1013 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001014 grid = configure = config = grid_configure
1015 bbox = grid_bbox = Misc.grid_bbox
1016 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1017 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001018 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001019 forget = grid_forget
1020 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001021 words = self.tk.splitlist(
1022 self.tk.call('grid', 'info', self._w))
1023 dict = {}
1024 for i in range(0, len(words), 2):
1025 key = words[i][1:]
1026 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001027 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001028 value = self._nametowidget(value)
1029 dict[key] = value
1030 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001031 info = grid_info
1032 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001033 return self._getints(
1034 self.tk.call(
1035 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001036 location = grid_location
1037 propagate = grid_propagate = Misc.grid_propagate
1038 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1039 size = grid_size = Misc.grid_size
1040 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001041
Guido van Rossum368e06b1997-11-07 20:38:49 +00001042class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001043 def _setup(self, master, cnf):
Guido van Rossumc4570481998-03-20 20:45:49 +00001044 if _support_default_root:
1045 global _default_root
1046 if not master:
1047 if not _default_root:
1048 _default_root = Tk()
1049 master = _default_root
Guido van Rossum18468821994-06-20 07:49:28 +00001050 self.master = master
1051 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001052 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001053 if cnf.has_key('name'):
1054 name = cnf['name']
1055 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001056 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001057 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001058 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001059 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001060 self._w = '.' + name
1061 else:
1062 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001063 self.children = {}
1064 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001065 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001066 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001067 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1068 if kw:
1069 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001070 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001071 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001072 classes = []
1073 for k in cnf.keys():
1074 if type(k) is ClassType:
1075 classes.append((k, cnf[k]))
1076 del cnf[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001077 self.tk.call(
1078 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001079 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001080 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001081 def destroy(self):
1082 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001083 if self.master.children.has_key(self._name):
1084 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001085 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001086 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001087 def _do(self, name, args=()):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001088 # XXX Obsolete -- better use self.tk.call directly!
Guido van Rossumf9756991998-04-29 21:57:08 +00001089 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001090
Guido van Rossum368e06b1997-11-07 20:38:49 +00001091class Widget(BaseWidget, Pack, Place, Grid):
1092 pass
1093
1094class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001095 def __init__(self, master=None, cnf={}, **kw):
1096 if kw:
1097 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001098 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001099 for wmkey in ['screen', 'class_', 'class', 'visual',
1100 'colormap']:
1101 if cnf.has_key(wmkey):
1102 val = cnf[wmkey]
1103 # TBD: a hack needed because some keys
1104 # are not valid as keyword arguments
1105 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1106 else: opt = '-'+wmkey
1107 extra = extra + (opt, val)
1108 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001109 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001110 root = self._root()
1111 self.iconname(root.iconname())
1112 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001113
1114class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001115 def __init__(self, master=None, cnf={}, **kw):
1116 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001117 def tkButtonEnter(self, *dummy):
1118 self.tk.call('tkButtonEnter', self._w)
1119 def tkButtonLeave(self, *dummy):
1120 self.tk.call('tkButtonLeave', self._w)
1121 def tkButtonDown(self, *dummy):
1122 self.tk.call('tkButtonDown', self._w)
1123 def tkButtonUp(self, *dummy):
1124 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001125 def tkButtonInvoke(self, *dummy):
1126 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001127 def flash(self):
1128 self.tk.call(self._w, 'flash')
1129 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001130 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001131
1132# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001133# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001134def AtEnd():
1135 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001136def AtInsert(*args):
1137 s = 'insert'
1138 for a in args:
1139 if a: s = s + (' ' + a)
1140 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001141def AtSelFirst():
1142 return 'sel.first'
1143def AtSelLast():
1144 return 'sel.last'
1145def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001146 if y is None:
1147 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001148 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001149 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001150
1151class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001152 def __init__(self, master=None, cnf={}, **kw):
1153 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001154 def addtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001155 self.tk.call((self._w, 'addtag') + args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001156 def addtag_above(self, newtag, tagOrId):
1157 self.addtag(newtag, 'above', tagOrId)
1158 def addtag_all(self, newtag):
1159 self.addtag(newtag, 'all')
1160 def addtag_below(self, newtag, tagOrId):
1161 self.addtag(newtag, 'below', tagOrId)
1162 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1163 self.addtag(newtag, 'closest', x, y, halo, start)
1164 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1165 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1166 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1167 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1168 def addtag_withtag(self, newtag, tagOrId):
1169 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001170 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001171 return self._getints(
1172 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001173 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001174 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001175 if funcid:
1176 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001177 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001178 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001179 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001180 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001181 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001182 self._w, 'canvasx', screenx, gridspacing))
1183 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum0132f691998-04-30 17:50:36 +00001184 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001185 self._w, 'canvasy', screeny, gridspacing))
1186 def coords(self, *args):
Guido van Rossum0001a111998-02-19 21:20:30 +00001187 # XXX Should use _flatten on args
Guido van Rossum0132f691998-04-30 17:50:36 +00001188 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00001189 self.tk.splitlist(
1190 self.tk.call((self._w, 'coords') + args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001191 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001192 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001193 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001194 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001195 args = args[:-1]
1196 else:
1197 cnf = {}
Guido van Rossum0132f691998-04-30 17:50:36 +00001198 return getint(apply(
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001199 self.tk.call,
1200 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001201 + args + self._options(cnf, kw)))
1202 def create_arc(self, *args, **kw):
1203 return self._create('arc', args, kw)
1204 def create_bitmap(self, *args, **kw):
1205 return self._create('bitmap', args, kw)
1206 def create_image(self, *args, **kw):
1207 return self._create('image', args, kw)
1208 def create_line(self, *args, **kw):
1209 return self._create('line', args, kw)
1210 def create_oval(self, *args, **kw):
1211 return self._create('oval', args, kw)
1212 def create_polygon(self, *args, **kw):
1213 return self._create('polygon', args, kw)
1214 def create_rectangle(self, *args, **kw):
1215 return self._create('rectangle', args, kw)
1216 def create_text(self, *args, **kw):
1217 return self._create('text', args, kw)
1218 def create_window(self, *args, **kw):
1219 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001220 def dchars(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001221 self.tk.call((self._w, 'dchars') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001222 def delete(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001223 self.tk.call((self._w, 'delete') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001224 def dtag(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001225 self.tk.call((self._w, 'dtag') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001226 def find(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001227 return self._getints(
1228 self.tk.call((self._w, 'find') + args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001229 def find_above(self, tagOrId):
1230 return self.find('above', tagOrId)
1231 def find_all(self):
1232 return self.find('all')
1233 def find_below(self, tagOrId):
1234 return self.find('below', tagOrId)
1235 def find_closest(self, x, y, halo=None, start=None):
1236 return self.find('closest', x, y, halo, start)
1237 def find_enclosed(self, x1, y1, x2, y2):
1238 return self.find('enclosed', x1, y1, x2, y2)
1239 def find_overlapping(self, x1, y1, x2, y2):
1240 return self.find('overlapping', x1, y1, x2, y2)
1241 def find_withtag(self, tagOrId):
1242 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001243 def focus(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001244 return self.tk.call((self._w, 'focus') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001245 def gettags(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001246 return self.tk.splitlist(
1247 self.tk.call((self._w, 'gettags') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001248 def icursor(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001249 self.tk.call((self._w, 'icursor') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001250 def index(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001251 return getint(self.tk.call((self._w, 'index') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00001252 def insert(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001253 self.tk.call((self._w, 'insert') + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001254 def itemcget(self, tagOrId, option):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001255 return self.tk.call(
1256 (self._w, 'itemcget') + (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001257 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001258 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001259 cnf = {}
1260 for x in self.tk.split(
Guido van Rossum0bd54331998-05-19 21:18:13 +00001261 self.tk.call(self._w,
1262 'itemconfigure', tagOrId)):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001263 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1264 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001265 if type(cnf) == StringType and not kw:
Guido van Rossum0bd54331998-05-19 21:18:13 +00001266 x = self.tk.split(self.tk.call(
1267 self._w, 'itemconfigure', tagOrId, '-'+cnf))
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001268 return (x[0][1:],) + x[1:]
Guido van Rossum0bd54331998-05-19 21:18:13 +00001269 self.tk.call((self._w, 'itemconfigure', tagOrId) +
1270 self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001271 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001272 def lower(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001273 self.tk.call((self._w, 'lower') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001274 def move(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001275 self.tk.call((self._w, 'move') + args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001276 def postscript(self, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001277 return self.tk.call((self._w, 'postscript') +
1278 self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001279 def tkraise(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001280 self.tk.call((self._w, 'raise') + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001281 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001282 def scale(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001283 self.tk.call((self._w, 'scale') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001284 def scan_mark(self, x, y):
1285 self.tk.call(self._w, 'scan', 'mark', x, y)
1286 def scan_dragto(self, x, y):
1287 self.tk.call(self._w, 'scan', 'dragto', x, y)
1288 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001289 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001290 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001291 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001292 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001293 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001294 def select_item(self):
1295 self.tk.call(self._w, 'select', 'item')
1296 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001297 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001298 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001299 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001300 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001301 if not args:
1302 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001303 self.tk.call((self._w, 'xview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001304 def xview_moveto(self, fraction):
1305 self.tk.call(self._w, 'xview', 'moveto', fraction)
1306 def xview_scroll(self, number, what):
1307 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001308 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001309 if not args:
1310 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001311 self.tk.call((self._w, 'yview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00001312 def yview_moveto(self, fraction):
1313 self.tk.call(self._w, 'yview', 'moveto', fraction)
1314 def yview_scroll(self, number, what):
1315 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001316
1317class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001318 def __init__(self, master=None, cnf={}, **kw):
1319 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001320 def deselect(self):
1321 self.tk.call(self._w, 'deselect')
1322 def flash(self):
1323 self.tk.call(self._w, 'flash')
1324 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001325 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001326 def select(self):
1327 self.tk.call(self._w, 'select')
1328 def toggle(self):
1329 self.tk.call(self._w, 'toggle')
1330
1331class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001332 def __init__(self, master=None, cnf={}, **kw):
1333 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001334 def delete(self, first, last=None):
1335 self.tk.call(self._w, 'delete', first, last)
1336 def get(self):
1337 return self.tk.call(self._w, 'get')
1338 def icursor(self, index):
1339 self.tk.call(self._w, 'icursor', index)
1340 def index(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001341 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001342 self._w, 'index', index))
1343 def insert(self, index, string):
1344 self.tk.call(self._w, 'insert', index, string)
1345 def scan_mark(self, x):
1346 self.tk.call(self._w, 'scan', 'mark', x)
1347 def scan_dragto(self, x):
1348 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001349 def selection_adjust(self, index):
1350 self.tk.call(self._w, 'selection', 'adjust', index)
1351 select_adjust = selection_adjust
1352 def selection_clear(self):
1353 self.tk.call(self._w, 'selection', 'clear')
1354 select_clear = selection_clear
1355 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001356 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001357 select_from = selection_from
1358 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001359 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001360 self.tk.call(self._w, 'selection', 'present'))
1361 select_present = selection_present
1362 def selection_range(self, start, end):
1363 self.tk.call(self._w, 'selection', 'range', start, end)
1364 select_range = selection_range
1365 def selection_to(self, index):
1366 self.tk.call(self._w, 'selection', 'to', index)
1367 select_to = selection_to
1368 def xview(self, index):
1369 self.tk.call(self._w, 'xview', index)
1370 def xview_moveto(self, fraction):
1371 self.tk.call(self._w, 'xview', 'moveto', fraction)
1372 def xview_scroll(self, number, what):
1373 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001374
1375class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001376 def __init__(self, master=None, cnf={}, **kw):
1377 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001378 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001379 if cnf.has_key('class_'):
1380 extra = ('-class', cnf['class_'])
1381 del cnf['class_']
1382 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001383 extra = ('-class', cnf['class'])
1384 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001385 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001386
1387class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001388 def __init__(self, master=None, cnf={}, **kw):
1389 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001390
Guido van Rossum18468821994-06-20 07:49:28 +00001391class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001392 def __init__(self, master=None, cnf={}, **kw):
1393 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001394 def activate(self, index):
1395 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001396 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001397 return self._getints(
1398 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001399 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001400 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001401 return self.tk.splitlist(self.tk.call(
1402 self._w, 'curselection'))
1403 def delete(self, first, last=None):
1404 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001405 def get(self, first, last=None):
1406 if last:
1407 return self.tk.splitlist(self.tk.call(
1408 self._w, 'get', first, last))
1409 else:
1410 return self.tk.call(self._w, 'get', first)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001411 def index(self, index):
1412 i = self.tk.call(self._w, 'index', index)
1413 if i == 'none': return None
1414 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001415 def insert(self, index, *elements):
Guido van Rossumf9756991998-04-29 21:57:08 +00001416 self.tk.call((self._w, 'insert', index) + elements)
Guido van Rossum18468821994-06-20 07:49:28 +00001417 def nearest(self, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001418 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001419 self._w, 'nearest', y))
1420 def scan_mark(self, x, y):
1421 self.tk.call(self._w, 'scan', 'mark', x, y)
1422 def scan_dragto(self, x, y):
1423 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001424 def see(self, index):
1425 self.tk.call(self._w, 'see', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001426 def selection_anchor(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001427 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001428 select_anchor = selection_anchor
1429 def selection_clear(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001430 self.tk.call(self._w,
1431 'selection', 'clear', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001432 select_clear = selection_clear
1433 def selection_includes(self, index):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001434 return self.tk.getboolean(self.tk.call(
1435 self._w, 'selection', 'includes', index))
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001436 select_includes = selection_includes
1437 def selection_set(self, first, last=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001438 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001439 select_set = selection_set
Guido van Rossum18468821994-06-20 07:49:28 +00001440 def size(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001441 return getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001442 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001443 if not what:
1444 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001445 self.tk.call((self._w, 'xview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001446 def xview_moveto(self, fraction):
1447 self.tk.call(self._w, 'xview', 'moveto', fraction)
1448 def xview_scroll(self, number, what):
1449 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001450 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001451 if not what:
1452 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001453 self.tk.call((self._w, 'yview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00001454 def yview_moveto(self, fraction):
1455 self.tk.call(self._w, 'yview', 'moveto', fraction)
1456 def yview_scroll(self, number, what):
1457 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001458
1459class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001460 def __init__(self, master=None, cnf={}, **kw):
1461 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001462 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001463 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001464 def tk_mbPost(self):
1465 self.tk.call('tk_mbPost', self._w)
1466 def tk_mbUnpost(self):
1467 self.tk.call('tk_mbUnpost')
1468 def tk_traverseToMenu(self, char):
1469 self.tk.call('tk_traverseToMenu', self._w, char)
1470 def tk_traverseWithinMenu(self, char):
1471 self.tk.call('tk_traverseWithinMenu', self._w, char)
1472 def tk_getMenuButtons(self):
1473 return self.tk.call('tk_getMenuButtons', self._w)
1474 def tk_nextMenu(self, count):
1475 self.tk.call('tk_nextMenu', count)
1476 def tk_nextMenuEntry(self, count):
1477 self.tk.call('tk_nextMenuEntry', count)
1478 def tk_invokeMenu(self):
1479 self.tk.call('tk_invokeMenu', self._w)
1480 def tk_firstMenu(self):
1481 self.tk.call('tk_firstMenu', self._w)
1482 def tk_mbButtonDown(self):
1483 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001484 def tk_popup(self, x, y, entry=""):
1485 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001486 def activate(self, index):
1487 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001488 def add(self, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001489 self.tk.call((self._w, 'add', itemType) +
1490 self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001491 def add_cascade(self, cnf={}, **kw):
1492 self.add('cascade', cnf or kw)
1493 def add_checkbutton(self, cnf={}, **kw):
1494 self.add('checkbutton', cnf or kw)
1495 def add_command(self, cnf={}, **kw):
1496 self.add('command', cnf or kw)
1497 def add_radiobutton(self, cnf={}, **kw):
1498 self.add('radiobutton', cnf or kw)
1499 def add_separator(self, cnf={}, **kw):
1500 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001501 def insert(self, index, itemType, cnf={}, **kw):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001502 self.tk.call((self._w, 'insert', index, itemType) +
1503 self._options(cnf, kw))
Guido van Rossum2caac731996-09-05 16:46:31 +00001504 def insert_cascade(self, index, cnf={}, **kw):
1505 self.insert(index, 'cascade', cnf or kw)
1506 def insert_checkbutton(self, index, cnf={}, **kw):
1507 self.insert(index, 'checkbutton', cnf or kw)
1508 def insert_command(self, index, cnf={}, **kw):
1509 self.insert(index, 'command', cnf or kw)
1510 def insert_radiobutton(self, index, cnf={}, **kw):
1511 self.insert(index, 'radiobutton', cnf or kw)
1512 def insert_separator(self, index, cnf={}, **kw):
1513 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001514 def delete(self, index1, index2=None):
1515 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001516 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001517 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001518 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001519 if cnf is None and not kw:
1520 cnf = {}
Guido van Rossumf9756991998-04-29 21:57:08 +00001521 for x in self.tk.split(self.tk.call(
1522 (self._w, 'entryconfigure', index))):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001523 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1524 return cnf
1525 if type(cnf) == StringType and not kw:
Guido van Rossumf9756991998-04-29 21:57:08 +00001526 x = self.tk.split(self.tk.call(
1527 (self._w, 'entryconfigure', index, '-'+cnf)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001528 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001529 self.tk.call((self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001530 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001531 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001532 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001533 i = self.tk.call(self._w, 'index', index)
1534 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00001535 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001536 def invoke(self, index):
1537 return self.tk.call(self._w, 'invoke', index)
1538 def post(self, x, y):
1539 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001540 def type(self, index):
1541 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001542 def unpost(self):
1543 self.tk.call(self._w, 'unpost')
1544 def yposition(self, index):
Guido van Rossum0132f691998-04-30 17:50:36 +00001545 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001546 self._w, 'yposition', index))
1547
1548class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001549 def __init__(self, master=None, cnf={}, **kw):
1550 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001551
1552class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001553 def __init__(self, master=None, cnf={}, **kw):
1554 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001555
1556class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001557 def __init__(self, master=None, cnf={}, **kw):
1558 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001559 def deselect(self):
1560 self.tk.call(self._w, 'deselect')
1561 def flash(self):
1562 self.tk.call(self._w, 'flash')
1563 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001564 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001565 def select(self):
1566 self.tk.call(self._w, 'select')
1567
1568class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001569 def __init__(self, master=None, cnf={}, **kw):
1570 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001571 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001572 value = self.tk.call(self._w, 'get')
1573 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00001574 return getint(value)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00001575 except ValueError:
Guido van Rossum0132f691998-04-30 17:50:36 +00001576 return getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001577 def set(self, value):
1578 self.tk.call(self._w, 'set', value)
Guido van Rossumb4750db1998-08-11 19:07:14 +00001579 def coords(self, value=None):
1580 return self._getints(self.tk.call(self._w, 'coords', value))
1581 def identify(self, x, y):
1582 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001583
1584class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001585 def __init__(self, master=None, cnf={}, **kw):
1586 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001587 def activate(self, index):
1588 self.tk.call(self._w, 'activate', index)
1589 def delta(self, deltax, deltay):
Guido van Rossum0132f691998-04-30 17:50:36 +00001590 return getdouble(
1591 self.tk.call(self._w, 'delta', deltax, deltay))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001592 def fraction(self, x, y):
Guido van Rossum0132f691998-04-30 17:50:36 +00001593 return getdouble(self.tk.call(self._w, 'fraction', x, y))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001594 def identify(self, x, y):
1595 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001596 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001597 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001598 def set(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001599 self.tk.call((self._w, 'set') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001600
1601class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001602 def __init__(self, master=None, cnf={}, **kw):
1603 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001604 def bbox(self, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001605 return self._getints(
1606 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001607 def tk_textSelectTo(self, index):
1608 self.tk.call('tk_textSelectTo', self._w, index)
1609 def tk_textBackspace(self):
1610 self.tk.call('tk_textBackspace', self._w)
1611 def tk_textIndexCloser(self, a, b, c):
1612 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1613 def tk_textResetAnchor(self, index):
1614 self.tk.call('tk_textResetAnchor', self._w, index)
1615 def compare(self, index1, op, index2):
1616 return self.tk.getboolean(self.tk.call(
1617 self._w, 'compare', index1, op, index2))
1618 def debug(self, boolean=None):
1619 return self.tk.getboolean(self.tk.call(
1620 self._w, 'debug', boolean))
1621 def delete(self, index1, index2=None):
1622 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001623 def dlineinfo(self, index):
1624 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001625 def get(self, index1, index2=None):
1626 return self.tk.call(self._w, 'get', index1, index2)
Guido van Rossumc86b7c61998-08-31 16:54:33 +00001627 # (Image commands are new in 8.0)
1628 def image_cget(self, index, option):
1629 if option[:1] != "-":
1630 option = "-" + option
1631 if option[-1:] == "_":
1632 option = option[:-1]
1633 return self.tk.call(self._w, "image", "cget", index, option)
1634 def image_configure(self, index, cnf={}, **kw):
1635 if not cnf and not kw:
1636 cnf = {}
1637 for x in self.tk.split(
1638 self.tk.call(
1639 self._w, "image", "configure", index)):
1640 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1641 return cnf
1642 apply(self.tk.call,
1643 (self._w, "image", "configure", index)
1644 + self._options(cnf, kw))
1645 def image_create(self, index, cnf={}, **kw):
1646 return apply(self.tk.call,
1647 (self._w, "image", "create", index)
1648 + self._options(cnf, kw))
1649 def image_names(self):
1650 return self.tk.call(self._w, "image", "names")
Guido van Rossum18468821994-06-20 07:49:28 +00001651 def index(self, index):
1652 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001653 def insert(self, index, chars, *args):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001654 self.tk.call((self._w, 'insert', index, chars) + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001655 def mark_gravity(self, markName, direction=None):
Guido van Rossumf9756991998-04-29 21:57:08 +00001656 return self.tk.call(
1657 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001658 def mark_names(self):
1659 return self.tk.splitlist(self.tk.call(
1660 self._w, 'mark', 'names'))
1661 def mark_set(self, markName, index):
1662 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001663 def mark_unset(self, *markNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001664 self.tk.call((self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001665 def scan_mark(self, x, y):
1666 self.tk.call(self._w, 'scan', 'mark', x, y)
1667 def scan_dragto(self, x, y):
1668 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001669 def search(self, pattern, index, stopindex=None,
1670 forwards=None, backwards=None, exact=None,
1671 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001672 args = [self._w, 'search']
1673 if forwards: args.append('-forwards')
1674 if backwards: args.append('-backwards')
1675 if exact: args.append('-exact')
1676 if regexp: args.append('-regexp')
1677 if nocase: args.append('-nocase')
1678 if count: args.append('-count'); args.append(count)
1679 if pattern[0] == '-': args.append('--')
1680 args.append(pattern)
1681 args.append(index)
1682 if stopindex: args.append(stopindex)
Guido van Rossumf9756991998-04-29 21:57:08 +00001683 return self.tk.call(tuple(args))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001684 def see(self, index):
1685 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001686 def tag_add(self, tagName, index1, index2=None):
1687 self.tk.call(
1688 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossum117a5a81998-03-27 21:26:51 +00001689 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossumef8f8811994-08-08 12:47:33 +00001690 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001691 if funcid:
1692 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001693 def tag_bind(self, tagName, sequence, func, add=None):
1694 return self._bind((self._w, 'tag', 'bind', tagName),
1695 sequence, func, add)
1696 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001697 if option[:1] != '-':
1698 option = '-' + option
1699 if option[-1:] == '_':
1700 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001701 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001702 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001703 if type(cnf) == StringType:
1704 x = self.tk.split(self.tk.call(
1705 self._w, 'tag', 'configure', tagName, '-'+cnf))
1706 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001707 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001708 (self._w, 'tag', 'configure', tagName)
1709 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001710 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001711 def tag_delete(self, *tagNames):
Guido van Rossumf9756991998-04-29 21:57:08 +00001712 self.tk.call((self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001713 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001714 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001715 def tag_names(self, index=None):
1716 return self.tk.splitlist(
1717 self.tk.call(self._w, 'tag', 'names', index))
1718 def tag_nextrange(self, tagName, index1, index2=None):
1719 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001720 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001721 def tag_prevrange(self, tagName, index1, index2=None):
1722 return self.tk.splitlist(self.tk.call(
1723 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001724 def tag_raise(self, tagName, aboveThis=None):
1725 self.tk.call(
1726 self._w, 'tag', 'raise', tagName, aboveThis)
1727 def tag_ranges(self, tagName):
1728 return self.tk.splitlist(self.tk.call(
1729 self._w, 'tag', 'ranges', tagName))
1730 def tag_remove(self, tagName, index1, index2=None):
1731 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001732 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001733 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001734 if option[:1] != '-':
1735 option = '-' + option
1736 if option[-1:] == '_':
1737 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001738 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001739 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001740 if type(cnf) == StringType:
1741 x = self.tk.split(self.tk.call(
1742 self._w, 'window', 'configure',
1743 index, '-'+cnf))
1744 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001745 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001746 (self._w, 'window', 'configure', index)
1747 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001748 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001749 def window_create(self, index, cnf={}, **kw):
Guido van Rossumf9756991998-04-29 21:57:08 +00001750 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001751 (self._w, 'window', 'create', index)
1752 + self._options(cnf, kw))
1753 def window_names(self):
1754 return self.tk.splitlist(
1755 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001756 def xview(self, *what):
1757 if not what:
1758 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001759 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001760 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001761 if not what:
1762 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00001763 self.tk.call((self._w, 'yview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001764 def yview_pickplace(self, *what):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001765 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00001766
Guido van Rossum28574b51996-10-21 15:16:51 +00001767class _setit:
1768 def __init__(self, var, value):
1769 self.__value = value
1770 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001771 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001772 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001773
1774class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001775 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001776 kw = {"borderwidth": 2, "textvariable": variable,
1777 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1778 "highlightthickness": 2}
1779 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001780 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001781 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1782 self.menuname = menu._w
1783 menu.add_command(label=value, command=_setit(variable, value))
1784 for v in values:
1785 menu.add_command(label=v, command=_setit(variable, v))
1786 self["menu"] = menu
1787
1788 def __getitem__(self, name):
1789 if name == 'menu':
1790 return self.__menu
1791 return Widget.__getitem__(self, name)
1792
1793 def destroy(self):
1794 Menubutton.destroy(self)
1795 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001796
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001797class Image:
Guido van Rossumc4570481998-03-20 20:45:49 +00001798 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001799 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00001800 if not master:
1801 master = _default_root
1802 if not master:
1803 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001804 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001805 if not name:
1806 name = `id(self)`
1807 # The following is needed for systems where id(x)
1808 # can return a negative number, such as Linux/m68k:
1809 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001810 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1811 elif kw: cnf = kw
1812 options = ()
1813 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001814 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001815 v = self._register(v)
1816 options = options + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001817 self.tk.call(('image', 'create', imgtype, name,) + options)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001818 self.name = name
1819 def __str__(self): return self.name
1820 def __del__(self):
1821 if self.name:
1822 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001823 def __setitem__(self, key, value):
1824 self.tk.call(self.name, 'configure', '-'+key, value)
1825 def __getitem__(self, key):
1826 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001827 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001828 res = ()
1829 for k, v in _cnfmerge(kw).items():
1830 if v is not None:
1831 if k[-1] == '_': k = k[:-1]
1832 if callable(v):
1833 v = self._register(v)
1834 res = res + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00001835 self.tk.call((self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001836 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001837 def height(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001838 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001839 self.tk.call('image', 'height', self.name))
1840 def type(self):
1841 return self.tk.call('image', 'type', self.name)
1842 def width(self):
Guido van Rossum0132f691998-04-30 17:50:36 +00001843 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001844 self.tk.call('image', 'width', self.name))
1845
1846class PhotoImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001847 def __init__(self, name=None, cnf={}, master=None, **kw):
1848 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001849 def blank(self):
1850 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001851 def cget(self, option):
1852 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001853 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001854 def __getitem__(self, key):
1855 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001856 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001857 def copy(self):
1858 destImage = PhotoImage()
1859 self.tk.call(destImage, 'copy', self.name)
1860 return destImage
1861 def zoom(self,x,y=''):
1862 destImage = PhotoImage()
1863 if y=='': y=x
1864 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1865 return destImage
1866 def subsample(self,x,y=''):
1867 destImage = PhotoImage()
1868 if y=='': y=x
1869 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1870 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001871 def get(self, x, y):
1872 return self.tk.call(self.name, 'get', x, y)
1873 def put(self, data, to=None):
1874 args = (self.name, 'put', data)
1875 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001876 if to[0] == '-to':
1877 to = to[1:]
1878 args = args + ('-to',) + tuple(to)
Guido van Rossumf9756991998-04-29 21:57:08 +00001879 self.tk.call(args)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001880 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001881 def write(self, filename, format=None, from_coords=None):
1882 args = (self.name, 'write', filename)
1883 if format:
1884 args = args + ('-format', format)
1885 if from_coords:
1886 args = args + ('-from',) + tuple(from_coords)
Guido van Rossumf9756991998-04-29 21:57:08 +00001887 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001888
1889class BitmapImage(Image):
Guido van Rossumc4570481998-03-20 20:45:49 +00001890 def __init__(self, name=None, cnf={}, master=None, **kw):
1891 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001892
1893def image_names(): return _default_root.tk.call('image', 'names')
1894def image_types(): return _default_root.tk.call('image', 'types')
1895
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001896######################################################################
1897# Extensions:
1898
1899class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001900 def __init__(self, master=None, cnf={}, **kw):
1901 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001902 self.bind('<Any-Enter>', self.tkButtonEnter)
1903 self.bind('<Any-Leave>', self.tkButtonLeave)
1904 self.bind('<1>', self.tkButtonDown)
1905 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001906
1907class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001908 def __init__(self, master=None, cnf={}, **kw):
1909 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001910 self.bind('<Any-Enter>', self.tkButtonEnter)
1911 self.bind('<Any-Leave>', self.tkButtonLeave)
1912 self.bind('<1>', self.tkButtonDown)
1913 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1914 self['fg'] = self['bg']
1915 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001916
Guido van Rossumc417ef81996-08-21 23:38:59 +00001917######################################################################
1918# Test:
1919
1920def _test():
1921 root = Tk()
1922 label = Label(root, text="Proof-of-existence test for Tk")
1923 label.pack()
1924 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001925 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001926 text="[%s]" % root.test['text']))
1927 test.pack()
1928 root.test = test
1929 quit = Button(root, text="QUIT", command=root.destroy)
1930 quit.pack()
Guido van Rossum268824e1998-06-19 04:34:19 +00001931 # The following three commands are needed so the window pops
1932 # up on top on Windows...
1933 root.iconify()
1934 root.update()
1935 root.deiconify()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001936 root.mainloop()
1937
1938if __name__ == '__main__':
1939 _test()