blob: 8b3b1e6930c625e2ad912030c643649250f47bb0 [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)
Guido van Rossum0001a111998-02-19 21:20:30 +000087 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +000088 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,
Guido van Rossum21df8f51998-02-24 23:26:18 +0000766 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +0000767 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 Rossum0001a111998-02-19 21:20:30 +00001137 # XXX Should use _flatten on args
Guido van Rossumc8b47911996-07-30 16:31:32 +00001138 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001139 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001140 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001141 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001142 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001143 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001144 args = args[:-1]
1145 else:
1146 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001147 return self.tk.getint(apply(
1148 self.tk.call,
1149 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001150 + args + self._options(cnf, kw)))
1151 def create_arc(self, *args, **kw):
1152 return self._create('arc', args, kw)
1153 def create_bitmap(self, *args, **kw):
1154 return self._create('bitmap', args, kw)
1155 def create_image(self, *args, **kw):
1156 return self._create('image', args, kw)
1157 def create_line(self, *args, **kw):
1158 return self._create('line', args, kw)
1159 def create_oval(self, *args, **kw):
1160 return self._create('oval', args, kw)
1161 def create_polygon(self, *args, **kw):
1162 return self._create('polygon', args, kw)
1163 def create_rectangle(self, *args, **kw):
1164 return self._create('rectangle', args, kw)
1165 def create_text(self, *args, **kw):
1166 return self._create('text', args, kw)
1167 def create_window(self, *args, **kw):
1168 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001169 def dchars(self, *args):
1170 self._do('dchars', args)
1171 def delete(self, *args):
1172 self._do('delete', args)
1173 def dtag(self, *args):
1174 self._do('dtag', args)
1175 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001176 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001177 def find_above(self, tagOrId):
1178 return self.find('above', tagOrId)
1179 def find_all(self):
1180 return self.find('all')
1181 def find_below(self, tagOrId):
1182 return self.find('below', tagOrId)
1183 def find_closest(self, x, y, halo=None, start=None):
1184 return self.find('closest', x, y, halo, start)
1185 def find_enclosed(self, x1, y1, x2, y2):
1186 return self.find('enclosed', x1, y1, x2, y2)
1187 def find_overlapping(self, x1, y1, x2, y2):
1188 return self.find('overlapping', x1, y1, x2, y2)
1189 def find_withtag(self, tagOrId):
1190 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001191 def focus(self, *args):
1192 return self._do('focus', args)
1193 def gettags(self, *args):
1194 return self.tk.splitlist(self._do('gettags', args))
1195 def icursor(self, *args):
1196 self._do('icursor', args)
1197 def index(self, *args):
1198 return self.tk.getint(self._do('index', args))
1199 def insert(self, *args):
1200 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001201 def itemcget(self, tagOrId, option):
1202 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001203 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001204 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001205 cnf = {}
1206 for x in self.tk.split(
Guido van Rossum9918e0c1997-08-18 14:44:04 +00001207 self._do('itemconfigure', (tagOrId,))):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001208 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1209 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001210 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001211 x = self.tk.split(self._do('itemconfigure',
1212 (tagOrId, '-'+cnf,)))
1213 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001214 self._do('itemconfigure', (tagOrId,)
1215 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001216 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001217 def lower(self, *args):
1218 self._do('lower', args)
1219 def move(self, *args):
1220 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001221 def postscript(self, cnf={}, **kw):
1222 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001223 def tkraise(self, *args):
1224 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001225 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001226 def scale(self, *args):
1227 self._do('scale', args)
1228 def scan_mark(self, x, y):
1229 self.tk.call(self._w, 'scan', 'mark', x, y)
1230 def scan_dragto(self, x, y):
1231 self.tk.call(self._w, 'scan', 'dragto', x, y)
1232 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001233 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001234 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001235 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001236 def select_from(self, tagOrId, index):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001237 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001238 def select_item(self):
1239 self.tk.call(self._w, 'select', 'item')
1240 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001241 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001242 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001243 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001244 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001245 if not args:
1246 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001247 apply(self.tk.call, (self._w, 'xview')+args)
1248 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001249 if not args:
1250 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001251 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001252
1253class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001254 def __init__(self, master=None, cnf={}, **kw):
1255 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001256 def deselect(self):
1257 self.tk.call(self._w, 'deselect')
1258 def flash(self):
1259 self.tk.call(self._w, 'flash')
1260 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001261 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001262 def select(self):
1263 self.tk.call(self._w, 'select')
1264 def toggle(self):
1265 self.tk.call(self._w, 'toggle')
1266
1267class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001268 def __init__(self, master=None, cnf={}, **kw):
1269 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001270 def delete(self, first, last=None):
1271 self.tk.call(self._w, 'delete', first, last)
1272 def get(self):
1273 return self.tk.call(self._w, 'get')
1274 def icursor(self, index):
1275 self.tk.call(self._w, 'icursor', index)
1276 def index(self, index):
1277 return self.tk.getint(self.tk.call(
1278 self._w, 'index', index))
1279 def insert(self, index, string):
1280 self.tk.call(self._w, 'insert', index, string)
1281 def scan_mark(self, x):
1282 self.tk.call(self._w, 'scan', 'mark', x)
1283 def scan_dragto(self, x):
1284 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001285 def selection_adjust(self, index):
1286 self.tk.call(self._w, 'selection', 'adjust', index)
1287 select_adjust = selection_adjust
1288 def selection_clear(self):
1289 self.tk.call(self._w, 'selection', 'clear')
1290 select_clear = selection_clear
1291 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001292 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001293 select_from = selection_from
1294 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001295 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001296 self.tk.call(self._w, 'selection', 'present'))
1297 select_present = selection_present
1298 def selection_range(self, start, end):
1299 self.tk.call(self._w, 'selection', 'range', start, end)
1300 select_range = selection_range
1301 def selection_to(self, index):
1302 self.tk.call(self._w, 'selection', 'to', index)
1303 select_to = selection_to
1304 def xview(self, index):
1305 self.tk.call(self._w, 'xview', index)
1306 def xview_moveto(self, fraction):
1307 self.tk.call(self._w, 'xview', 'moveto', fraction)
1308 def xview_scroll(self, number, what):
1309 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001310
1311class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001312 def __init__(self, master=None, cnf={}, **kw):
1313 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001314 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001315 if cnf.has_key('class_'):
1316 extra = ('-class', cnf['class_'])
1317 del cnf['class_']
1318 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001319 extra = ('-class', cnf['class'])
1320 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001321 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001322
1323class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001324 def __init__(self, master=None, cnf={}, **kw):
1325 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001326
Guido van Rossum18468821994-06-20 07:49:28 +00001327class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001328 def __init__(self, master=None, cnf={}, **kw):
1329 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001330 def activate(self, index):
1331 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001332 def bbox(self, *args):
1333 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001334 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001335 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001336 return self.tk.splitlist(self.tk.call(
1337 self._w, 'curselection'))
1338 def delete(self, first, last=None):
1339 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001340 def get(self, first, last=None):
1341 if last:
1342 return self.tk.splitlist(self.tk.call(
1343 self._w, 'get', first, last))
1344 else:
1345 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001346 def insert(self, index, *elements):
1347 apply(self.tk.call,
1348 (self._w, 'insert', index) + elements)
1349 def nearest(self, y):
1350 return self.tk.getint(self.tk.call(
1351 self._w, 'nearest', y))
1352 def scan_mark(self, x, y):
1353 self.tk.call(self._w, 'scan', 'mark', x, y)
1354 def scan_dragto(self, x, y):
1355 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001356 def see(self, index):
1357 self.tk.call(self._w, 'see', index)
1358 def index(self, index):
1359 i = self.tk.call(self._w, 'index', index)
1360 if i == 'none': return None
1361 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001362 def select_anchor(self, index):
1363 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001364 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001365 def select_clear(self, first, last=None):
1366 self.tk.call(self._w,
1367 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001368 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001369 def select_includes(self, index):
1370 return self.tk.getboolean(self.tk.call(
1371 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001372 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001373 def select_set(self, first, last=None):
1374 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001375 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001376 def size(self):
1377 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001378 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001379 if not what:
1380 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001381 apply(self.tk.call, (self._w, 'xview')+what)
1382 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001383 if not what:
1384 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001385 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001386
1387class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001388 def __init__(self, master=None, cnf={}, **kw):
1389 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001390 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001391 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001392 def tk_mbPost(self):
1393 self.tk.call('tk_mbPost', self._w)
1394 def tk_mbUnpost(self):
1395 self.tk.call('tk_mbUnpost')
1396 def tk_traverseToMenu(self, char):
1397 self.tk.call('tk_traverseToMenu', self._w, char)
1398 def tk_traverseWithinMenu(self, char):
1399 self.tk.call('tk_traverseWithinMenu', self._w, char)
1400 def tk_getMenuButtons(self):
1401 return self.tk.call('tk_getMenuButtons', self._w)
1402 def tk_nextMenu(self, count):
1403 self.tk.call('tk_nextMenu', count)
1404 def tk_nextMenuEntry(self, count):
1405 self.tk.call('tk_nextMenuEntry', count)
1406 def tk_invokeMenu(self):
1407 self.tk.call('tk_invokeMenu', self._w)
1408 def tk_firstMenu(self):
1409 self.tk.call('tk_firstMenu', self._w)
1410 def tk_mbButtonDown(self):
1411 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001412 def tk_popup(self, x, y, entry=""):
1413 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001414 def activate(self, index):
1415 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001416 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001417 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001418 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001419 def add_cascade(self, cnf={}, **kw):
1420 self.add('cascade', cnf or kw)
1421 def add_checkbutton(self, cnf={}, **kw):
1422 self.add('checkbutton', cnf or kw)
1423 def add_command(self, cnf={}, **kw):
1424 self.add('command', cnf or kw)
1425 def add_radiobutton(self, cnf={}, **kw):
1426 self.add('radiobutton', cnf or kw)
1427 def add_separator(self, cnf={}, **kw):
1428 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001429 def insert(self, index, itemType, cnf={}, **kw):
1430 apply(self.tk.call, (self._w, 'insert', index, itemType)
1431 + self._options(cnf, kw))
1432 def insert_cascade(self, index, cnf={}, **kw):
1433 self.insert(index, 'cascade', cnf or kw)
1434 def insert_checkbutton(self, index, cnf={}, **kw):
1435 self.insert(index, 'checkbutton', cnf or kw)
1436 def insert_command(self, index, cnf={}, **kw):
1437 self.insert(index, 'command', cnf or kw)
1438 def insert_radiobutton(self, index, cnf={}, **kw):
1439 self.insert(index, 'radiobutton', cnf or kw)
1440 def insert_separator(self, index, cnf={}, **kw):
1441 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001442 def delete(self, index1, index2=None):
1443 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001444 def entrycget(self, index, option):
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001445 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001446 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001447 if cnf is None and not kw:
1448 cnf = {}
1449 for x in self.tk.split(apply(self.tk.call,
1450 (self._w, 'entryconfigure', index))):
1451 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1452 return cnf
1453 if type(cnf) == StringType and not kw:
1454 x = self.tk.split(apply(self.tk.call,
1455 (self._w, 'entryconfigure', index, '-'+cnf)))
1456 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001457 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001458 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001459 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00001460 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001461 i = self.tk.call(self._w, 'index', index)
1462 if i == 'none': return None
1463 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001464 def invoke(self, index):
1465 return self.tk.call(self._w, 'invoke', index)
1466 def post(self, x, y):
1467 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001468 def type(self, index):
1469 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001470 def unpost(self):
1471 self.tk.call(self._w, 'unpost')
1472 def yposition(self, index):
1473 return self.tk.getint(self.tk.call(
1474 self._w, 'yposition', index))
1475
1476class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001477 def __init__(self, master=None, cnf={}, **kw):
1478 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001479
1480class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001481 def __init__(self, master=None, cnf={}, **kw):
1482 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001483
1484class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001485 def __init__(self, master=None, cnf={}, **kw):
1486 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001487 def deselect(self):
1488 self.tk.call(self._w, 'deselect')
1489 def flash(self):
1490 self.tk.call(self._w, 'flash')
1491 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001492 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001493 def select(self):
1494 self.tk.call(self._w, 'select')
1495
1496class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001497 def __init__(self, master=None, cnf={}, **kw):
1498 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001499 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001500 value = self.tk.call(self._w, 'get')
1501 try:
1502 return self.tk.getint(value)
1503 except TclError:
1504 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001505 def set(self, value):
1506 self.tk.call(self._w, 'set', value)
1507
1508class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001509 def __init__(self, master=None, cnf={}, **kw):
1510 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001511 def activate(self, index):
1512 self.tk.call(self._w, 'activate', index)
1513 def delta(self, deltax, deltay):
1514 return self.getdouble(self.tk.call(
1515 self._w, 'delta', deltax, deltay))
1516 def fraction(self, x, y):
1517 return self.getdouble(self.tk.call(
1518 self._w, 'fraction', x, y))
1519 def identify(self, x, y):
1520 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001521 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001522 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001523 def set(self, *args):
1524 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001525
1526class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001527 def __init__(self, master=None, cnf={}, **kw):
1528 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001529 def bbox(self, *args):
1530 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001531 def tk_textSelectTo(self, index):
1532 self.tk.call('tk_textSelectTo', self._w, index)
1533 def tk_textBackspace(self):
1534 self.tk.call('tk_textBackspace', self._w)
1535 def tk_textIndexCloser(self, a, b, c):
1536 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1537 def tk_textResetAnchor(self, index):
1538 self.tk.call('tk_textResetAnchor', self._w, index)
1539 def compare(self, index1, op, index2):
1540 return self.tk.getboolean(self.tk.call(
1541 self._w, 'compare', index1, op, index2))
1542 def debug(self, boolean=None):
1543 return self.tk.getboolean(self.tk.call(
1544 self._w, 'debug', boolean))
1545 def delete(self, index1, index2=None):
1546 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001547 def dlineinfo(self, index):
1548 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001549 def get(self, index1, index2=None):
1550 return self.tk.call(self._w, 'get', index1, index2)
1551 def index(self, index):
1552 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001553 def insert(self, index, chars, *args):
1554 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001555 def mark_gravity(self, markName, direction=None):
1556 return apply(self.tk.call,
1557 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001558 def mark_names(self):
1559 return self.tk.splitlist(self.tk.call(
1560 self._w, 'mark', 'names'))
1561 def mark_set(self, markName, index):
1562 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001563 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001564 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001565 def scan_mark(self, x, y):
1566 self.tk.call(self._w, 'scan', 'mark', x, y)
1567 def scan_dragto(self, x, y):
1568 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001569 def search(self, pattern, index, stopindex=None,
1570 forwards=None, backwards=None, exact=None,
1571 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001572 args = [self._w, 'search']
1573 if forwards: args.append('-forwards')
1574 if backwards: args.append('-backwards')
1575 if exact: args.append('-exact')
1576 if regexp: args.append('-regexp')
1577 if nocase: args.append('-nocase')
1578 if count: args.append('-count'); args.append(count)
1579 if pattern[0] == '-': args.append('--')
1580 args.append(pattern)
1581 args.append(index)
1582 if stopindex: args.append(stopindex)
1583 return apply(self.tk.call, tuple(args))
1584 def see(self, index):
1585 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001586 def tag_add(self, tagName, index1, index2=None):
1587 self.tk.call(
1588 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001589 def tag_unbind(self, tagName, sequence):
1590 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001591 def tag_bind(self, tagName, sequence, func, add=None):
1592 return self._bind((self._w, 'tag', 'bind', tagName),
1593 sequence, func, add)
1594 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001595 if option[:1] != '-':
1596 option = '-' + option
1597 if option[-1:] == '_':
1598 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001599 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001600 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001601 if type(cnf) == StringType:
1602 x = self.tk.split(self.tk.call(
1603 self._w, 'tag', 'configure', tagName, '-'+cnf))
1604 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001605 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001606 (self._w, 'tag', 'configure', tagName)
1607 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001608 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001609 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001610 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001611 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001612 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001613 def tag_names(self, index=None):
1614 return self.tk.splitlist(
1615 self.tk.call(self._w, 'tag', 'names', index))
1616 def tag_nextrange(self, tagName, index1, index2=None):
1617 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001618 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00001619 def tag_prevrange(self, tagName, index1, index2=None):
1620 return self.tk.splitlist(self.tk.call(
1621 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001622 def tag_raise(self, tagName, aboveThis=None):
1623 self.tk.call(
1624 self._w, 'tag', 'raise', tagName, aboveThis)
1625 def tag_ranges(self, tagName):
1626 return self.tk.splitlist(self.tk.call(
1627 self._w, 'tag', 'ranges', tagName))
1628 def tag_remove(self, tagName, index1, index2=None):
1629 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001630 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001631 def window_cget(self, index, option):
Guido van Rossum7814ea61997-12-11 17:08:52 +00001632 if option[:1] != '-':
1633 option = '-' + option
1634 if option[-1:] == '_':
1635 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001636 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001637 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001638 if type(cnf) == StringType:
1639 x = self.tk.split(self.tk.call(
1640 self._w, 'window', 'configure',
1641 index, '-'+cnf))
1642 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001643 apply(self.tk.call,
1644 (self._w, 'window', 'configure', index)
1645 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001646 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001647 def window_create(self, index, cnf={}, **kw):
1648 apply(self.tk.call,
1649 (self._w, 'window', 'create', index)
1650 + self._options(cnf, kw))
1651 def window_names(self):
1652 return self.tk.splitlist(
1653 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001654 def xview(self, *what):
1655 if not what:
1656 return self._getdoubles(self.tk.call(self._w, 'xview'))
1657 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001658 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001659 if not what:
1660 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001661 apply(self.tk.call, (self._w, 'yview')+what)
1662 def yview_pickplace(self, *what):
1663 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001664
Guido van Rossum28574b51996-10-21 15:16:51 +00001665class _setit:
1666 def __init__(self, var, value):
1667 self.__value = value
1668 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001669 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001670 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001671
1672class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001673 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001674 kw = {"borderwidth": 2, "textvariable": variable,
1675 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1676 "highlightthickness": 2}
1677 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001678 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001679 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1680 self.menuname = menu._w
1681 menu.add_command(label=value, command=_setit(variable, value))
1682 for v in values:
1683 menu.add_command(label=v, command=_setit(variable, v))
1684 self["menu"] = menu
1685
1686 def __getitem__(self, name):
1687 if name == 'menu':
1688 return self.__menu
1689 return Widget.__getitem__(self, name)
1690
1691 def destroy(self):
1692 Menubutton.destroy(self)
1693 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001694
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001695class Image:
1696 def __init__(self, imgtype, name=None, cnf={}, **kw):
1697 self.name = None
1698 master = _default_root
1699 if not master: raise RuntimeError, 'Too early to create image'
1700 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001701 if not name:
1702 name = `id(self)`
1703 # The following is needed for systems where id(x)
1704 # can return a negative number, such as Linux/m68k:
1705 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001706 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1707 elif kw: cnf = kw
1708 options = ()
1709 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001710 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001711 v = self._register(v)
1712 options = options + ('-'+k, v)
1713 apply(self.tk.call,
1714 ('image', 'create', imgtype, name,) + options)
1715 self.name = name
1716 def __str__(self): return self.name
1717 def __del__(self):
1718 if self.name:
1719 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001720 def __setitem__(self, key, value):
1721 self.tk.call(self.name, 'configure', '-'+key, value)
1722 def __getitem__(self, key):
1723 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001724 def configure(self, **kw):
Guido van Rossum83710131996-12-27 15:33:17 +00001725 res = ()
1726 for k, v in _cnfmerge(kw).items():
1727 if v is not None:
1728 if k[-1] == '_': k = k[:-1]
1729 if callable(v):
1730 v = self._register(v)
1731 res = res + ('-'+k, v)
1732 apply(self.tk.call, (self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001733 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001734 def height(self):
1735 return self.tk.getint(
1736 self.tk.call('image', 'height', self.name))
1737 def type(self):
1738 return self.tk.call('image', 'type', self.name)
1739 def width(self):
1740 return self.tk.getint(
1741 self.tk.call('image', 'width', self.name))
1742
1743class PhotoImage(Image):
1744 def __init__(self, name=None, cnf={}, **kw):
1745 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001746 def blank(self):
1747 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001748 def cget(self, option):
1749 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001750 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001751 def __getitem__(self, key):
1752 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001753 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001754 def copy(self):
1755 destImage = PhotoImage()
1756 self.tk.call(destImage, 'copy', self.name)
1757 return destImage
1758 def zoom(self,x,y=''):
1759 destImage = PhotoImage()
1760 if y=='': y=x
1761 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1762 return destImage
1763 def subsample(self,x,y=''):
1764 destImage = PhotoImage()
1765 if y=='': y=x
1766 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1767 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001768 def get(self, x, y):
1769 return self.tk.call(self.name, 'get', x, y)
1770 def put(self, data, to=None):
1771 args = (self.name, 'put', data)
1772 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00001773 if to[0] == '-to':
1774 to = to[1:]
1775 args = args + ('-to',) + tuple(to)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001776 apply(self.tk.call, args)
1777 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001778 def write(self, filename, format=None, from_coords=None):
1779 args = (self.name, 'write', filename)
1780 if format:
1781 args = args + ('-format', format)
1782 if from_coords:
1783 args = args + ('-from',) + tuple(from_coords)
1784 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001785
1786class BitmapImage(Image):
1787 def __init__(self, name=None, cnf={}, **kw):
1788 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1789
1790def image_names(): return _default_root.tk.call('image', 'names')
1791def image_types(): return _default_root.tk.call('image', 'types')
1792
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001793######################################################################
1794# Extensions:
1795
1796class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001797 def __init__(self, master=None, cnf={}, **kw):
1798 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001799 self.bind('<Any-Enter>', self.tkButtonEnter)
1800 self.bind('<Any-Leave>', self.tkButtonLeave)
1801 self.bind('<1>', self.tkButtonDown)
1802 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001803
1804class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001805 def __init__(self, master=None, cnf={}, **kw):
1806 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001807 self.bind('<Any-Enter>', self.tkButtonEnter)
1808 self.bind('<Any-Leave>', self.tkButtonLeave)
1809 self.bind('<1>', self.tkButtonDown)
1810 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1811 self['fg'] = self['bg']
1812 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001813
Guido van Rossumc417ef81996-08-21 23:38:59 +00001814######################################################################
1815# Test:
1816
1817def _test():
1818 root = Tk()
1819 label = Label(root, text="Proof-of-existence test for Tk")
1820 label.pack()
1821 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00001822 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00001823 text="[%s]" % root.test['text']))
1824 test.pack()
1825 root.test = test
1826 quit = Button(root, text="QUIT", command=root.destroy)
1827 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001828 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001829 root.mainloop()
1830
1831if __name__ == '__main__':
1832 _test()
1833
Guido van Rossum37dcab11996-05-16 16:00:19 +00001834
1835# Emacs cruft
1836# Local Variables:
1837# py-indent-offset: 8
1838# End: