blob: 9d5fa6f61e78095440c0872b56aed2735750f06e [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):
46 for k, v in c.items():
47 cnf[k] = v
48 return cnf
49
50class Event:
51 pass
52
Guido van Rossumaec5dc91994-06-27 07:55:12 +000053_default_root = None
54
Guido van Rossum45853db1994-06-20 12:19:19 +000055def _tkerror(err):
Guido van Rossum18468821994-06-20 07:49:28 +000056 pass
57
Guido van Rossum97aeca11994-07-07 13:12:12 +000058def _exit(code='0'):
Guido van Rossum37dcab11996-05-16 16:00:19 +000059 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +000060
Guido van Rossumaec5dc91994-06-27 07:55:12 +000061_varnum = 0
62class Variable:
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000063 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000064 def __init__(self, master=None):
65 global _default_root
66 global _varnum
67 if master:
68 self._tk = master.tk
69 else:
70 self._tk = _default_root.tk
71 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 Rossumaec5dc91994-06-27 07:55:12 +000080
81class StringVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +000082 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +000083 def __init__(self, master=None):
84 Variable.__init__(self, master)
85 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000086 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +000087
88class IntVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000089 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000090 def __init__(self, master=None):
91 Variable.__init__(self, master)
92 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +000093 return self._tk.getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +000094
95class DoubleVar(Variable):
Guido van Rossum0b96b941996-12-27 15:30:20 +000096 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +000097 def __init__(self, master=None):
98 Variable.__init__(self, master)
99 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000100 return self._tk.getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000101
102class BooleanVar(Variable):
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000103 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000104 def __init__(self, master=None):
105 Variable.__init__(self, master)
106 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000107 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000108
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000109def mainloop(n=0):
110 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000111
112def getint(s):
113 return _default_root.tk.getint(s)
114
115def getdouble(s):
116 return _default_root.tk.getdouble(s)
117
118def getboolean(s):
119 return _default_root.tk.getboolean(s)
120
Guido van Rossum18468821994-06-20 07:49:28 +0000121class Misc:
Fred Drake526749b1997-05-03 04:16:23 +0000122 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000123 def destroy(self):
124 if self._tclCommands is not None:
125 for name in self._tclCommands:
126 #print '- Tkinter: deleted command', name
127 self.tk.deletecommand(name)
128 self._tclCommands = None
129 def deletecommand(self, name):
130 #print '- Tkinter: deleted command', name
131 self.tk.deletecommand(name)
132 index = self._tclCommands.index(name)
133 del self._tclCommands[index]
Guido van Rossum18468821994-06-20 07:49:28 +0000134 def tk_strictMotif(self, boolean=None):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000135 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000136 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000137 def tk_bisque(self):
138 self.tk.call('tk_bisque')
139 def tk_setPalette(self, *args, **kw):
Fred Drake3faf9b41996-10-04 19:23:04 +0000140 apply(self.tk.call, ('tk_setPalette',)
141 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000142 def tk_menuBar(self, *args):
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000143 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000144 def wait_variable(self, name='PY_VAR'):
Guido van Rossum18468821994-06-20 07:49:28 +0000145 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000146 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000147 def wait_window(self, window=None):
148 if window == None:
149 window = self
150 self.tk.call('tkwait', 'window', window._w)
151 def wait_visibility(self, window=None):
152 if window == None:
153 window = self
154 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000155 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum18468821994-06-20 07:49:28 +0000156 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000157 def getvar(self, name='PY_VAR'):
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000158 return self.tk.getvar(name)
159 def getint(self, s):
160 return self.tk.getint(s)
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000161 def getdouble(self, s):
162 return self.tk.getdouble(s)
163 def getboolean(self, s):
164 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000165 def focus_set(self):
Guido van Rossum18468821994-06-20 07:49:28 +0000166 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000167 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000168 def focus_force(self):
169 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000170 def focus_get(self):
171 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000172 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000173 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000174 def focus_displayof(self):
175 name = self.tk.call('focus', '-displayof', self._w)
176 if name == 'none' or not name: return None
177 return self._nametowidget(name)
178 def focus_lastfor(self):
179 name = self.tk.call('focus', '-lastfor', self._w)
180 if name == 'none' or not name: return None
181 return self._nametowidget(name)
182 def tk_focusFollowsMouse(self):
183 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000184 def tk_focusNext(self):
185 name = self.tk.call('tk_focusNext', self._w)
186 if not name: return None
187 return self._nametowidget(name)
188 def tk_focusPrev(self):
189 name = self.tk.call('tk_focusPrev', self._w)
190 if not name: return None
191 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000192 def after(self, ms, func=None, *args):
193 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000194 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000195 self.tk.call('after', ms)
196 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000197 # XXX Disgusting hack to clean up after calling func
198 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000199 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000200 try:
201 apply(func, args)
202 finally:
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000203 self.deletecommand(tmp[0])
Guido van Rossum08a40381994-06-21 11:44:21 +0000204 name = self._register(callit)
205 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000206 return self.tk.call('after', ms, name)
207 def after_idle(self, func, *args):
208 return apply(self.after, ('idle', func) + args)
209 def after_cancel(self, id):
210 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000211 def bell(self, displayof=0):
212 apply(self.tk.call, ('bell',) + self._displayof(displayof))
213 # Clipboard handling:
214 def clipboard_clear(self, **kw):
215 if not kw.has_key('displayof'): kw['displayof'] = self._w
216 apply(self.tk.call,
217 ('clipboard', 'clear') + self._options(kw))
218 def clipboard_append(self, string, **kw):
219 if not kw.has_key('displayof'): kw['displayof'] = self._w
220 apply(self.tk.call,
221 ('clipboard', 'append') + self._options(kw)
222 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000223 # XXX grab current w/o window argument
224 def grab_current(self):
225 name = self.tk.call('grab', 'current', self._w)
226 if not name: return None
227 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000228 def grab_release(self):
229 self.tk.call('grab', 'release', self._w)
230 def grab_set(self):
231 self.tk.call('grab', 'set', self._w)
232 def grab_set_global(self):
233 self.tk.call('grab', 'set', '-global', self._w)
234 def grab_status(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000235 status = self.tk.call('grab', 'status', self._w)
236 if status == 'none': status = None
237 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000238 def lower(self, belowThis=None):
239 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000240 def option_add(self, pattern, value, priority = None):
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000241 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000242 def option_clear(self):
243 self.tk.call('option', 'clear')
244 def option_get(self, name, className):
245 return self.tk.call('option', 'get', self._w, name, className)
246 def option_readfile(self, fileName, priority = None):
247 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000248 def selection_clear(self, **kw):
249 if not kw.has_key('displayof'): kw['displayof'] = self._w
250 apply(self.tk.call, ('selection', 'clear') + self._options(kw))
251 def selection_get(self, **kw):
252 if not kw.has_key('displayof'): kw['displayof'] = self._w
253 return apply(self.tk.call,
254 ('selection', 'get') + self._options(kw))
255 def selection_handle(self, command, **kw):
256 name = self._register(command)
257 apply(self.tk.call,
258 ('selection', 'handle') + self._options(kw)
259 + (self._w, name))
260 def selection_own(self, **kw):
261 "Become owner of X selection."
262 apply(self.tk.call,
263 ('selection', 'own') + self._options(kw) + (self._w,))
264 def selection_own_get(self, **kw):
265 "Find owner of X selection."
266 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossum76f587b1997-01-21 23:22:03 +0000267 name = apply(self.tk.call,
268 ('selection', 'own') + self._options(kw))
269 if not name: return None
270 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000271 def send(self, interp, cmd, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000272 return apply(self.tk.call, ('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000273 def lower(self, belowThis=None):
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000274 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000275 def tkraise(self, aboveThis=None):
276 self.tk.call('raise', self._w, aboveThis)
277 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000278 def colormodel(self, value=None):
279 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000280 def winfo_atom(self, name, displayof=0):
281 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
282 return self.tk.getint(apply(self.tk.call, args))
283 def winfo_atomname(self, id, displayof=0):
284 args = ('winfo', 'atomname') \
285 + self._displayof(displayof) + (id,)
286 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000287 def winfo_cells(self):
288 return self.tk.getint(
289 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000290 def winfo_children(self):
291 return map(self._nametowidget,
292 self.tk.splitlist(self.tk.call(
293 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000294 def winfo_class(self):
295 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000296 def winfo_colormapfull(self):
297 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000298 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000299 def winfo_containing(self, rootX, rootY, displayof=0):
300 args = ('winfo', 'containing') \
301 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000302 name = apply(self.tk.call, args)
303 if not name: return None
304 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000305 def winfo_depth(self):
306 return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
307 def winfo_exists(self):
308 return self.tk.getint(
309 self.tk.call('winfo', 'exists', self._w))
310 def winfo_fpixels(self, number):
311 return self.tk.getdouble(self.tk.call(
312 'winfo', 'fpixels', self._w, number))
313 def winfo_geometry(self):
314 return self.tk.call('winfo', 'geometry', self._w)
315 def winfo_height(self):
316 return self.tk.getint(
317 self.tk.call('winfo', 'height', self._w))
318 def winfo_id(self):
319 return self.tk.getint(
320 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000321 def winfo_interps(self, displayof=0):
322 args = ('winfo', 'interps') + self._displayof(displayof)
323 return self.tk.splitlist(apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000324 def winfo_ismapped(self):
325 return self.tk.getint(
326 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000327 def winfo_manager(self):
328 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000329 def winfo_name(self):
330 return self.tk.call('winfo', 'name', self._w)
331 def winfo_parent(self):
332 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000333 def winfo_pathname(self, id, displayof=0):
334 args = ('winfo', 'pathname') \
335 + self._displayof(displayof) + (id,)
336 return apply(self.tk.call, args)
Guido van Rossum18468821994-06-20 07:49:28 +0000337 def winfo_pixels(self, number):
338 return self.tk.getint(
339 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000340 def winfo_pointerx(self):
341 return self.tk.getint(
342 self.tk.call('winfo', 'pointerx', self._w))
343 def winfo_pointerxy(self):
344 return self._getints(
345 self.tk.call('winfo', 'pointerxy', self._w))
346 def winfo_pointery(self):
347 return self.tk.getint(
348 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000349 def winfo_reqheight(self):
350 return self.tk.getint(
351 self.tk.call('winfo', 'reqheight', self._w))
352 def winfo_reqwidth(self):
353 return self.tk.getint(
354 self.tk.call('winfo', 'reqwidth', self._w))
355 def winfo_rgb(self, color):
356 return self._getints(
357 self.tk.call('winfo', 'rgb', self._w, color))
358 def winfo_rootx(self):
359 return self.tk.getint(
360 self.tk.call('winfo', 'rootx', self._w))
361 def winfo_rooty(self):
362 return self.tk.getint(
363 self.tk.call('winfo', 'rooty', self._w))
364 def winfo_screen(self):
365 return self.tk.call('winfo', 'screen', self._w)
366 def winfo_screencells(self):
367 return self.tk.getint(
368 self.tk.call('winfo', 'screencells', self._w))
369 def winfo_screendepth(self):
370 return self.tk.getint(
371 self.tk.call('winfo', 'screendepth', self._w))
372 def winfo_screenheight(self):
373 return self.tk.getint(
374 self.tk.call('winfo', 'screenheight', self._w))
375 def winfo_screenmmheight(self):
376 return self.tk.getint(
377 self.tk.call('winfo', 'screenmmheight', self._w))
378 def winfo_screenmmwidth(self):
379 return self.tk.getint(
380 self.tk.call('winfo', 'screenmmwidth', self._w))
381 def winfo_screenvisual(self):
382 return self.tk.call('winfo', 'screenvisual', self._w)
383 def winfo_screenwidth(self):
384 return self.tk.getint(
385 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000386 def winfo_server(self):
387 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000388 def winfo_toplevel(self):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000389 return self._nametowidget(self.tk.call(
390 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000391 def winfo_viewable(self):
392 return self.tk.getint(
393 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000394 def winfo_visual(self):
395 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000396 def winfo_visualid(self):
397 return self.tk.call('winfo', 'visualid', self._w)
398 def winfo_visualsavailable(self, includeids=0):
399 data = self.tk.split(
400 self.tk.call('winfo', 'visualsavailable', self._w,
401 includeids and 'includeids' or None))
402 def parseitem(x, self=self):
403 return x[:1] + tuple(map(self.tk.getint, x[1:]))
404 return map(parseitem, data)
Guido van Rossum18468821994-06-20 07:49:28 +0000405 def winfo_vrootheight(self):
406 return self.tk.getint(
407 self.tk.call('winfo', 'vrootheight', self._w))
408 def winfo_vrootwidth(self):
409 return self.tk.getint(
410 self.tk.call('winfo', 'vrootwidth', self._w))
411 def winfo_vrootx(self):
412 return self.tk.getint(
413 self.tk.call('winfo', 'vrootx', self._w))
414 def winfo_vrooty(self):
415 return self.tk.getint(
416 self.tk.call('winfo', 'vrooty', self._w))
417 def winfo_width(self):
418 return self.tk.getint(
419 self.tk.call('winfo', 'width', self._w))
420 def winfo_x(self):
421 return self.tk.getint(
422 self.tk.call('winfo', 'x', self._w))
423 def winfo_y(self):
424 return self.tk.getint(
425 self.tk.call('winfo', 'y', self._w))
426 def update(self):
427 self.tk.call('update')
428 def update_idletasks(self):
429 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000430 def bindtags(self, tagList=None):
431 if tagList is None:
432 return self.tk.splitlist(
433 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000434 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000435 self.tk.call('bindtags', self._w, tagList)
436 def _bind(self, what, sequence, func, add):
437 if func:
438 cmd = ("%sset _tkinter_break [%s %s]\n"
439 'if {"$_tkinter_break" == "break"} break\n') \
440 % (add and '+' or '',
441 self._register(func, self._substitute),
442 _string.join(self._subst_format))
443 apply(self.tk.call, what + (sequence, cmd))
444 elif func == '':
445 apply(self.tk.call, what + (sequence, func))
446 else:
447 return apply(self.tk.call, what + (sequence,))
448 def bind(self, sequence=None, func=None, add=None):
449 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossumef8f8811994-08-08 12:47:33 +0000450 def unbind(self, sequence):
451 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000452 def bind_all(self, sequence=None, func=None, add=None):
453 return self._bind(('bind', 'all'), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000454 def unbind_all(self, sequence):
455 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000456 def bind_class(self, className, sequence=None, func=None, add=None):
457 self._bind(('bind', className), sequence, func, add)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000458 def unbind_class(self, className, sequence):
459 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000460 def mainloop(self, n=0):
461 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000462 def quit(self):
463 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000464 def _getints(self, string):
Guido van Rossum45853db1994-06-20 12:19:19 +0000465 if not string: return None
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000466 return tuple(map(self.tk.getint, self.tk.splitlist(string)))
467 def _getdoubles(self, string):
468 if not string: return None
469 return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000470 def _getboolean(self, string):
471 if string:
472 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000473 def _displayof(self, displayof):
474 if displayof:
475 return ('-displayof', displayof)
476 if displayof is None:
477 return ('-displayof', self._w)
478 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000479 def _options(self, cnf, kw = None):
480 if kw:
481 cnf = _cnfmerge((cnf, kw))
482 else:
483 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000484 res = ()
485 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000486 if v is not None:
487 if k[-1] == '_': k = k[:-1]
488 if callable(v):
489 v = self._register(v)
490 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000491 return res
Guido van Rossum45853db1994-06-20 12:19:19 +0000492 def _nametowidget(self, name):
493 w = self
494 if name[0] == '.':
495 w = w._root()
496 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000497 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000498 while name:
499 i = find(name, '.')
500 if i >= 0:
501 name, tail = name[:i], name[i+1:]
502 else:
503 tail = ''
504 w = w.children[name]
505 name = tail
506 return w
Guido van Rossum18468821994-06-20 07:49:28 +0000507 def _register(self, func, subst=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000508 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000509 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000510 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000511 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000512 except AttributeError:
513 pass
514 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000515 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000516 except AttributeError:
517 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000518 self.tk.createcommand(name, f)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000519 if self._tclCommands is None:
520 self._tclCommands = []
521 self._tclCommands.append(name)
522 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +0000523 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +0000524 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +0000525 def _root(self):
526 w = self
527 while w.master: w = w.master
528 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000529 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +0000530 '%s', '%t', '%w', '%x', '%y',
531 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
532 def _substitute(self, *args):
533 tk = self.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000534 if len(args) != len(self._subst_format): return args
Guido van Rossum45853db1994-06-20 12:19:19 +0000535 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
536 # Missing: (a, c, d, m, o, v, B, R)
537 e = Event()
538 e.serial = tk.getint(nsign)
539 e.num = tk.getint(b)
540 try: e.focus = tk.getboolean(f)
541 except TclError: pass
542 e.height = tk.getint(h)
543 e.keycode = tk.getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +0000544 # For Visibility events, event state is a string and
545 # not an integer:
546 try:
547 e.state = tk.getint(s)
548 except TclError:
549 e.state = s
Guido van Rossum45853db1994-06-20 12:19:19 +0000550 e.time = tk.getint(t)
551 e.width = tk.getint(w)
552 e.x = tk.getint(x)
553 e.y = tk.getint(y)
554 e.char = A
555 try: e.send_event = tk.getboolean(E)
556 except TclError: pass
557 e.keysym = K
558 e.keysym_num = tk.getint(N)
559 e.type = T
560 e.widget = self._nametowidget(W)
561 e.x_root = tk.getint(X)
562 e.y_root = tk.getint(Y)
563 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000564 def _report_exception(self):
565 import sys
566 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
567 root = self._root()
568 root.report_callback_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000569
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000570class CallWrapper:
571 def __init__(self, func, subst, widget):
Guido van Rossum18468821994-06-20 07:49:28 +0000572 self.func = func
573 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000574 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +0000575 def __call__(self, *args):
Guido van Rossum18468821994-06-20 07:49:28 +0000576 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000577 if self.subst:
578 args = apply(self.subst, args)
579 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +0000580 except SystemExit, msg:
581 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +0000582 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000583 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +0000584
585class Wm:
586 def aspect(self,
587 minNumer=None, minDenom=None,
588 maxNumer=None, maxDenom=None):
589 return self._getints(
590 self.tk.call('wm', 'aspect', self._w,
591 minNumer, minDenom,
592 maxNumer, maxDenom))
593 def client(self, name=None):
594 return self.tk.call('wm', 'client', self._w, name)
Fred Drake3c602d71996-09-27 14:06:54 +0000595 def colormapwindows(self, *wlist):
596 args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
597 return map(self._nametowidget, apply(self.tk.call, args))
Guido van Rossum18468821994-06-20 07:49:28 +0000598 def command(self, value=None):
599 return self.tk.call('wm', 'command', self._w, value)
600 def deiconify(self):
601 return self.tk.call('wm', 'deiconify', self._w)
602 def focusmodel(self, model=None):
603 return self.tk.call('wm', 'focusmodel', self._w, model)
604 def frame(self):
605 return self.tk.call('wm', 'frame', self._w)
606 def geometry(self, newGeometry=None):
607 return self.tk.call('wm', 'geometry', self._w, newGeometry)
608 def grid(self,
609 baseWidht=None, baseHeight=None,
610 widthInc=None, heightInc=None):
611 return self._getints(self.tk.call(
612 'wm', 'grid', self._w,
613 baseWidht, baseHeight, widthInc, heightInc))
614 def group(self, pathName=None):
615 return self.tk.call('wm', 'group', self._w, pathName)
616 def iconbitmap(self, bitmap=None):
617 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
618 def iconify(self):
619 return self.tk.call('wm', 'iconify', self._w)
620 def iconmask(self, bitmap=None):
621 return self.tk.call('wm', 'iconmask', self._w, bitmap)
622 def iconname(self, newName=None):
623 return self.tk.call('wm', 'iconname', self._w, newName)
624 def iconposition(self, x=None, y=None):
625 return self._getints(self.tk.call(
626 'wm', 'iconposition', self._w, x, y))
627 def iconwindow(self, pathName=None):
628 return self.tk.call('wm', 'iconwindow', self._w, pathName)
629 def maxsize(self, width=None, height=None):
630 return self._getints(self.tk.call(
631 'wm', 'maxsize', self._w, width, height))
632 def minsize(self, width=None, height=None):
633 return self._getints(self.tk.call(
634 'wm', 'minsize', self._w, width, height))
635 def overrideredirect(self, boolean=None):
636 return self._getboolean(self.tk.call(
637 'wm', 'overrideredirect', self._w, boolean))
638 def positionfrom(self, who=None):
639 return self.tk.call('wm', 'positionfrom', self._w, who)
640 def protocol(self, name=None, func=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000641 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +0000642 command = self._register(func)
643 else:
644 command = func
645 return self.tk.call(
646 'wm', 'protocol', self._w, name, command)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000647 def resizable(self, width=None, height=None):
648 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossum18468821994-06-20 07:49:28 +0000649 def sizefrom(self, who=None):
650 return self.tk.call('wm', 'sizefrom', self._w, who)
651 def state(self):
652 return self.tk.call('wm', 'state', self._w)
653 def title(self, string=None):
654 return self.tk.call('wm', 'title', self._w, string)
655 def transient(self, master=None):
656 return self.tk.call('wm', 'transient', self._w, master)
657 def withdraw(self):
658 return self.tk.call('wm', 'withdraw', self._w)
659
660class Tk(Misc, Wm):
661 _w = '.'
662 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000663 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +0000664 self.master = None
665 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +0000666 if baseName is None:
667 import sys, os
668 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +0000669 baseName, ext = os.path.splitext(baseName)
670 if ext not in ('.py', 'pyc'): baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +0000671 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000672 try:
673 # Disable event scanning except for Command-Period
674 import MacOS
675 MacOS.EnableAppswitch(0)
676 except ImportError:
677 pass
678 else:
679 # Work around nasty MacTk bug
680 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000681 # Version sanity checks
682 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000683 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000684 raise RuntimeError, \
685 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000686 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000687 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +0000688 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000689 raise RuntimeError, \
690 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +0000691 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +0000692 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000693 raise RuntimeError, \
694 "Tk 4.0 or higher is required; found Tk %s" \
695 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +0000696 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +0000697 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +0000698 self.readprofile(baseName, className)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000699 if not _default_root:
700 _default_root = self
Guido van Rossum45853db1994-06-20 12:19:19 +0000701 def destroy(self):
702 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +0000703 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000704 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000705 def __str__(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000706 return self._w
Guido van Rossum27b77a41994-07-12 15:52:32 +0000707 def readprofile(self, baseName, className):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000708 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +0000709 if os.environ.has_key('HOME'): home = os.environ['HOME']
710 else: home = os.curdir
711 class_tcl = os.path.join(home, '.%s.tcl' % className)
712 class_py = os.path.join(home, '.%s.py' % className)
713 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
714 base_py = os.path.join(home, '.%s.py' % baseName)
715 dir = {'self': self}
716 exec 'from Tkinter import *' in dir
717 if os.path.isfile(class_tcl):
718 print 'source', `class_tcl`
719 self.tk.call('source', class_tcl)
720 if os.path.isfile(class_py):
721 print 'execfile', `class_py`
722 execfile(class_py, dir)
723 if os.path.isfile(base_tcl):
724 print 'source', `base_tcl`
725 self.tk.call('source', base_tcl)
726 if os.path.isfile(base_py):
727 print 'execfile', `base_py`
728 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000729 def report_callback_exception(self, exc, val, tb):
730 import traceback
731 print "Exception in Tkinter callback"
732 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +0000733
734class Pack:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000735 def config(self, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +0000736 apply(self.tk.call,
737 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000738 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000739 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000740 pack = config
741 def __setitem__(self, key, value):
742 Pack.config({key: value})
743 def forget(self):
744 self.tk.call('pack', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000745 pack_forget = forget
746 def info(self):
Guido van Rossum69170c51994-07-11 15:21:31 +0000747 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +0000748 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +0000749 dict = {}
750 for i in range(0, len(words), 2):
751 key = words[i][1:]
752 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000753 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +0000754 value = self._nametowidget(value)
755 dict[key] = value
756 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000757 pack_info = info
Guido van Rossum5505d561994-12-30 17:16:35 +0000758 _noarg_ = ['_noarg_']
759 def propagate(self, flag=_noarg_):
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000760 if flag is Pack._noarg_:
Guido van Rossum18468821994-06-20 07:49:28 +0000761 return self._getboolean(self.tk.call(
762 'pack', 'propagate', self._w))
Guido van Rossum5505d561994-12-30 17:16:35 +0000763 else:
764 self.tk.call('pack', 'propagate', self._w, flag)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000765 pack_propagate = propagate
Guido van Rossum18468821994-06-20 07:49:28 +0000766 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000767 return map(self._nametowidget,
768 self.tk.splitlist(
769 self.tk.call('pack', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000770 pack_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000771
772class Place:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000773 def config(self, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000774 for k in ['in_']:
775 if kw.has_key(k):
776 kw[k[:-1]] = kw[k]
777 del kw[k]
Guido van Rossum18468821994-06-20 07:49:28 +0000778 apply(self.tk.call,
779 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000780 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000781 configure = config
Guido van Rossum18468821994-06-20 07:49:28 +0000782 place = config
783 def __setitem__(self, key, value):
784 Place.config({key: value})
785 def forget(self):
786 self.tk.call('place', 'forget', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000787 place_forget = forget
Guido van Rossum18468821994-06-20 07:49:28 +0000788 def info(self):
Guido van Rossum63e39ae1996-05-16 17:53:48 +0000789 words = self.tk.splitlist(
790 self.tk.call('place', 'info', self._w))
791 dict = {}
792 for i in range(0, len(words), 2):
793 key = words[i][1:]
794 value = words[i+1]
795 if value[:1] == '.':
796 value = self._nametowidget(value)
797 dict[key] = value
798 return dict
Guido van Rossum37dcab11996-05-16 16:00:19 +0000799 place_info = info
Guido van Rossum18468821994-06-20 07:49:28 +0000800 def slaves(self):
Guido van Rossum45853db1994-06-20 12:19:19 +0000801 return map(self._nametowidget,
802 self.tk.splitlist(
803 self.tk.call(
804 'place', 'slaves', self._w)))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000805 place_slaves = slaves
Guido van Rossum18468821994-06-20 07:49:28 +0000806
Guido van Rossum37dcab11996-05-16 16:00:19 +0000807class Grid:
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000808 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000809 def config(self, cnf={}, **kw):
810 apply(self.tk.call,
811 ('grid', 'configure', self._w)
812 + self._options(cnf, kw))
813 grid = config
814 def __setitem__(self, key, value):
815 Grid.config({key: value})
816 def bbox(self, column, row):
817 return self._getints(
818 self.tk.call(
819 'grid', 'bbox', self._w, column, row)) or None
820 grid_bbox = bbox
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000821 def columnconfigure(self, index, cnf={}, **kw):
822 if type(cnf) is not DictionaryType and not kw:
823 options = self._options({cnf: None})
824 else:
825 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000826 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000827 ('grid', 'columnconfigure', self._w, index)
828 + options)
829 if options == ('-minsize', None):
830 return self.tk.getint(res) or None
831 elif options == ('-weight', None):
832 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000833 def forget(self):
834 self.tk.call('grid', 'forget', self._w)
835 grid_forget = forget
836 def info(self):
837 words = self.tk.splitlist(
838 self.tk.call('grid', 'info', self._w))
839 dict = {}
840 for i in range(0, len(words), 2):
841 key = words[i][1:]
842 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +0000843 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +0000844 value = self._nametowidget(value)
845 dict[key] = value
846 return dict
847 grid_info = info
848 def location(self, x, y):
849 return self._getints(
850 self.tk.call(
851 'grid', 'location', self._w, x, y)) or None
852 _noarg_ = ['_noarg_']
853 def propagate(self, flag=_noarg_):
854 if flag is Grid._noarg_:
855 return self._getboolean(self.tk.call(
856 'grid', 'propagate', self._w))
857 else:
858 self.tk.call('grid', 'propagate', self._w, flag)
859 grid_propagate = propagate
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000860 def rowconfigure(self, index, cnf={}, **kw):
861 if type(cnf) is not DictionaryType and not kw:
862 options = self._options({cnf: None})
863 else:
864 options = self._options(cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000865 res = apply(self.tk.call,
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000866 ('grid', 'rowconfigure', self._w, index)
867 + options)
868 if options == ('-minsize', None):
869 return self.tk.getint(res) or None
870 elif options == ('-weight', None):
871 return self.tk.getdouble(res) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +0000872 def size(self):
873 return self._getints(
874 self.tk.call('grid', 'size', self._w)) or None
875 def slaves(self, *args):
876 return map(self._nametowidget,
877 self.tk.splitlist(
878 apply(self.tk.call,
879 ('grid', 'slaves', self._w) + args)))
880 grid_slaves = slaves
881
882class Widget(Misc, Pack, Place, Grid):
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000883 def _setup(self, master, cnf):
Guido van Rossum45853db1994-06-20 12:19:19 +0000884 global _default_root
Guido van Rossum18468821994-06-20 07:49:28 +0000885 if not master:
Guido van Rossum45853db1994-06-20 12:19:19 +0000886 if not _default_root:
887 _default_root = Tk()
888 master = _default_root
889 if not _default_root:
890 _default_root = master
Guido van Rossum18468821994-06-20 07:49:28 +0000891 self.master = master
892 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +0000893 name = None
Guido van Rossum18468821994-06-20 07:49:28 +0000894 if cnf.has_key('name'):
895 name = cnf['name']
896 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +0000897 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +0000898 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000899 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000900 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000901 self._w = '.' + name
902 else:
903 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000904 self.children = {}
905 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000906 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000907 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000908 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
909 if kw:
910 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000911 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000912 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000913 classes = []
914 for k in cnf.keys():
915 if type(k) is ClassType:
916 classes.append((k, cnf[k]))
917 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000918 apply(self.tk.call,
919 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000920 for k, v in classes:
921 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000922 def config(self, cnf=None, **kw):
923 # XXX ought to generalize this so tag_config etc. can use it
924 if kw:
925 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000926 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000927 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000928 if cnf is None:
929 cnf = {}
930 for x in self.tk.split(
931 self.tk.call(self._w, 'configure')):
932 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
933 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000934 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000935 x = self.tk.split(self.tk.call(
936 self._w, 'configure', '-'+cnf))
937 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000938 apply(self.tk.call, (self._w, 'configure')
939 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000940 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000941 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000942 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000943 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000944 def __setitem__(self, key, value):
945 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000946 def keys(self):
947 return map(lambda x: x[0][1:],
948 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000949 def __str__(self):
950 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000951 def destroy(self):
952 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000953 if self.master.children.has_key(self._name):
954 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000955 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000956 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000957 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000958 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000959
960class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000961 def __init__(self, master=None, cnf={}, **kw):
962 if kw:
963 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000964 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000965 for wmkey in ['screen', 'class_', 'class', 'visual',
966 'colormap']:
967 if cnf.has_key(wmkey):
968 val = cnf[wmkey]
969 # TBD: a hack needed because some keys
970 # are not valid as keyword arguments
971 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
972 else: opt = '-'+wmkey
973 extra = extra + (opt, val)
974 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000975 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000976 root = self._root()
977 self.iconname(root.iconname())
978 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000979
980class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000981 def __init__(self, master=None, cnf={}, **kw):
982 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000983 def tkButtonEnter(self, *dummy):
984 self.tk.call('tkButtonEnter', self._w)
985 def tkButtonLeave(self, *dummy):
986 self.tk.call('tkButtonLeave', self._w)
987 def tkButtonDown(self, *dummy):
988 self.tk.call('tkButtonDown', self._w)
989 def tkButtonUp(self, *dummy):
990 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000991 def tkButtonInvoke(self, *dummy):
992 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000993 def flash(self):
994 self.tk.call(self._w, 'flash')
995 def invoke(self):
996 self.tk.call(self._w, 'invoke')
997
998# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000999# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001000def AtEnd():
1001 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001002def AtInsert(*args):
1003 s = 'insert'
1004 for a in args:
1005 if a: s = s + (' ' + a)
1006 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001007def AtSelFirst():
1008 return 'sel.first'
1009def AtSelLast():
1010 return 'sel.last'
1011def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001012 if y is None:
1013 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001014 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001015 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001016
1017class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001018 def __init__(self, master=None, cnf={}, **kw):
1019 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001020 def addtag(self, *args):
1021 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001022 def addtag_above(self, newtag, tagOrId):
1023 self.addtag(newtag, 'above', tagOrId)
1024 def addtag_all(self, newtag):
1025 self.addtag(newtag, 'all')
1026 def addtag_below(self, newtag, tagOrId):
1027 self.addtag(newtag, 'below', tagOrId)
1028 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1029 self.addtag(newtag, 'closest', x, y, halo, start)
1030 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1031 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1032 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1033 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1034 def addtag_withtag(self, newtag, tagOrId):
1035 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001036 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001037 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001038 def tag_unbind(self, tagOrId, sequence):
1039 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001040 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001041 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001042 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001043 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001044 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001045 self._w, 'canvasx', screenx, gridspacing))
1046 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001047 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001048 self._w, 'canvasy', screeny, gridspacing))
1049 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001050 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001051 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001052 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001053 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001054 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001055 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001056 args = args[:-1]
1057 else:
1058 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001059 return self.tk.getint(apply(
1060 self.tk.call,
1061 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001062 + args + self._options(cnf, kw)))
1063 def create_arc(self, *args, **kw):
1064 return self._create('arc', args, kw)
1065 def create_bitmap(self, *args, **kw):
1066 return self._create('bitmap', args, kw)
1067 def create_image(self, *args, **kw):
1068 return self._create('image', args, kw)
1069 def create_line(self, *args, **kw):
1070 return self._create('line', args, kw)
1071 def create_oval(self, *args, **kw):
1072 return self._create('oval', args, kw)
1073 def create_polygon(self, *args, **kw):
1074 return self._create('polygon', args, kw)
1075 def create_rectangle(self, *args, **kw):
1076 return self._create('rectangle', args, kw)
1077 def create_text(self, *args, **kw):
1078 return self._create('text', args, kw)
1079 def create_window(self, *args, **kw):
1080 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001081 def dchars(self, *args):
1082 self._do('dchars', args)
1083 def delete(self, *args):
1084 self._do('delete', args)
1085 def dtag(self, *args):
1086 self._do('dtag', args)
1087 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001088 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001089 def find_above(self, tagOrId):
1090 return self.find('above', tagOrId)
1091 def find_all(self):
1092 return self.find('all')
1093 def find_below(self, tagOrId):
1094 return self.find('below', tagOrId)
1095 def find_closest(self, x, y, halo=None, start=None):
1096 return self.find('closest', x, y, halo, start)
1097 def find_enclosed(self, x1, y1, x2, y2):
1098 return self.find('enclosed', x1, y1, x2, y2)
1099 def find_overlapping(self, x1, y1, x2, y2):
1100 return self.find('overlapping', x1, y1, x2, y2)
1101 def find_withtag(self, tagOrId):
1102 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001103 def focus(self, *args):
1104 return self._do('focus', args)
1105 def gettags(self, *args):
1106 return self.tk.splitlist(self._do('gettags', args))
1107 def icursor(self, *args):
1108 self._do('icursor', args)
1109 def index(self, *args):
1110 return self.tk.getint(self._do('index', args))
1111 def insert(self, *args):
1112 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001113 def itemcget(self, tagOrId, option):
1114 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001115 def itemconfig(self, tagOrId, cnf=None, **kw):
1116 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001117 cnf = {}
1118 for x in self.tk.split(
1119 self._do('itemconfigure', (tagOrId))):
1120 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1121 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001122 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001123 x = self.tk.split(self._do('itemconfigure',
1124 (tagOrId, '-'+cnf,)))
1125 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001126 self._do('itemconfigure', (tagOrId,)
1127 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001128 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001129 def lower(self, *args):
1130 self._do('lower', args)
1131 def move(self, *args):
1132 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001133 def postscript(self, cnf={}, **kw):
1134 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001135 def tkraise(self, *args):
1136 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001137 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001138 def scale(self, *args):
1139 self._do('scale', args)
1140 def scan_mark(self, x, y):
1141 self.tk.call(self._w, 'scan', 'mark', x, y)
1142 def scan_dragto(self, x, y):
1143 self.tk.call(self._w, 'scan', 'dragto', x, y)
1144 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001145 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001146 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001147 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001148 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001149 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001150 def select_item(self):
1151 self.tk.call(self._w, 'select', 'item')
1152 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001153 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001154 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001155 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001156 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001157 if not args:
1158 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001159 apply(self.tk.call, (self._w, 'xview')+args)
1160 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001161 if not args:
1162 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001163 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001164
1165class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001166 def __init__(self, master=None, cnf={}, **kw):
1167 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001168 def deselect(self):
1169 self.tk.call(self._w, 'deselect')
1170 def flash(self):
1171 self.tk.call(self._w, 'flash')
1172 def invoke(self):
1173 self.tk.call(self._w, 'invoke')
1174 def select(self):
1175 self.tk.call(self._w, 'select')
1176 def toggle(self):
1177 self.tk.call(self._w, 'toggle')
1178
1179class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001180 def __init__(self, master=None, cnf={}, **kw):
1181 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001182 def delete(self, first, last=None):
1183 self.tk.call(self._w, 'delete', first, last)
1184 def get(self):
1185 return self.tk.call(self._w, 'get')
1186 def icursor(self, index):
1187 self.tk.call(self._w, 'icursor', index)
1188 def index(self, index):
1189 return self.tk.getint(self.tk.call(
1190 self._w, 'index', index))
1191 def insert(self, index, string):
1192 self.tk.call(self._w, 'insert', index, string)
1193 def scan_mark(self, x):
1194 self.tk.call(self._w, 'scan', 'mark', x)
1195 def scan_dragto(self, x):
1196 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001197 def selection_adjust(self, index):
1198 self.tk.call(self._w, 'selection', 'adjust', index)
1199 select_adjust = selection_adjust
1200 def selection_clear(self):
1201 self.tk.call(self._w, 'selection', 'clear')
1202 select_clear = selection_clear
1203 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001204 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001205 select_from = selection_from
1206 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001207 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001208 self.tk.call(self._w, 'selection', 'present'))
1209 select_present = selection_present
1210 def selection_range(self, start, end):
1211 self.tk.call(self._w, 'selection', 'range', start, end)
1212 select_range = selection_range
1213 def selection_to(self, index):
1214 self.tk.call(self._w, 'selection', 'to', index)
1215 select_to = selection_to
1216 def xview(self, index):
1217 self.tk.call(self._w, 'xview', index)
1218 def xview_moveto(self, fraction):
1219 self.tk.call(self._w, 'xview', 'moveto', fraction)
1220 def xview_scroll(self, number, what):
1221 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001222
1223class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001224 def __init__(self, master=None, cnf={}, **kw):
1225 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001226 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001227 if cnf.has_key('class_'):
1228 extra = ('-class', cnf['class_'])
1229 del cnf['class_']
1230 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001231 extra = ('-class', cnf['class'])
1232 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001233 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001234
1235class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001236 def __init__(self, master=None, cnf={}, **kw):
1237 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001238
Guido van Rossum18468821994-06-20 07:49:28 +00001239class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001240 def __init__(self, master=None, cnf={}, **kw):
1241 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001242 def activate(self, index):
1243 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001244 def bbox(self, *args):
1245 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001246 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001247 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001248 return self.tk.splitlist(self.tk.call(
1249 self._w, 'curselection'))
1250 def delete(self, first, last=None):
1251 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001252 def get(self, first, last=None):
1253 if last:
1254 return self.tk.splitlist(self.tk.call(
1255 self._w, 'get', first, last))
1256 else:
1257 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001258 def insert(self, index, *elements):
1259 apply(self.tk.call,
1260 (self._w, 'insert', index) + elements)
1261 def nearest(self, y):
1262 return self.tk.getint(self.tk.call(
1263 self._w, 'nearest', y))
1264 def scan_mark(self, x, y):
1265 self.tk.call(self._w, 'scan', 'mark', x, y)
1266 def scan_dragto(self, x, y):
1267 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001268 def see(self, index):
1269 self.tk.call(self._w, 'see', index)
1270 def index(self, index):
1271 i = self.tk.call(self._w, 'index', index)
1272 if i == 'none': return None
1273 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001274 def select_anchor(self, index):
1275 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001276 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001277 def select_clear(self, first, last=None):
1278 self.tk.call(self._w,
1279 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001280 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001281 def select_includes(self, index):
1282 return self.tk.getboolean(self.tk.call(
1283 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001284 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001285 def select_set(self, first, last=None):
1286 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001287 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001288 def size(self):
1289 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001290 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001291 if not what:
1292 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001293 apply(self.tk.call, (self._w, 'xview')+what)
1294 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001295 if not what:
1296 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001297 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001298
1299class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001300 def __init__(self, master=None, cnf={}, **kw):
1301 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001302 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001303 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001304 def tk_mbPost(self):
1305 self.tk.call('tk_mbPost', self._w)
1306 def tk_mbUnpost(self):
1307 self.tk.call('tk_mbUnpost')
1308 def tk_traverseToMenu(self, char):
1309 self.tk.call('tk_traverseToMenu', self._w, char)
1310 def tk_traverseWithinMenu(self, char):
1311 self.tk.call('tk_traverseWithinMenu', self._w, char)
1312 def tk_getMenuButtons(self):
1313 return self.tk.call('tk_getMenuButtons', self._w)
1314 def tk_nextMenu(self, count):
1315 self.tk.call('tk_nextMenu', count)
1316 def tk_nextMenuEntry(self, count):
1317 self.tk.call('tk_nextMenuEntry', count)
1318 def tk_invokeMenu(self):
1319 self.tk.call('tk_invokeMenu', self._w)
1320 def tk_firstMenu(self):
1321 self.tk.call('tk_firstMenu', self._w)
1322 def tk_mbButtonDown(self):
1323 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001324 def tk_popup(self, x, y, entry=""):
1325 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001326 def activate(self, index):
1327 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001328 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001329 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001330 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001331 def add_cascade(self, cnf={}, **kw):
1332 self.add('cascade', cnf or kw)
1333 def add_checkbutton(self, cnf={}, **kw):
1334 self.add('checkbutton', cnf or kw)
1335 def add_command(self, cnf={}, **kw):
1336 self.add('command', cnf or kw)
1337 def add_radiobutton(self, cnf={}, **kw):
1338 self.add('radiobutton', cnf or kw)
1339 def add_separator(self, cnf={}, **kw):
1340 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001341 def insert(self, index, itemType, cnf={}, **kw):
1342 apply(self.tk.call, (self._w, 'insert', index, itemType)
1343 + self._options(cnf, kw))
1344 def insert_cascade(self, index, cnf={}, **kw):
1345 self.insert(index, 'cascade', cnf or kw)
1346 def insert_checkbutton(self, index, cnf={}, **kw):
1347 self.insert(index, 'checkbutton', cnf or kw)
1348 def insert_command(self, index, cnf={}, **kw):
1349 self.insert(index, 'command', cnf or kw)
1350 def insert_radiobutton(self, index, cnf={}, **kw):
1351 self.insert(index, 'radiobutton', cnf or kw)
1352 def insert_separator(self, index, cnf={}, **kw):
1353 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001354 def delete(self, index1, index2=None):
1355 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001356 def entryconfig(self, index, cnf=None, **kw):
1357 if cnf is None and not kw:
1358 cnf = {}
1359 for x in self.tk.split(apply(self.tk.call,
1360 (self._w, 'entryconfigure', index))):
1361 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1362 return cnf
1363 if type(cnf) == StringType and not kw:
1364 x = self.tk.split(apply(self.tk.call,
1365 (self._w, 'entryconfigure', index, '-'+cnf)))
1366 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001367 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001368 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001369 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001370 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001371 i = self.tk.call(self._w, 'index', index)
1372 if i == 'none': return None
1373 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001374 def invoke(self, index):
1375 return self.tk.call(self._w, 'invoke', index)
1376 def post(self, x, y):
1377 self.tk.call(self._w, 'post', x, y)
1378 def unpost(self):
1379 self.tk.call(self._w, 'unpost')
1380 def yposition(self, index):
1381 return self.tk.getint(self.tk.call(
1382 self._w, 'yposition', index))
1383
1384class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001385 def __init__(self, master=None, cnf={}, **kw):
1386 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001387
1388class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001389 def __init__(self, master=None, cnf={}, **kw):
1390 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001391
1392class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001393 def __init__(self, master=None, cnf={}, **kw):
1394 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001395 def deselect(self):
1396 self.tk.call(self._w, 'deselect')
1397 def flash(self):
1398 self.tk.call(self._w, 'flash')
1399 def invoke(self):
1400 self.tk.call(self._w, 'invoke')
1401 def select(self):
1402 self.tk.call(self._w, 'select')
1403
1404class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001405 def __init__(self, master=None, cnf={}, **kw):
1406 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001407 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001408 value = self.tk.call(self._w, 'get')
1409 try:
1410 return self.tk.getint(value)
1411 except TclError:
1412 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001413 def set(self, value):
1414 self.tk.call(self._w, 'set', value)
1415
1416class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001417 def __init__(self, master=None, cnf={}, **kw):
1418 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001419 def activate(self, index):
1420 self.tk.call(self._w, 'activate', index)
1421 def delta(self, deltax, deltay):
1422 return self.getdouble(self.tk.call(
1423 self._w, 'delta', deltax, deltay))
1424 def fraction(self, x, y):
1425 return self.getdouble(self.tk.call(
1426 self._w, 'fraction', x, y))
1427 def identify(self, x, y):
1428 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001429 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001430 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001431 def set(self, *args):
1432 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001433
1434class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001435 def __init__(self, master=None, cnf={}, **kw):
1436 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001437 def bbox(self, *args):
1438 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001439 def tk_textSelectTo(self, index):
1440 self.tk.call('tk_textSelectTo', self._w, index)
1441 def tk_textBackspace(self):
1442 self.tk.call('tk_textBackspace', self._w)
1443 def tk_textIndexCloser(self, a, b, c):
1444 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1445 def tk_textResetAnchor(self, index):
1446 self.tk.call('tk_textResetAnchor', self._w, index)
1447 def compare(self, index1, op, index2):
1448 return self.tk.getboolean(self.tk.call(
1449 self._w, 'compare', index1, op, index2))
1450 def debug(self, boolean=None):
1451 return self.tk.getboolean(self.tk.call(
1452 self._w, 'debug', boolean))
1453 def delete(self, index1, index2=None):
1454 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001455 def dlineinfo(self, index):
1456 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001457 def get(self, index1, index2=None):
1458 return self.tk.call(self._w, 'get', index1, index2)
1459 def index(self, index):
1460 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001461 def insert(self, index, chars, *args):
1462 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001463 def mark_gravity(self, markName, direction=None):
1464 return apply(self.tk.call,
1465 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001466 def mark_names(self):
1467 return self.tk.splitlist(self.tk.call(
1468 self._w, 'mark', 'names'))
1469 def mark_set(self, markName, index):
1470 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001471 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001472 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001473 def scan_mark(self, x, y):
1474 self.tk.call(self._w, 'scan', 'mark', x, y)
1475 def scan_dragto(self, x, y):
1476 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001477 def search(self, pattern, index, stopindex=None,
1478 forwards=None, backwards=None, exact=None,
1479 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001480 args = [self._w, 'search']
1481 if forwards: args.append('-forwards')
1482 if backwards: args.append('-backwards')
1483 if exact: args.append('-exact')
1484 if regexp: args.append('-regexp')
1485 if nocase: args.append('-nocase')
1486 if count: args.append('-count'); args.append(count)
1487 if pattern[0] == '-': args.append('--')
1488 args.append(pattern)
1489 args.append(index)
1490 if stopindex: args.append(stopindex)
1491 return apply(self.tk.call, tuple(args))
1492 def see(self, index):
1493 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001494 def tag_add(self, tagName, index1, index2=None):
1495 self.tk.call(
1496 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001497 def tag_unbind(self, tagName, sequence):
1498 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001499 def tag_bind(self, tagName, sequence, func, add=None):
1500 return self._bind((self._w, 'tag', 'bind', tagName),
1501 sequence, func, add)
1502 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001503 if option[:1] != '-':
1504 option = '-' + option
1505 if option[-1:] == '_':
1506 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001507 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001508 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001509 if type(cnf) == StringType:
1510 x = self.tk.split(self.tk.call(
1511 self._w, 'tag', 'configure', tagName, '-'+cnf))
1512 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001513 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001514 (self._w, 'tag', 'configure', tagName)
1515 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001516 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001517 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001518 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001519 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001520 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001521 def tag_names(self, index=None):
1522 return self.tk.splitlist(
1523 self.tk.call(self._w, 'tag', 'names', index))
1524 def tag_nextrange(self, tagName, index1, index2=None):
1525 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001526 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001527 def tag_raise(self, tagName, aboveThis=None):
1528 self.tk.call(
1529 self._w, 'tag', 'raise', tagName, aboveThis)
1530 def tag_ranges(self, tagName):
1531 return self.tk.splitlist(self.tk.call(
1532 self._w, 'tag', 'ranges', tagName))
1533 def tag_remove(self, tagName, index1, index2=None):
1534 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001535 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001536 def window_cget(self, index, option):
1537 return self.tk.call(self._w, 'window', 'cget', index, option)
1538 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001539 if type(cnf) == StringType:
1540 x = self.tk.split(self.tk.call(
1541 self._w, 'window', 'configure',
1542 index, '-'+cnf))
1543 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001544 apply(self.tk.call,
1545 (self._w, 'window', 'configure', index)
1546 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001547 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001548 def window_create(self, index, cnf={}, **kw):
1549 apply(self.tk.call,
1550 (self._w, 'window', 'create', index)
1551 + self._options(cnf, kw))
1552 def window_names(self):
1553 return self.tk.splitlist(
1554 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001555 def xview(self, *what):
1556 if not what:
1557 return self._getdoubles(self.tk.call(self._w, 'xview'))
1558 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001559 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001560 if not what:
1561 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001562 apply(self.tk.call, (self._w, 'yview')+what)
1563 def yview_pickplace(self, *what):
1564 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001565
Guido van Rossum28574b51996-10-21 15:16:51 +00001566class _setit:
1567 def __init__(self, var, value):
1568 self.__value = value
1569 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001570 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001571 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001572
1573class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001574 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001575 kw = {"borderwidth": 2, "textvariable": variable,
1576 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1577 "highlightthickness": 2}
1578 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001579 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001580 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1581 self.menuname = menu._w
1582 menu.add_command(label=value, command=_setit(variable, value))
1583 for v in values:
1584 menu.add_command(label=v, command=_setit(variable, v))
1585 self["menu"] = menu
1586
1587 def __getitem__(self, name):
1588 if name == 'menu':
1589 return self.__menu
1590 return Widget.__getitem__(self, name)
1591
1592 def destroy(self):
1593 Menubutton.destroy(self)
1594 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001595
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001596class Image:
1597 def __init__(self, imgtype, name=None, cnf={}, **kw):
1598 self.name = None
1599 master = _default_root
1600 if not master: raise RuntimeError, 'Too early to create image'
1601 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001602 if not name:
1603 name = `id(self)`
1604 # The following is needed for systems where id(x)
1605 # can return a negative number, such as Linux/m68k:
1606 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001607 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1608 elif kw: cnf = kw
1609 options = ()
1610 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001611 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001612 v = self._register(v)
1613 options = options + ('-'+k, v)
1614 apply(self.tk.call,
1615 ('image', 'create', imgtype, name,) + options)
1616 self.name = name
1617 def __str__(self): return self.name
1618 def __del__(self):
1619 if self.name:
1620 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001621 def __setitem__(self, key, value):
1622 self.tk.call(self.name, 'configure', '-'+key, value)
1623 def __getitem__(self, key):
1624 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001625 def config(self, **kw):
1626 res = ()
1627 for k, v in _cnfmerge(kw).items():
1628 if v is not None:
1629 if k[-1] == '_': k = k[:-1]
1630 if callable(v):
1631 v = self._register(v)
1632 res = res + ('-'+k, v)
1633 apply(self.tk.call, (self.name, 'config') + res)
1634 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001635 def height(self):
1636 return self.tk.getint(
1637 self.tk.call('image', 'height', self.name))
1638 def type(self):
1639 return self.tk.call('image', 'type', self.name)
1640 def width(self):
1641 return self.tk.getint(
1642 self.tk.call('image', 'width', self.name))
1643
1644class PhotoImage(Image):
1645 def __init__(self, name=None, cnf={}, **kw):
1646 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001647 def blank(self):
1648 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001649 def cget(self, option):
1650 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001651 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001652 def __getitem__(self, key):
1653 return self.tk.call(self.name, 'cget', '-' + key)
1654 def copy(self):
1655 destImage = PhotoImage()
1656 self.tk.call(destImage, 'copy', self.name)
1657 return destImage
1658 def zoom(self,x,y=''):
1659 destImage = PhotoImage()
1660 if y=='': y=x
1661 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1662 return destImage
1663 def subsample(self,x,y=''):
1664 destImage = PhotoImage()
1665 if y=='': y=x
1666 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1667 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001668 def get(self, x, y):
1669 return self.tk.call(self.name, 'get', x, y)
1670 def put(self, data, to=None):
1671 args = (self.name, 'put', data)
1672 if to:
1673 args = args + to
1674 apply(self.tk.call, args)
1675 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001676 def write(self, filename, format=None, from_coords=None):
1677 args = (self.name, 'write', filename)
1678 if format:
1679 args = args + ('-format', format)
1680 if from_coords:
1681 args = args + ('-from',) + tuple(from_coords)
1682 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001683
1684class BitmapImage(Image):
1685 def __init__(self, name=None, cnf={}, **kw):
1686 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1687
1688def image_names(): return _default_root.tk.call('image', 'names')
1689def image_types(): return _default_root.tk.call('image', 'types')
1690
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001691######################################################################
1692# Extensions:
1693
1694class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001695 def __init__(self, master=None, cnf={}, **kw):
1696 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001697 self.bind('<Any-Enter>', self.tkButtonEnter)
1698 self.bind('<Any-Leave>', self.tkButtonLeave)
1699 self.bind('<1>', self.tkButtonDown)
1700 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001701
1702class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001703 def __init__(self, master=None, cnf={}, **kw):
1704 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001705 self.bind('<Any-Enter>', self.tkButtonEnter)
1706 self.bind('<Any-Leave>', self.tkButtonLeave)
1707 self.bind('<1>', self.tkButtonDown)
1708 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1709 self['fg'] = self['bg']
1710 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001711
Guido van Rossumc417ef81996-08-21 23:38:59 +00001712######################################################################
1713# Test:
1714
1715def _test():
1716 root = Tk()
1717 label = Label(root, text="Proof-of-existence test for Tk")
1718 label.pack()
1719 test = Button(root, text="Click me!",
1720 command=lambda root=root: root.test.config(
1721 text="[%s]" % root.test['text']))
1722 test.pack()
1723 root.test = test
1724 quit = Button(root, text="QUIT", command=root.destroy)
1725 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001726 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001727 root.mainloop()
1728
1729if __name__ == '__main__':
1730 _test()
1731
Guido van Rossum37dcab11996-05-16 16:00:19 +00001732
1733# Emacs cruft
1734# Local Variables:
1735# py-indent-offset: 8
1736# End: