blob: 5220c9d5565ad25105ddf53a604c491fb6035f9f [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# Tkinter.py -- Tk/Tcl widget wrappers
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002
Guido van Rossum37dcab11996-05-16 16:00:19 +00003__version__ = "$Revision$"
4
Guido van Rossum95806091997-02-15 18:33:24 +00005import _tkinter # If this fails your Python is not configured for Tk
6tkinter = _tkinter # b/w compat for export
7TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +00008from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +00009from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000010import string; _string = string; del string
Guido van Rossum18468821994-06-20 07:49:28 +000011
Guido van Rossum95806091997-02-15 18:33:24 +000012TkVersion = _string.atof(_tkinter.TK_VERSION)
13TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000014
Guido van Rossumd6615ab1997-08-05 02:35:01 +000015READABLE = _tkinter.READABLE
16WRITABLE = _tkinter.WRITABLE
17EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000018
19# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
20try: _tkinter.createfilehandler
21except AttributeError: _tkinter.createfilehandler = None
22try: _tkinter.deletefilehandler
23except AttributeError: _tkinter.deletefilehandler = None
Guido van Rossum36269991996-05-16 17:11:27 +000024
25
Guido van Rossum2dcf5291994-07-06 09:23:20 +000026def _flatten(tuple):
27 res = ()
28 for item in tuple:
29 if type(item) in (TupleType, ListType):
30 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000031 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000032 res = res + (item,)
33 return res
34
35def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000036 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000037 return cnfs
38 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000039 return cnfs
40 else:
41 cnf = {}
42 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000043 try:
44 cnf.update(c)
45 except (AttributeError, TypeError), msg:
46 print "_cnfmerge: fallback due to:", msg
47 for k, v in c.items():
48 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000049 return cnf
50
51class Event:
52 pass
53
Guido van Rossumaec5dc91994-06-27 07:55:12 +000054_default_root = None
55
Guido van Rossum45853db1994-06-20 12:19:19 +000056def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000057 pass
58
Guido van Rossum97aeca11994-07-07 13:12:12 +000059def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000060 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000061
Guido van Rossumaec5dc91994-06-27 07:55:12 +000062_varnum = 0
63class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000064 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000065 def __init__(self, master=None):
Guido van Rossumaec5dc91994-06-27 07:55:12 +000066 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +000067 if not master:
68 master = _default_root
69 self._master = master
70 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +000071 self._name = 'PY_VAR' + `_varnum`
72 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000073 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000074 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000075 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000076 def __str__(self):
77 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000079 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +000080 def trace_variable(self, mode, callback):
81 cbname = self._master._register(callback)
82 self._tk.call("trace", "variable", self._name, mode, cbname)
83 return cbname
84 trace = trace_variable
85 def trace_vdelete(self, mode, cbname):
86 self._tk.call("trace", "vdelete", self._name, mode, cbname)
87 self._tk.deletecommand(cbname)
88 def trace_vinfo(self):
89 return map(self._tk.split, self._tk.splitlist(
90 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000091
92class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000093 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000094 def __init__(self, master=None):
95 Variable.__init__(self, master)
96 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000097 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000098
99class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000100 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000101 def __init__(self, master=None):
102 Variable.__init__(self, master)
103 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000104 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000105
106class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000107 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000108 def __init__(self, master=None):
109 Variable.__init__(self, master)
110 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000111 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000112
113class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000114 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000115 def __init__(self, master=None):
116 Variable.__init__(self, master)
117 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000118 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000119
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000120def mainloop(n=0):
121 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000122
123def getint(s):
124 return _default_root.tk.getint(s)
125
126def getdouble(s):
127 return _default_root.tk.getdouble(s)
128
129def getboolean(s):
130 return _default_root.tk.getboolean(s)
131
Guido van Rossum368e06b1997-11-07 20:38:49 +0000132# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000133class Misc:
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000134 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000135 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000136 def destroy(self):
137 if self._tclCommands is not None:
138 for name in self._tclCommands:
139 #print '- Tkinter: deleted command', name
140 self.tk.deletecommand(name)
141 self._tclCommands = None
142 def deletecommand(self, name):
143 #print '- Tkinter: deleted command', name
144 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000145 try:
146 self._tclCommands.remove(name)
147 except ValueError:
148 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000149 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000150 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000151 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000152 def tk_bisque(self):
153 self.tk.call('tk_bisque')
154 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000155 apply(self.tk.call, ('tk_setPalette',)
156 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000157 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000158 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000159 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000160 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000161 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000162 def wait_window(self, window=None):
163 if window == None:
164 window = self
165 self.tk.call('tkwait', 'window', window._w)
166 def wait_visibility(self, window=None):
167 if window == None:
168 window = self
169 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000170 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000171 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000172 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000173 return self.tk.getvar(name)
174 def getint(self, s):
175 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000176 def getdouble(self, s):
177 return self.tk.getdouble(s)
178 def getboolean(self, s):
179 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000180 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000181 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000182 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000183 def focus_force(self):
184 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000185 def focus_get(self):
186 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000187 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000188 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000189 def focus_displayof(self):
190 name = self.tk.call('focus', '-displayof', self._w)
191 if name == 'none' or not name: return None
192 return self._nametowidget(name)
193 def focus_lastfor(self):
194 name = self.tk.call('focus', '-lastfor', self._w)
195 if name == 'none' or not name: return None
196 return self._nametowidget(name)
197 def tk_focusFollowsMouse(self):
198 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000199 def tk_focusNext(self):
200 name = self.tk.call('tk_focusNext', self._w)
201 if not name: return None
202 return self._nametowidget(name)
203 def tk_focusPrev(self):
204 name = self.tk.call('tk_focusPrev', self._w)
205 if not name: return None
206 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000207 def after(self, ms, func=None, *args):
208 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000209 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000210 self.tk.call('after', ms)
211 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000212 # XXX Disgusting hack to clean up after calling func
213 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000214 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000215 try:
216 apply(func, args)
217 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000218 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000219 name = self._register(callit)
220 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000221 return self.tk.call('after', ms, name)
222 def after_idle(self, func, *args):
223 return apply(self.after, ('idle', func) + args)
224 def after_cancel(self, id):
225 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000226 def bell(self, displayof=0):
227 apply(self.tk.call, ('bell',) + self._displayof(displayof))
228 # Clipboard handling:
229 def clipboard_clear(self, **kw):
230 if not kw.has_key('displayof'): kw['displayof'] = self._w
231 apply(self.tk.call,
232 ('clipboard', 'clear') + self._options(kw))
233 def clipboard_append(self, string, **kw):
234 if not kw.has_key('displayof'): kw['displayof'] = self._w
235 apply(self.tk.call,
236 ('clipboard', 'append') + self._options(kw)
237 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000238 # XXX grab current w/o window argument
239 def grab_current(self):
240 name = self.tk.call('grab', 'current', self._w)
241 if not name: return None
242 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000243 def grab_release(self):
244 self.tk.call('grab', 'release', self._w)
245 def grab_set(self):
246 self.tk.call('grab', 'set', self._w)
247 def grab_set_global(self):
248 self.tk.call('grab', 'set', '-global', self._w)
249 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000250 status = self.tk.call('grab', 'status', self._w)
251 if status == 'none': status = None
252 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000253 def lower(self, belowThis=None):
254 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000255 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000256 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000257 def option_clear(self):
258 self.tk.call('option', 'clear')
259 def option_get(self, name, className):
260 return self.tk.call('option', 'get', self._w, name, className)
261 def option_readfile(self, fileName, priority = None):
262 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000263 def selection_clear(self, **kw):
264 if not kw.has_key('displayof'): kw['displayof'] = self._w
265 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
266 def selection_get(self, **kw):
267 if not kw.has_key('displayof'): kw['displayof'] = self._w
268 return apply(self.tk.call,
269 ('selection', 'get') + self._options(kw))
270 def selection_handle(self, command, **kw):
271 name = self._register(command)
272 apply(self.tk.call,
273 ('selection', 'handle') + self._options(kw)
274 + (self._w, name))
275 def selection_own(self, **kw):
276 "Become owner of X selection."
277 apply(self.tk.call,
278 ('selection', 'own') + self._options(kw) + (self._w,))
279 def selection_own_get(self, **kw):
280 "Find owner of X selection."
281 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000282 name = apply(self.tk.call,
283 ('selection', 'own') + self._options(kw))
284 if not name: return None
285 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000286 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000287 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000288 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000289 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000290 def tkraise(self, aboveThis=None):
291 self.tk.call('raise', self._w, aboveThis)
292 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000293 def colormodel(self, value=None):
294 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000295 def winfo_atom(self, name, displayof=0):
296 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
297 return self.tk.getint(apply(self.tk.call, args))
298 def winfo_atomname(self, id, displayof=0):
299 args = ('winfo', 'atomname') \
300 + self._displayof(displayof) + (id,)
301 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000302 def winfo_cells(self):
303 return self.tk.getint(
304 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000305 def winfo_children(self):
306 return map(self._nametowidget,
307 self.tk.splitlist(self.tk.call(
308 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000309 def winfo_class(self):
310 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000311 def winfo_colormapfull(self):
312 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000313 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000314 def winfo_containing(self, rootX, rootY, displayof=0):
315 args = ('winfo', 'containing') \
316 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000317 name = apply(self.tk.call, args)
318 if not name: return None
319 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000320 def winfo_depth(self):
321 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
322 def winfo_exists(self):
323 return self.tk.getint(
324 self.tk.call('winfo', 'exists', self._w))
325 def winfo_fpixels(self, number):
326 return self.tk.getdouble(self.tk.call(
327 'winfo', 'fpixels', self._w, number))
328 def winfo_geometry(self):
329 return self.tk.call('winfo', 'geometry', self._w)
330 def winfo_height(self):
331 return self.tk.getint(
332 self.tk.call('winfo', 'height', self._w))
333 def winfo_id(self):
334 return self.tk.getint(
335 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000336 def winfo_interps(self, displayof=0):
337 args = ('winfo', 'interps') + self._displayof(displayof)
338 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000339 def winfo_ismapped(self):
340 return self.tk.getint(
341 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000342 def winfo_manager(self):
343 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000344 def winfo_name(self):
345 return self.tk.call('winfo', 'name', self._w)
346 def winfo_parent(self):
347 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000348 def winfo_pathname(self, id, displayof=0):
349 args = ('winfo', 'pathname') \
350 + self._displayof(displayof) + (id,)
351 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000352 def winfo_pixels(self, number):
353 return self.tk.getint(
354 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000355 def winfo_pointerx(self):
356 return self.tk.getint(
357 self.tk.call('winfo', 'pointerx', self._w))
358 def winfo_pointerxy(self):
359 return self._getints(
360 self.tk.call('winfo', 'pointerxy', self._w))
361 def winfo_pointery(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000364 def winfo_reqheight(self):
365 return self.tk.getint(
366 self.tk.call('winfo', 'reqheight', self._w))
367 def winfo_reqwidth(self):
368 return self.tk.getint(
369 self.tk.call('winfo', 'reqwidth', self._w))
370 def winfo_rgb(self, color):
371 return self._getints(
372 self.tk.call('winfo', 'rgb', self._w, color))
373 def winfo_rootx(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'rootx', self._w))
376 def winfo_rooty(self):
377 return self.tk.getint(
378 self.tk.call('winfo', 'rooty', self._w))
379 def winfo_screen(self):
380 return self.tk.call('winfo', 'screen', self._w)
381 def winfo_screencells(self):
382 return self.tk.getint(
383 self.tk.call('winfo', 'screencells', self._w))
384 def winfo_screendepth(self):
385 return self.tk.getint(
386 self.tk.call('winfo', 'screendepth', self._w))
387 def winfo_screenheight(self):
388 return self.tk.getint(
389 self.tk.call('winfo', 'screenheight', self._w))
390 def winfo_screenmmheight(self):
391 return self.tk.getint(
392 self.tk.call('winfo', 'screenmmheight', self._w))
393 def winfo_screenmmwidth(self):
394 return self.tk.getint(
395 self.tk.call('winfo', 'screenmmwidth', self._w))
396 def winfo_screenvisual(self):
397 return self.tk.call('winfo', 'screenvisual', self._w)
398 def winfo_screenwidth(self):
399 return self.tk.getint(
400 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000401 def winfo_server(self):
402 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000403 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000404 return self._nametowidget(self.tk.call(
405 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000406 def winfo_viewable(self):
407 return self.tk.getint(
408 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000409 def winfo_visual(self):
410 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000411 def winfo_visualid(self):
412 return self.tk.call('winfo', 'visualid', self._w)
413 def winfo_visualsavailable(self, includeids=0):
414 data = self.tk.split(
415 self.tk.call('winfo', 'visualsavailable', self._w,
416 includeids and 'includeids' or None))
417 def parseitem(x, self=self):
418 return x[:1] + tuple(map(self.tk.getint, x[1:]))
419 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000420 def winfo_vrootheight(self):
421 return self.tk.getint(
422 self.tk.call('winfo', 'vrootheight', self._w))
423 def winfo_vrootwidth(self):
424 return self.tk.getint(
425 self.tk.call('winfo', 'vrootwidth', self._w))
426 def winfo_vrootx(self):
427 return self.tk.getint(
428 self.tk.call('winfo', 'vrootx', self._w))
429 def winfo_vrooty(self):
430 return self.tk.getint(
431 self.tk.call('winfo', 'vrooty', self._w))
432 def winfo_width(self):
433 return self.tk.getint(
434 self.tk.call('winfo', 'width', self._w))
435 def winfo_x(self):
436 return self.tk.getint(
437 self.tk.call('winfo', 'x', self._w))
438 def winfo_y(self):
439 return self.tk.getint(
440 self.tk.call('winfo', 'y', self._w))
441 def update(self):
442 self.tk.call('update')
443 def update_idletasks(self):
444 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000445 def bindtags(self, tagList=None):
446 if tagList is None:
447 return self.tk.splitlist(
448 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000449 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000450 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000451 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000452 if func:
453 cmd = ("%sset _tkinter_break [%s %s]\n"
454 'if {"$_tkinter_break" == "break"} break\n') \
455 % (add and '+' or '',
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000456 self._register(func, self._substitute,
457 needcleanup),
Guido van Rossum37dcab11996-05-16 16:00:19 +0000458 _string.join(self._subst_format))
459 apply(self.tk.call, what + (sequence, cmd))
460 elif func == '':
461 apply(self.tk.call, what + (sequence, func))
462 else:
463 return apply(self.tk.call, what + (sequence,))
464 def bind(self, sequence=None, func=None, add=None):
465 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000466 def unbind(self, sequence):
467 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000468 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000469 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000470 def unbind_all(self, sequence):
471 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000472 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000473 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000474 def unbind_class(self, className, sequence):
475 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000476 def mainloop(self, n=0):
477 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000478 def quit(self):
479 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000480 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000481 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000482 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
483 def _getdoubles(self, string):
484 if not string: return None
485 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000486 def _getboolean(self, string):
487 if string:
488 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000489 def _displayof(self, displayof):
490 if displayof:
491 return ('-displayof', displayof)
492 if displayof is None:
493 return ('-displayof', self._w)
494 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000495 def _options(self, cnf, kw = None):
496 if kw:
497 cnf = _cnfmerge((cnf, kw))
498 else:
499 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000500 res = ()
501 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000502 if v is not None:
503 if k[-1] == '_': k = k[:-1]
504 if callable(v):
505 v = self._register(v)
506 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000507 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000508 def nametowidget(self, name):
Guido van Rossum45853db1994-06-20 12:19:19 +0000509 w = self
510 if name[0] == '.':
511 w = w._root()
512 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000513 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000514 while name:
515 i = find(name, '.')
516 if i >= 0:
517 name, tail = name[:i], name[i+1:]
518 else:
519 tail = ''
520 w = w.children[name]
521 name = tail
522 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000523 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000524 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000525 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000526 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000527 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000528 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000529 except AttributeError:
530 pass
531 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000532 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000533 except AttributeError:
534 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000535 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000536 if needcleanup:
537 if self._tclCommands is None:
538 self._tclCommands = []
539 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000540 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000541 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000542 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000543 def _root(self):
544 w = self
545 while w.master: w = w.master
546 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000547 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000548 '%s', '%t', '%w', '%x', '%y',
549 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
550 def _substitute(self, *args):
551 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000552 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000553 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
554 # Missing: (a, c, d, m, o, v, B, R)
555 e = Event()
556 e.serial = tk.getint(nsign)
557 e.num = tk.getint(b)
558 try: e.focus = tk.getboolean(f)
559 except TclError: pass
560 e.height = tk.getint(h)
561 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000562 # For Visibility events, event state is a string and
563 # not an integer:
564 try:
565 e.state = tk.getint(s)
566 except TclError:
567 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000568 e.time = tk.getint(t)
569 e.width = tk.getint(w)
570 e.x = tk.getint(x)
571 e.y = tk.getint(y)
572 e.char = A
573 try: e.send_event = tk.getboolean(E)
574 except TclError: pass
575 e.keysym = K
576 e.keysym_num = tk.getint(N)
577 e.type = T
578 e.widget = self._nametowidget(W)
579 e.x_root = tk.getint(X)
580 e.y_root = tk.getint(Y)
581 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000582 def _report_exception(self):
583 import sys
584 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
585 root = self._root()
586 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000587 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000588 def configure(self, cnf=None, **kw):
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000589 # XXX ought to generalize this so tag_config etc. can use it
590 if kw:
591 cnf = _cnfmerge((cnf, kw))
592 elif cnf:
593 cnf = _cnfmerge(cnf)
594 if cnf is None:
595 cnf = {}
596 for x in self.tk.split(
597 self.tk.call(self._w, 'configure')):
598 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
599 return cnf
600 if type(cnf) is StringType:
601 x = self.tk.split(self.tk.call(
602 self._w, 'configure', '-'+cnf))
603 return (x[0][1:],) + x[1:]
604 apply(self.tk.call, (self._w, 'configure')
605 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000606 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000607 def cget(self, key):
608 return self.tk.call(self._w, 'cget', '-' + key)
609 __getitem__ = cget
610 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +0000611 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +0000612 def keys(self):
613 return map(lambda x: x[0][1:],
614 self.tk.split(self.tk.call(self._w, 'configure')))
615 def __str__(self):
616 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +0000617 # Pack methods that apply to the master
618 _noarg_ = ['_noarg_']
619 def pack_propagate(self, flag=_noarg_):
620 if flag is Misc._noarg_:
621 return self._getboolean(self.tk.call(
622 'pack', 'propagate', self._w))
623 else:
624 self.tk.call('pack', 'propagate', self._w, flag)
625 propagate = pack_propagate
626 def pack_slaves(self):
627 return map(self._nametowidget,
628 self.tk.splitlist(
629 self.tk.call('pack', 'slaves', self._w)))
630 slaves = pack_slaves
631 # Place method that applies to the master
632 def place_slaves(self):
633 return map(self._nametowidget,
634 self.tk.splitlist(
635 self.tk.call(
636 'place', 'slaves', self._w)))
637 # Grid methods that apply to the master
638 def grid_bbox(self, column, row):
639 return self._getints(
640 self.tk.call(
641 'grid', 'bbox', self._w, column, row)) or None
642 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000643 def _grid_configure(self, command, index, cnf, kw):
644 if type(cnf) is StringType and not kw:
645 if cnf[-1:] == '_':
646 cnf = cnf[:-1]
647 if cnf[:1] != '-':
648 cnf = '-'+cnf
649 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000650 else:
651 options = self._options(cnf, kw)
652 if not options:
653 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000654 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000655 words = self.tk.splitlist(res)
656 dict = {}
657 for i in range(0, len(words), 2):
658 key = words[i][1:]
659 value = words[i+1]
660 if not value:
661 value = None
662 elif '.' in value:
663 value = self.tk.getdouble(value)
664 else:
665 value = self.tk.getint(value)
666 dict[key] = value
667 return dict
668 res = apply(self.tk.call,
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000669 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000670 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000671 if len(options) == 1:
672 if not res: return None
673 # In Tk 7.5, -width can be a float
674 if '.' in res: return self.tk.getdouble(res)
675 return self.tk.getint(res)
676 def grid_columnconfigure(self, index, cnf={}, **kw):
677 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000678 columnconfigure = grid_columnconfigure
679 def grid_propagate(self, flag=_noarg_):
680 if flag is Misc._noarg_:
681 return self._getboolean(self.tk.call(
682 'grid', 'propagate', self._w))
683 else:
684 self.tk.call('grid', 'propagate', self._w, flag)
685 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000686 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000687 rowconfigure = grid_rowconfigure
688 def grid_size(self):
689 return self._getints(
690 self.tk.call('grid', 'size', self._w)) or None
691 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +0000692 def grid_slaves(self, row=None, column=None):
693 args = ()
Guido van Rossum9fd41e31997-12-29 19:59:33 +0000694 if row:
695 args = args + ('-row', row)
696 if column:
697 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000698 return map(self._nametowidget,
699 self.tk.splitlist(
700 apply(self.tk.call,
701 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +0000702
Guido van Rossum80f8be81997-12-02 19:51:39 +0000703 # Support for the "event" command, new in Tk 4.2.
704 # By Case Roole.
705
706 def event_add(self,virtual, *sequences):
707 args = ('event', 'add', virtual) + sequences
708 apply( _default_root.tk.call, args )
709
710 def event_delete(self,virtual,*sequences):
711 args = ('event', 'delete', virtual) + sequences
712 apply( _default_root.tk.call, args )
713
714 def event_generate(self, sequence, **kw):
715 args = ('event', 'generate', self._w, sequence)
716 for k,v in kw.items():
717 args = args + ('-%s' % k,str(v))
718 apply( _default_root.tk.call, args )
719
720 def event_info(self,virtual=None):
721 args = ('event', 'info')
722 if virtual is not None: args = args + (virtual,)
723 s = apply( _default_root.tk.call, args )
724 return _string.split(s)
725
726
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000727class CallWrapper:
728 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000729 self.func = func
730 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000731 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000732 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000733 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000734 if self.subst:
735 args = apply(self.subst, args)
736 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000737 except SystemExit, msg:
738 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000739 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000740 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000741
742class Wm:
743 def aspect(self,
744 minNumer=None, minDenom=None,
745 maxNumer=None, maxDenom=None):
746 return self._getints(
747 self.tk.call('wm', 'aspect', self._w,
748 minNumer, minDenom,
749 maxNumer, maxDenom))
750 def client(self, name=None):
751 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000752 def colormapwindows(self, *wlist):
753 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
754 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000755 def command(self, value=None):
756 return self.tk.call('wm', 'command', self._w, value)
757 def deiconify(self):
758 return self.tk.call('wm', 'deiconify', self._w)
759 def focusmodel(self, model=None):
760 return self.tk.call('wm', 'focusmodel', self._w, model)
761 def frame(self):
762 return self.tk.call('wm', 'frame', self._w)
763 def geometry(self, newGeometry=None):
764 return self.tk.call('wm', 'geometry', self._w, newGeometry)
765 def grid(self,
766 baseWidht=None, baseHeight=None,
767 widthInc=None, heightInc=None):
768 return self._getints(self.tk.call(
769 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +0000770 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossum18468821994-06-20 07:49:28 +0000771 def group(self, pathName=None):
772 return self.tk.call('wm', 'group', self._w, pathName)
773 def iconbitmap(self, bitmap=None):
774 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
775 def iconify(self):
776 return self.tk.call('wm', 'iconify', self._w)
777 def iconmask(self, bitmap=None):
778 return self.tk.call('wm', 'iconmask', self._w, bitmap)
779 def iconname(self, newName=None):
780 return self.tk.call('wm', 'iconname', self._w, newName)
781 def iconposition(self, x=None, y=None):
782 return self._getints(self.tk.call(
783 'wm', 'iconposition', self._w, x, y))
784 def iconwindow(self, pathName=None):
785 return self.tk.call('wm', 'iconwindow', self._w, pathName)
786 def maxsize(self, width=None, height=None):
787 return self._getints(self.tk.call(
788 'wm', 'maxsize', self._w, width, height))
789 def minsize(self, width=None, height=None):
790 return self._getints(self.tk.call(
791 'wm', 'minsize', self._w, width, height))
792 def overrideredirect(self, boolean=None):
793 return self._getboolean(self.tk.call(
794 'wm', 'overrideredirect', self._w, boolean))
795 def positionfrom(self, who=None):
796 return self.tk.call('wm', 'positionfrom', self._w, who)
797 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000798 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000799 command = self._register(func)
800 else:
801 command = func
802 return self.tk.call(
803 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000804 def resizable(self, width=None, height=None):
805 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000806 def sizefrom(self, who=None):
807 return self.tk.call('wm', 'sizefrom', self._w, who)
808 def state(self):
809 return self.tk.call('wm', 'state', self._w)
810 def title(self, string=None):
811 return self.tk.call('wm', 'title', self._w, string)
812 def transient(self, master=None):
813 return self.tk.call('wm', 'transient', self._w, master)
814 def withdraw(self):
815 return self.tk.call('wm', 'withdraw', self._w)
816
817class Tk(Misc, Wm):
818 _w = '.'
819 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000820 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000821 self.master = None
822 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000823 if baseName is None:
824 import sys, os
825 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000826 baseName, ext = os.path.splitext(baseName)
827 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000828 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000829 try:
830 # Disable event scanning except for Command-Period
831 import MacOS
Guido van Rossum9d9af2c1997-08-12 18:21:08 +0000832 try:
833 MacOS.SchedParams(1, 0)
834 except AttributeError:
835 # pre-1.5, use old routine
836 MacOS.EnableAppswitch(0)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000837 except ImportError:
838 pass
839 else:
840 # Work around nasty MacTk bug
841 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000842 # Version sanity checks
843 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000844 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000845 raise RuntimeError, \
846 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000847 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000848 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000849 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000850 raise RuntimeError, \
851 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000852 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000853 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000854 raise RuntimeError, \
855 "Tk 4.0 or higher is required; found Tk %s" \
856 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000857 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000858 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000859 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000860 if not _default_root:
861 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000862 def destroy(self):
863 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000864 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000865 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +0000866 global _default_root
867 if _default_root is self:
868 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +0000869 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000870 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000871 if os.environ.has_key('HOME'): home = os.environ['HOME']
872 else: home = os.curdir
873 class_tcl = os.path.join(home, '.%s.tcl' % className)
874 class_py = os.path.join(home, '.%s.py' % className)
875 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
876 base_py = os.path.join(home, '.%s.py' % baseName)
877 dir = {'self': self}
878 exec 'from Tkinter import *' in dir
879 if os.path.isfile(class_tcl):
880 print 'source', `class_tcl`
881 self.tk.call('source', class_tcl)
882 if os.path.isfile(class_py):
883 print 'execfile', `class_py`
884 execfile(class_py, dir)
885 if os.path.isfile(base_tcl):
886 print 'source', `base_tcl`
887 self.tk.call('source', base_tcl)
888 if os.path.isfile(base_py):
889 print 'execfile', `base_py`
890 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000891 def report_callback_exception(self, exc, val, tb):
892 import traceback
893 print "Exception in Tkinter callback"
894 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000895
Guido van Rossum368e06b1997-11-07 20:38:49 +0000896# Ideally, the classes Pack, Place and Grid disappear, the
897# pack/place/grid methods are defined on the Widget class, and
898# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
899# ...), with pack(), place() and grid() being short for
900# pack_configure(), place_configure() and grid_columnconfigure(), and
901# forget() being short for pack_forget(). As a practical matter, I'm
902# afraid that there is too much code out there that may be using the
903# Pack, Place or Grid class, so I leave them intact -- but only as
904# backwards compatibility features. Also note that those methods that
905# take a master as argument (e.g. pack_propagate) have been moved to
906# the Misc class (which now incorporates all methods common between
907# toplevel and interior widgets). Again, for compatibility, these are
908# copied into the Pack, Place or Grid class.
909
Guido van Rossum18468821994-06-20 07:49:28 +0000910class Pack:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000911 def pack_configure(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000912 apply(self.tk.call,
913 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000914 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000915 pack = configure = config = pack_configure
916 def pack_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000917 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000918 forget = pack_forget
919 def pack_info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000920 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000921 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000922 dict = {}
923 for i in range(0, len(words), 2):
924 key = words[i][1:]
925 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000926 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000927 value = self._nametowidget(value)
928 dict[key] = value
929 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000930 info = pack_info
931 propagate = pack_propagate = Misc.pack_propagate
932 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000933
934class Place:
Guido van Rossum368e06b1997-11-07 20:38:49 +0000935 def place_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000936 for k in ['in_']:
937 if kw.has_key(k):
938 kw[k[:-1]] = kw[k]
939 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000940 apply(self.tk.call,
941 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000942 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000943 place = configure = config = place_configure
944 def place_forget(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000945 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000946 forget = place_forget
947 def place_info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000948 words = self.tk.splitlist(
949 self.tk.call('place', 'info', self._w))
950 dict = {}
951 for i in range(0, len(words), 2):
952 key = words[i][1:]
953 value = words[i+1]
954 if value[:1] == '.':
955 value = self._nametowidget(value)
956 dict[key] = value
957 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000958 info = place_info
959 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000960
Guido van Rossum37dcab11996-05-16 16:00:19 +0000961class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000962 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000963 def grid_configure(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000964 apply(self.tk.call,
965 ('grid', 'configure', self._w)
966 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +0000967 grid = configure = config = grid_configure
968 bbox = grid_bbox = Misc.grid_bbox
969 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
970 def grid_forget(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000971 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +0000972 forget = grid_forget
973 def grid_info(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000974 words = self.tk.splitlist(
975 self.tk.call('grid', 'info', self._w))
976 dict = {}
977 for i in range(0, len(words), 2):
978 key = words[i][1:]
979 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000980 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000981 value = self._nametowidget(value)
982 dict[key] = value
983 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +0000984 info = grid_info
985 def grid_location(self, x, y):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000986 return self._getints(
987 self.tk.call(
988 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +0000989 location = grid_location
990 propagate = grid_propagate = Misc.grid_propagate
991 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
992 size = grid_size = Misc.grid_size
993 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +0000994
Guido van Rossum368e06b1997-11-07 20:38:49 +0000995class BaseWidget(Misc):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000996 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000997 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000998 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000999 if not _default_root:
1000 _default_root = Tk()
1001 master = _default_root
1002 if not _default_root:
1003 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +00001004 self.master = master
1005 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001006 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001007 if cnf.has_key('name'):
1008 name = cnf['name']
1009 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001010 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001011 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001012 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001013 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001014 self._w = '.' + name
1015 else:
1016 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001017 self.children = {}
1018 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001019 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001020 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001021 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1022 if kw:
1023 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001024 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001025 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001026 classes = []
1027 for k in cnf.keys():
1028 if type(k) is ClassType:
1029 classes.append((k, cnf[k]))
1030 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001031 apply(self.tk.call,
1032 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001033 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001034 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001035 def destroy(self):
1036 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001037 if self.master.children.has_key(self._name):
1038 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001039 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001040 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001041 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001042 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001043
Guido van Rossum368e06b1997-11-07 20:38:49 +00001044class Widget(BaseWidget, Pack, Place, Grid):
1045 pass
1046
1047class Toplevel(BaseWidget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001048 def __init__(self, master=None, cnf={}, **kw):
1049 if kw:
1050 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001051 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001052 for wmkey in ['screen', 'class_', 'class', 'visual',
1053 'colormap']:
1054 if cnf.has_key(wmkey):
1055 val = cnf[wmkey]
1056 # TBD: a hack needed because some keys
1057 # are not valid as keyword arguments
1058 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1059 else: opt = '-'+wmkey
1060 extra = extra + (opt, val)
1061 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001062 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001063 root = self._root()
1064 self.iconname(root.iconname())
1065 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +00001066
1067class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001068 def __init__(self, master=None, cnf={}, **kw):
1069 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001070 def tkButtonEnter(self, *dummy):
1071 self.tk.call('tkButtonEnter', self._w)
1072 def tkButtonLeave(self, *dummy):
1073 self.tk.call('tkButtonLeave', self._w)
1074 def tkButtonDown(self, *dummy):
1075 self.tk.call('tkButtonDown', self._w)
1076 def tkButtonUp(self, *dummy):
1077 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001078 def tkButtonInvoke(self, *dummy):
1079 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001080 def flash(self):
1081 self.tk.call(self._w, 'flash')
1082 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001083 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001084
1085# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001086# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001087def AtEnd():
1088 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001089def AtInsert(*args):
1090 s = 'insert'
1091 for a in args:
1092 if a: s = s + (' ' + a)
1093 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001094def AtSelFirst():
1095 return 'sel.first'
1096def AtSelLast():
1097 return 'sel.last'
1098def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001099 if y is None:
1100 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001101 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001102 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001103
1104class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001105 def __init__(self, master=None, cnf={}, **kw):
1106 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001107 def addtag(self, *args):
1108 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001109 def addtag_above(self, newtag, tagOrId):
1110 self.addtag(newtag, 'above', tagOrId)
1111 def addtag_all(self, newtag):
1112 self.addtag(newtag, 'all')
1113 def addtag_below(self, newtag, tagOrId):
1114 self.addtag(newtag, 'below', tagOrId)
1115 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1116 self.addtag(newtag, 'closest', x, y, halo, start)
1117 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1118 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1119 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1120 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1121 def addtag_withtag(self, newtag, tagOrId):
1122 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001123 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001124 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001125 def tag_unbind(self, tagOrId, sequence):
1126 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001127 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001128 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001129 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001130 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001131 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001132 self._w, 'canvasx', screenx, gridspacing))
1133 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001134 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001135 self._w, 'canvasy', screeny, gridspacing))
1136 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001137 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001138 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001139 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001140 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001141 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001142 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001143 args = args[:-1]
1144 else:
1145 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001146 return self.tk.getint(apply(
1147 self.tk.call,
1148 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001149 + args + self._options(cnf, kw)))
1150 def create_arc(self, *args, **kw):
1151 return self._create('arc', args, kw)
1152 def create_bitmap(self, *args, **kw):
1153 return self._create('bitmap', args, kw)
1154 def create_image(self, *args, **kw):
1155 return self._create('image', args, kw)
1156 def create_line(self, *args, **kw):
1157 return self._create('line', args, kw)
1158 def create_oval(self, *args, **kw):
1159 return self._create('oval', args, kw)
1160 def create_polygon(self, *args, **kw):
1161 return self._create('polygon', args, kw)
1162 def create_rectangle(self, *args, **kw):
1163 return self._create('rectangle', args, kw)
1164 def create_text(self, *args, **kw):
1165 return self._create('text', args, kw)
1166 def create_window(self, *args, **kw):
1167 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001168 def dchars(self, *args):
1169 self._do('dchars', args)
1170 def delete(self, *args):
1171 self._do('delete', args)
1172 def dtag(self, *args):
1173 self._do('dtag', args)
1174 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001175 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001176 def find_above(self, tagOrId):
1177 return self.find('above', tagOrId)
1178 def find_all(self):
1179 return self.find('all')
1180 def find_below(self, tagOrId):
1181 return self.find('below', tagOrId)
1182 def find_closest(self, x, y, halo=None, start=None):
1183 return self.find('closest', x, y, halo, start)
1184 def find_enclosed(self, x1, y1, x2, y2):
1185 return self.find('enclosed', x1, y1, x2, y2)
1186 def find_overlapping(self, x1, y1, x2, y2):
1187 return self.find('overlapping', x1, y1, x2, y2)
1188 def find_withtag(self, tagOrId):
1189 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001190 def focus(self, *args):
1191 return self._do('focus', args)
1192 def gettags(self, *args):
1193 return self.tk.splitlist(self._do('gettags', args))
1194 def icursor(self, *args):
1195 self._do('icursor', args)
1196 def index(self, *args):
1197 return self.tk.getint(self._do('index', args))
1198 def insert(self, *args):
1199 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001200 def itemcget(self, tagOrId, option):
1201 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001202 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001203 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001204 cnf = {}
1205 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001206 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001207 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1208 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001209 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001210 x = self.tk.split(self._do('itemconfigure',
1211 (tagOrId, '-'+cnf,)))
1212 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001213 self._do('itemconfigure', (tagOrId,)
1214 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001215 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001216 def lower(self, *args):
1217 self._do('lower', args)
1218 def move(self, *args):
1219 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001220 def postscript(self, cnf={}, **kw):
1221 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001222 def tkraise(self, *args):
1223 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001224 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001225 def scale(self, *args):
1226 self._do('scale', args)
1227 def scan_mark(self, x, y):
1228 self.tk.call(self._w, 'scan', 'mark', x, y)
1229 def scan_dragto(self, x, y):
1230 self.tk.call(self._w, 'scan', 'dragto', x, y)
1231 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001232 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001233 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001234 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001235 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001236 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001237 def select_item(self):
1238 self.tk.call(self._w, 'select', 'item')
1239 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001240 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001241 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001242 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001243 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001244 if not args:
1245 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001246 apply(self.tk.call, (self._w, 'xview')+args)
1247 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001248 if not args:
1249 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001250 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001251
1252class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001253 def __init__(self, master=None, cnf={}, **kw):
1254 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001255 def deselect(self):
1256 self.tk.call(self._w, 'deselect')
1257 def flash(self):
1258 self.tk.call(self._w, 'flash')
1259 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001260 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001261 def select(self):
1262 self.tk.call(self._w, 'select')
1263 def toggle(self):
1264 self.tk.call(self._w, 'toggle')
1265
1266class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001267 def __init__(self, master=None, cnf={}, **kw):
1268 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001269 def delete(self, first, last=None):
1270 self.tk.call(self._w, 'delete', first, last)
1271 def get(self):
1272 return self.tk.call(self._w, 'get')
1273 def icursor(self, index):
1274 self.tk.call(self._w, 'icursor', index)
1275 def index(self, index):
1276 return self.tk.getint(self.tk.call(
1277 self._w, 'index', index))
1278 def insert(self, index, string):
1279 self.tk.call(self._w, 'insert', index, string)
1280 def scan_mark(self, x):
1281 self.tk.call(self._w, 'scan', 'mark', x)
1282 def scan_dragto(self, x):
1283 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001284 def selection_adjust(self, index):
1285 self.tk.call(self._w, 'selection', 'adjust', index)
1286 select_adjust = selection_adjust
1287 def selection_clear(self):
1288 self.tk.call(self._w, 'selection', 'clear')
1289 select_clear = selection_clear
1290 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001291 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001292 select_from = selection_from
1293 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001294 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001295 self.tk.call(self._w, 'selection', 'present'))
1296 select_present = selection_present
1297 def selection_range(self, start, end):
1298 self.tk.call(self._w, 'selection', 'range', start, end)
1299 select_range = selection_range
1300 def selection_to(self, index):
1301 self.tk.call(self._w, 'selection', 'to', index)
1302 select_to = selection_to
1303 def xview(self, index):
1304 self.tk.call(self._w, 'xview', index)
1305 def xview_moveto(self, fraction):
1306 self.tk.call(self._w, 'xview', 'moveto', fraction)
1307 def xview_scroll(self, number, what):
1308 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001309
1310class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001311 def __init__(self, master=None, cnf={}, **kw):
1312 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001313 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001314 if cnf.has_key('class_'):
1315 extra = ('-class', cnf['class_'])
1316 del cnf['class_']
1317 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001318 extra = ('-class', cnf['class'])
1319 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001320 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001321
1322class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001323 def __init__(self, master=None, cnf={}, **kw):
1324 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001325
Guido van Rossum18468821994-06-20 07:49:28 +00001326class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001327 def __init__(self, master=None, cnf={}, **kw):
1328 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001329 def activate(self, index):
1330 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001331 def bbox(self, *args):
1332 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001333 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001334 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001335 return self.tk.splitlist(self.tk.call(
1336 self._w, 'curselection'))
1337 def delete(self, first, last=None):
1338 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001339 def get(self, first, last=None):
1340 if last:
1341 return self.tk.splitlist(self.tk.call(
1342 self._w, 'get', first, last))
1343 else:
1344 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001345 def insert(self, index, *elements):
1346 apply(self.tk.call,
1347 (self._w, 'insert', index) + elements)
1348 def nearest(self, y):
1349 return self.tk.getint(self.tk.call(
1350 self._w, 'nearest', y))
1351 def scan_mark(self, x, y):
1352 self.tk.call(self._w, 'scan', 'mark', x, y)
1353 def scan_dragto(self, x, y):
1354 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001355 def see(self, index):
1356 self.tk.call(self._w, 'see', index)
1357 def index(self, index):
1358 i = self.tk.call(self._w, 'index', index)
1359 if i == 'none': return None
1360 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001361 def select_anchor(self, index):
1362 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001363 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001364 def select_clear(self, first, last=None):
1365 self.tk.call(self._w,
1366 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001367 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001368 def select_includes(self, index):
1369 return self.tk.getboolean(self.tk.call(
1370 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001371 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001372 def select_set(self, first, last=None):
1373 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001374 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001375 def size(self):
1376 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001377 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001378 if not what:
1379 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001380 apply(self.tk.call, (self._w, 'xview')+what)
1381 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001382 if not what:
1383 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001384 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001385
1386class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001387 def __init__(self, master=None, cnf={}, **kw):
1388 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001389 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001390 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001391 def tk_mbPost(self):
1392 self.tk.call('tk_mbPost', self._w)
1393 def tk_mbUnpost(self):
1394 self.tk.call('tk_mbUnpost')
1395 def tk_traverseToMenu(self, char):
1396 self.tk.call('tk_traverseToMenu', self._w, char)
1397 def tk_traverseWithinMenu(self, char):
1398 self.tk.call('tk_traverseWithinMenu', self._w, char)
1399 def tk_getMenuButtons(self):
1400 return self.tk.call('tk_getMenuButtons', self._w)
1401 def tk_nextMenu(self, count):
1402 self.tk.call('tk_nextMenu', count)
1403 def tk_nextMenuEntry(self, count):
1404 self.tk.call('tk_nextMenuEntry', count)
1405 def tk_invokeMenu(self):
1406 self.tk.call('tk_invokeMenu', self._w)
1407 def tk_firstMenu(self):
1408 self.tk.call('tk_firstMenu', self._w)
1409 def tk_mbButtonDown(self):
1410 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001411 def tk_popup(self, x, y, entry=""):
1412 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001413 def activate(self, index):
1414 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001415 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001416 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001417 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001418 def add_cascade(self, cnf={}, **kw):
1419 self.add('cascade', cnf or kw)
1420 def add_checkbutton(self, cnf={}, **kw):
1421 self.add('checkbutton', cnf or kw)
1422 def add_command(self, cnf={}, **kw):
1423 self.add('command', cnf or kw)
1424 def add_radiobutton(self, cnf={}, **kw):
1425 self.add('radiobutton', cnf or kw)
1426 def add_separator(self, cnf={}, **kw):
1427 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001428 def insert(self, index, itemType, cnf={}, **kw):
1429 apply(self.tk.call, (self._w, 'insert', index, itemType)
1430 + self._options(cnf, kw))
1431 def insert_cascade(self, index, cnf={}, **kw):
1432 self.insert(index, 'cascade', cnf or kw)
1433 def insert_checkbutton(self, index, cnf={}, **kw):
1434 self.insert(index, 'checkbutton', cnf or kw)
1435 def insert_command(self, index, cnf={}, **kw):
1436 self.insert(index, 'command', cnf or kw)
1437 def insert_radiobutton(self, index, cnf={}, **kw):
1438 self.insert(index, 'radiobutton', cnf or kw)
1439 def insert_separator(self, index, cnf={}, **kw):
1440 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001441 def delete(self, index1, index2=None):
1442 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001443 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001444 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001445 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001446 if cnf is None and not kw:
1447 cnf = {}
1448 for x in self.tk.split(apply(self.tk.call,
1449 (self._w, 'entryconfigure', index))):
1450 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1451 return cnf
1452 if type(cnf) == StringType and not kw:
1453 x = self.tk.split(apply(self.tk.call,
1454 (self._w, 'entryconfigure', index, '-'+cnf)))
1455 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001456 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001457 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001458 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001459 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001460 i = self.tk.call(self._w, 'index', index)
1461 if i == 'none': return None
1462 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001463 def invoke(self, index):
1464 return self.tk.call(self._w, 'invoke', index)
1465 def post(self, x, y):
1466 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001467 def type(self, index):
1468 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001469 def unpost(self):
1470 self.tk.call(self._w, 'unpost')
1471 def yposition(self, index):
1472 return self.tk.getint(self.tk.call(
1473 self._w, 'yposition', index))
1474
1475class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001476 def __init__(self, master=None, cnf={}, **kw):
1477 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001478
1479class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001480 def __init__(self, master=None, cnf={}, **kw):
1481 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001482
1483class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001484 def __init__(self, master=None, cnf={}, **kw):
1485 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001486 def deselect(self):
1487 self.tk.call(self._w, 'deselect')
1488 def flash(self):
1489 self.tk.call(self._w, 'flash')
1490 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001491 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001492 def select(self):
1493 self.tk.call(self._w, 'select')
1494
1495class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001496 def __init__(self, master=None, cnf={}, **kw):
1497 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001498 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001499 value = self.tk.call(self._w, 'get')
1500 try:
1501 return self.tk.getint(value)
1502 except TclError:
1503 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001504 def set(self, value):
1505 self.tk.call(self._w, 'set', value)
1506
1507class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001508 def __init__(self, master=None, cnf={}, **kw):
1509 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001510 def activate(self, index):
1511 self.tk.call(self._w, 'activate', index)
1512 def delta(self, deltax, deltay):
1513 return self.getdouble(self.tk.call(
1514 self._w, 'delta', deltax, deltay))
1515 def fraction(self, x, y):
1516 return self.getdouble(self.tk.call(
1517 self._w, 'fraction', x, y))
1518 def identify(self, x, y):
1519 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001520 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001521 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001522 def set(self, *args):
1523 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001524
1525class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001526 def __init__(self, master=None, cnf={}, **kw):
1527 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001528 def bbox(self, *args):
1529 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001530 def tk_textSelectTo(self, index):
1531 self.tk.call('tk_textSelectTo', self._w, index)
1532 def tk_textBackspace(self):
1533 self.tk.call('tk_textBackspace', self._w)
1534 def tk_textIndexCloser(self, a, b, c):
1535 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1536 def tk_textResetAnchor(self, index):
1537 self.tk.call('tk_textResetAnchor', self._w, index)
1538 def compare(self, index1, op, index2):
1539 return self.tk.getboolean(self.tk.call(
1540 self._w, 'compare', index1, op, index2))
1541 def debug(self, boolean=None):
1542 return self.tk.getboolean(self.tk.call(
1543 self._w, 'debug', boolean))
1544 def delete(self, index1, index2=None):
1545 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001546 def dlineinfo(self, index):
1547 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001548 def get(self, index1, index2=None):
1549 return self.tk.call(self._w, 'get', index1, index2)
1550 def index(self, index):
1551 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001552 def insert(self, index, chars, *args):
1553 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001554 def mark_gravity(self, markName, direction=None):
1555 return apply(self.tk.call,
1556 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001557 def mark_names(self):
1558 return self.tk.splitlist(self.tk.call(
1559 self._w, 'mark', 'names'))
1560 def mark_set(self, markName, index):
1561 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001562 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001563 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001564 def scan_mark(self, x, y):
1565 self.tk.call(self._w, 'scan', 'mark', x, y)
1566 def scan_dragto(self, x, y):
1567 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001568 def search(self, pattern, index, stopindex=None,
1569 forwards=None, backwards=None, exact=None,
1570 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001571 args = [self._w, 'search']
1572 if forwards: args.append('-forwards')
1573 if backwards: args.append('-backwards')
1574 if exact: args.append('-exact')
1575 if regexp: args.append('-regexp')
1576 if nocase: args.append('-nocase')
1577 if count: args.append('-count'); args.append(count)
1578 if pattern[0] == '-': args.append('--')
1579 args.append(pattern)
1580 args.append(index)
1581 if stopindex: args.append(stopindex)
1582 return apply(self.tk.call, tuple(args))
1583 def see(self, index):
1584 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001585 def tag_add(self, tagName, index1, index2=None):
1586 self.tk.call(
1587 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001588 def tag_unbind(self, tagName, sequence):
1589 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001590 def tag_bind(self, tagName, sequence, func, add=None):
1591 return self._bind((self._w, 'tag', 'bind', tagName),
1592 sequence, func, add)
1593 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001594 if option[:1] != '-':
1595 option = '-' + option
1596 if option[-1:] == '_':
1597 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001598 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001599 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001600 if type(cnf) == StringType:
1601 x = self.tk.split(self.tk.call(
1602 self._w, 'tag', 'configure', tagName, '-'+cnf))
1603 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001604 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001605 (self._w, 'tag', 'configure', tagName)
1606 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001607 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001608 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001609 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001610 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001611 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001612 def tag_names(self, index=None):
1613 return self.tk.splitlist(
1614 self.tk.call(self._w, 'tag', 'names', index))
1615 def tag_nextrange(self, tagName, index1, index2=None):
1616 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001617 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001618 def tag_prevrange(self, tagName, index1, index2=None):
1619 return self.tk.splitlist(self.tk.call(
1620 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001621 def tag_raise(self, tagName, aboveThis=None):
1622 self.tk.call(
1623 self._w, 'tag', 'raise', tagName, aboveThis)
1624 def tag_ranges(self, tagName):
1625 return self.tk.splitlist(self.tk.call(
1626 self._w, 'tag', 'ranges', tagName))
1627 def tag_remove(self, tagName, index1, index2=None):
1628 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001629 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001630 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001631 if option[:1] != '-':
1632 option = '-' + option
1633 if option[-1:] == '_':
1634 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001635 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001636 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001637 if type(cnf) == StringType:
1638 x = self.tk.split(self.tk.call(
1639 self._w, 'window', 'configure',
1640 index, '-'+cnf))
1641 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001642 apply(self.tk.call,
1643 (self._w, 'window', 'configure', index)
1644 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001645 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001646 def window_create(self, index, cnf={}, **kw):
1647 apply(self.tk.call,
1648 (self._w, 'window', 'create', index)
1649 + self._options(cnf, kw))
1650 def window_names(self):
1651 return self.tk.splitlist(
1652 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001653 def xview(self, *what):
1654 if not what:
1655 return self._getdoubles(self.tk.call(self._w, 'xview'))
1656 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001657 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001658 if not what:
1659 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001660 apply(self.tk.call, (self._w, 'yview')+what)
1661 def yview_pickplace(self, *what):
1662 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001663
Guido van Rossum28574b51996-10-21 15:16:51 +00001664class _setit:
1665 def __init__(self, var, value):
1666 self.__value = value
1667 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001668 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001669 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001670
1671class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001672 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001673 kw = {"borderwidth": 2, "textvariable": variable,
1674 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1675 "highlightthickness": 2}
1676 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001677 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001678 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1679 self.menuname = menu._w
1680 menu.add_command(label=value, command=_setit(variable, value))
1681 for v in values:
1682 menu.add_command(label=v, command=_setit(variable, v))
1683 self["menu"] = menu
1684
1685 def __getitem__(self, name):
1686 if name == 'menu':
1687 return self.__menu
1688 return Widget.__getitem__(self, name)
1689
1690 def destroy(self):
1691 Menubutton.destroy(self)
1692 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001693
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001694class Image:
1695 def __init__(self, imgtype, name=None, cnf={}, **kw):
1696 self.name = None
1697 master = _default_root
1698 if not master: raise RuntimeError, 'Too early to create image'
1699 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001700 if not name:
1701 name = `id(self)`
1702 # The following is needed for systems where id(x)
1703 # can return a negative number, such as Linux/m68k:
1704 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001705 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1706 elif kw: cnf = kw
1707 options = ()
1708 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001709 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001710 v = self._register(v)
1711 options = options + ('-'+k, v)
1712 apply(self.tk.call,
1713 ('image', 'create', imgtype, name,) + options)
1714 self.name = name
1715 def __str__(self): return self.name
1716 def __del__(self):
1717 if self.name:
1718 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001719 def __setitem__(self, key, value):
1720 self.tk.call(self.name, 'configure', '-'+key, value)
1721 def __getitem__(self, key):
1722 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001723 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001724 res = ()
1725 for k, v in _cnfmerge(kw).items():
1726 if v is not None:
1727 if k[-1] == '_': k = k[:-1]
1728 if callable(v):
1729 v = self._register(v)
1730 res = res + ('-'+k, v)
1731 apply(self.tk.call, (self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001732 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001733 def height(self):
1734 return self.tk.getint(
1735 self.tk.call('image', 'height', self.name))
1736 def type(self):
1737 return self.tk.call('image', 'type', self.name)
1738 def width(self):
1739 return self.tk.getint(
1740 self.tk.call('image', 'width', self.name))
1741
1742class PhotoImage(Image):
1743 def __init__(self, name=None, cnf={}, **kw):
1744 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001745 def blank(self):
1746 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001747 def cget(self, option):
1748 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001749 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001750 def __getitem__(self, key):
1751 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001752 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001753 def copy(self):
1754 destImage = PhotoImage()
1755 self.tk.call(destImage, 'copy', self.name)
1756 return destImage
1757 def zoom(self,x,y=''):
1758 destImage = PhotoImage()
1759 if y=='': y=x
1760 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1761 return destImage
1762 def subsample(self,x,y=''):
1763 destImage = PhotoImage()
1764 if y=='': y=x
1765 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1766 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001767 def get(self, x, y):
1768 return self.tk.call(self.name, 'get', x, y)
1769 def put(self, data, to=None):
1770 args = (self.name, 'put', data)
1771 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001772 if to[0] == '-to':
1773 to = to[1:]
1774 args = args + ('-to',) + tuple(to)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001775 apply(self.tk.call, args)
1776 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001777 def write(self, filename, format=None, from_coords=None):
1778 args = (self.name, 'write', filename)
1779 if format:
1780 args = args + ('-format', format)
1781 if from_coords:
1782 args = args + ('-from',) + tuple(from_coords)
1783 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001784
1785class BitmapImage(Image):
1786 def __init__(self, name=None, cnf={}, **kw):
1787 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1788
1789def image_names(): return _default_root.tk.call('image', 'names')
1790def image_types(): return _default_root.tk.call('image', 'types')
1791
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001792######################################################################
1793# Extensions:
1794
1795class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001796 def __init__(self, master=None, cnf={}, **kw):
1797 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001798 self.bind('<Any-Enter>', self.tkButtonEnter)
1799 self.bind('<Any-Leave>', self.tkButtonLeave)
1800 self.bind('<1>', self.tkButtonDown)
1801 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001802
1803class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001804 def __init__(self, master=None, cnf={}, **kw):
1805 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001806 self.bind('<Any-Enter>', self.tkButtonEnter)
1807 self.bind('<Any-Leave>', self.tkButtonLeave)
1808 self.bind('<1>', self.tkButtonDown)
1809 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1810 self['fg'] = self['bg']
1811 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001812
Guido van Rossumc417ef81996-08-21 23:38:59 +00001813######################################################################
1814# Test:
1815
1816def _test():
1817 root = Tk()
1818 label = Label(root, text="Proof-of-existence test for Tk")
1819 label.pack()
1820 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001821 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001822 text="[%s]" % root.test['text']))
1823 test.pack()
1824 root.test = test
1825 quit = Button(root, text="QUIT", command=root.destroy)
1826 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001827 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001828 root.mainloop()
1829
1830if __name__ == '__main__':
1831 _test()
1832
Guido van Rossum37dcab11996-05-16 16:00:19 +00001833
1834# Emacs cruft
1835# Local Variables:
1836# py-indent-offset: 8
1837# End: