blob: 2c47480cbe565dd1a1cc3236bf32d7442e93ad6b [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 Rossum36269991996-05-16 17:11:27 +000015######################################################################
16# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
17# they are defined here (and not in Tkconstants):
18######################################################################
19if TkVersion >= 4.1:
20 READABLE = 2
21 WRITABLE = 4
22 EXCEPTION = 8
23else:
24 READABLE = 1
25 WRITABLE = 2
26 EXCEPTION = 4
27
28
Guido van Rossum2dcf5291994-07-06 09:23:20 +000029def _flatten(tuple):
30 res = ()
31 for item in tuple:
32 if type(item) in (TupleType, ListType):
33 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000034 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000035 res = res + (item,)
36 return res
37
38def _cnfmerge(cnfs):
Guido van Rossum761c5ab1995-07-14 15:29:10 +000039 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000040 return cnfs
41 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000042 return cnfs
43 else:
44 cnf = {}
45 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000046 try:
47 cnf.update(c)
48 except (AttributeError, TypeError), msg:
49 print "_cnfmerge: fallback due to:", msg
50 for k, v in c.items():
51 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000052 return cnf
53
54class Event:
55 pass
56
Guido van Rossumaec5dc91994-06-27 07:55:12 +000057_default_root = None
58
Guido van Rossum45853db1994-06-20 12:19:19 +000059def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000060 pass
61
Guido van Rossum97aeca11994-07-07 13:12:12 +000062def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000063 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000064
Guido van Rossumaec5dc91994-06-27 07:55:12 +000065_varnum = 0
66class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000067 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000068 def __init__(self, master=None):
69 global _default_root
70 global _varnum
71 if master:
72 self._tk = master.tk
73 else:
74 self._tk = _default_root.tk
75 self._name = 'PY_VAR' + `_varnum`
76 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000077 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000078 def __del__(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000079 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000080 def __str__(self):
81 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +000082 def set(self, value):
Guido van Rossum37dcab11996-05-16 16:00:19 +000083 return self._tk.globalsetvar(self._name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000084
85class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000086 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000087 def __init__(self, master=None):
88 Variable.__init__(self, master)
89 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000090 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000091
92class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000093 _default = 0
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.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000098
99class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +0000100 _default = 0.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.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000105
106class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000107 _default = "false"
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.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000112
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000113def mainloop(n=0):
114 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000115
116def getint(s):
117 return _default_root.tk.getint(s)
118
119def getdouble(s):
120 return _default_root.tk.getdouble(s)
121
122def getboolean(s):
123 return _default_root.tk.getboolean(s)
124
Guido van Rossum18468821994-06-20 07:49:28 +0000125class Misc:
Fred Drake526749b1997-05-03 04:16:23 +0000126 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000127 def destroy(self):
128 if self._tclCommands is not None:
129 for name in self._tclCommands:
130 #print '- Tkinter: deleted command', name
131 self.tk.deletecommand(name)
132 self._tclCommands = None
133 def deletecommand(self, name):
134 #print '- Tkinter: deleted command', name
135 self.tk.deletecommand(name)
136 index = self._tclCommands.index(name)
137 del self._tclCommands[index]
Guido van Rossum18468821994-06-20 07:49:28 +0000138 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000139 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000140 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000141 def tk_bisque(self):
142 self.tk.call('tk_bisque')
143 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000144 apply(self.tk.call, ('tk_setPalette',)
145 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000146 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000147 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000148 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000149 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000150 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000151 def wait_window(self, window=None):
152 if window == None:
153 window = self
154 self.tk.call('tkwait', 'window', window._w)
155 def wait_visibility(self, window=None):
156 if window == None:
157 window = self
158 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000159 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000160 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000161 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000162 return self.tk.getvar(name)
163 def getint(self, s):
164 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000165 def getdouble(self, s):
166 return self.tk.getdouble(s)
167 def getboolean(self, s):
168 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000169 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000170 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000171 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000172 def focus_force(self):
173 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000174 def focus_get(self):
175 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000176 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000177 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000178 def focus_displayof(self):
179 name = self.tk.call('focus', '-displayof', self._w)
180 if name == 'none' or not name: return None
181 return self._nametowidget(name)
182 def focus_lastfor(self):
183 name = self.tk.call('focus', '-lastfor', self._w)
184 if name == 'none' or not name: return None
185 return self._nametowidget(name)
186 def tk_focusFollowsMouse(self):
187 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000188 def tk_focusNext(self):
189 name = self.tk.call('tk_focusNext', self._w)
190 if not name: return None
191 return self._nametowidget(name)
192 def tk_focusPrev(self):
193 name = self.tk.call('tk_focusPrev', self._w)
194 if not name: return None
195 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000196 def after(self, ms, func=None, *args):
197 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000198 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000199 self.tk.call('after', ms)
200 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000201 # XXX Disgusting hack to clean up after calling func
202 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000203 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000204 try:
205 apply(func, args)
206 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000207 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000208 name = self._register(callit)
209 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000210 return self.tk.call('after', ms, name)
211 def after_idle(self, func, *args):
212 return apply(self.after, ('idle', func) + args)
213 def after_cancel(self, id):
214 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000215 def bell(self, displayof=0):
216 apply(self.tk.call, ('bell',) + self._displayof(displayof))
217 # Clipboard handling:
218 def clipboard_clear(self, **kw):
219 if not kw.has_key('displayof'): kw['displayof'] = self._w
220 apply(self.tk.call,
221 ('clipboard', 'clear') + self._options(kw))
222 def clipboard_append(self, string, **kw):
223 if not kw.has_key('displayof'): kw['displayof'] = self._w
224 apply(self.tk.call,
225 ('clipboard', 'append') + self._options(kw)
226 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000227 # XXX grab current w/o window argument
228 def grab_current(self):
229 name = self.tk.call('grab', 'current', self._w)
230 if not name: return None
231 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000232 def grab_release(self):
233 self.tk.call('grab', 'release', self._w)
234 def grab_set(self):
235 self.tk.call('grab', 'set', self._w)
236 def grab_set_global(self):
237 self.tk.call('grab', 'set', '-global', self._w)
238 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000239 status = self.tk.call('grab', 'status', self._w)
240 if status == 'none': status = None
241 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000242 def lower(self, belowThis=None):
243 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000244 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000245 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000246 def option_clear(self):
247 self.tk.call('option', 'clear')
248 def option_get(self, name, className):
249 return self.tk.call('option', 'get', self._w, name, className)
250 def option_readfile(self, fileName, priority = None):
251 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000252 def selection_clear(self, **kw):
253 if not kw.has_key('displayof'): kw['displayof'] = self._w
254 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
255 def selection_get(self, **kw):
256 if not kw.has_key('displayof'): kw['displayof'] = self._w
257 return apply(self.tk.call,
258 ('selection', 'get') + self._options(kw))
259 def selection_handle(self, command, **kw):
260 name = self._register(command)
261 apply(self.tk.call,
262 ('selection', 'handle') + self._options(kw)
263 + (self._w, name))
264 def selection_own(self, **kw):
265 "Become owner of X selection."
266 apply(self.tk.call,
267 ('selection', 'own') + self._options(kw) + (self._w,))
268 def selection_own_get(self, **kw):
269 "Find owner of X selection."
270 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000271 name = apply(self.tk.call,
272 ('selection', 'own') + self._options(kw))
273 if not name: return None
274 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000275 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000276 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000277 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000278 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000279 def tkraise(self, aboveThis=None):
280 self.tk.call('raise', self._w, aboveThis)
281 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000282 def colormodel(self, value=None):
283 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000284 def winfo_atom(self, name, displayof=0):
285 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
286 return self.tk.getint(apply(self.tk.call, args))
287 def winfo_atomname(self, id, displayof=0):
288 args = ('winfo', 'atomname') \
289 + self._displayof(displayof) + (id,)
290 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000291 def winfo_cells(self):
292 return self.tk.getint(
293 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000294 def winfo_children(self):
295 return map(self._nametowidget,
296 self.tk.splitlist(self.tk.call(
297 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000298 def winfo_class(self):
299 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000300 def winfo_colormapfull(self):
301 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000302 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000303 def winfo_containing(self, rootX, rootY, displayof=0):
304 args = ('winfo', 'containing') \
305 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000306 name = apply(self.tk.call, args)
307 if not name: return None
308 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000309 def winfo_depth(self):
310 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
311 def winfo_exists(self):
312 return self.tk.getint(
313 self.tk.call('winfo', 'exists', self._w))
314 def winfo_fpixels(self, number):
315 return self.tk.getdouble(self.tk.call(
316 'winfo', 'fpixels', self._w, number))
317 def winfo_geometry(self):
318 return self.tk.call('winfo', 'geometry', self._w)
319 def winfo_height(self):
320 return self.tk.getint(
321 self.tk.call('winfo', 'height', self._w))
322 def winfo_id(self):
323 return self.tk.getint(
324 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000325 def winfo_interps(self, displayof=0):
326 args = ('winfo', 'interps') + self._displayof(displayof)
327 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000328 def winfo_ismapped(self):
329 return self.tk.getint(
330 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000331 def winfo_manager(self):
332 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000333 def winfo_name(self):
334 return self.tk.call('winfo', 'name', self._w)
335 def winfo_parent(self):
336 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000337 def winfo_pathname(self, id, displayof=0):
338 args = ('winfo', 'pathname') \
339 + self._displayof(displayof) + (id,)
340 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000341 def winfo_pixels(self, number):
342 return self.tk.getint(
343 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000344 def winfo_pointerx(self):
345 return self.tk.getint(
346 self.tk.call('winfo', 'pointerx', self._w))
347 def winfo_pointerxy(self):
348 return self._getints(
349 self.tk.call('winfo', 'pointerxy', self._w))
350 def winfo_pointery(self):
351 return self.tk.getint(
352 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000353 def winfo_reqheight(self):
354 return self.tk.getint(
355 self.tk.call('winfo', 'reqheight', self._w))
356 def winfo_reqwidth(self):
357 return self.tk.getint(
358 self.tk.call('winfo', 'reqwidth', self._w))
359 def winfo_rgb(self, color):
360 return self._getints(
361 self.tk.call('winfo', 'rgb', self._w, color))
362 def winfo_rootx(self):
363 return self.tk.getint(
364 self.tk.call('winfo', 'rootx', self._w))
365 def winfo_rooty(self):
366 return self.tk.getint(
367 self.tk.call('winfo', 'rooty', self._w))
368 def winfo_screen(self):
369 return self.tk.call('winfo', 'screen', self._w)
370 def winfo_screencells(self):
371 return self.tk.getint(
372 self.tk.call('winfo', 'screencells', self._w))
373 def winfo_screendepth(self):
374 return self.tk.getint(
375 self.tk.call('winfo', 'screendepth', self._w))
376 def winfo_screenheight(self):
377 return self.tk.getint(
378 self.tk.call('winfo', 'screenheight', self._w))
379 def winfo_screenmmheight(self):
380 return self.tk.getint(
381 self.tk.call('winfo', 'screenmmheight', self._w))
382 def winfo_screenmmwidth(self):
383 return self.tk.getint(
384 self.tk.call('winfo', 'screenmmwidth', self._w))
385 def winfo_screenvisual(self):
386 return self.tk.call('winfo', 'screenvisual', self._w)
387 def winfo_screenwidth(self):
388 return self.tk.getint(
389 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000390 def winfo_server(self):
391 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000392 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000393 return self._nametowidget(self.tk.call(
394 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000395 def winfo_viewable(self):
396 return self.tk.getint(
397 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000398 def winfo_visual(self):
399 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000400 def winfo_visualid(self):
401 return self.tk.call('winfo', 'visualid', self._w)
402 def winfo_visualsavailable(self, includeids=0):
403 data = self.tk.split(
404 self.tk.call('winfo', 'visualsavailable', self._w,
405 includeids and 'includeids' or None))
406 def parseitem(x, self=self):
407 return x[:1] + tuple(map(self.tk.getint, x[1:]))
408 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000409 def winfo_vrootheight(self):
410 return self.tk.getint(
411 self.tk.call('winfo', 'vrootheight', self._w))
412 def winfo_vrootwidth(self):
413 return self.tk.getint(
414 self.tk.call('winfo', 'vrootwidth', self._w))
415 def winfo_vrootx(self):
416 return self.tk.getint(
417 self.tk.call('winfo', 'vrootx', self._w))
418 def winfo_vrooty(self):
419 return self.tk.getint(
420 self.tk.call('winfo', 'vrooty', self._w))
421 def winfo_width(self):
422 return self.tk.getint(
423 self.tk.call('winfo', 'width', self._w))
424 def winfo_x(self):
425 return self.tk.getint(
426 self.tk.call('winfo', 'x', self._w))
427 def winfo_y(self):
428 return self.tk.getint(
429 self.tk.call('winfo', 'y', self._w))
430 def update(self):
431 self.tk.call('update')
432 def update_idletasks(self):
433 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000434 def bindtags(self, tagList=None):
435 if tagList is None:
436 return self.tk.splitlist(
437 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000438 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000439 self.tk.call('bindtags', self._w, tagList)
440 def _bind(self, what, sequence, func, add):
441 if func:
442 cmd = ("%sset _tkinter_break [%s %s]\n"
443 'if {"$_tkinter_break" == "break"} break\n') \
444 % (add and '+' or '',
445 self._register(func, self._substitute),
446 _string.join(self._subst_format))
447 apply(self.tk.call, what + (sequence, cmd))
448 elif func == '':
449 apply(self.tk.call, what + (sequence, func))
450 else:
451 return apply(self.tk.call, what + (sequence,))
452 def bind(self, sequence=None, func=None, add=None):
453 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000454 def unbind(self, sequence):
455 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000456 def bind_all(self, sequence=None, func=None, add=None):
457 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000458 def unbind_all(self, sequence):
459 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000460 def bind_class(self, className, sequence=None, func=None, add=None):
461 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000462 def unbind_class(self, className, sequence):
463 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000464 def mainloop(self, n=0):
465 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000466 def quit(self):
467 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000468 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000469 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000470 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
471 def _getdoubles(self, string):
472 if not string: return None
473 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000474 def _getboolean(self, string):
475 if string:
476 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000477 def _displayof(self, displayof):
478 if displayof:
479 return ('-displayof', displayof)
480 if displayof is None:
481 return ('-displayof', self._w)
482 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000483 def _options(self, cnf, kw = None):
484 if kw:
485 cnf = _cnfmerge((cnf, kw))
486 else:
487 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000488 res = ()
489 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000490 if v is not None:
491 if k[-1] == '_': k = k[:-1]
492 if callable(v):
493 v = self._register(v)
494 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000495 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000496 def _nametowidget(self, name):
497 w = self
498 if name[0] == '.':
499 w = w._root()
500 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000501 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000502 while name:
503 i = find(name, '.')
504 if i >= 0:
505 name, tail = name[:i], name[i+1:]
506 else:
507 tail = ''
508 w = w.children[name]
509 name = tail
510 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000511 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000512 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000513 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000514 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000515 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000516 except AttributeError:
517 pass
518 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000519 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000520 except AttributeError:
521 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000522 self.tk.createcommand(name, f)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000523 if self._tclCommands is None:
524 self._tclCommands = []
525 self._tclCommands.append(name)
526 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000527 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000528 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000529 def _root(self):
530 w = self
531 while w.master: w = w.master
532 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000533 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000534 '%s', '%t', '%w', '%x', '%y',
535 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
536 def _substitute(self, *args):
537 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000538 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000539 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
540 # Missing: (a, c, d, m, o, v, B, R)
541 e = Event()
542 e.serial = tk.getint(nsign)
543 e.num = tk.getint(b)
544 try: e.focus = tk.getboolean(f)
545 except TclError: pass
546 e.height = tk.getint(h)
547 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000548 # For Visibility events, event state is a string and
549 # not an integer:
550 try:
551 e.state = tk.getint(s)
552 except TclError:
553 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000554 e.time = tk.getint(t)
555 e.width = tk.getint(w)
556 e.x = tk.getint(x)
557 e.y = tk.getint(y)
558 e.char = A
559 try: e.send_event = tk.getboolean(E)
560 except TclError: pass
561 e.keysym = K
562 e.keysym_num = tk.getint(N)
563 e.type = T
564 e.widget = self._nametowidget(W)
565 e.x_root = tk.getint(X)
566 e.y_root = tk.getint(Y)
567 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000568 def _report_exception(self):
569 import sys
570 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
571 root = self._root()
572 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000573
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000574class CallWrapper:
575 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000576 self.func = func
577 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000578 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000579 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000580 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000581 if self.subst:
582 args = apply(self.subst, args)
583 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000584 except SystemExit, msg:
585 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000586 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000587 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000588
589class Wm:
590 def aspect(self,
591 minNumer=None, minDenom=None,
592 maxNumer=None, maxDenom=None):
593 return self._getints(
594 self.tk.call('wm', 'aspect', self._w,
595 minNumer, minDenom,
596 maxNumer, maxDenom))
597 def client(self, name=None):
598 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000599 def colormapwindows(self, *wlist):
600 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
601 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000602 def command(self, value=None):
603 return self.tk.call('wm', 'command', self._w, value)
604 def deiconify(self):
605 return self.tk.call('wm', 'deiconify', self._w)
606 def focusmodel(self, model=None):
607 return self.tk.call('wm', 'focusmodel', self._w, model)
608 def frame(self):
609 return self.tk.call('wm', 'frame', self._w)
610 def geometry(self, newGeometry=None):
611 return self.tk.call('wm', 'geometry', self._w, newGeometry)
612 def grid(self,
613 baseWidht=None, baseHeight=None,
614 widthInc=None, heightInc=None):
615 return self._getints(self.tk.call(
616 'wm', 'grid', self._w,
617 baseWidht, baseHeight, widthInc, heightInc))
618 def group(self, pathName=None):
619 return self.tk.call('wm', 'group', self._w, pathName)
620 def iconbitmap(self, bitmap=None):
621 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
622 def iconify(self):
623 return self.tk.call('wm', 'iconify', self._w)
624 def iconmask(self, bitmap=None):
625 return self.tk.call('wm', 'iconmask', self._w, bitmap)
626 def iconname(self, newName=None):
627 return self.tk.call('wm', 'iconname', self._w, newName)
628 def iconposition(self, x=None, y=None):
629 return self._getints(self.tk.call(
630 'wm', 'iconposition', self._w, x, y))
631 def iconwindow(self, pathName=None):
632 return self.tk.call('wm', 'iconwindow', self._w, pathName)
633 def maxsize(self, width=None, height=None):
634 return self._getints(self.tk.call(
635 'wm', 'maxsize', self._w, width, height))
636 def minsize(self, width=None, height=None):
637 return self._getints(self.tk.call(
638 'wm', 'minsize', self._w, width, height))
639 def overrideredirect(self, boolean=None):
640 return self._getboolean(self.tk.call(
641 'wm', 'overrideredirect', self._w, boolean))
642 def positionfrom(self, who=None):
643 return self.tk.call('wm', 'positionfrom', self._w, who)
644 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000645 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000646 command = self._register(func)
647 else:
648 command = func
649 return self.tk.call(
650 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000651 def resizable(self, width=None, height=None):
652 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000653 def sizefrom(self, who=None):
654 return self.tk.call('wm', 'sizefrom', self._w, who)
655 def state(self):
656 return self.tk.call('wm', 'state', self._w)
657 def title(self, string=None):
658 return self.tk.call('wm', 'title', self._w, string)
659 def transient(self, master=None):
660 return self.tk.call('wm', 'transient', self._w, master)
661 def withdraw(self):
662 return self.tk.call('wm', 'withdraw', self._w)
663
664class Tk(Misc, Wm):
665 _w = '.'
666 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000667 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000668 self.master = None
669 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000670 if baseName is None:
671 import sys, os
672 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000673 baseName, ext = os.path.splitext(baseName)
674 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000675 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000676 try:
677 # Disable event scanning except for Command-Period
678 import MacOS
679 MacOS.EnableAppswitch(0)
680 except ImportError:
681 pass
682 else:
683 # Work around nasty MacTk bug
684 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000685 # Version sanity checks
686 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000687 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000688 raise RuntimeError, \
689 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000690 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000691 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000692 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000693 raise RuntimeError, \
694 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000695 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000696 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000697 raise RuntimeError, \
698 "Tk 4.0 or higher is required; found Tk %s" \
699 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000700 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000701 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000702 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000703 if not _default_root:
704 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000705 def destroy(self):
706 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000707 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000708 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000709 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000710 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000711 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000712 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000713 if os.environ.has_key('HOME'): home = os.environ['HOME']
714 else: home = os.curdir
715 class_tcl = os.path.join(home, '.%s.tcl' % className)
716 class_py = os.path.join(home, '.%s.py' % className)
717 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
718 base_py = os.path.join(home, '.%s.py' % baseName)
719 dir = {'self': self}
720 exec 'from Tkinter import *' in dir
721 if os.path.isfile(class_tcl):
722 print 'source', `class_tcl`
723 self.tk.call('source', class_tcl)
724 if os.path.isfile(class_py):
725 print 'execfile', `class_py`
726 execfile(class_py, dir)
727 if os.path.isfile(base_tcl):
728 print 'source', `base_tcl`
729 self.tk.call('source', base_tcl)
730 if os.path.isfile(base_py):
731 print 'execfile', `base_py`
732 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000733 def report_callback_exception(self, exc, val, tb):
734 import traceback
735 print "Exception in Tkinter callback"
736 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000737
738class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000739 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000740 apply(self.tk.call,
741 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000742 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000743 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000744 pack = config
745 def __setitem__(self, key, value):
746 Pack.config({key: value})
747 def forget(self):
748 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000749 pack_forget = forget
750 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000751 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000752 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000753 dict = {}
754 for i in range(0, len(words), 2):
755 key = words[i][1:]
756 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000757 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000758 value = self._nametowidget(value)
759 dict[key] = value
760 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000761 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000762 _noarg_ = ['_noarg_']
763 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000764 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000765 return self._getboolean(self.tk.call(
766 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000767 else:
768 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000769 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000770 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000771 return map(self._nametowidget,
772 self.tk.splitlist(
773 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000774 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000775
776class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000777 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000778 for k in ['in_']:
779 if kw.has_key(k):
780 kw[k[:-1]] = kw[k]
781 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000782 apply(self.tk.call,
783 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000784 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000785 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000786 place = config
787 def __setitem__(self, key, value):
788 Place.config({key: value})
789 def forget(self):
790 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000791 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000792 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000793 words = self.tk.splitlist(
794 self.tk.call('place', 'info', self._w))
795 dict = {}
796 for i in range(0, len(words), 2):
797 key = words[i][1:]
798 value = words[i+1]
799 if value[:1] == '.':
800 value = self._nametowidget(value)
801 dict[key] = value
802 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000803 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000804 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000805 return map(self._nametowidget,
806 self.tk.splitlist(
807 self.tk.call(
808 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000809 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000810
Guido van Rossum37dcab11996-05-16 16:00:19 +0000811class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000812 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000813 def config(self, cnf={}, **kw):
814 apply(self.tk.call,
815 ('grid', 'configure', self._w)
816 + self._options(cnf, kw))
817 grid = config
818 def __setitem__(self, key, value):
819 Grid.config({key: value})
820 def bbox(self, column, row):
821 return self._getints(
822 self.tk.call(
823 'grid', 'bbox', self._w, column, row)) or None
824 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000825 def columnconfigure(self, index, cnf={}, **kw):
826 if type(cnf) is not DictionaryType and not kw:
827 options = self._options({cnf: None})
828 else:
829 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000830 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000831 ('grid', 'columnconfigure', self._w, index)
832 + options)
833 if options == ('-minsize', None):
834 return self.tk.getint(res) or None
835 elif options == ('-weight', None):
836 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000837 def forget(self):
838 self.tk.call('grid', 'forget', self._w)
839 grid_forget = forget
840 def info(self):
841 words = self.tk.splitlist(
842 self.tk.call('grid', 'info', self._w))
843 dict = {}
844 for i in range(0, len(words), 2):
845 key = words[i][1:]
846 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000847 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000848 value = self._nametowidget(value)
849 dict[key] = value
850 return dict
851 grid_info = info
852 def location(self, x, y):
853 return self._getints(
854 self.tk.call(
855 'grid', 'location', self._w, x, y)) or None
856 _noarg_ = ['_noarg_']
857 def propagate(self, flag=_noarg_):
858 if flag is Grid._noarg_:
859 return self._getboolean(self.tk.call(
860 'grid', 'propagate', self._w))
861 else:
862 self.tk.call('grid', 'propagate', self._w, flag)
863 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000864 def rowconfigure(self, index, cnf={}, **kw):
865 if type(cnf) is not DictionaryType and not kw:
866 options = self._options({cnf: None})
867 else:
868 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000869 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000870 ('grid', 'rowconfigure', self._w, index)
871 + options)
872 if options == ('-minsize', None):
873 return self.tk.getint(res) or None
874 elif options == ('-weight', None):
875 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000876 def size(self):
877 return self._getints(
878 self.tk.call('grid', 'size', self._w)) or None
879 def slaves(self, *args):
880 return map(self._nametowidget,
881 self.tk.splitlist(
882 apply(self.tk.call,
883 ('grid', 'slaves', self._w) + args)))
884 grid_slaves = slaves
885
886class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000887 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000888 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000889 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000890 if not _default_root:
891 _default_root = Tk()
892 master = _default_root
893 if not _default_root:
894 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000895 self.master = master
896 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000897 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000898 if cnf.has_key('name'):
899 name = cnf['name']
900 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000901 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000902 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000903 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000904 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000905 self._w = '.' + name
906 else:
907 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000908 self.children = {}
909 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000910 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000911 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000912 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
913 if kw:
914 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000915 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000916 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000917 classes = []
918 for k in cnf.keys():
919 if type(k) is ClassType:
920 classes.append((k, cnf[k]))
921 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000922 apply(self.tk.call,
923 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000924 for k, v in classes:
925 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000926 def config(self, cnf=None, **kw):
927 # XXX ought to generalize this so tag_config etc. can use it
928 if kw:
929 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000930 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000931 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000932 if cnf is None:
933 cnf = {}
934 for x in self.tk.split(
935 self.tk.call(self._w, 'configure')):
936 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
937 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000938 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000939 x = self.tk.split(self.tk.call(
940 self._w, 'configure', '-'+cnf))
941 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000942 apply(self.tk.call, (self._w, 'configure')
943 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000944 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000945 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000946 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000947 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000948 def __setitem__(self, key, value):
949 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000950 def keys(self):
951 return map(lambda x: x[0][1:],
952 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000953 def __str__(self):
954 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000955 def destroy(self):
956 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000957 if self.master.children.has_key(self._name):
958 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000959 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000960 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000961 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000962 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000963
964class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000965 def __init__(self, master=None, cnf={}, **kw):
966 if kw:
967 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000968 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000969 for wmkey in ['screen', 'class_', 'class', 'visual',
970 'colormap']:
971 if cnf.has_key(wmkey):
972 val = cnf[wmkey]
973 # TBD: a hack needed because some keys
974 # are not valid as keyword arguments
975 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
976 else: opt = '-'+wmkey
977 extra = extra + (opt, val)
978 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000979 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000980 root = self._root()
981 self.iconname(root.iconname())
982 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000983
984class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000985 def __init__(self, master=None, cnf={}, **kw):
986 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000987 def tkButtonEnter(self, *dummy):
988 self.tk.call('tkButtonEnter', self._w)
989 def tkButtonLeave(self, *dummy):
990 self.tk.call('tkButtonLeave', self._w)
991 def tkButtonDown(self, *dummy):
992 self.tk.call('tkButtonDown', self._w)
993 def tkButtonUp(self, *dummy):
994 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000995 def tkButtonInvoke(self, *dummy):
996 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000997 def flash(self):
998 self.tk.call(self._w, 'flash')
999 def invoke(self):
1000 self.tk.call(self._w, 'invoke')
1001
1002# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001003# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001004def AtEnd():
1005 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001006def AtInsert(*args):
1007 s = 'insert'
1008 for a in args:
1009 if a: s = s + (' ' + a)
1010 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001011def AtSelFirst():
1012 return 'sel.first'
1013def AtSelLast():
1014 return 'sel.last'
1015def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001016 if y is None:
1017 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001018 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001019 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001020
1021class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001022 def __init__(self, master=None, cnf={}, **kw):
1023 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001024 def addtag(self, *args):
1025 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001026 def addtag_above(self, newtag, tagOrId):
1027 self.addtag(newtag, 'above', tagOrId)
1028 def addtag_all(self, newtag):
1029 self.addtag(newtag, 'all')
1030 def addtag_below(self, newtag, tagOrId):
1031 self.addtag(newtag, 'below', tagOrId)
1032 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1033 self.addtag(newtag, 'closest', x, y, halo, start)
1034 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1035 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1036 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1037 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1038 def addtag_withtag(self, newtag, tagOrId):
1039 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001040 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001041 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001042 def tag_unbind(self, tagOrId, sequence):
1043 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001044 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001045 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001046 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001047 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001048 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001049 self._w, 'canvasx', screenx, gridspacing))
1050 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001051 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001052 self._w, 'canvasy', screeny, gridspacing))
1053 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001054 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001055 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001056 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001057 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001058 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001059 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001060 args = args[:-1]
1061 else:
1062 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001063 return self.tk.getint(apply(
1064 self.tk.call,
1065 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001066 + args + self._options(cnf, kw)))
1067 def create_arc(self, *args, **kw):
1068 return self._create('arc', args, kw)
1069 def create_bitmap(self, *args, **kw):
1070 return self._create('bitmap', args, kw)
1071 def create_image(self, *args, **kw):
1072 return self._create('image', args, kw)
1073 def create_line(self, *args, **kw):
1074 return self._create('line', args, kw)
1075 def create_oval(self, *args, **kw):
1076 return self._create('oval', args, kw)
1077 def create_polygon(self, *args, **kw):
1078 return self._create('polygon', args, kw)
1079 def create_rectangle(self, *args, **kw):
1080 return self._create('rectangle', args, kw)
1081 def create_text(self, *args, **kw):
1082 return self._create('text', args, kw)
1083 def create_window(self, *args, **kw):
1084 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001085 def dchars(self, *args):
1086 self._do('dchars', args)
1087 def delete(self, *args):
1088 self._do('delete', args)
1089 def dtag(self, *args):
1090 self._do('dtag', args)
1091 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001092 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001093 def find_above(self, tagOrId):
1094 return self.find('above', tagOrId)
1095 def find_all(self):
1096 return self.find('all')
1097 def find_below(self, tagOrId):
1098 return self.find('below', tagOrId)
1099 def find_closest(self, x, y, halo=None, start=None):
1100 return self.find('closest', x, y, halo, start)
1101 def find_enclosed(self, x1, y1, x2, y2):
1102 return self.find('enclosed', x1, y1, x2, y2)
1103 def find_overlapping(self, x1, y1, x2, y2):
1104 return self.find('overlapping', x1, y1, x2, y2)
1105 def find_withtag(self, tagOrId):
1106 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001107 def focus(self, *args):
1108 return self._do('focus', args)
1109 def gettags(self, *args):
1110 return self.tk.splitlist(self._do('gettags', args))
1111 def icursor(self, *args):
1112 self._do('icursor', args)
1113 def index(self, *args):
1114 return self.tk.getint(self._do('index', args))
1115 def insert(self, *args):
1116 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001117 def itemcget(self, tagOrId, option):
1118 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001119 def itemconfig(self, tagOrId, cnf=None, **kw):
1120 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001121 cnf = {}
1122 for x in self.tk.split(
1123 self._do('itemconfigure', (tagOrId))):
1124 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1125 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001126 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001127 x = self.tk.split(self._do('itemconfigure',
1128 (tagOrId, '-'+cnf,)))
1129 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001130 self._do('itemconfigure', (tagOrId,)
1131 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001132 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001133 def lower(self, *args):
1134 self._do('lower', args)
1135 def move(self, *args):
1136 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001137 def postscript(self, cnf={}, **kw):
1138 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001139 def tkraise(self, *args):
1140 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001141 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001142 def scale(self, *args):
1143 self._do('scale', args)
1144 def scan_mark(self, x, y):
1145 self.tk.call(self._w, 'scan', 'mark', x, y)
1146 def scan_dragto(self, x, y):
1147 self.tk.call(self._w, 'scan', 'dragto', x, y)
1148 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001149 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001151 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001152 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001153 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001154 def select_item(self):
1155 self.tk.call(self._w, 'select', 'item')
1156 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001157 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001158 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001159 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001160 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001161 if not args:
1162 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001163 apply(self.tk.call, (self._w, 'xview')+args)
1164 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001165 if not args:
1166 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001167 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001168
1169class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001170 def __init__(self, master=None, cnf={}, **kw):
1171 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001172 def deselect(self):
1173 self.tk.call(self._w, 'deselect')
1174 def flash(self):
1175 self.tk.call(self._w, 'flash')
1176 def invoke(self):
1177 self.tk.call(self._w, 'invoke')
1178 def select(self):
1179 self.tk.call(self._w, 'select')
1180 def toggle(self):
1181 self.tk.call(self._w, 'toggle')
1182
1183class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001184 def __init__(self, master=None, cnf={}, **kw):
1185 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001186 def delete(self, first, last=None):
1187 self.tk.call(self._w, 'delete', first, last)
1188 def get(self):
1189 return self.tk.call(self._w, 'get')
1190 def icursor(self, index):
1191 self.tk.call(self._w, 'icursor', index)
1192 def index(self, index):
1193 return self.tk.getint(self.tk.call(
1194 self._w, 'index', index))
1195 def insert(self, index, string):
1196 self.tk.call(self._w, 'insert', index, string)
1197 def scan_mark(self, x):
1198 self.tk.call(self._w, 'scan', 'mark', x)
1199 def scan_dragto(self, x):
1200 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001201 def selection_adjust(self, index):
1202 self.tk.call(self._w, 'selection', 'adjust', index)
1203 select_adjust = selection_adjust
1204 def selection_clear(self):
1205 self.tk.call(self._w, 'selection', 'clear')
1206 select_clear = selection_clear
1207 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001208 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001209 select_from = selection_from
1210 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001211 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001212 self.tk.call(self._w, 'selection', 'present'))
1213 select_present = selection_present
1214 def selection_range(self, start, end):
1215 self.tk.call(self._w, 'selection', 'range', start, end)
1216 select_range = selection_range
1217 def selection_to(self, index):
1218 self.tk.call(self._w, 'selection', 'to', index)
1219 select_to = selection_to
1220 def xview(self, index):
1221 self.tk.call(self._w, 'xview', index)
1222 def xview_moveto(self, fraction):
1223 self.tk.call(self._w, 'xview', 'moveto', fraction)
1224 def xview_scroll(self, number, what):
1225 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001226
1227class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001228 def __init__(self, master=None, cnf={}, **kw):
1229 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001230 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001231 if cnf.has_key('class_'):
1232 extra = ('-class', cnf['class_'])
1233 del cnf['class_']
1234 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001235 extra = ('-class', cnf['class'])
1236 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001237 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001238
1239class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001240 def __init__(self, master=None, cnf={}, **kw):
1241 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001242
Guido van Rossum18468821994-06-20 07:49:28 +00001243class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001244 def __init__(self, master=None, cnf={}, **kw):
1245 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001246 def activate(self, index):
1247 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001248 def bbox(self, *args):
1249 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001250 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001251 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001252 return self.tk.splitlist(self.tk.call(
1253 self._w, 'curselection'))
1254 def delete(self, first, last=None):
1255 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001256 def get(self, first, last=None):
1257 if last:
1258 return self.tk.splitlist(self.tk.call(
1259 self._w, 'get', first, last))
1260 else:
1261 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001262 def insert(self, index, *elements):
1263 apply(self.tk.call,
1264 (self._w, 'insert', index) + elements)
1265 def nearest(self, y):
1266 return self.tk.getint(self.tk.call(
1267 self._w, 'nearest', y))
1268 def scan_mark(self, x, y):
1269 self.tk.call(self._w, 'scan', 'mark', x, y)
1270 def scan_dragto(self, x, y):
1271 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001272 def see(self, index):
1273 self.tk.call(self._w, 'see', index)
1274 def index(self, index):
1275 i = self.tk.call(self._w, 'index', index)
1276 if i == 'none': return None
1277 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001278 def select_anchor(self, index):
1279 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001280 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001281 def select_clear(self, first, last=None):
1282 self.tk.call(self._w,
1283 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001284 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001285 def select_includes(self, index):
1286 return self.tk.getboolean(self.tk.call(
1287 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001288 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001289 def select_set(self, first, last=None):
1290 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001291 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001292 def size(self):
1293 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001294 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001295 if not what:
1296 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001297 apply(self.tk.call, (self._w, 'xview')+what)
1298 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001299 if not what:
1300 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001301 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001302
1303class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001304 def __init__(self, master=None, cnf={}, **kw):
1305 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001306 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001307 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001308 def tk_mbPost(self):
1309 self.tk.call('tk_mbPost', self._w)
1310 def tk_mbUnpost(self):
1311 self.tk.call('tk_mbUnpost')
1312 def tk_traverseToMenu(self, char):
1313 self.tk.call('tk_traverseToMenu', self._w, char)
1314 def tk_traverseWithinMenu(self, char):
1315 self.tk.call('tk_traverseWithinMenu', self._w, char)
1316 def tk_getMenuButtons(self):
1317 return self.tk.call('tk_getMenuButtons', self._w)
1318 def tk_nextMenu(self, count):
1319 self.tk.call('tk_nextMenu', count)
1320 def tk_nextMenuEntry(self, count):
1321 self.tk.call('tk_nextMenuEntry', count)
1322 def tk_invokeMenu(self):
1323 self.tk.call('tk_invokeMenu', self._w)
1324 def tk_firstMenu(self):
1325 self.tk.call('tk_firstMenu', self._w)
1326 def tk_mbButtonDown(self):
1327 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001328 def tk_popup(self, x, y, entry=""):
1329 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001330 def activate(self, index):
1331 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001332 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001333 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001334 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001335 def add_cascade(self, cnf={}, **kw):
1336 self.add('cascade', cnf or kw)
1337 def add_checkbutton(self, cnf={}, **kw):
1338 self.add('checkbutton', cnf or kw)
1339 def add_command(self, cnf={}, **kw):
1340 self.add('command', cnf or kw)
1341 def add_radiobutton(self, cnf={}, **kw):
1342 self.add('radiobutton', cnf or kw)
1343 def add_separator(self, cnf={}, **kw):
1344 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001345 def insert(self, index, itemType, cnf={}, **kw):
1346 apply(self.tk.call, (self._w, 'insert', index, itemType)
1347 + self._options(cnf, kw))
1348 def insert_cascade(self, index, cnf={}, **kw):
1349 self.insert(index, 'cascade', cnf or kw)
1350 def insert_checkbutton(self, index, cnf={}, **kw):
1351 self.insert(index, 'checkbutton', cnf or kw)
1352 def insert_command(self, index, cnf={}, **kw):
1353 self.insert(index, 'command', cnf or kw)
1354 def insert_radiobutton(self, index, cnf={}, **kw):
1355 self.insert(index, 'radiobutton', cnf or kw)
1356 def insert_separator(self, index, cnf={}, **kw):
1357 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001358 def delete(self, index1, index2=None):
1359 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001360 def entryconfig(self, index, cnf=None, **kw):
1361 if cnf is None and not kw:
1362 cnf = {}
1363 for x in self.tk.split(apply(self.tk.call,
1364 (self._w, 'entryconfigure', index))):
1365 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1366 return cnf
1367 if type(cnf) == StringType and not kw:
1368 x = self.tk.split(apply(self.tk.call,
1369 (self._w, 'entryconfigure', index, '-'+cnf)))
1370 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001371 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001372 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001373 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001374 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001375 i = self.tk.call(self._w, 'index', index)
1376 if i == 'none': return None
1377 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001378 def invoke(self, index):
1379 return self.tk.call(self._w, 'invoke', index)
1380 def post(self, x, y):
1381 self.tk.call(self._w, 'post', x, y)
1382 def unpost(self):
1383 self.tk.call(self._w, 'unpost')
1384 def yposition(self, index):
1385 return self.tk.getint(self.tk.call(
1386 self._w, 'yposition', index))
1387
1388class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001389 def __init__(self, master=None, cnf={}, **kw):
1390 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001391
1392class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001393 def __init__(self, master=None, cnf={}, **kw):
1394 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001395
1396class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001397 def __init__(self, master=None, cnf={}, **kw):
1398 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001399 def deselect(self):
1400 self.tk.call(self._w, 'deselect')
1401 def flash(self):
1402 self.tk.call(self._w, 'flash')
1403 def invoke(self):
1404 self.tk.call(self._w, 'invoke')
1405 def select(self):
1406 self.tk.call(self._w, 'select')
1407
1408class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001409 def __init__(self, master=None, cnf={}, **kw):
1410 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001411 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001412 value = self.tk.call(self._w, 'get')
1413 try:
1414 return self.tk.getint(value)
1415 except TclError:
1416 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001417 def set(self, value):
1418 self.tk.call(self._w, 'set', value)
1419
1420class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001421 def __init__(self, master=None, cnf={}, **kw):
1422 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001423 def activate(self, index):
1424 self.tk.call(self._w, 'activate', index)
1425 def delta(self, deltax, deltay):
1426 return self.getdouble(self.tk.call(
1427 self._w, 'delta', deltax, deltay))
1428 def fraction(self, x, y):
1429 return self.getdouble(self.tk.call(
1430 self._w, 'fraction', x, y))
1431 def identify(self, x, y):
1432 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001433 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001434 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001435 def set(self, *args):
1436 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001437
1438class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001439 def __init__(self, master=None, cnf={}, **kw):
1440 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001441 def bbox(self, *args):
1442 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001443 def tk_textSelectTo(self, index):
1444 self.tk.call('tk_textSelectTo', self._w, index)
1445 def tk_textBackspace(self):
1446 self.tk.call('tk_textBackspace', self._w)
1447 def tk_textIndexCloser(self, a, b, c):
1448 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1449 def tk_textResetAnchor(self, index):
1450 self.tk.call('tk_textResetAnchor', self._w, index)
1451 def compare(self, index1, op, index2):
1452 return self.tk.getboolean(self.tk.call(
1453 self._w, 'compare', index1, op, index2))
1454 def debug(self, boolean=None):
1455 return self.tk.getboolean(self.tk.call(
1456 self._w, 'debug', boolean))
1457 def delete(self, index1, index2=None):
1458 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001459 def dlineinfo(self, index):
1460 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001461 def get(self, index1, index2=None):
1462 return self.tk.call(self._w, 'get', index1, index2)
1463 def index(self, index):
1464 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001465 def insert(self, index, chars, *args):
1466 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001467 def mark_gravity(self, markName, direction=None):
1468 return apply(self.tk.call,
1469 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001470 def mark_names(self):
1471 return self.tk.splitlist(self.tk.call(
1472 self._w, 'mark', 'names'))
1473 def mark_set(self, markName, index):
1474 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001475 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001476 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001477 def scan_mark(self, x, y):
1478 self.tk.call(self._w, 'scan', 'mark', x, y)
1479 def scan_dragto(self, x, y):
1480 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001481 def search(self, pattern, index, stopindex=None,
1482 forwards=None, backwards=None, exact=None,
1483 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001484 args = [self._w, 'search']
1485 if forwards: args.append('-forwards')
1486 if backwards: args.append('-backwards')
1487 if exact: args.append('-exact')
1488 if regexp: args.append('-regexp')
1489 if nocase: args.append('-nocase')
1490 if count: args.append('-count'); args.append(count)
1491 if pattern[0] == '-': args.append('--')
1492 args.append(pattern)
1493 args.append(index)
1494 if stopindex: args.append(stopindex)
1495 return apply(self.tk.call, tuple(args))
1496 def see(self, index):
1497 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001498 def tag_add(self, tagName, index1, index2=None):
1499 self.tk.call(
1500 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001501 def tag_unbind(self, tagName, sequence):
1502 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001503 def tag_bind(self, tagName, sequence, func, add=None):
1504 return self._bind((self._w, 'tag', 'bind', tagName),
1505 sequence, func, add)
1506 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001507 if option[:1] != '-':
1508 option = '-' + option
1509 if option[-1:] == '_':
1510 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001511 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001512 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001513 if type(cnf) == StringType:
1514 x = self.tk.split(self.tk.call(
1515 self._w, 'tag', 'configure', tagName, '-'+cnf))
1516 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001517 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001518 (self._w, 'tag', 'configure', tagName)
1519 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001520 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001521 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001522 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001523 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001524 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001525 def tag_names(self, index=None):
1526 return self.tk.splitlist(
1527 self.tk.call(self._w, 'tag', 'names', index))
1528 def tag_nextrange(self, tagName, index1, index2=None):
1529 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001530 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001531 def tag_raise(self, tagName, aboveThis=None):
1532 self.tk.call(
1533 self._w, 'tag', 'raise', tagName, aboveThis)
1534 def tag_ranges(self, tagName):
1535 return self.tk.splitlist(self.tk.call(
1536 self._w, 'tag', 'ranges', tagName))
1537 def tag_remove(self, tagName, index1, index2=None):
1538 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001539 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001540 def window_cget(self, index, option):
1541 return self.tk.call(self._w, 'window', 'cget', index, option)
1542 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001543 if type(cnf) == StringType:
1544 x = self.tk.split(self.tk.call(
1545 self._w, 'window', 'configure',
1546 index, '-'+cnf))
1547 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001548 apply(self.tk.call,
1549 (self._w, 'window', 'configure', index)
1550 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001551 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001552 def window_create(self, index, cnf={}, **kw):
1553 apply(self.tk.call,
1554 (self._w, 'window', 'create', index)
1555 + self._options(cnf, kw))
1556 def window_names(self):
1557 return self.tk.splitlist(
1558 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001559 def xview(self, *what):
1560 if not what:
1561 return self._getdoubles(self.tk.call(self._w, 'xview'))
1562 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001563 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001564 if not what:
1565 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001566 apply(self.tk.call, (self._w, 'yview')+what)
1567 def yview_pickplace(self, *what):
1568 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001569
Guido van Rossum28574b51996-10-21 15:16:51 +00001570class _setit:
1571 def __init__(self, var, value):
1572 self.__value = value
1573 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001574 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001575 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001576
1577class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001578 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001579 kw = {"borderwidth": 2, "textvariable": variable,
1580 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1581 "highlightthickness": 2}
1582 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001583 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001584 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1585 self.menuname = menu._w
1586 menu.add_command(label=value, command=_setit(variable, value))
1587 for v in values:
1588 menu.add_command(label=v, command=_setit(variable, v))
1589 self["menu"] = menu
1590
1591 def __getitem__(self, name):
1592 if name == 'menu':
1593 return self.__menu
1594 return Widget.__getitem__(self, name)
1595
1596 def destroy(self):
1597 Menubutton.destroy(self)
1598 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001599
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001600class Image:
1601 def __init__(self, imgtype, name=None, cnf={}, **kw):
1602 self.name = None
1603 master = _default_root
1604 if not master: raise RuntimeError, 'Too early to create image'
1605 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001606 if not name:
1607 name = `id(self)`
1608 # The following is needed for systems where id(x)
1609 # can return a negative number, such as Linux/m68k:
1610 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001611 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1612 elif kw: cnf = kw
1613 options = ()
1614 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001615 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001616 v = self._register(v)
1617 options = options + ('-'+k, v)
1618 apply(self.tk.call,
1619 ('image', 'create', imgtype, name,) + options)
1620 self.name = name
1621 def __str__(self): return self.name
1622 def __del__(self):
1623 if self.name:
1624 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001625 def __setitem__(self, key, value):
1626 self.tk.call(self.name, 'configure', '-'+key, value)
1627 def __getitem__(self, key):
1628 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001629 def config(self, **kw):
1630 res = ()
1631 for k, v in _cnfmerge(kw).items():
1632 if v is not None:
1633 if k[-1] == '_': k = k[:-1]
1634 if callable(v):
1635 v = self._register(v)
1636 res = res + ('-'+k, v)
1637 apply(self.tk.call, (self.name, 'config') + res)
1638 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001639 def height(self):
1640 return self.tk.getint(
1641 self.tk.call('image', 'height', self.name))
1642 def type(self):
1643 return self.tk.call('image', 'type', self.name)
1644 def width(self):
1645 return self.tk.getint(
1646 self.tk.call('image', 'width', self.name))
1647
1648class PhotoImage(Image):
1649 def __init__(self, name=None, cnf={}, **kw):
1650 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001651 def blank(self):
1652 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001653 def cget(self, option):
1654 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001655 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001656 def __getitem__(self, key):
1657 return self.tk.call(self.name, 'cget', '-' + key)
1658 def copy(self):
1659 destImage = PhotoImage()
1660 self.tk.call(destImage, 'copy', self.name)
1661 return destImage
1662 def zoom(self,x,y=''):
1663 destImage = PhotoImage()
1664 if y=='': y=x
1665 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1666 return destImage
1667 def subsample(self,x,y=''):
1668 destImage = PhotoImage()
1669 if y=='': y=x
1670 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1671 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001672 def get(self, x, y):
1673 return self.tk.call(self.name, 'get', x, y)
1674 def put(self, data, to=None):
1675 args = (self.name, 'put', data)
1676 if to:
1677 args = args + to
1678 apply(self.tk.call, args)
1679 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001680 def write(self, filename, format=None, from_coords=None):
1681 args = (self.name, 'write', filename)
1682 if format:
1683 args = args + ('-format', format)
1684 if from_coords:
1685 args = args + ('-from',) + tuple(from_coords)
1686 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001687
1688class BitmapImage(Image):
1689 def __init__(self, name=None, cnf={}, **kw):
1690 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1691
1692def image_names(): return _default_root.tk.call('image', 'names')
1693def image_types(): return _default_root.tk.call('image', 'types')
1694
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001695######################################################################
1696# Extensions:
1697
1698class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001699 def __init__(self, master=None, cnf={}, **kw):
1700 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001701 self.bind('<Any-Enter>', self.tkButtonEnter)
1702 self.bind('<Any-Leave>', self.tkButtonLeave)
1703 self.bind('<1>', self.tkButtonDown)
1704 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001705
1706class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001707 def __init__(self, master=None, cnf={}, **kw):
1708 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001709 self.bind('<Any-Enter>', self.tkButtonEnter)
1710 self.bind('<Any-Leave>', self.tkButtonLeave)
1711 self.bind('<1>', self.tkButtonDown)
1712 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1713 self['fg'] = self['bg']
1714 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001715
Guido van Rossumc417ef81996-08-21 23:38:59 +00001716######################################################################
1717# Test:
1718
1719def _test():
1720 root = Tk()
1721 label = Label(root, text="Proof-of-existence test for Tk")
1722 label.pack()
1723 test = Button(root, text="Click me!",
1724 command=lambda root=root: root.test.config(
1725 text="[%s]" % root.test['text']))
1726 test.pack()
1727 root.test = test
1728 quit = Button(root, text="QUIT", command=root.destroy)
1729 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001730 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001731 root.mainloop()
1732
1733if __name__ == '__main__':
1734 _test()
1735
Guido van Rossum37dcab11996-05-16 16:00:19 +00001736
1737# Emacs cruft
1738# Local Variables:
1739# py-indent-offset: 8
1740# End: