blob: d567467fa551b0d226ca822a8f54e19103d82d24 [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
893 if cnf.has_key('name'):
894 name = cnf['name']
895 del cnf['name']
896 else:
897 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +0000898 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000899 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +0000900 self._w = '.' + name
901 else:
902 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000903 self.children = {}
904 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000905 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000906 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000907 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
908 if kw:
909 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000910 self.widgetName = widgetName
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000911 Widget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000912 classes = []
913 for k in cnf.keys():
914 if type(k) is ClassType:
915 classes.append((k, cnf[k]))
916 del cnf[k]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000917 apply(self.tk.call,
918 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +0000919 for k, v in classes:
920 k.config(self, v)
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000921 def config(self, cnf=None, **kw):
922 # XXX ought to generalize this so tag_config etc. can use it
923 if kw:
924 cnf = _cnfmerge((cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000925 elif cnf:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000926 cnf = _cnfmerge(cnf)
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000927 if cnf is None:
928 cnf = {}
929 for x in self.tk.split(
930 self.tk.call(self._w, 'configure')):
931 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
932 return cnf
Guido van Rossum37dcab11996-05-16 16:00:19 +0000933 if type(cnf) is StringType:
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000934 x = self.tk.split(self.tk.call(
935 self._w, 'configure', '-'+cnf))
936 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +0000937 apply(self.tk.call, (self._w, 'configure')
938 + self._options(cnf))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000939 configure = config
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000940 def cget(self, key):
Guido van Rossum37dcab11996-05-16 16:00:19 +0000941 return self.tk.call(self._w, 'cget', '-' + key)
Guido van Rossum422cc7f1996-05-21 20:30:07 +0000942 __getitem__ = cget
Guido van Rossum18468821994-06-20 07:49:28 +0000943 def __setitem__(self, key, value):
944 Widget.config(self, {key: value})
Guido van Rossum9b68fd91994-06-23 07:40:14 +0000945 def keys(self):
946 return map(lambda x: x[0][1:],
947 self.tk.split(self.tk.call(self._w, 'configure')))
Guido van Rossum18468821994-06-20 07:49:28 +0000948 def __str__(self):
949 return self._w
Guido van Rossum45853db1994-06-20 12:19:19 +0000950 def destroy(self):
951 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +0000952 if self.master.children.has_key(self._name):
953 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +0000954 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000955 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +0000956 def _do(self, name, args=()):
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000957 return apply(self.tk.call, (self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +0000958
959class Toplevel(Widget, Wm):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000960 def __init__(self, master=None, cnf={}, **kw):
961 if kw:
962 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +0000963 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +0000964 for wmkey in ['screen', 'class_', 'class', 'visual',
965 'colormap']:
966 if cnf.has_key(wmkey):
967 val = cnf[wmkey]
968 # TBD: a hack needed because some keys
969 # are not valid as keyword arguments
970 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
971 else: opt = '-'+wmkey
972 extra = extra + (opt, val)
973 del cnf[wmkey]
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000974 Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +0000975 root = self._root()
976 self.iconname(root.iconname())
977 self.title(root.title())
Guido van Rossum18468821994-06-20 07:49:28 +0000978
979class Button(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000980 def __init__(self, master=None, cnf={}, **kw):
981 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000982 def tkButtonEnter(self, *dummy):
983 self.tk.call('tkButtonEnter', self._w)
984 def tkButtonLeave(self, *dummy):
985 self.tk.call('tkButtonLeave', self._w)
986 def tkButtonDown(self, *dummy):
987 self.tk.call('tkButtonDown', self._w)
988 def tkButtonUp(self, *dummy):
989 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +0000990 def tkButtonInvoke(self, *dummy):
991 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000992 def flash(self):
993 self.tk.call(self._w, 'flash')
994 def invoke(self):
995 self.tk.call(self._w, 'invoke')
996
997# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000998# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +0000999def AtEnd():
1000 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001001def AtInsert(*args):
1002 s = 'insert'
1003 for a in args:
1004 if a: s = s + (' ' + a)
1005 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001006def AtSelFirst():
1007 return 'sel.first'
1008def AtSelLast():
1009 return 'sel.last'
1010def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001011 if y is None:
1012 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001013 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001014 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001015
1016class Canvas(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001017 def __init__(self, master=None, cnf={}, **kw):
1018 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001019 def addtag(self, *args):
1020 self._do('addtag', args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001021 def addtag_above(self, newtag, tagOrId):
1022 self.addtag(newtag, 'above', tagOrId)
1023 def addtag_all(self, newtag):
1024 self.addtag(newtag, 'all')
1025 def addtag_below(self, newtag, tagOrId):
1026 self.addtag(newtag, 'below', tagOrId)
1027 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1028 self.addtag(newtag, 'closest', x, y, halo, start)
1029 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1030 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1031 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1032 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1033 def addtag_withtag(self, newtag, tagOrId):
1034 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001035 def bbox(self, *args):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001036 return self._getints(self._do('bbox', args)) or None
Guido van Rossumef8f8811994-08-08 12:47:33 +00001037 def tag_unbind(self, tagOrId, sequence):
1038 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001039 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +00001040 return self._bind((self._w, 'bind', tagOrId),
Guido van Rossum37dcab11996-05-16 16:00:19 +00001041 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001042 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001043 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001044 self._w, 'canvasx', screenx, gridspacing))
1045 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001046 return self.tk.getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001047 self._w, 'canvasy', screeny, gridspacing))
1048 def coords(self, *args):
Guido van Rossumc8b47911996-07-30 16:31:32 +00001049 return map(self.tk.getdouble,
Guido van Rossum9afdabf1996-07-30 20:16:21 +00001050 self.tk.splitlist(self._do('coords', args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001051 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum08a40381994-06-21 11:44:21 +00001052 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001053 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001054 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001055 args = args[:-1]
1056 else:
1057 cnf = {}
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001058 return self.tk.getint(apply(
1059 self.tk.call,
1060 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001061 + args + self._options(cnf, kw)))
1062 def create_arc(self, *args, **kw):
1063 return self._create('arc', args, kw)
1064 def create_bitmap(self, *args, **kw):
1065 return self._create('bitmap', args, kw)
1066 def create_image(self, *args, **kw):
1067 return self._create('image', args, kw)
1068 def create_line(self, *args, **kw):
1069 return self._create('line', args, kw)
1070 def create_oval(self, *args, **kw):
1071 return self._create('oval', args, kw)
1072 def create_polygon(self, *args, **kw):
1073 return self._create('polygon', args, kw)
1074 def create_rectangle(self, *args, **kw):
1075 return self._create('rectangle', args, kw)
1076 def create_text(self, *args, **kw):
1077 return self._create('text', args, kw)
1078 def create_window(self, *args, **kw):
1079 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001080 def dchars(self, *args):
1081 self._do('dchars', args)
1082 def delete(self, *args):
1083 self._do('delete', args)
1084 def dtag(self, *args):
1085 self._do('dtag', args)
1086 def find(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001087 return self._getints(self._do('find', args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001088 def find_above(self, tagOrId):
1089 return self.find('above', tagOrId)
1090 def find_all(self):
1091 return self.find('all')
1092 def find_below(self, tagOrId):
1093 return self.find('below', tagOrId)
1094 def find_closest(self, x, y, halo=None, start=None):
1095 return self.find('closest', x, y, halo, start)
1096 def find_enclosed(self, x1, y1, x2, y2):
1097 return self.find('enclosed', x1, y1, x2, y2)
1098 def find_overlapping(self, x1, y1, x2, y2):
1099 return self.find('overlapping', x1, y1, x2, y2)
1100 def find_withtag(self, tagOrId):
1101 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001102 def focus(self, *args):
1103 return self._do('focus', args)
1104 def gettags(self, *args):
1105 return self.tk.splitlist(self._do('gettags', args))
1106 def icursor(self, *args):
1107 self._do('icursor', args)
1108 def index(self, *args):
1109 return self.tk.getint(self._do('index', args))
1110 def insert(self, *args):
1111 self._do('insert', args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001112 def itemcget(self, tagOrId, option):
1113 return self._do('itemcget', (tagOrId, '-'+option))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001114 def itemconfig(self, tagOrId, cnf=None, **kw):
1115 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001116 cnf = {}
1117 for x in self.tk.split(
1118 self._do('itemconfigure', (tagOrId))):
1119 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1120 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001121 if type(cnf) == StringType and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001122 x = self.tk.split(self._do('itemconfigure',
1123 (tagOrId, '-'+cnf,)))
1124 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001125 self._do('itemconfigure', (tagOrId,)
1126 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001127 itemconfigure = itemconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001128 def lower(self, *args):
1129 self._do('lower', args)
1130 def move(self, *args):
1131 self._do('move', args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001132 def postscript(self, cnf={}, **kw):
1133 return self._do('postscript', self._options(cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001134 def tkraise(self, *args):
1135 self._do('raise', args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +00001136 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +00001137 def scale(self, *args):
1138 self._do('scale', args)
1139 def scan_mark(self, x, y):
1140 self.tk.call(self._w, 'scan', 'mark', x, y)
1141 def scan_dragto(self, x, y):
1142 self.tk.call(self._w, 'scan', 'dragto', x, y)
1143 def select_adjust(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001144 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001145 def select_clear(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001146 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00001147 def select_from(self, tagOrId, index):
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001148 self.tk.call(self._w, 'select', 'set', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001149 def select_item(self):
1150 self.tk.call(self._w, 'select', 'item')
1151 def select_to(self, tagOrId, index):
Guido van Rossum08a40381994-06-21 11:44:21 +00001152 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00001153 def type(self, tagOrId):
Guido van Rossum08a40381994-06-21 11:44:21 +00001154 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001155 def xview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001156 if not args:
1157 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001158 apply(self.tk.call, (self._w, 'xview')+args)
1159 def yview(self, *args):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001160 if not args:
1161 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001162 apply(self.tk.call, (self._w, 'yview')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001163
1164class Checkbutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001165 def __init__(self, master=None, cnf={}, **kw):
1166 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001167 def deselect(self):
1168 self.tk.call(self._w, 'deselect')
1169 def flash(self):
1170 self.tk.call(self._w, 'flash')
1171 def invoke(self):
1172 self.tk.call(self._w, 'invoke')
1173 def select(self):
1174 self.tk.call(self._w, 'select')
1175 def toggle(self):
1176 self.tk.call(self._w, 'toggle')
1177
1178class Entry(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001179 def __init__(self, master=None, cnf={}, **kw):
1180 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001181 def delete(self, first, last=None):
1182 self.tk.call(self._w, 'delete', first, last)
1183 def get(self):
1184 return self.tk.call(self._w, 'get')
1185 def icursor(self, index):
1186 self.tk.call(self._w, 'icursor', index)
1187 def index(self, index):
1188 return self.tk.getint(self.tk.call(
1189 self._w, 'index', index))
1190 def insert(self, index, string):
1191 self.tk.call(self._w, 'insert', index, string)
1192 def scan_mark(self, x):
1193 self.tk.call(self._w, 'scan', 'mark', x)
1194 def scan_dragto(self, x):
1195 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001196 def selection_adjust(self, index):
1197 self.tk.call(self._w, 'selection', 'adjust', index)
1198 select_adjust = selection_adjust
1199 def selection_clear(self):
1200 self.tk.call(self._w, 'selection', 'clear')
1201 select_clear = selection_clear
1202 def selection_from(self, index):
Guido van Rossum42b78e61996-09-06 14:20:23 +00001203 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001204 select_from = selection_from
1205 def selection_present(self):
Guido van Rossum1d59df21995-08-11 14:21:06 +00001206 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001207 self.tk.call(self._w, 'selection', 'present'))
1208 select_present = selection_present
1209 def selection_range(self, start, end):
1210 self.tk.call(self._w, 'selection', 'range', start, end)
1211 select_range = selection_range
1212 def selection_to(self, index):
1213 self.tk.call(self._w, 'selection', 'to', index)
1214 select_to = selection_to
1215 def xview(self, index):
1216 self.tk.call(self._w, 'xview', index)
1217 def xview_moveto(self, fraction):
1218 self.tk.call(self._w, 'xview', 'moveto', fraction)
1219 def xview_scroll(self, number, what):
1220 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00001221
1222class Frame(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001223 def __init__(self, master=None, cnf={}, **kw):
1224 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001225 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00001226 if cnf.has_key('class_'):
1227 extra = ('-class', cnf['class_'])
1228 del cnf['class_']
1229 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00001230 extra = ('-class', cnf['class'])
1231 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001232 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00001233
1234class Label(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001235 def __init__(self, master=None, cnf={}, **kw):
1236 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001237
Guido van Rossum18468821994-06-20 07:49:28 +00001238class Listbox(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001239 def __init__(self, master=None, cnf={}, **kw):
1240 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00001241 def activate(self, index):
1242 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001243 def bbox(self, *args):
1244 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001245 def curselection(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001246 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00001247 return self.tk.splitlist(self.tk.call(
1248 self._w, 'curselection'))
1249 def delete(self, first, last=None):
1250 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001251 def get(self, first, last=None):
1252 if last:
1253 return self.tk.splitlist(self.tk.call(
1254 self._w, 'get', first, last))
1255 else:
1256 return self.tk.call(self._w, 'get', first)
Guido van Rossum18468821994-06-20 07:49:28 +00001257 def insert(self, index, *elements):
1258 apply(self.tk.call,
1259 (self._w, 'insert', index) + elements)
1260 def nearest(self, y):
1261 return self.tk.getint(self.tk.call(
1262 self._w, 'nearest', y))
1263 def scan_mark(self, x, y):
1264 self.tk.call(self._w, 'scan', 'mark', x, y)
1265 def scan_dragto(self, x, y):
1266 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001267 def see(self, index):
1268 self.tk.call(self._w, 'see', index)
1269 def index(self, index):
1270 i = self.tk.call(self._w, 'index', index)
1271 if i == 'none': return None
1272 return self.tk.getint(i)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001273 def select_anchor(self, index):
1274 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001275 selection_anchor = select_anchor
Guido van Rossum37dcab11996-05-16 16:00:19 +00001276 def select_clear(self, first, last=None):
1277 self.tk.call(self._w,
1278 'selection', 'clear', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001279 selection_clear = select_clear
Guido van Rossum37dcab11996-05-16 16:00:19 +00001280 def select_includes(self, index):
1281 return self.tk.getboolean(self.tk.call(
1282 self._w, 'selection', 'includes', index))
Guido van Rossum764d6c71997-02-14 16:21:16 +00001283 selection_includes = select_includes
Guido van Rossum37dcab11996-05-16 16:00:19 +00001284 def select_set(self, first, last=None):
1285 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum764d6c71997-02-14 16:21:16 +00001286 selection_set = select_set
Guido van Rossum18468821994-06-20 07:49:28 +00001287 def size(self):
1288 return self.tk.getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001289 def xview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001290 if not what:
1291 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001292 apply(self.tk.call, (self._w, 'xview')+what)
1293 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001294 if not what:
1295 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001296 apply(self.tk.call, (self._w, 'yview')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001297
1298class Menu(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001299 def __init__(self, master=None, cnf={}, **kw):
1300 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001301 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00001302 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00001303 def tk_mbPost(self):
1304 self.tk.call('tk_mbPost', self._w)
1305 def tk_mbUnpost(self):
1306 self.tk.call('tk_mbUnpost')
1307 def tk_traverseToMenu(self, char):
1308 self.tk.call('tk_traverseToMenu', self._w, char)
1309 def tk_traverseWithinMenu(self, char):
1310 self.tk.call('tk_traverseWithinMenu', self._w, char)
1311 def tk_getMenuButtons(self):
1312 return self.tk.call('tk_getMenuButtons', self._w)
1313 def tk_nextMenu(self, count):
1314 self.tk.call('tk_nextMenu', count)
1315 def tk_nextMenuEntry(self, count):
1316 self.tk.call('tk_nextMenuEntry', count)
1317 def tk_invokeMenu(self):
1318 self.tk.call('tk_invokeMenu', self._w)
1319 def tk_firstMenu(self):
1320 self.tk.call('tk_firstMenu', self._w)
1321 def tk_mbButtonDown(self):
1322 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001323 def tk_popup(self, x, y, entry=""):
1324 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00001325 def activate(self, index):
1326 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001327 def add(self, itemType, cnf={}, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00001328 apply(self.tk.call, (self._w, 'add', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001329 + self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00001330 def add_cascade(self, cnf={}, **kw):
1331 self.add('cascade', cnf or kw)
1332 def add_checkbutton(self, cnf={}, **kw):
1333 self.add('checkbutton', cnf or kw)
1334 def add_command(self, cnf={}, **kw):
1335 self.add('command', cnf or kw)
1336 def add_radiobutton(self, cnf={}, **kw):
1337 self.add('radiobutton', cnf or kw)
1338 def add_separator(self, cnf={}, **kw):
1339 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00001340 def insert(self, index, itemType, cnf={}, **kw):
1341 apply(self.tk.call, (self._w, 'insert', index, itemType)
1342 + self._options(cnf, kw))
1343 def insert_cascade(self, index, cnf={}, **kw):
1344 self.insert(index, 'cascade', cnf or kw)
1345 def insert_checkbutton(self, index, cnf={}, **kw):
1346 self.insert(index, 'checkbutton', cnf or kw)
1347 def insert_command(self, index, cnf={}, **kw):
1348 self.insert(index, 'command', cnf or kw)
1349 def insert_radiobutton(self, index, cnf={}, **kw):
1350 self.insert(index, 'radiobutton', cnf or kw)
1351 def insert_separator(self, index, cnf={}, **kw):
1352 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001353 def delete(self, index1, index2=None):
1354 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001355 def entryconfig(self, index, cnf=None, **kw):
1356 if cnf is None and not kw:
1357 cnf = {}
1358 for x in self.tk.split(apply(self.tk.call,
1359 (self._w, 'entryconfigure', index))):
1360 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1361 return cnf
1362 if type(cnf) == StringType and not kw:
1363 x = self.tk.split(apply(self.tk.call,
1364 (self._w, 'entryconfigure', index, '-'+cnf)))
1365 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001366 apply(self.tk.call, (self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001367 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001368 entryconfigure = entryconfig
Guido van Rossum18468821994-06-20 07:49:28 +00001369 def index(self, index):
Guido van Rossum535cf0c1994-06-27 07:55:59 +00001370 i = self.tk.call(self._w, 'index', index)
1371 if i == 'none': return None
1372 return self.tk.getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00001373 def invoke(self, index):
1374 return self.tk.call(self._w, 'invoke', index)
1375 def post(self, x, y):
1376 self.tk.call(self._w, 'post', x, y)
1377 def unpost(self):
1378 self.tk.call(self._w, 'unpost')
1379 def yposition(self, index):
1380 return self.tk.getint(self.tk.call(
1381 self._w, 'yposition', index))
1382
1383class Menubutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001384 def __init__(self, master=None, cnf={}, **kw):
1385 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001386
1387class Message(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001388 def __init__(self, master=None, cnf={}, **kw):
1389 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001390
1391class Radiobutton(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001392 def __init__(self, master=None, cnf={}, **kw):
1393 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001394 def deselect(self):
1395 self.tk.call(self._w, 'deselect')
1396 def flash(self):
1397 self.tk.call(self._w, 'flash')
1398 def invoke(self):
1399 self.tk.call(self._w, 'invoke')
1400 def select(self):
1401 self.tk.call(self._w, 'select')
1402
1403class Scale(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001404 def __init__(self, master=None, cnf={}, **kw):
1405 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001406 def get(self):
Guido van Rossum14957471996-10-23 14:16:28 +00001407 value = self.tk.call(self._w, 'get')
1408 try:
1409 return self.tk.getint(value)
1410 except TclError:
1411 return self.tk.getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00001412 def set(self, value):
1413 self.tk.call(self._w, 'set', value)
1414
1415class Scrollbar(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001416 def __init__(self, master=None, cnf={}, **kw):
1417 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001418 def activate(self, index):
1419 self.tk.call(self._w, 'activate', index)
1420 def delta(self, deltax, deltay):
1421 return self.getdouble(self.tk.call(
1422 self._w, 'delta', deltax, deltay))
1423 def fraction(self, x, y):
1424 return self.getdouble(self.tk.call(
1425 self._w, 'fraction', x, y))
1426 def identify(self, x, y):
1427 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00001428 def get(self):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001429 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001430 def set(self, *args):
1431 apply(self.tk.call, (self._w, 'set')+args)
Guido van Rossum18468821994-06-20 07:49:28 +00001432
1433class Text(Widget):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001434 def __init__(self, master=None, cnf={}, **kw):
1435 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001436 def bbox(self, *args):
1437 return self._getints(self._do('bbox', args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001438 def tk_textSelectTo(self, index):
1439 self.tk.call('tk_textSelectTo', self._w, index)
1440 def tk_textBackspace(self):
1441 self.tk.call('tk_textBackspace', self._w)
1442 def tk_textIndexCloser(self, a, b, c):
1443 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
1444 def tk_textResetAnchor(self, index):
1445 self.tk.call('tk_textResetAnchor', self._w, index)
1446 def compare(self, index1, op, index2):
1447 return self.tk.getboolean(self.tk.call(
1448 self._w, 'compare', index1, op, index2))
1449 def debug(self, boolean=None):
1450 return self.tk.getboolean(self.tk.call(
1451 self._w, 'debug', boolean))
1452 def delete(self, index1, index2=None):
1453 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001454 def dlineinfo(self, index):
1455 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00001456 def get(self, index1, index2=None):
1457 return self.tk.call(self._w, 'get', index1, index2)
1458 def index(self, index):
1459 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001460 def insert(self, index, chars, *args):
1461 apply(self.tk.call, (self._w, 'insert', index, chars)+args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001462 def mark_gravity(self, markName, direction=None):
1463 return apply(self.tk.call,
1464 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00001465 def mark_names(self):
1466 return self.tk.splitlist(self.tk.call(
1467 self._w, 'mark', 'names'))
1468 def mark_set(self, markName, index):
1469 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001470 def mark_unset(self, *markNames):
Guido van Rossum18468821994-06-20 07:49:28 +00001471 apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001472 def scan_mark(self, x, y):
1473 self.tk.call(self._w, 'scan', 'mark', x, y)
1474 def scan_dragto(self, x, y):
1475 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001476 def search(self, pattern, index, stopindex=None,
1477 forwards=None, backwards=None, exact=None,
1478 regexp=None, nocase=None, count=None):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001479 args = [self._w, 'search']
1480 if forwards: args.append('-forwards')
1481 if backwards: args.append('-backwards')
1482 if exact: args.append('-exact')
1483 if regexp: args.append('-regexp')
1484 if nocase: args.append('-nocase')
1485 if count: args.append('-count'); args.append(count)
1486 if pattern[0] == '-': args.append('--')
1487 args.append(pattern)
1488 args.append(index)
1489 if stopindex: args.append(stopindex)
1490 return apply(self.tk.call, tuple(args))
1491 def see(self, index):
1492 self.tk.call(self._w, 'see', index)
Guido van Rossum18468821994-06-20 07:49:28 +00001493 def tag_add(self, tagName, index1, index2=None):
1494 self.tk.call(
1495 self._w, 'tag', 'add', tagName, index1, index2)
Guido van Rossumef8f8811994-08-08 12:47:33 +00001496 def tag_unbind(self, tagName, sequence):
1497 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001498 def tag_bind(self, tagName, sequence, func, add=None):
1499 return self._bind((self._w, 'tag', 'bind', tagName),
1500 sequence, func, add)
1501 def tag_cget(self, tagName, option):
Guido van Rossum73eba251996-11-11 19:10:58 +00001502 if option[:1] != '-':
1503 option = '-' + option
1504 if option[-1:] == '_':
1505 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00001506 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001507 def tag_config(self, tagName, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001508 if type(cnf) == StringType:
1509 x = self.tk.split(self.tk.call(
1510 self._w, 'tag', 'configure', tagName, '-'+cnf))
1511 return (x[0][1:],) + x[1:]
Guido van Rossum18468821994-06-20 07:49:28 +00001512 apply(self.tk.call,
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001513 (self._w, 'tag', 'configure', tagName)
1514 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001515 tag_configure = tag_config
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001516 def tag_delete(self, *tagNames):
Guido van Rossum2a390311994-07-06 10:20:11 +00001517 apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00001518 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum97aeca11994-07-07 13:12:12 +00001519 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00001520 def tag_names(self, index=None):
1521 return self.tk.splitlist(
1522 self.tk.call(self._w, 'tag', 'names', index))
1523 def tag_nextrange(self, tagName, index1, index2=None):
1524 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00001525 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00001526 def tag_raise(self, tagName, aboveThis=None):
1527 self.tk.call(
1528 self._w, 'tag', 'raise', tagName, aboveThis)
1529 def tag_ranges(self, tagName):
1530 return self.tk.splitlist(self.tk.call(
1531 self._w, 'tag', 'ranges', tagName))
1532 def tag_remove(self, tagName, index1, index2=None):
1533 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00001534 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001535 def window_cget(self, index, option):
1536 return self.tk.call(self._w, 'window', 'cget', index, option)
1537 def window_config(self, index, cnf={}, **kw):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001538 if type(cnf) == StringType:
1539 x = self.tk.split(self.tk.call(
1540 self._w, 'window', 'configure',
1541 index, '-'+cnf))
1542 return (x[0][1:],) + x[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001543 apply(self.tk.call,
1544 (self._w, 'window', 'configure', index)
1545 + self._options(cnf, kw))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001546 window_configure = window_config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001547 def window_create(self, index, cnf={}, **kw):
1548 apply(self.tk.call,
1549 (self._w, 'window', 'create', index)
1550 + self._options(cnf, kw))
1551 def window_names(self):
1552 return self.tk.splitlist(
1553 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001554 def xview(self, *what):
1555 if not what:
1556 return self._getdoubles(self.tk.call(self._w, 'xview'))
1557 apply(self.tk.call, (self._w, 'xview')+what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001558 def yview(self, *what):
Guido van Rossum37dcab11996-05-16 16:00:19 +00001559 if not what:
1560 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001561 apply(self.tk.call, (self._w, 'yview')+what)
1562 def yview_pickplace(self, *what):
1563 apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
Guido van Rossum18468821994-06-20 07:49:28 +00001564
Guido van Rossum28574b51996-10-21 15:16:51 +00001565class _setit:
1566 def __init__(self, var, value):
1567 self.__value = value
1568 self.__var = var
Guido van Rossum28574b51996-10-21 15:16:51 +00001569 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00001570 self.__var.set(self.__value)
Guido van Rossum28574b51996-10-21 15:16:51 +00001571
1572class OptionMenu(Menubutton):
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001573 def __init__(self, master, variable, value, *values):
Guido van Rossum28574b51996-10-21 15:16:51 +00001574 kw = {"borderwidth": 2, "textvariable": variable,
1575 "indicatoron": 1, "relief": RAISED, "anchor": "c",
1576 "highlightthickness": 2}
1577 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001578 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00001579 menu = self.__menu = Menu(self, name="menu", tearoff=0)
1580 self.menuname = menu._w
1581 menu.add_command(label=value, command=_setit(variable, value))
1582 for v in values:
1583 menu.add_command(label=v, command=_setit(variable, v))
1584 self["menu"] = menu
1585
1586 def __getitem__(self, name):
1587 if name == 'menu':
1588 return self.__menu
1589 return Widget.__getitem__(self, name)
1590
1591 def destroy(self):
1592 Menubutton.destroy(self)
1593 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00001594
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001595class Image:
1596 def __init__(self, imgtype, name=None, cnf={}, **kw):
1597 self.name = None
1598 master = _default_root
1599 if not master: raise RuntimeError, 'Too early to create image'
1600 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00001601 if not name:
1602 name = `id(self)`
1603 # The following is needed for systems where id(x)
1604 # can return a negative number, such as Linux/m68k:
1605 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001606 if kw and cnf: cnf = _cnfmerge((cnf, kw))
1607 elif kw: cnf = kw
1608 options = ()
1609 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00001610 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001611 v = self._register(v)
1612 options = options + ('-'+k, v)
1613 apply(self.tk.call,
1614 ('image', 'create', imgtype, name,) + options)
1615 self.name = name
1616 def __str__(self): return self.name
1617 def __del__(self):
1618 if self.name:
1619 self.tk.call('image', 'delete', self.name)
Guido van Rossum71b1a901995-09-18 21:54:35 +00001620 def __setitem__(self, key, value):
1621 self.tk.call(self.name, 'configure', '-'+key, value)
1622 def __getitem__(self, key):
1623 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum83710131996-12-27 15:33:17 +00001624 def config(self, **kw):
1625 res = ()
1626 for k, v in _cnfmerge(kw).items():
1627 if v is not None:
1628 if k[-1] == '_': k = k[:-1]
1629 if callable(v):
1630 v = self._register(v)
1631 res = res + ('-'+k, v)
1632 apply(self.tk.call, (self.name, 'config') + res)
1633 configure = config
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001634 def height(self):
1635 return self.tk.getint(
1636 self.tk.call('image', 'height', self.name))
1637 def type(self):
1638 return self.tk.call('image', 'type', self.name)
1639 def width(self):
1640 return self.tk.getint(
1641 self.tk.call('image', 'width', self.name))
1642
1643class PhotoImage(Image):
1644 def __init__(self, name=None, cnf={}, **kw):
1645 apply(Image.__init__, (self, 'photo', name, cnf), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001646 def blank(self):
1647 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00001648 def cget(self, option):
1649 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001650 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00001651 def __getitem__(self, key):
1652 return self.tk.call(self.name, 'cget', '-' + key)
1653 def copy(self):
1654 destImage = PhotoImage()
1655 self.tk.call(destImage, 'copy', self.name)
1656 return destImage
1657 def zoom(self,x,y=''):
1658 destImage = PhotoImage()
1659 if y=='': y=x
1660 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
1661 return destImage
1662 def subsample(self,x,y=''):
1663 destImage = PhotoImage()
1664 if y=='': y=x
1665 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
1666 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00001667 def get(self, x, y):
1668 return self.tk.call(self.name, 'get', x, y)
1669 def put(self, data, to=None):
1670 args = (self.name, 'put', data)
1671 if to:
1672 args = args + to
1673 apply(self.tk.call, args)
1674 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00001675 def write(self, filename, format=None, from_coords=None):
1676 args = (self.name, 'write', filename)
1677 if format:
1678 args = args + ('-format', format)
1679 if from_coords:
1680 args = args + ('-from',) + tuple(from_coords)
1681 apply(self.tk.call, args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001682
1683class BitmapImage(Image):
1684 def __init__(self, name=None, cnf={}, **kw):
1685 apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
1686
1687def image_names(): return _default_root.tk.call('image', 'names')
1688def image_types(): return _default_root.tk.call('image', 'types')
1689
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001690######################################################################
1691# Extensions:
1692
1693class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001694 def __init__(self, master=None, cnf={}, **kw):
1695 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001696 self.bind('<Any-Enter>', self.tkButtonEnter)
1697 self.bind('<Any-Leave>', self.tkButtonLeave)
1698 self.bind('<1>', self.tkButtonDown)
1699 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001700
1701class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001702 def __init__(self, master=None, cnf={}, **kw):
1703 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001704 self.bind('<Any-Enter>', self.tkButtonEnter)
1705 self.bind('<Any-Leave>', self.tkButtonLeave)
1706 self.bind('<1>', self.tkButtonDown)
1707 self.bind('<ButtonRelease-1>', self.tkButtonUp)
1708 self['fg'] = self['bg']
1709 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00001710
Guido van Rossumc417ef81996-08-21 23:38:59 +00001711######################################################################
1712# Test:
1713
1714def _test():
1715 root = Tk()
1716 label = Label(root, text="Proof-of-existence test for Tk")
1717 label.pack()
1718 test = Button(root, text="Click me!",
1719 command=lambda root=root: root.test.config(
1720 text="[%s]" % root.test['text']))
1721 test.pack()
1722 root.test = test
1723 quit = Button(root, text="QUIT", command=root.destroy)
1724 quit.pack()
Guido van Rossum16cd3321997-05-09 00:59:43 +00001725 root.tkraise()
Guido van Rossumc417ef81996-08-21 23:38:59 +00001726 root.mainloop()
1727
1728if __name__ == '__main__':
1729 _test()
1730
Guido van Rossum37dcab11996-05-16 16:00:19 +00001731
1732# Emacs cruft
1733# Local Variables:
1734# py-indent-offset: 8
1735# End: