blob: bd36d10f586e033b9aaf369ad94ce6b84a167bb7 [file] [log] [blame]
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001"""Wrapper functions for Tcl/Tk.
2
3Tkinter provides classes which allow the display, positioning and
4control of widgets. Toplevel widgets are Tk and Toplevel. Other
5widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00006Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox
Raymond Hettingerff41c482003-04-06 09:01:11 +00007LabelFrame and PanedWindow.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00008
Raymond Hettingerff41c482003-04-06 09:01:11 +00009Properties of the widgets are specified with keyword arguments.
10Keyword arguments have the same name as the corresponding resource
Martin v. Löwis2ec36272002-10-13 10:22:08 +000011under Tk.
Guido van Rossum5917ecb2000-06-29 16:30:50 +000012
13Widgets are positioned with one of the geometry managers Place, Pack
14or Grid. These managers can be called with methods place, pack, grid
15available in every Widget.
16
Guido van Rossuma0adb922001-09-01 18:29:55 +000017Actions are bound to events by resources (e.g. keyword argument
18command) or with the method bind.
Guido van Rossum5917ecb2000-06-29 16:30:50 +000019
20Example (Hello, World):
21import Tkinter
22from Tkconstants import *
23tk = Tkinter.Tk()
24frame = Tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
25frame.pack(fill=BOTH,expand=1)
26label = Tkinter.Label(frame, text="Hello, World")
27label.pack(fill=X, expand=1)
28button = Tkinter.Button(frame,text="Exit",command=tk.destroy)
29button.pack(side=BOTTOM)
30tk.mainloop()
31"""
Guido van Rossum2dcf5291994-07-06 09:23:20 +000032
Guido van Rossum37dcab11996-05-16 16:00:19 +000033__version__ = "$Revision$"
34
Guido van Rossumf8d579c1999-01-04 18:06:45 +000035import sys
36if sys.platform == "win32":
Fredrik Lundh06d28152000-08-09 18:03:12 +000037 import FixTk # Attempt to configure Tcl/Tk without requiring PATH
Guido van Rossumf8d579c1999-01-04 18:06:45 +000038import _tkinter # If this fails your Python may not be configured for Tk
Guido van Rossum95806091997-02-15 18:33:24 +000039tkinter = _tkinter # b/w compat for export
40TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +000041from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +000042from Tkconstants import *
Guido van Rossumf0c891a1998-04-29 21:43:36 +000043try:
Fredrik Lundh06d28152000-08-09 18:03:12 +000044 import MacOS; _MacOS = MacOS; del MacOS
Guido van Rossumf0c891a1998-04-29 21:43:36 +000045except ImportError:
Fredrik Lundh06d28152000-08-09 18:03:12 +000046 _MacOS = None
Guido van Rossum18468821994-06-20 07:49:28 +000047
Martin v. Löwis8c8aa5d2002-11-26 21:39:48 +000048wantobjects = 1
Martin v. Löwisffad6332002-11-26 09:28:05 +000049
Eric S. Raymondfc170b12001-02-09 11:51:27 +000050TkVersion = float(_tkinter.TK_VERSION)
51TclVersion = float(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000052
Guido van Rossumd6615ab1997-08-05 02:35:01 +000053READABLE = _tkinter.READABLE
54WRITABLE = _tkinter.WRITABLE
55EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000056
57# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
58try: _tkinter.createfilehandler
59except AttributeError: _tkinter.createfilehandler = None
60try: _tkinter.deletefilehandler
61except AttributeError: _tkinter.deletefilehandler = None
Fredrik Lundh06d28152000-08-09 18:03:12 +000062
63
Guido van Rossum2dcf5291994-07-06 09:23:20 +000064def _flatten(tuple):
Fredrik Lundh06d28152000-08-09 18:03:12 +000065 """Internal function."""
66 res = ()
67 for item in tuple:
68 if type(item) in (TupleType, ListType):
69 res = res + _flatten(item)
70 elif item is not None:
71 res = res + (item,)
72 return res
Guido van Rossum2dcf5291994-07-06 09:23:20 +000073
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000074try: _flatten = _tkinter._flatten
75except AttributeError: pass
76
Guido van Rossum2dcf5291994-07-06 09:23:20 +000077def _cnfmerge(cnfs):
Fredrik Lundh06d28152000-08-09 18:03:12 +000078 """Internal function."""
79 if type(cnfs) is DictionaryType:
80 return cnfs
81 elif type(cnfs) in (NoneType, StringType):
82 return cnfs
83 else:
84 cnf = {}
85 for c in _flatten(cnfs):
86 try:
87 cnf.update(c)
88 except (AttributeError, TypeError), msg:
89 print "_cnfmerge: fallback due to:", msg
90 for k, v in c.items():
91 cnf[k] = v
92 return cnf
Guido van Rossum2dcf5291994-07-06 09:23:20 +000093
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000094try: _cnfmerge = _tkinter._cnfmerge
95except AttributeError: pass
96
Guido van Rossum2dcf5291994-07-06 09:23:20 +000097class Event:
Fredrik Lundh06d28152000-08-09 18:03:12 +000098 """Container for the properties of an event.
Guido van Rossum5917ecb2000-06-29 16:30:50 +000099
Fredrik Lundh06d28152000-08-09 18:03:12 +0000100 Instances of this type are generated if one of the following events occurs:
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000101
Fredrik Lundh06d28152000-08-09 18:03:12 +0000102 KeyPress, KeyRelease - for keyboard events
103 ButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse events
104 Visibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate,
105 Colormap, Gravity, Reparent, Property, Destroy, Activate,
106 Deactivate - for window events.
107
108 If a callback function for one of these events is registered
109 using bind, bind_all, bind_class, or tag_bind, the callback is
110 called with an Event as first argument. It will have the
111 following attributes (in braces are the event types for which
112 the attribute is valid):
113
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000114 serial - serial number of event
Fredrik Lundh06d28152000-08-09 18:03:12 +0000115 num - mouse button pressed (ButtonPress, ButtonRelease)
116 focus - whether the window has the focus (Enter, Leave)
117 height - height of the exposed window (Configure, Expose)
118 width - width of the exposed window (Configure, Expose)
119 keycode - keycode of the pressed key (KeyPress, KeyRelease)
120 state - state of the event as a number (ButtonPress, ButtonRelease,
121 Enter, KeyPress, KeyRelease,
122 Leave, Motion)
123 state - state as a string (Visibility)
124 time - when the event occurred
125 x - x-position of the mouse
126 y - y-position of the mouse
127 x_root - x-position of the mouse on the screen
128 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
129 y_root - y-position of the mouse on the screen
130 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
131 char - pressed character (KeyPress, KeyRelease)
132 send_event - see X/Windows documentation
133 keysym - keysym of the the event as a string (KeyPress, KeyRelease)
134 keysym_num - keysym of the event as a number (KeyPress, KeyRelease)
135 type - type of the event as a number
136 widget - widget in which the event occurred
137 delta - delta of wheel movement (MouseWheel)
138 """
139 pass
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000140
Guido van Rossumc4570481998-03-20 20:45:49 +0000141_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000142_default_root = None
143
Guido van Rossumc4570481998-03-20 20:45:49 +0000144def NoDefaultRoot():
Fredrik Lundh06d28152000-08-09 18:03:12 +0000145 """Inhibit setting of default root window.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000146
Fredrik Lundh06d28152000-08-09 18:03:12 +0000147 Call this function to inhibit that the first instance of
148 Tk is used for windows without an explicit parent window.
149 """
150 global _support_default_root
151 _support_default_root = 0
152 global _default_root
153 _default_root = None
154 del _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +0000155
Guido van Rossum45853db1994-06-20 12:19:19 +0000156def _tkerror(err):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000157 """Internal function."""
158 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000159
Guido van Rossum97aeca11994-07-07 13:12:12 +0000160def _exit(code='0'):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000161 """Internal function. Calling it will throw the exception SystemExit."""
162 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +0000163
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000164_varnum = 0
165class Variable:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000166 """Internal class. Base class to define value holders for e.g. buttons."""
167 _default = ""
168 def __init__(self, master=None):
169 """Construct a variable with an optional MASTER as master widget.
170 The variable is named PY_VAR_number in Tcl.
171 """
172 global _varnum
173 if not master:
174 master = _default_root
175 self._master = master
176 self._tk = master.tk
177 self._name = 'PY_VAR' + `_varnum`
178 _varnum = _varnum + 1
179 self.set(self._default)
180 def __del__(self):
181 """Unset the variable in Tcl."""
182 self._tk.globalunsetvar(self._name)
183 def __str__(self):
184 """Return the name of the variable in Tcl."""
185 return self._name
186 def set(self, value):
187 """Set the variable to VALUE."""
188 return self._tk.globalsetvar(self._name, value)
189 def trace_variable(self, mode, callback):
190 """Define a trace callback for the variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000191
Fredrik Lundh06d28152000-08-09 18:03:12 +0000192 MODE is one of "r", "w", "u" for read, write, undefine.
193 CALLBACK must be a function which is called when
194 the variable is read, written or undefined.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000195
Fredrik Lundh06d28152000-08-09 18:03:12 +0000196 Return the name of the callback.
197 """
198 cbname = self._master._register(callback)
199 self._tk.call("trace", "variable", self._name, mode, cbname)
200 return cbname
201 trace = trace_variable
202 def trace_vdelete(self, mode, cbname):
203 """Delete the trace callback for a variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000204
Fredrik Lundh06d28152000-08-09 18:03:12 +0000205 MODE is one of "r", "w", "u" for read, write, undefine.
206 CBNAME is the name of the callback returned from trace_variable or trace.
207 """
208 self._tk.call("trace", "vdelete", self._name, mode, cbname)
209 self._master.deletecommand(cbname)
210 def trace_vinfo(self):
211 """Return all trace callback information."""
212 return map(self._tk.split, self._tk.splitlist(
213 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000214
215class StringVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000216 """Value holder for strings variables."""
217 _default = ""
218 def __init__(self, master=None):
219 """Construct a string variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000220
Fredrik Lundh06d28152000-08-09 18:03:12 +0000221 MASTER can be given as master widget."""
222 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000223
Fredrik Lundh06d28152000-08-09 18:03:12 +0000224 def get(self):
225 """Return value of variable as string."""
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000226 value = self._tk.globalgetvar(self._name)
227 if isinstance(value, basestring):
228 return value
229 return str(value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000230
231class IntVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000232 """Value holder for integer variables."""
233 _default = 0
234 def __init__(self, master=None):
235 """Construct an integer variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000236
Fredrik Lundh06d28152000-08-09 18:03:12 +0000237 MASTER can be given as master widget."""
238 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000239
Martin v. Löwis70c3dda2003-01-22 09:17:38 +0000240 def set(self, value):
241 """Set the variable to value, converting booleans to integers."""
242 if isinstance(value, bool):
243 value = int(value)
244 return Variable.set(self, value)
245
Fredrik Lundh06d28152000-08-09 18:03:12 +0000246 def get(self):
247 """Return the value of the variable as an integer."""
248 return getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000249
250class DoubleVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000251 """Value holder for float variables."""
252 _default = 0.0
253 def __init__(self, master=None):
254 """Construct a float variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000255
Fredrik Lundh06d28152000-08-09 18:03:12 +0000256 MASTER can be given as a master widget."""
257 Variable.__init__(self, master)
258
259 def get(self):
260 """Return the value of the variable as a float."""
261 return getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000262
263class BooleanVar(Variable):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000264 """Value holder for boolean variables."""
265 _default = "false"
266 def __init__(self, master=None):
267 """Construct a boolean variable.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000268
Fredrik Lundh06d28152000-08-09 18:03:12 +0000269 MASTER can be given as a master widget."""
270 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000271
Fredrik Lundh06d28152000-08-09 18:03:12 +0000272 def get(self):
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000273 """Return the value of the variable as a bool."""
Fredrik Lundh06d28152000-08-09 18:03:12 +0000274 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000275
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000276def mainloop(n=0):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000277 """Run the main loop of Tcl."""
278 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000279
Guido van Rossum0132f691998-04-30 17:50:36 +0000280getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000281
Guido van Rossum0132f691998-04-30 17:50:36 +0000282getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000283
284def getboolean(s):
Fredrik Lundh06d28152000-08-09 18:03:12 +0000285 """Convert true and false to integer values 1 and 0."""
286 return _default_root.tk.getboolean(s)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000287
Guido van Rossum368e06b1997-11-07 20:38:49 +0000288# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000289class Misc:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000290 """Internal class.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000291
Fredrik Lundh06d28152000-08-09 18:03:12 +0000292 Base class which defines methods common for interior widgets."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000293
Fredrik Lundh06d28152000-08-09 18:03:12 +0000294 # XXX font command?
295 _tclCommands = None
296 def destroy(self):
297 """Internal function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000298
Fredrik Lundh06d28152000-08-09 18:03:12 +0000299 Delete all Tcl commands created for
300 this widget in the Tcl interpreter."""
301 if self._tclCommands is not None:
302 for name in self._tclCommands:
303 #print '- Tkinter: deleted command', name
304 self.tk.deletecommand(name)
305 self._tclCommands = None
306 def deletecommand(self, name):
307 """Internal function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000308
Fredrik Lundh06d28152000-08-09 18:03:12 +0000309 Delete the Tcl command provided in NAME."""
310 #print '- Tkinter: deleted command', name
311 self.tk.deletecommand(name)
312 try:
313 self._tclCommands.remove(name)
314 except ValueError:
315 pass
316 def tk_strictMotif(self, boolean=None):
317 """Set Tcl internal variable, whether the look and feel
318 should adhere to Motif.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000319
Fredrik Lundh06d28152000-08-09 18:03:12 +0000320 A parameter of 1 means adhere to Motif (e.g. no color
321 change if mouse passes over slider).
322 Returns the set value."""
323 return self.tk.getboolean(self.tk.call(
324 'set', 'tk_strictMotif', boolean))
325 def tk_bisque(self):
326 """Change the color scheme to light brown as used in Tk 3.6 and before."""
327 self.tk.call('tk_bisque')
328 def tk_setPalette(self, *args, **kw):
329 """Set a new color scheme for all widget elements.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000330
Fredrik Lundh06d28152000-08-09 18:03:12 +0000331 A single color as argument will cause that all colors of Tk
332 widget elements are derived from this.
333 Alternatively several keyword parameters and its associated
334 colors can be given. The following keywords are valid:
335 activeBackground, foreground, selectColor,
336 activeForeground, highlightBackground, selectBackground,
337 background, highlightColor, selectForeground,
338 disabledForeground, insertBackground, troughColor."""
339 self.tk.call(('tk_setPalette',)
340 + _flatten(args) + _flatten(kw.items()))
341 def tk_menuBar(self, *args):
342 """Do not use. Needed in Tk 3.6 and earlier."""
343 pass # obsolete since Tk 4.0
344 def wait_variable(self, name='PY_VAR'):
345 """Wait until the variable is modified.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000346
Fredrik Lundh06d28152000-08-09 18:03:12 +0000347 A parameter of type IntVar, StringVar, DoubleVar or
348 BooleanVar must be given."""
349 self.tk.call('tkwait', 'variable', name)
350 waitvar = wait_variable # XXX b/w compat
351 def wait_window(self, window=None):
352 """Wait until a WIDGET is destroyed.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000353
Fredrik Lundh06d28152000-08-09 18:03:12 +0000354 If no parameter is given self is used."""
Fred Drake132dce22000-12-12 23:11:42 +0000355 if window is None:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000356 window = self
357 self.tk.call('tkwait', 'window', window._w)
358 def wait_visibility(self, window=None):
359 """Wait until the visibility of a WIDGET changes
360 (e.g. it appears).
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000361
Fredrik Lundh06d28152000-08-09 18:03:12 +0000362 If no parameter is given self is used."""
Fred Drake132dce22000-12-12 23:11:42 +0000363 if window is None:
Fredrik Lundh06d28152000-08-09 18:03:12 +0000364 window = self
365 self.tk.call('tkwait', 'visibility', window._w)
366 def setvar(self, name='PY_VAR', value='1'):
367 """Set Tcl variable NAME to VALUE."""
368 self.tk.setvar(name, value)
369 def getvar(self, name='PY_VAR'):
370 """Return value of Tcl variable NAME."""
371 return self.tk.getvar(name)
372 getint = int
373 getdouble = float
374 def getboolean(self, s):
Martin v. Löwisbfe175c2003-04-16 19:42:51 +0000375 """Return a boolean value for Tcl boolean values true and false given as parameter."""
Fredrik Lundh06d28152000-08-09 18:03:12 +0000376 return self.tk.getboolean(s)
377 def focus_set(self):
378 """Direct input focus to this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000379
Fredrik Lundh06d28152000-08-09 18:03:12 +0000380 If the application currently does not have the focus
381 this widget will get the focus if the application gets
382 the focus through the window manager."""
383 self.tk.call('focus', self._w)
384 focus = focus_set # XXX b/w compat?
385 def focus_force(self):
386 """Direct input focus to this widget even if the
387 application does not have the focus. Use with
388 caution!"""
389 self.tk.call('focus', '-force', self._w)
390 def focus_get(self):
391 """Return the widget which has currently the focus in the
392 application.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000393
Fredrik Lundh06d28152000-08-09 18:03:12 +0000394 Use focus_displayof to allow working with several
395 displays. Return None if application does not have
396 the focus."""
397 name = self.tk.call('focus')
398 if name == 'none' or not name: return None
399 return self._nametowidget(name)
400 def focus_displayof(self):
401 """Return the widget which has currently the focus on the
402 display where this widget is located.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000403
Fredrik Lundh06d28152000-08-09 18:03:12 +0000404 Return None if the application does not have the focus."""
405 name = self.tk.call('focus', '-displayof', self._w)
406 if name == 'none' or not name: return None
407 return self._nametowidget(name)
408 def focus_lastfor(self):
409 """Return the widget which would have the focus if top level
410 for this widget gets the focus from the window manager."""
411 name = self.tk.call('focus', '-lastfor', self._w)
412 if name == 'none' or not name: return None
413 return self._nametowidget(name)
414 def tk_focusFollowsMouse(self):
415 """The widget under mouse will get automatically focus. Can not
416 be disabled easily."""
417 self.tk.call('tk_focusFollowsMouse')
418 def tk_focusNext(self):
419 """Return the next widget in the focus order which follows
420 widget which has currently the focus.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000421
Fredrik Lundh06d28152000-08-09 18:03:12 +0000422 The focus order first goes to the next child, then to
423 the children of the child recursively and then to the
424 next sibling which is higher in the stacking order. A
425 widget is omitted if it has the takefocus resource set
426 to 0."""
427 name = self.tk.call('tk_focusNext', self._w)
428 if not name: return None
429 return self._nametowidget(name)
430 def tk_focusPrev(self):
431 """Return previous widget in the focus order. See tk_focusNext for details."""
432 name = self.tk.call('tk_focusPrev', self._w)
433 if not name: return None
434 return self._nametowidget(name)
435 def after(self, ms, func=None, *args):
436 """Call function once after given time.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000437
Fredrik Lundh06d28152000-08-09 18:03:12 +0000438 MS specifies the time in milliseconds. FUNC gives the
439 function which shall be called. Additional parameters
440 are given as parameters to the function call. Return
441 identifier to cancel scheduling with after_cancel."""
442 if not func:
443 # I'd rather use time.sleep(ms*0.001)
444 self.tk.call('after', ms)
445 else:
446 # XXX Disgusting hack to clean up after calling func
447 tmp = []
448 def callit(func=func, args=args, self=self, tmp=tmp):
449 try:
Raymond Hettingerff41c482003-04-06 09:01:11 +0000450 func(*args)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000451 finally:
452 try:
453 self.deletecommand(tmp[0])
454 except TclError:
455 pass
456 name = self._register(callit)
457 tmp.append(name)
458 return self.tk.call('after', ms, name)
459 def after_idle(self, func, *args):
460 """Call FUNC once if the Tcl main loop has no event to
461 process.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000462
Fredrik Lundh06d28152000-08-09 18:03:12 +0000463 Return an identifier to cancel the scheduling with
464 after_cancel."""
Raymond Hettingerff41c482003-04-06 09:01:11 +0000465 return self.after('idle', func, *args)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000466 def after_cancel(self, id):
467 """Cancel scheduling of function identified with ID.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000468
Fredrik Lundh06d28152000-08-09 18:03:12 +0000469 Identifier returned by after or after_idle must be
470 given as first parameter."""
471 self.tk.call('after', 'cancel', id)
472 def bell(self, displayof=0):
473 """Ring a display's bell."""
474 self.tk.call(('bell',) + self._displayof(displayof))
475 # Clipboard handling:
476 def clipboard_clear(self, **kw):
477 """Clear the data in the Tk clipboard.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000478
Fredrik Lundh06d28152000-08-09 18:03:12 +0000479 A widget specified for the optional displayof keyword
480 argument specifies the target display."""
481 if not kw.has_key('displayof'): kw['displayof'] = self._w
482 self.tk.call(('clipboard', 'clear') + self._options(kw))
483 def clipboard_append(self, string, **kw):
484 """Append STRING to the Tk clipboard.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000485
Fredrik Lundh06d28152000-08-09 18:03:12 +0000486 A widget specified at the optional displayof keyword
487 argument specifies the target display. The clipboard
488 can be retrieved with selection_get."""
489 if not kw.has_key('displayof'): kw['displayof'] = self._w
490 self.tk.call(('clipboard', 'append') + self._options(kw)
491 + ('--', string))
492 # XXX grab current w/o window argument
493 def grab_current(self):
494 """Return widget which has currently the grab in this application
495 or None."""
496 name = self.tk.call('grab', 'current', self._w)
497 if not name: return None
498 return self._nametowidget(name)
499 def grab_release(self):
500 """Release grab for this widget if currently set."""
501 self.tk.call('grab', 'release', self._w)
502 def grab_set(self):
503 """Set grab for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000504
Fredrik Lundh06d28152000-08-09 18:03:12 +0000505 A grab directs all events to this and descendant
506 widgets in the application."""
507 self.tk.call('grab', 'set', self._w)
508 def grab_set_global(self):
509 """Set global grab for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000510
Fredrik Lundh06d28152000-08-09 18:03:12 +0000511 A global grab directs all events to this and
512 descendant widgets on the display. Use with caution -
513 other applications do not get events anymore."""
514 self.tk.call('grab', 'set', '-global', self._w)
515 def grab_status(self):
516 """Return None, "local" or "global" if this widget has
517 no, a local or a global grab."""
518 status = self.tk.call('grab', 'status', self._w)
519 if status == 'none': status = None
520 return status
521 def lower(self, belowThis=None):
522 """Lower this widget in the stacking order."""
523 self.tk.call('lower', self._w, belowThis)
524 def option_add(self, pattern, value, priority = None):
525 """Set a VALUE (second parameter) for an option
526 PATTERN (first parameter).
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000527
Fredrik Lundh06d28152000-08-09 18:03:12 +0000528 An optional third parameter gives the numeric priority
529 (defaults to 80)."""
530 self.tk.call('option', 'add', pattern, value, priority)
531 def option_clear(self):
532 """Clear the option database.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000533
Fredrik Lundh06d28152000-08-09 18:03:12 +0000534 It will be reloaded if option_add is called."""
535 self.tk.call('option', 'clear')
536 def option_get(self, name, className):
537 """Return the value for an option NAME for this widget
538 with CLASSNAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000539
Fredrik Lundh06d28152000-08-09 18:03:12 +0000540 Values with higher priority override lower values."""
541 return self.tk.call('option', 'get', self._w, name, className)
542 def option_readfile(self, fileName, priority = None):
543 """Read file FILENAME into the option database.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000544
Fredrik Lundh06d28152000-08-09 18:03:12 +0000545 An optional second parameter gives the numeric
546 priority."""
547 self.tk.call('option', 'readfile', fileName, priority)
548 def selection_clear(self, **kw):
549 """Clear the current X selection."""
550 if not kw.has_key('displayof'): kw['displayof'] = self._w
551 self.tk.call(('selection', 'clear') + self._options(kw))
552 def selection_get(self, **kw):
553 """Return the contents of the current X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000554
Fredrik Lundh06d28152000-08-09 18:03:12 +0000555 A keyword parameter selection specifies the name of
556 the selection and defaults to PRIMARY. A keyword
557 parameter displayof specifies a widget on the display
558 to use."""
559 if not kw.has_key('displayof'): kw['displayof'] = self._w
560 return self.tk.call(('selection', 'get') + self._options(kw))
561 def selection_handle(self, command, **kw):
562 """Specify a function COMMAND to call if the X
563 selection owned by this widget is queried by another
564 application.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000565
Fredrik Lundh06d28152000-08-09 18:03:12 +0000566 This function must return the contents of the
567 selection. The function will be called with the
568 arguments OFFSET and LENGTH which allows the chunking
569 of very long selections. The following keyword
570 parameters can be provided:
571 selection - name of the selection (default PRIMARY),
572 type - type of the selection (e.g. STRING, FILE_NAME)."""
573 name = self._register(command)
574 self.tk.call(('selection', 'handle') + self._options(kw)
575 + (self._w, name))
576 def selection_own(self, **kw):
577 """Become owner of X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000578
Fredrik Lundh06d28152000-08-09 18:03:12 +0000579 A keyword parameter selection specifies the name of
580 the selection (default PRIMARY)."""
581 self.tk.call(('selection', 'own') +
582 self._options(kw) + (self._w,))
583 def selection_own_get(self, **kw):
584 """Return owner of X selection.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000585
Fredrik Lundh06d28152000-08-09 18:03:12 +0000586 The following keyword parameter can
587 be provided:
588 selection - name of the selection (default PRIMARY),
589 type - type of the selection (e.g. STRING, FILE_NAME)."""
590 if not kw.has_key('displayof'): kw['displayof'] = self._w
591 name = self.tk.call(('selection', 'own') + self._options(kw))
592 if not name: return None
593 return self._nametowidget(name)
594 def send(self, interp, cmd, *args):
595 """Send Tcl command CMD to different interpreter INTERP to be executed."""
596 return self.tk.call(('send', interp, cmd) + args)
597 def lower(self, belowThis=None):
598 """Lower this widget in the stacking order."""
599 self.tk.call('lower', self._w, belowThis)
600 def tkraise(self, aboveThis=None):
601 """Raise this widget in the stacking order."""
602 self.tk.call('raise', self._w, aboveThis)
603 lift = tkraise
604 def colormodel(self, value=None):
605 """Useless. Not implemented in Tk."""
606 return self.tk.call('tk', 'colormodel', self._w, value)
607 def winfo_atom(self, name, displayof=0):
608 """Return integer which represents atom NAME."""
609 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
610 return getint(self.tk.call(args))
611 def winfo_atomname(self, id, displayof=0):
612 """Return name of atom with identifier ID."""
613 args = ('winfo', 'atomname') \
614 + self._displayof(displayof) + (id,)
615 return self.tk.call(args)
616 def winfo_cells(self):
617 """Return number of cells in the colormap for this widget."""
618 return getint(
619 self.tk.call('winfo', 'cells', self._w))
620 def winfo_children(self):
621 """Return a list of all widgets which are children of this widget."""
Martin v. Löwisf2041b82002-03-27 17:15:57 +0000622 result = []
623 for child in self.tk.splitlist(
624 self.tk.call('winfo', 'children', self._w)):
625 try:
626 # Tcl sometimes returns extra windows, e.g. for
627 # menus; those need to be skipped
628 result.append(self._nametowidget(child))
629 except KeyError:
630 pass
631 return result
632
Fredrik Lundh06d28152000-08-09 18:03:12 +0000633 def winfo_class(self):
634 """Return window class name of this widget."""
635 return self.tk.call('winfo', 'class', self._w)
636 def winfo_colormapfull(self):
637 """Return true if at the last color request the colormap was full."""
638 return self.tk.getboolean(
639 self.tk.call('winfo', 'colormapfull', self._w))
640 def winfo_containing(self, rootX, rootY, displayof=0):
641 """Return the widget which is at the root coordinates ROOTX, ROOTY."""
642 args = ('winfo', 'containing') \
643 + self._displayof(displayof) + (rootX, rootY)
644 name = self.tk.call(args)
645 if not name: return None
646 return self._nametowidget(name)
647 def winfo_depth(self):
648 """Return the number of bits per pixel."""
649 return getint(self.tk.call('winfo', 'depth', self._w))
650 def winfo_exists(self):
651 """Return true if this widget exists."""
652 return getint(
653 self.tk.call('winfo', 'exists', self._w))
654 def winfo_fpixels(self, number):
655 """Return the number of pixels for the given distance NUMBER
656 (e.g. "3c") as float."""
657 return getdouble(self.tk.call(
658 'winfo', 'fpixels', self._w, number))
659 def winfo_geometry(self):
660 """Return geometry string for this widget in the form "widthxheight+X+Y"."""
661 return self.tk.call('winfo', 'geometry', self._w)
662 def winfo_height(self):
663 """Return height of this widget."""
664 return getint(
665 self.tk.call('winfo', 'height', self._w))
666 def winfo_id(self):
667 """Return identifier ID for this widget."""
668 return self.tk.getint(
669 self.tk.call('winfo', 'id', self._w))
670 def winfo_interps(self, displayof=0):
671 """Return the name of all Tcl interpreters for this display."""
672 args = ('winfo', 'interps') + self._displayof(displayof)
673 return self.tk.splitlist(self.tk.call(args))
674 def winfo_ismapped(self):
675 """Return true if this widget is mapped."""
676 return getint(
677 self.tk.call('winfo', 'ismapped', self._w))
678 def winfo_manager(self):
679 """Return the window mananger name for this widget."""
680 return self.tk.call('winfo', 'manager', self._w)
681 def winfo_name(self):
682 """Return the name of this widget."""
683 return self.tk.call('winfo', 'name', self._w)
684 def winfo_parent(self):
685 """Return the name of the parent of this widget."""
686 return self.tk.call('winfo', 'parent', self._w)
687 def winfo_pathname(self, id, displayof=0):
688 """Return the pathname of the widget given by ID."""
689 args = ('winfo', 'pathname') \
690 + self._displayof(displayof) + (id,)
691 return self.tk.call(args)
692 def winfo_pixels(self, number):
693 """Rounded integer value of winfo_fpixels."""
694 return getint(
695 self.tk.call('winfo', 'pixels', self._w, number))
696 def winfo_pointerx(self):
697 """Return the x coordinate of the pointer on the root window."""
698 return getint(
699 self.tk.call('winfo', 'pointerx', self._w))
700 def winfo_pointerxy(self):
701 """Return a tuple of x and y coordinates of the pointer on the root window."""
702 return self._getints(
703 self.tk.call('winfo', 'pointerxy', self._w))
704 def winfo_pointery(self):
705 """Return the y coordinate of the pointer on the root window."""
706 return getint(
707 self.tk.call('winfo', 'pointery', self._w))
708 def winfo_reqheight(self):
709 """Return requested height of this widget."""
710 return getint(
711 self.tk.call('winfo', 'reqheight', self._w))
712 def winfo_reqwidth(self):
713 """Return requested width of this widget."""
714 return getint(
715 self.tk.call('winfo', 'reqwidth', self._w))
716 def winfo_rgb(self, color):
717 """Return tuple of decimal values for red, green, blue for
718 COLOR in this widget."""
719 return self._getints(
720 self.tk.call('winfo', 'rgb', self._w, color))
721 def winfo_rootx(self):
722 """Return x coordinate of upper left corner of this widget on the
723 root window."""
724 return getint(
725 self.tk.call('winfo', 'rootx', self._w))
726 def winfo_rooty(self):
727 """Return y coordinate of upper left corner of this widget on the
728 root window."""
729 return getint(
730 self.tk.call('winfo', 'rooty', self._w))
731 def winfo_screen(self):
732 """Return the screen name of this widget."""
733 return self.tk.call('winfo', 'screen', self._w)
734 def winfo_screencells(self):
735 """Return the number of the cells in the colormap of the screen
736 of this widget."""
737 return getint(
738 self.tk.call('winfo', 'screencells', self._w))
739 def winfo_screendepth(self):
740 """Return the number of bits per pixel of the root window of the
741 screen of this widget."""
742 return getint(
743 self.tk.call('winfo', 'screendepth', self._w))
744 def winfo_screenheight(self):
745 """Return the number of pixels of the height of the screen of this widget
746 in pixel."""
747 return getint(
748 self.tk.call('winfo', 'screenheight', self._w))
749 def winfo_screenmmheight(self):
750 """Return the number of pixels of the height of the screen of
751 this widget in mm."""
752 return getint(
753 self.tk.call('winfo', 'screenmmheight', self._w))
754 def winfo_screenmmwidth(self):
755 """Return the number of pixels of the width of the screen of
756 this widget in mm."""
757 return getint(
758 self.tk.call('winfo', 'screenmmwidth', self._w))
759 def winfo_screenvisual(self):
760 """Return one of the strings directcolor, grayscale, pseudocolor,
761 staticcolor, staticgray, or truecolor for the default
762 colormodel of this screen."""
763 return self.tk.call('winfo', 'screenvisual', self._w)
764 def winfo_screenwidth(self):
765 """Return the number of pixels of the width of the screen of
766 this widget in pixel."""
767 return getint(
768 self.tk.call('winfo', 'screenwidth', self._w))
769 def winfo_server(self):
770 """Return information of the X-Server of the screen of this widget in
771 the form "XmajorRminor vendor vendorVersion"."""
772 return self.tk.call('winfo', 'server', self._w)
773 def winfo_toplevel(self):
774 """Return the toplevel widget of this widget."""
775 return self._nametowidget(self.tk.call(
776 'winfo', 'toplevel', self._w))
777 def winfo_viewable(self):
778 """Return true if the widget and all its higher ancestors are mapped."""
779 return getint(
780 self.tk.call('winfo', 'viewable', self._w))
781 def winfo_visual(self):
782 """Return one of the strings directcolor, grayscale, pseudocolor,
783 staticcolor, staticgray, or truecolor for the
784 colormodel of this widget."""
785 return self.tk.call('winfo', 'visual', self._w)
786 def winfo_visualid(self):
787 """Return the X identifier for the visual for this widget."""
788 return self.tk.call('winfo', 'visualid', self._w)
789 def winfo_visualsavailable(self, includeids=0):
790 """Return a list of all visuals available for the screen
791 of this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000792
Fredrik Lundh06d28152000-08-09 18:03:12 +0000793 Each item in the list consists of a visual name (see winfo_visual), a
794 depth and if INCLUDEIDS=1 is given also the X identifier."""
795 data = self.tk.split(
796 self.tk.call('winfo', 'visualsavailable', self._w,
797 includeids and 'includeids' or None))
Fredrik Lundh24037f72000-08-09 19:26:47 +0000798 if type(data) is StringType:
799 data = [self.tk.split(data)]
Fredrik Lundh06d28152000-08-09 18:03:12 +0000800 return map(self.__winfo_parseitem, data)
801 def __winfo_parseitem(self, t):
802 """Internal function."""
803 return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
804 def __winfo_getint(self, x):
805 """Internal function."""
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000806 return int(x, 0)
Fredrik Lundh06d28152000-08-09 18:03:12 +0000807 def winfo_vrootheight(self):
808 """Return the height of the virtual root window associated with this
809 widget in pixels. If there is no virtual root window return the
810 height of the screen."""
811 return getint(
812 self.tk.call('winfo', 'vrootheight', self._w))
813 def winfo_vrootwidth(self):
814 """Return the width of the virtual root window associated with this
815 widget in pixel. If there is no virtual root window return the
816 width of the screen."""
817 return getint(
818 self.tk.call('winfo', 'vrootwidth', self._w))
819 def winfo_vrootx(self):
820 """Return the x offset of the virtual root relative to the root
821 window of the screen of this widget."""
822 return getint(
823 self.tk.call('winfo', 'vrootx', self._w))
824 def winfo_vrooty(self):
825 """Return the y offset of the virtual root relative to the root
826 window of the screen of this widget."""
827 return getint(
828 self.tk.call('winfo', 'vrooty', self._w))
829 def winfo_width(self):
830 """Return the width of this widget."""
831 return getint(
832 self.tk.call('winfo', 'width', self._w))
833 def winfo_x(self):
834 """Return the x coordinate of the upper left corner of this widget
835 in the parent."""
836 return getint(
837 self.tk.call('winfo', 'x', self._w))
838 def winfo_y(self):
839 """Return the y coordinate of the upper left corner of this widget
840 in the parent."""
841 return getint(
842 self.tk.call('winfo', 'y', self._w))
843 def update(self):
844 """Enter event loop until all pending events have been processed by Tcl."""
845 self.tk.call('update')
846 def update_idletasks(self):
847 """Enter event loop until all idle callbacks have been called. This
848 will update the display of windows but not process events caused by
849 the user."""
850 self.tk.call('update', 'idletasks')
851 def bindtags(self, tagList=None):
852 """Set or get the list of bindtags for this widget.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000853
Fredrik Lundh06d28152000-08-09 18:03:12 +0000854 With no argument return the list of all bindtags associated with
855 this widget. With a list of strings as argument the bindtags are
856 set to this list. The bindtags determine in which order events are
857 processed (see bind)."""
858 if tagList is None:
859 return self.tk.splitlist(
860 self.tk.call('bindtags', self._w))
861 else:
862 self.tk.call('bindtags', self._w, tagList)
863 def _bind(self, what, sequence, func, add, needcleanup=1):
864 """Internal function."""
865 if type(func) is StringType:
866 self.tk.call(what + (sequence, func))
867 elif func:
868 funcid = self._register(func, self._substitute,
869 needcleanup)
870 cmd = ('%sif {"[%s %s]" == "break"} break\n'
871 %
872 (add and '+' or '',
Martin v. Löwisc8718c12001-08-09 16:57:33 +0000873 funcid, self._subst_format_str))
Fredrik Lundh06d28152000-08-09 18:03:12 +0000874 self.tk.call(what + (sequence, cmd))
875 return funcid
876 elif sequence:
877 return self.tk.call(what + (sequence,))
878 else:
879 return self.tk.splitlist(self.tk.call(what))
880 def bind(self, sequence=None, func=None, add=None):
881 """Bind to this widget at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000882
Fredrik Lundh06d28152000-08-09 18:03:12 +0000883 SEQUENCE is a string of concatenated event
884 patterns. An event pattern is of the form
885 <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one
886 of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
887 Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
888 B3, Alt, Button4, B4, Double, Button5, B5 Triple,
889 Mod1, M1. TYPE is one of Activate, Enter, Map,
890 ButtonPress, Button, Expose, Motion, ButtonRelease
891 FocusIn, MouseWheel, Circulate, FocusOut, Property,
892 Colormap, Gravity Reparent, Configure, KeyPress, Key,
893 Unmap, Deactivate, KeyRelease Visibility, Destroy,
894 Leave and DETAIL is the button number for ButtonPress,
895 ButtonRelease and DETAIL is the Keysym for KeyPress and
896 KeyRelease. Examples are
897 <Control-Button-1> for pressing Control and mouse button 1 or
898 <Alt-A> for pressing A and the Alt key (KeyPress can be omitted).
899 An event pattern can also be a virtual event of the form
900 <<AString>> where AString can be arbitrary. This
901 event can be generated by event_generate.
902 If events are concatenated they must appear shortly
903 after each other.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000904
Fredrik Lundh06d28152000-08-09 18:03:12 +0000905 FUNC will be called if the event sequence occurs with an
906 instance of Event as argument. If the return value of FUNC is
907 "break" no further bound function is invoked.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000908
Fredrik Lundh06d28152000-08-09 18:03:12 +0000909 An additional boolean parameter ADD specifies whether FUNC will
910 be called additionally to the other bound function or whether
911 it will replace the previous function.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000912
Fredrik Lundh06d28152000-08-09 18:03:12 +0000913 Bind will return an identifier to allow deletion of the bound function with
914 unbind without memory leak.
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000915
Fredrik Lundh06d28152000-08-09 18:03:12 +0000916 If FUNC or SEQUENCE is omitted the bound function or list
917 of bound events are returned."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000918
Fredrik Lundh06d28152000-08-09 18:03:12 +0000919 return self._bind(('bind', self._w), sequence, func, add)
920 def unbind(self, sequence, funcid=None):
921 """Unbind for this widget for event SEQUENCE the
922 function identified with FUNCID."""
923 self.tk.call('bind', self._w, sequence, '')
924 if funcid:
925 self.deletecommand(funcid)
926 def bind_all(self, sequence=None, func=None, add=None):
927 """Bind to all widgets at an event SEQUENCE a call to function FUNC.
928 An additional boolean parameter ADD specifies whether FUNC will
929 be called additionally to the other bound function or whether
930 it will replace the previous function. See bind for the return value."""
931 return self._bind(('bind', 'all'), sequence, func, add, 0)
932 def unbind_all(self, sequence):
933 """Unbind for all widgets for event SEQUENCE all functions."""
934 self.tk.call('bind', 'all' , sequence, '')
935 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000936
Fredrik Lundh06d28152000-08-09 18:03:12 +0000937 """Bind to widgets with bindtag CLASSNAME at event
938 SEQUENCE a call of function FUNC. An additional
939 boolean parameter ADD specifies whether FUNC will be
940 called additionally to the other bound function or
941 whether it will replace the previous function. See bind for
942 the return value."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000943
Fredrik Lundh06d28152000-08-09 18:03:12 +0000944 return self._bind(('bind', className), sequence, func, add, 0)
945 def unbind_class(self, className, sequence):
946 """Unbind for a all widgets with bindtag CLASSNAME for event SEQUENCE
947 all functions."""
948 self.tk.call('bind', className , sequence, '')
949 def mainloop(self, n=0):
950 """Call the mainloop of Tk."""
951 self.tk.mainloop(n)
952 def quit(self):
953 """Quit the Tcl interpreter. All widgets will be destroyed."""
954 self.tk.quit()
955 def _getints(self, string):
956 """Internal function."""
957 if string:
958 return tuple(map(getint, self.tk.splitlist(string)))
959 def _getdoubles(self, string):
960 """Internal function."""
961 if string:
962 return tuple(map(getdouble, self.tk.splitlist(string)))
963 def _getboolean(self, string):
964 """Internal function."""
965 if string:
966 return self.tk.getboolean(string)
967 def _displayof(self, displayof):
968 """Internal function."""
969 if displayof:
970 return ('-displayof', displayof)
971 if displayof is None:
972 return ('-displayof', self._w)
973 return ()
974 def _options(self, cnf, kw = None):
975 """Internal function."""
976 if kw:
977 cnf = _cnfmerge((cnf, kw))
978 else:
979 cnf = _cnfmerge(cnf)
980 res = ()
981 for k, v in cnf.items():
982 if v is not None:
983 if k[-1] == '_': k = k[:-1]
984 if callable(v):
985 v = self._register(v)
986 res = res + ('-'+k, v)
987 return res
988 def nametowidget(self, name):
989 """Return the Tkinter instance of a widget identified by
990 its Tcl name NAME."""
991 w = self
992 if name[0] == '.':
993 w = w._root()
994 name = name[1:]
Fredrik Lundh06d28152000-08-09 18:03:12 +0000995 while name:
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000996 i = name.find('.')
Fredrik Lundh06d28152000-08-09 18:03:12 +0000997 if i >= 0:
998 name, tail = name[:i], name[i+1:]
999 else:
1000 tail = ''
1001 w = w.children[name]
1002 name = tail
1003 return w
1004 _nametowidget = nametowidget
1005 def _register(self, func, subst=None, needcleanup=1):
1006 """Return a newly created Tcl function. If this
1007 function is called, the Python function FUNC will
1008 be executed. An optional function SUBST can
1009 be given which will be executed before FUNC."""
1010 f = CallWrapper(func, subst, self).__call__
1011 name = `id(f)`
1012 try:
1013 func = func.im_func
1014 except AttributeError:
1015 pass
1016 try:
1017 name = name + func.__name__
1018 except AttributeError:
1019 pass
1020 self.tk.createcommand(name, f)
1021 if needcleanup:
1022 if self._tclCommands is None:
1023 self._tclCommands = []
1024 self._tclCommands.append(name)
1025 #print '+ Tkinter created command', name
1026 return name
1027 register = _register
1028 def _root(self):
1029 """Internal function."""
1030 w = self
1031 while w.master: w = w.master
1032 return w
1033 _subst_format = ('%#', '%b', '%f', '%h', '%k',
1034 '%s', '%t', '%w', '%x', '%y',
1035 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D')
Martin v. Löwisc8718c12001-08-09 16:57:33 +00001036 _subst_format_str = " ".join(_subst_format)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001037 def _substitute(self, *args):
1038 """Internal function."""
1039 if len(args) != len(self._subst_format): return args
1040 getboolean = self.tk.getboolean
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001041
Fredrik Lundh06d28152000-08-09 18:03:12 +00001042 getint = int
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001043 def getint_event(s):
1044 """Tk changed behavior in 8.4.2, returning "??" rather more often."""
1045 try:
1046 return int(s)
1047 except ValueError:
1048 return s
1049
Fredrik Lundh06d28152000-08-09 18:03:12 +00001050 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args
1051 # Missing: (a, c, d, m, o, v, B, R)
1052 e = Event()
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001053 # serial field: valid vor all events
1054 # number of button: ButtonPress and ButtonRelease events only
1055 # height field: Configure, ConfigureRequest, Create,
1056 # ResizeRequest, and Expose events only
1057 # keycode field: KeyPress and KeyRelease events only
1058 # time field: "valid for events that contain a time field"
1059 # width field: Configure, ConfigureRequest, Create, ResizeRequest,
1060 # and Expose events only
1061 # x field: "valid for events that contain a x field"
1062 # y field: "valid for events that contain a y field"
1063 # keysym as decimal: KeyPress and KeyRelease events only
1064 # x_root, y_root fields: ButtonPress, ButtonRelease, KeyPress,
1065 # KeyRelease,and Motion events
Fredrik Lundh06d28152000-08-09 18:03:12 +00001066 e.serial = getint(nsign)
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001067 e.num = getint_event(b)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001068 try: e.focus = getboolean(f)
1069 except TclError: pass
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001070 e.height = getint_event(h)
1071 e.keycode = getint_event(k)
1072 e.state = getint_event(s)
1073 e.time = getint_event(t)
1074 e.width = getint_event(w)
1075 e.x = getint_event(x)
1076 e.y = getint_event(y)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001077 e.char = A
1078 try: e.send_event = getboolean(E)
1079 except TclError: pass
1080 e.keysym = K
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001081 e.keysym_num = getint_event(N)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001082 e.type = T
1083 try:
1084 e.widget = self._nametowidget(W)
1085 except KeyError:
1086 e.widget = W
Martin v. Löwis043bbc72003-03-29 09:47:21 +00001087 e.x_root = getint_event(X)
1088 e.y_root = getint_event(Y)
Fredrik Lundha249f162000-09-07 15:05:09 +00001089 try:
1090 e.delta = getint(D)
1091 except ValueError:
1092 e.delta = 0
Fredrik Lundh06d28152000-08-09 18:03:12 +00001093 return (e,)
1094 def _report_exception(self):
1095 """Internal function."""
1096 import sys
1097 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
1098 root = self._root()
1099 root.report_callback_exception(exc, val, tb)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001100 def _configure(self, cmd, cnf, kw):
1101 """Internal function."""
1102 if kw:
1103 cnf = _cnfmerge((cnf, kw))
1104 elif cnf:
1105 cnf = _cnfmerge(cnf)
1106 if cnf is None:
1107 cnf = {}
1108 for x in self.tk.split(
1109 self.tk.call(_flatten((self._w, cmd)))):
1110 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1111 return cnf
1112 if type(cnf) is StringType:
1113 x = self.tk.split(
1114 self.tk.call(_flatten((self._w, cmd, '-'+cnf))))
1115 return (x[0][1:],) + x[1:]
1116 self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
Fredrik Lundh06d28152000-08-09 18:03:12 +00001117 # These used to be defined in Widget:
1118 def configure(self, cnf=None, **kw):
1119 """Configure resources of a widget.
Barry Warsaw107e6231998-12-15 00:44:15 +00001120
Fredrik Lundh06d28152000-08-09 18:03:12 +00001121 The values for resources are specified as keyword
1122 arguments. To get an overview about
1123 the allowed keyword arguments call the method keys.
1124 """
Martin v. Löwis6ce13152002-10-10 14:36:13 +00001125 return self._configure('configure', cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001126 config = configure
1127 def cget(self, key):
1128 """Return the resource value for a KEY given as string."""
1129 return self.tk.call(self._w, 'cget', '-' + key)
1130 __getitem__ = cget
1131 def __setitem__(self, key, value):
1132 self.configure({key: value})
1133 def keys(self):
1134 """Return a list of all resource names of this widget."""
1135 return map(lambda x: x[0][1:],
1136 self.tk.split(self.tk.call(self._w, 'configure')))
1137 def __str__(self):
1138 """Return the window path name of this widget."""
1139 return self._w
1140 # Pack methods that apply to the master
1141 _noarg_ = ['_noarg_']
1142 def pack_propagate(self, flag=_noarg_):
1143 """Set or get the status for propagation of geometry information.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001144
Fredrik Lundh06d28152000-08-09 18:03:12 +00001145 A boolean argument specifies whether the geometry information
1146 of the slaves will determine the size of this widget. If no argument
1147 is given the current setting will be returned.
1148 """
1149 if flag is Misc._noarg_:
1150 return self._getboolean(self.tk.call(
1151 'pack', 'propagate', self._w))
1152 else:
1153 self.tk.call('pack', 'propagate', self._w, flag)
1154 propagate = pack_propagate
1155 def pack_slaves(self):
1156 """Return a list of all slaves of this widget
1157 in its packing order."""
1158 return map(self._nametowidget,
1159 self.tk.splitlist(
1160 self.tk.call('pack', 'slaves', self._w)))
1161 slaves = pack_slaves
1162 # Place method that applies to the master
1163 def place_slaves(self):
1164 """Return a list of all slaves of this widget
1165 in its packing order."""
1166 return map(self._nametowidget,
1167 self.tk.splitlist(
1168 self.tk.call(
1169 'place', 'slaves', self._w)))
1170 # Grid methods that apply to the master
1171 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
1172 """Return a tuple of integer coordinates for the bounding
1173 box of this widget controlled by the geometry manager grid.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001174
Fredrik Lundh06d28152000-08-09 18:03:12 +00001175 If COLUMN, ROW is given the bounding box applies from
1176 the cell with row and column 0 to the specified
1177 cell. If COL2 and ROW2 are given the bounding box
1178 starts at that cell.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001179
Fredrik Lundh06d28152000-08-09 18:03:12 +00001180 The returned integers specify the offset of the upper left
1181 corner in the master widget and the width and height.
1182 """
1183 args = ('grid', 'bbox', self._w)
1184 if column is not None and row is not None:
1185 args = args + (column, row)
1186 if col2 is not None and row2 is not None:
1187 args = args + (col2, row2)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001188 return self._getints(self.tk.call(*args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00001189
Fredrik Lundh06d28152000-08-09 18:03:12 +00001190 bbox = grid_bbox
1191 def _grid_configure(self, command, index, cnf, kw):
1192 """Internal function."""
1193 if type(cnf) is StringType and not kw:
1194 if cnf[-1:] == '_':
1195 cnf = cnf[:-1]
1196 if cnf[:1] != '-':
1197 cnf = '-'+cnf
1198 options = (cnf,)
1199 else:
1200 options = self._options(cnf, kw)
1201 if not options:
1202 res = self.tk.call('grid',
1203 command, self._w, index)
1204 words = self.tk.splitlist(res)
1205 dict = {}
1206 for i in range(0, len(words), 2):
1207 key = words[i][1:]
1208 value = words[i+1]
1209 if not value:
1210 value = None
1211 elif '.' in value:
1212 value = getdouble(value)
1213 else:
1214 value = getint(value)
1215 dict[key] = value
1216 return dict
1217 res = self.tk.call(
1218 ('grid', command, self._w, index)
1219 + options)
1220 if len(options) == 1:
1221 if not res: return None
1222 # In Tk 7.5, -width can be a float
1223 if '.' in res: return getdouble(res)
1224 return getint(res)
1225 def grid_columnconfigure(self, index, cnf={}, **kw):
1226 """Configure column INDEX of a grid.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001227
Fredrik Lundh06d28152000-08-09 18:03:12 +00001228 Valid resources are minsize (minimum size of the column),
1229 weight (how much does additional space propagate to this column)
1230 and pad (how much space to let additionally)."""
1231 return self._grid_configure('columnconfigure', index, cnf, kw)
1232 columnconfigure = grid_columnconfigure
Martin v. Löwisdc579092001-10-13 09:33:51 +00001233 def grid_location(self, x, y):
1234 """Return a tuple of column and row which identify the cell
1235 at which the pixel at position X and Y inside the master
1236 widget is located."""
1237 return self._getints(
1238 self.tk.call(
1239 'grid', 'location', self._w, x, y)) or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00001240 def grid_propagate(self, flag=_noarg_):
1241 """Set or get the status for propagation of geometry information.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001242
Fredrik Lundh06d28152000-08-09 18:03:12 +00001243 A boolean argument specifies whether the geometry information
1244 of the slaves will determine the size of this widget. If no argument
1245 is given, the current setting will be returned.
1246 """
1247 if flag is Misc._noarg_:
1248 return self._getboolean(self.tk.call(
1249 'grid', 'propagate', self._w))
1250 else:
1251 self.tk.call('grid', 'propagate', self._w, flag)
1252 def grid_rowconfigure(self, index, cnf={}, **kw):
1253 """Configure row INDEX of a grid.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001254
Fredrik Lundh06d28152000-08-09 18:03:12 +00001255 Valid resources are minsize (minimum size of the row),
1256 weight (how much does additional space propagate to this row)
1257 and pad (how much space to let additionally)."""
1258 return self._grid_configure('rowconfigure', index, cnf, kw)
1259 rowconfigure = grid_rowconfigure
1260 def grid_size(self):
1261 """Return a tuple of the number of column and rows in the grid."""
1262 return self._getints(
1263 self.tk.call('grid', 'size', self._w)) or None
1264 size = grid_size
1265 def grid_slaves(self, row=None, column=None):
1266 """Return a list of all slaves of this widget
1267 in its packing order."""
1268 args = ()
1269 if row is not None:
1270 args = args + ('-row', row)
1271 if column is not None:
1272 args = args + ('-column', column)
1273 return map(self._nametowidget,
1274 self.tk.splitlist(self.tk.call(
1275 ('grid', 'slaves', self._w) + args)))
Guido van Rossum80f8be81997-12-02 19:51:39 +00001276
Fredrik Lundh06d28152000-08-09 18:03:12 +00001277 # Support for the "event" command, new in Tk 4.2.
1278 # By Case Roole.
Guido van Rossum80f8be81997-12-02 19:51:39 +00001279
Fredrik Lundh06d28152000-08-09 18:03:12 +00001280 def event_add(self, virtual, *sequences):
1281 """Bind a virtual event VIRTUAL (of the form <<Name>>)
1282 to an event SEQUENCE such that the virtual event is triggered
1283 whenever SEQUENCE occurs."""
1284 args = ('event', 'add', virtual) + sequences
1285 self.tk.call(args)
Guido van Rossumc2966511998-04-10 19:16:10 +00001286
Fredrik Lundh06d28152000-08-09 18:03:12 +00001287 def event_delete(self, virtual, *sequences):
1288 """Unbind a virtual event VIRTUAL from SEQUENCE."""
1289 args = ('event', 'delete', virtual) + sequences
1290 self.tk.call(args)
Guido van Rossumc2966511998-04-10 19:16:10 +00001291
Fredrik Lundh06d28152000-08-09 18:03:12 +00001292 def event_generate(self, sequence, **kw):
1293 """Generate an event SEQUENCE. Additional
1294 keyword arguments specify parameter of the event
1295 (e.g. x, y, rootx, rooty)."""
1296 args = ('event', 'generate', self._w, sequence)
1297 for k, v in kw.items():
1298 args = args + ('-%s' % k, str(v))
1299 self.tk.call(args)
1300
1301 def event_info(self, virtual=None):
1302 """Return a list of all virtual events or the information
1303 about the SEQUENCE bound to the virtual event VIRTUAL."""
1304 return self.tk.splitlist(
1305 self.tk.call('event', 'info', virtual))
1306
1307 # Image related commands
1308
1309 def image_names(self):
1310 """Return a list of all existing image names."""
1311 return self.tk.call('image', 'names')
1312
1313 def image_types(self):
1314 """Return a list of all available image types (e.g. phote bitmap)."""
1315 return self.tk.call('image', 'types')
Guido van Rossumc2966511998-04-10 19:16:10 +00001316
Guido van Rossum80f8be81997-12-02 19:51:39 +00001317
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001318class CallWrapper:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001319 """Internal class. Stores function to call when some user
1320 defined Tcl function is called e.g. after an event occurred."""
1321 def __init__(self, func, subst, widget):
1322 """Store FUNC, SUBST and WIDGET as members."""
1323 self.func = func
1324 self.subst = subst
1325 self.widget = widget
1326 def __call__(self, *args):
1327 """Apply first function SUBST to arguments, than FUNC."""
1328 try:
1329 if self.subst:
Raymond Hettingerff41c482003-04-06 09:01:11 +00001330 args = self.subst(*args)
1331 return self.func(*args)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001332 except SystemExit, msg:
1333 raise SystemExit, msg
1334 except:
1335 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +00001336
Guido van Rossume365a591998-05-01 19:48:20 +00001337
Guido van Rossum18468821994-06-20 07:49:28 +00001338class Wm:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001339 """Provides functions for the communication with the window manager."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00001340
Fredrik Lundh06d28152000-08-09 18:03:12 +00001341 def wm_aspect(self,
1342 minNumer=None, minDenom=None,
1343 maxNumer=None, maxDenom=None):
1344 """Instruct the window manager to set the aspect ratio (width/height)
1345 of this widget to be between MINNUMER/MINDENOM and MAXNUMER/MAXDENOM. Return a tuple
1346 of the actual values if no argument is given."""
1347 return self._getints(
1348 self.tk.call('wm', 'aspect', self._w,
1349 minNumer, minDenom,
1350 maxNumer, maxDenom))
1351 aspect = wm_aspect
Raymond Hettingerff41c482003-04-06 09:01:11 +00001352
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001353 def wm_attributes(self, *args):
1354 """This subcommand returns or sets platform specific attributes
Raymond Hettingerff41c482003-04-06 09:01:11 +00001355
1356 The first form returns a list of the platform specific flags and
1357 their values. The second form returns the value for the specific
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001358 option. The third form sets one or more of the values. The values
1359 are as follows:
Raymond Hettingerff41c482003-04-06 09:01:11 +00001360
1361 On Windows, -disabled gets or sets whether the window is in a
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001362 disabled state. -toolwindow gets or sets the style of the window
Raymond Hettingerff41c482003-04-06 09:01:11 +00001363 to toolwindow (as defined in the MSDN). -topmost gets or sets
1364 whether this is a topmost window (displays above all other
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001365 windows).
Raymond Hettingerff41c482003-04-06 09:01:11 +00001366
1367 On Macintosh, XXXXX
1368
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001369 On Unix, there are currently no special attribute values.
1370 """
1371 args = ('wm', 'attributes', self._w) + args
1372 return self.tk.call(args)
1373 attributes=wm_attributes
Raymond Hettingerff41c482003-04-06 09:01:11 +00001374
Fredrik Lundh06d28152000-08-09 18:03:12 +00001375 def wm_client(self, name=None):
1376 """Store NAME in WM_CLIENT_MACHINE property of this widget. Return
1377 current value."""
1378 return self.tk.call('wm', 'client', self._w, name)
1379 client = wm_client
1380 def wm_colormapwindows(self, *wlist):
1381 """Store list of window names (WLIST) into WM_COLORMAPWINDOWS property
1382 of this widget. This list contains windows whose colormaps differ from their
1383 parents. Return current list of widgets if WLIST is empty."""
1384 if len(wlist) > 1:
1385 wlist = (wlist,) # Tk needs a list of windows here
1386 args = ('wm', 'colormapwindows', self._w) + wlist
1387 return map(self._nametowidget, self.tk.call(args))
1388 colormapwindows = wm_colormapwindows
1389 def wm_command(self, value=None):
1390 """Store VALUE in WM_COMMAND property. It is the command
1391 which shall be used to invoke the application. Return current
1392 command if VALUE is None."""
1393 return self.tk.call('wm', 'command', self._w, value)
1394 command = wm_command
1395 def wm_deiconify(self):
1396 """Deiconify this widget. If it was never mapped it will not be mapped.
1397 On Windows it will raise this widget and give it the focus."""
1398 return self.tk.call('wm', 'deiconify', self._w)
1399 deiconify = wm_deiconify
1400 def wm_focusmodel(self, model=None):
1401 """Set focus model to MODEL. "active" means that this widget will claim
1402 the focus itself, "passive" means that the window manager shall give
1403 the focus. Return current focus model if MODEL is None."""
1404 return self.tk.call('wm', 'focusmodel', self._w, model)
1405 focusmodel = wm_focusmodel
1406 def wm_frame(self):
1407 """Return identifier for decorative frame of this widget if present."""
1408 return self.tk.call('wm', 'frame', self._w)
1409 frame = wm_frame
1410 def wm_geometry(self, newGeometry=None):
1411 """Set geometry to NEWGEOMETRY of the form =widthxheight+x+y. Return
1412 current value if None is given."""
1413 return self.tk.call('wm', 'geometry', self._w, newGeometry)
1414 geometry = wm_geometry
1415 def wm_grid(self,
1416 baseWidth=None, baseHeight=None,
1417 widthInc=None, heightInc=None):
1418 """Instruct the window manager that this widget shall only be
1419 resized on grid boundaries. WIDTHINC and HEIGHTINC are the width and
1420 height of a grid unit in pixels. BASEWIDTH and BASEHEIGHT are the
1421 number of grid units requested in Tk_GeometryRequest."""
1422 return self._getints(self.tk.call(
1423 'wm', 'grid', self._w,
1424 baseWidth, baseHeight, widthInc, heightInc))
1425 grid = wm_grid
1426 def wm_group(self, pathName=None):
1427 """Set the group leader widgets for related widgets to PATHNAME. Return
1428 the group leader of this widget if None is given."""
1429 return self.tk.call('wm', 'group', self._w, pathName)
1430 group = wm_group
1431 def wm_iconbitmap(self, bitmap=None):
1432 """Set bitmap for the iconified widget to BITMAP. Return
1433 the bitmap if None is given."""
1434 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
1435 iconbitmap = wm_iconbitmap
1436 def wm_iconify(self):
1437 """Display widget as icon."""
1438 return self.tk.call('wm', 'iconify', self._w)
1439 iconify = wm_iconify
1440 def wm_iconmask(self, bitmap=None):
1441 """Set mask for the icon bitmap of this widget. Return the
1442 mask if None is given."""
1443 return self.tk.call('wm', 'iconmask', self._w, bitmap)
1444 iconmask = wm_iconmask
1445 def wm_iconname(self, newName=None):
1446 """Set the name of the icon for this widget. Return the name if
1447 None is given."""
1448 return self.tk.call('wm', 'iconname', self._w, newName)
1449 iconname = wm_iconname
1450 def wm_iconposition(self, x=None, y=None):
1451 """Set the position of the icon of this widget to X and Y. Return
1452 a tuple of the current values of X and X if None is given."""
1453 return self._getints(self.tk.call(
1454 'wm', 'iconposition', self._w, x, y))
1455 iconposition = wm_iconposition
1456 def wm_iconwindow(self, pathName=None):
1457 """Set widget PATHNAME to be displayed instead of icon. Return the current
1458 value if None is given."""
1459 return self.tk.call('wm', 'iconwindow', self._w, pathName)
1460 iconwindow = wm_iconwindow
1461 def wm_maxsize(self, width=None, height=None):
1462 """Set max WIDTH and HEIGHT for this widget. If the window is gridded
1463 the values are given in grid units. Return the current values if None
1464 is given."""
1465 return self._getints(self.tk.call(
1466 'wm', 'maxsize', self._w, width, height))
1467 maxsize = wm_maxsize
1468 def wm_minsize(self, width=None, height=None):
1469 """Set min WIDTH and HEIGHT for this widget. If the window is gridded
1470 the values are given in grid units. Return the current values if None
1471 is given."""
1472 return self._getints(self.tk.call(
1473 'wm', 'minsize', self._w, width, height))
1474 minsize = wm_minsize
1475 def wm_overrideredirect(self, boolean=None):
1476 """Instruct the window manager to ignore this widget
1477 if BOOLEAN is given with 1. Return the current value if None
1478 is given."""
1479 return self._getboolean(self.tk.call(
1480 'wm', 'overrideredirect', self._w, boolean))
1481 overrideredirect = wm_overrideredirect
1482 def wm_positionfrom(self, who=None):
1483 """Instruct the window manager that the position of this widget shall
1484 be defined by the user if WHO is "user", and by its own policy if WHO is
1485 "program"."""
1486 return self.tk.call('wm', 'positionfrom', self._w, who)
1487 positionfrom = wm_positionfrom
1488 def wm_protocol(self, name=None, func=None):
1489 """Bind function FUNC to command NAME for this widget.
1490 Return the function bound to NAME if None is given. NAME could be
1491 e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW"."""
1492 if callable(func):
1493 command = self._register(func)
1494 else:
1495 command = func
1496 return self.tk.call(
1497 'wm', 'protocol', self._w, name, command)
1498 protocol = wm_protocol
1499 def wm_resizable(self, width=None, height=None):
1500 """Instruct the window manager whether this width can be resized
1501 in WIDTH or HEIGHT. Both values are boolean values."""
1502 return self.tk.call('wm', 'resizable', self._w, width, height)
1503 resizable = wm_resizable
1504 def wm_sizefrom(self, who=None):
1505 """Instruct the window manager that the size of this widget shall
1506 be defined by the user if WHO is "user", and by its own policy if WHO is
1507 "program"."""
1508 return self.tk.call('wm', 'sizefrom', self._w, who)
1509 sizefrom = wm_sizefrom
Fredrik Lundh289ad8f2000-08-09 19:11:59 +00001510 def wm_state(self, newstate=None):
1511 """Query or set the state of this widget as one of normal, icon,
1512 iconic (see wm_iconwindow), withdrawn, or zoomed (Windows only)."""
1513 return self.tk.call('wm', 'state', self._w, newstate)
Fredrik Lundh06d28152000-08-09 18:03:12 +00001514 state = wm_state
1515 def wm_title(self, string=None):
1516 """Set the title of this widget."""
1517 return self.tk.call('wm', 'title', self._w, string)
1518 title = wm_title
1519 def wm_transient(self, master=None):
1520 """Instruct the window manager that this widget is transient
1521 with regard to widget MASTER."""
1522 return self.tk.call('wm', 'transient', self._w, master)
1523 transient = wm_transient
1524 def wm_withdraw(self):
1525 """Withdraw this widget from the screen such that it is unmapped
1526 and forgotten by the window manager. Re-draw it with wm_deiconify."""
1527 return self.tk.call('wm', 'withdraw', self._w)
1528 withdraw = wm_withdraw
Guido van Rossume365a591998-05-01 19:48:20 +00001529
Guido van Rossum18468821994-06-20 07:49:28 +00001530
1531class Tk(Misc, Wm):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001532 """Toplevel widget of Tk which represents mostly the main window
1533 of an appliation. It has an associated Tcl interpreter."""
1534 _w = '.'
1535 def __init__(self, screenName=None, baseName=None, className='Tk'):
1536 """Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
1537 be created. BASENAME will be used for the identification of the profile file (see
1538 readprofile).
1539 It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME
1540 is the name of the widget class."""
1541 global _default_root
1542 self.master = None
1543 self.children = {}
1544 if baseName is None:
1545 import sys, os
1546 baseName = os.path.basename(sys.argv[0])
1547 baseName, ext = os.path.splitext(baseName)
1548 if ext not in ('.py', '.pyc', '.pyo'):
1549 baseName = baseName + ext
1550 self.tk = _tkinter.create(screenName, baseName, className)
Martin v. Löwis8c8aa5d2002-11-26 21:39:48 +00001551 self.tk.wantobjects(wantobjects)
Jack Jansenbe92af02001-08-23 13:25:59 +00001552 if _MacOS and hasattr(_MacOS, 'SchedParams'):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001553 # Disable event scanning except for Command-Period
1554 _MacOS.SchedParams(1, 0)
1555 # Work around nasty MacTk bug
1556 # XXX Is this one still needed?
1557 self.update()
1558 # Version sanity checks
1559 tk_version = self.tk.getvar('tk_version')
1560 if tk_version != _tkinter.TK_VERSION:
1561 raise RuntimeError, \
1562 "tk.h version (%s) doesn't match libtk.a version (%s)" \
1563 % (_tkinter.TK_VERSION, tk_version)
1564 tcl_version = self.tk.getvar('tcl_version')
1565 if tcl_version != _tkinter.TCL_VERSION:
1566 raise RuntimeError, \
1567 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
1568 % (_tkinter.TCL_VERSION, tcl_version)
1569 if TkVersion < 4.0:
1570 raise RuntimeError, \
1571 "Tk 4.0 or higher is required; found Tk %s" \
1572 % str(TkVersion)
1573 self.tk.createcommand('tkerror', _tkerror)
1574 self.tk.createcommand('exit', _exit)
1575 self.readprofile(baseName, className)
1576 if _support_default_root and not _default_root:
1577 _default_root = self
1578 self.protocol("WM_DELETE_WINDOW", self.destroy)
1579 def destroy(self):
1580 """Destroy this and all descendants widgets. This will
1581 end the application of this Tcl interpreter."""
1582 for c in self.children.values(): c.destroy()
1583 self.tk.call('destroy', self._w)
1584 Misc.destroy(self)
1585 global _default_root
1586 if _support_default_root and _default_root is self:
1587 _default_root = None
1588 def readprofile(self, baseName, className):
1589 """Internal function. It reads BASENAME.tcl and CLASSNAME.tcl into
1590 the Tcl Interpreter and calls execfile on BASENAME.py and CLASSNAME.py if
1591 such a file exists in the home directory."""
1592 import os
1593 if os.environ.has_key('HOME'): home = os.environ['HOME']
1594 else: home = os.curdir
1595 class_tcl = os.path.join(home, '.%s.tcl' % className)
1596 class_py = os.path.join(home, '.%s.py' % className)
1597 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
1598 base_py = os.path.join(home, '.%s.py' % baseName)
1599 dir = {'self': self}
1600 exec 'from Tkinter import *' in dir
1601 if os.path.isfile(class_tcl):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001602 self.tk.call('source', class_tcl)
1603 if os.path.isfile(class_py):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001604 execfile(class_py, dir)
1605 if os.path.isfile(base_tcl):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001606 self.tk.call('source', base_tcl)
1607 if os.path.isfile(base_py):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001608 execfile(base_py, dir)
1609 def report_callback_exception(self, exc, val, tb):
1610 """Internal function. It reports exception on sys.stderr."""
1611 import traceback, sys
1612 sys.stderr.write("Exception in Tkinter callback\n")
1613 sys.last_type = exc
1614 sys.last_value = val
1615 sys.last_traceback = tb
1616 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +00001617
Guido van Rossum368e06b1997-11-07 20:38:49 +00001618# Ideally, the classes Pack, Place and Grid disappear, the
1619# pack/place/grid methods are defined on the Widget class, and
1620# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
1621# ...), with pack(), place() and grid() being short for
1622# pack_configure(), place_configure() and grid_columnconfigure(), and
1623# forget() being short for pack_forget(). As a practical matter, I'm
1624# afraid that there is too much code out there that may be using the
1625# Pack, Place or Grid class, so I leave them intact -- but only as
1626# backwards compatibility features. Also note that those methods that
1627# take a master as argument (e.g. pack_propagate) have been moved to
1628# the Misc class (which now incorporates all methods common between
1629# toplevel and interior widgets). Again, for compatibility, these are
1630# copied into the Pack, Place or Grid class.
1631
Guido van Rossum18468821994-06-20 07:49:28 +00001632class Pack:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001633 """Geometry manager Pack.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001634
Fredrik Lundh06d28152000-08-09 18:03:12 +00001635 Base class to use the methods pack_* in every widget."""
1636 def pack_configure(self, cnf={}, **kw):
1637 """Pack a widget in the parent widget. Use as options:
1638 after=widget - pack it after you have packed widget
1639 anchor=NSEW (or subset) - position widget according to
1640 given direction
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001641 before=widget - pack it before you will pack widget
Martin v. Löwisbfe175c2003-04-16 19:42:51 +00001642 expand=bool - expand widget if parent size grows
Fredrik Lundh06d28152000-08-09 18:03:12 +00001643 fill=NONE or X or Y or BOTH - fill widget if widget grows
1644 in=master - use master to contain this widget
1645 ipadx=amount - add internal padding in x direction
1646 ipady=amount - add internal padding in y direction
1647 padx=amount - add padding in x direction
1648 pady=amount - add padding in y direction
1649 side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
1650 """
1651 self.tk.call(
1652 ('pack', 'configure', self._w)
1653 + self._options(cnf, kw))
1654 pack = configure = config = pack_configure
1655 def pack_forget(self):
1656 """Unmap this widget and do not use it for the packing order."""
1657 self.tk.call('pack', 'forget', self._w)
1658 forget = pack_forget
1659 def pack_info(self):
1660 """Return information about the packing options
1661 for this widget."""
1662 words = self.tk.splitlist(
1663 self.tk.call('pack', 'info', self._w))
1664 dict = {}
1665 for i in range(0, len(words), 2):
1666 key = words[i][1:]
1667 value = words[i+1]
1668 if value[:1] == '.':
1669 value = self._nametowidget(value)
1670 dict[key] = value
1671 return dict
1672 info = pack_info
1673 propagate = pack_propagate = Misc.pack_propagate
1674 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001675
1676class Place:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001677 """Geometry manager Place.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001678
Fredrik Lundh06d28152000-08-09 18:03:12 +00001679 Base class to use the methods place_* in every widget."""
1680 def place_configure(self, cnf={}, **kw):
1681 """Place a widget in the parent widget. Use as options:
1682 in=master - master relative to which the widget is placed.
1683 x=amount - locate anchor of this widget at position x of master
1684 y=amount - locate anchor of this widget at position y of master
1685 relx=amount - locate anchor of this widget between 0.0 and 1.0
1686 relative to width of master (1.0 is right edge)
1687 rely=amount - locate anchor of this widget between 0.0 and 1.0
1688 relative to height of master (1.0 is bottom edge)
1689 anchor=NSEW (or subset) - position anchor according to given direction
1690 width=amount - width of this widget in pixel
1691 height=amount - height of this widget in pixel
1692 relwidth=amount - width of this widget between 0.0 and 1.0
1693 relative to width of master (1.0 is the same width
1694 as the master)
1695 relheight=amount - height of this widget between 0.0 and 1.0
1696 relative to height of master (1.0 is the same
1697 height as the master)
1698 bordermode="inside" or "outside" - whether to take border width of master widget
1699 into account
1700 """
1701 for k in ['in_']:
1702 if kw.has_key(k):
1703 kw[k[:-1]] = kw[k]
1704 del kw[k]
1705 self.tk.call(
1706 ('place', 'configure', self._w)
1707 + self._options(cnf, kw))
1708 place = configure = config = place_configure
1709 def place_forget(self):
1710 """Unmap this widget."""
1711 self.tk.call('place', 'forget', self._w)
1712 forget = place_forget
1713 def place_info(self):
1714 """Return information about the placing options
1715 for this widget."""
1716 words = self.tk.splitlist(
1717 self.tk.call('place', 'info', self._w))
1718 dict = {}
1719 for i in range(0, len(words), 2):
1720 key = words[i][1:]
1721 value = words[i+1]
1722 if value[:1] == '.':
1723 value = self._nametowidget(value)
1724 dict[key] = value
1725 return dict
1726 info = place_info
1727 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001728
Guido van Rossum37dcab11996-05-16 16:00:19 +00001729class Grid:
Fredrik Lundh06d28152000-08-09 18:03:12 +00001730 """Geometry manager Grid.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001731
Fredrik Lundh06d28152000-08-09 18:03:12 +00001732 Base class to use the methods grid_* in every widget."""
1733 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
1734 def grid_configure(self, cnf={}, **kw):
1735 """Position a widget in the parent widget in a grid. Use as options:
1736 column=number - use cell identified with given column (starting with 0)
1737 columnspan=number - this widget will span several columns
1738 in=master - use master to contain this widget
1739 ipadx=amount - add internal padding in x direction
1740 ipady=amount - add internal padding in y direction
1741 padx=amount - add padding in x direction
1742 pady=amount - add padding in y direction
1743 row=number - use cell identified with given row (starting with 0)
1744 rowspan=number - this widget will span several rows
1745 sticky=NSEW - if cell is larger on which sides will this
1746 widget stick to the cell boundary
1747 """
1748 self.tk.call(
1749 ('grid', 'configure', self._w)
1750 + self._options(cnf, kw))
1751 grid = configure = config = grid_configure
1752 bbox = grid_bbox = Misc.grid_bbox
1753 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1754 def grid_forget(self):
1755 """Unmap this widget."""
1756 self.tk.call('grid', 'forget', self._w)
1757 forget = grid_forget
1758 def grid_remove(self):
1759 """Unmap this widget but remember the grid options."""
1760 self.tk.call('grid', 'remove', self._w)
1761 def grid_info(self):
1762 """Return information about the options
1763 for positioning this widget in a grid."""
1764 words = self.tk.splitlist(
1765 self.tk.call('grid', 'info', self._w))
1766 dict = {}
1767 for i in range(0, len(words), 2):
1768 key = words[i][1:]
1769 value = words[i+1]
1770 if value[:1] == '.':
1771 value = self._nametowidget(value)
1772 dict[key] = value
1773 return dict
1774 info = grid_info
Martin v. Löwisdc579092001-10-13 09:33:51 +00001775 location = grid_location = Misc.grid_location
Fredrik Lundh06d28152000-08-09 18:03:12 +00001776 propagate = grid_propagate = Misc.grid_propagate
1777 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1778 size = grid_size = Misc.grid_size
1779 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001780
Guido van Rossum368e06b1997-11-07 20:38:49 +00001781class BaseWidget(Misc):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001782 """Internal class."""
1783 def _setup(self, master, cnf):
1784 """Internal function. Sets up information about children."""
1785 if _support_default_root:
1786 global _default_root
1787 if not master:
1788 if not _default_root:
1789 _default_root = Tk()
1790 master = _default_root
1791 self.master = master
1792 self.tk = master.tk
1793 name = None
1794 if cnf.has_key('name'):
1795 name = cnf['name']
1796 del cnf['name']
1797 if not name:
1798 name = `id(self)`
1799 self._name = name
1800 if master._w=='.':
1801 self._w = '.' + name
1802 else:
1803 self._w = master._w + '.' + name
1804 self.children = {}
1805 if self.master.children.has_key(self._name):
1806 self.master.children[self._name].destroy()
1807 self.master.children[self._name] = self
1808 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
1809 """Construct a widget with the parent widget MASTER, a name WIDGETNAME
1810 and appropriate options."""
1811 if kw:
1812 cnf = _cnfmerge((cnf, kw))
1813 self.widgetName = widgetName
1814 BaseWidget._setup(self, master, cnf)
1815 classes = []
1816 for k in cnf.keys():
1817 if type(k) is ClassType:
1818 classes.append((k, cnf[k]))
1819 del cnf[k]
1820 self.tk.call(
1821 (widgetName, self._w) + extra + self._options(cnf))
1822 for k, v in classes:
1823 k.configure(self, v)
1824 def destroy(self):
1825 """Destroy this and all descendants widgets."""
1826 for c in self.children.values(): c.destroy()
1827 if self.master.children.has_key(self._name):
1828 del self.master.children[self._name]
1829 self.tk.call('destroy', self._w)
1830 Misc.destroy(self)
1831 def _do(self, name, args=()):
1832 # XXX Obsolete -- better use self.tk.call directly!
1833 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001834
Guido van Rossum368e06b1997-11-07 20:38:49 +00001835class Widget(BaseWidget, Pack, Place, Grid):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001836 """Internal class.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001837
Fredrik Lundh06d28152000-08-09 18:03:12 +00001838 Base class for a widget which can be positioned with the geometry managers
1839 Pack, Place or Grid."""
1840 pass
Guido van Rossum368e06b1997-11-07 20:38:49 +00001841
1842class Toplevel(BaseWidget, Wm):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001843 """Toplevel widget, e.g. for dialogs."""
1844 def __init__(self, master=None, cnf={}, **kw):
1845 """Construct a toplevel widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001846
Fredrik Lundh06d28152000-08-09 18:03:12 +00001847 Valid resource names: background, bd, bg, borderwidth, class,
1848 colormap, container, cursor, height, highlightbackground,
1849 highlightcolor, highlightthickness, menu, relief, screen, takefocus,
1850 use, visual, width."""
1851 if kw:
1852 cnf = _cnfmerge((cnf, kw))
1853 extra = ()
1854 for wmkey in ['screen', 'class_', 'class', 'visual',
1855 'colormap']:
1856 if cnf.has_key(wmkey):
1857 val = cnf[wmkey]
1858 # TBD: a hack needed because some keys
1859 # are not valid as keyword arguments
1860 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1861 else: opt = '-'+wmkey
1862 extra = extra + (opt, val)
1863 del cnf[wmkey]
1864 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
1865 root = self._root()
1866 self.iconname(root.iconname())
1867 self.title(root.title())
1868 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum18468821994-06-20 07:49:28 +00001869
1870class Button(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001871 """Button widget."""
1872 def __init__(self, master=None, cnf={}, **kw):
1873 """Construct a button widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00001874
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001875 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00001876
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001877 activebackground, activeforeground, anchor,
1878 background, bitmap, borderwidth, cursor,
Raymond Hettingerff41c482003-04-06 09:01:11 +00001879 disabledforeground, font, foreground
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001880 highlightbackground, highlightcolor,
Raymond Hettingerff41c482003-04-06 09:01:11 +00001881 highlightthickness, image, justify,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001882 padx, pady, relief, repeatdelay,
Raymond Hettingerff41c482003-04-06 09:01:11 +00001883 repeatinterval, takefocus, text,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001884 textvariable, underline, wraplength
Raymond Hettingerff41c482003-04-06 09:01:11 +00001885
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001886 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00001887
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001888 command, compound, default, height,
1889 overrelief, state, width
1890 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00001891 Widget.__init__(self, master, 'button', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001892
Fredrik Lundh06d28152000-08-09 18:03:12 +00001893 def tkButtonEnter(self, *dummy):
1894 self.tk.call('tkButtonEnter', self._w)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001895
Fredrik Lundh06d28152000-08-09 18:03:12 +00001896 def tkButtonLeave(self, *dummy):
1897 self.tk.call('tkButtonLeave', self._w)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001898
Fredrik Lundh06d28152000-08-09 18:03:12 +00001899 def tkButtonDown(self, *dummy):
1900 self.tk.call('tkButtonDown', self._w)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001901
Fredrik Lundh06d28152000-08-09 18:03:12 +00001902 def tkButtonUp(self, *dummy):
1903 self.tk.call('tkButtonUp', self._w)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001904
Fredrik Lundh06d28152000-08-09 18:03:12 +00001905 def tkButtonInvoke(self, *dummy):
1906 self.tk.call('tkButtonInvoke', self._w)
Raymond Hettingerff41c482003-04-06 09:01:11 +00001907
Fredrik Lundh06d28152000-08-09 18:03:12 +00001908 def flash(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00001909 """Flash the button.
1910
1911 This is accomplished by redisplaying
1912 the button several times, alternating between active and
1913 normal colors. At the end of the flash the button is left
1914 in the same normal/active state as when the command was
1915 invoked. This command is ignored if the button's state is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001916 disabled.
1917 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00001918 self.tk.call(self._w, 'flash')
Raymond Hettingerff41c482003-04-06 09:01:11 +00001919
Fredrik Lundh06d28152000-08-09 18:03:12 +00001920 def invoke(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00001921 """Invoke the command associated with the button.
1922
1923 The return value is the return value from the command,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00001924 or an empty string if there is no command associated with
1925 the button. This command is ignored if the button's state
1926 is disabled.
1927 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00001928 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001929
1930# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001931# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001932def AtEnd():
Fredrik Lundh06d28152000-08-09 18:03:12 +00001933 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001934def AtInsert(*args):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001935 s = 'insert'
1936 for a in args:
1937 if a: s = s + (' ' + a)
1938 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001939def AtSelFirst():
Fredrik Lundh06d28152000-08-09 18:03:12 +00001940 return 'sel.first'
Guido van Rossum18468821994-06-20 07:49:28 +00001941def AtSelLast():
Fredrik Lundh06d28152000-08-09 18:03:12 +00001942 return 'sel.last'
Guido van Rossum18468821994-06-20 07:49:28 +00001943def At(x, y=None):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001944 if y is None:
1945 return '@' + `x`
1946 else:
1947 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001948
1949class Canvas(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00001950 """Canvas widget to display graphical elements like lines or text."""
1951 def __init__(self, master=None, cnf={}, **kw):
1952 """Construct a canvas widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001953
Fredrik Lundh06d28152000-08-09 18:03:12 +00001954 Valid resource names: background, bd, bg, borderwidth, closeenough,
1955 confine, cursor, height, highlightbackground, highlightcolor,
1956 highlightthickness, insertbackground, insertborderwidth,
1957 insertofftime, insertontime, insertwidth, offset, relief,
1958 scrollregion, selectbackground, selectborderwidth, selectforeground,
1959 state, takefocus, width, xscrollcommand, xscrollincrement,
1960 yscrollcommand, yscrollincrement."""
1961 Widget.__init__(self, master, 'canvas', cnf, kw)
1962 def addtag(self, *args):
1963 """Internal function."""
1964 self.tk.call((self._w, 'addtag') + args)
1965 def addtag_above(self, newtag, tagOrId):
1966 """Add tag NEWTAG to all items above TAGORID."""
1967 self.addtag(newtag, 'above', tagOrId)
1968 def addtag_all(self, newtag):
1969 """Add tag NEWTAG to all items."""
1970 self.addtag(newtag, 'all')
1971 def addtag_below(self, newtag, tagOrId):
1972 """Add tag NEWTAG to all items below TAGORID."""
1973 self.addtag(newtag, 'below', tagOrId)
1974 def addtag_closest(self, newtag, x, y, halo=None, start=None):
1975 """Add tag NEWTAG to item which is closest to pixel at X, Y.
1976 If several match take the top-most.
1977 All items closer than HALO are considered overlapping (all are
1978 closests). If START is specified the next below this tag is taken."""
1979 self.addtag(newtag, 'closest', x, y, halo, start)
1980 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
1981 """Add tag NEWTAG to all items in the rectangle defined
1982 by X1,Y1,X2,Y2."""
1983 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1984 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
1985 """Add tag NEWTAG to all items which overlap the rectangle
1986 defined by X1,Y1,X2,Y2."""
1987 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1988 def addtag_withtag(self, newtag, tagOrId):
1989 """Add tag NEWTAG to all items with TAGORID."""
1990 self.addtag(newtag, 'withtag', tagOrId)
1991 def bbox(self, *args):
1992 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
1993 which encloses all items with tags specified as arguments."""
1994 return self._getints(
1995 self.tk.call((self._w, 'bbox') + args)) or None
1996 def tag_unbind(self, tagOrId, sequence, funcid=None):
1997 """Unbind for all items with TAGORID for event SEQUENCE the
1998 function identified with FUNCID."""
1999 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
2000 if funcid:
2001 self.deletecommand(funcid)
2002 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
2003 """Bind to all items with TAGORID at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002004
Fredrik Lundh06d28152000-08-09 18:03:12 +00002005 An additional boolean parameter ADD specifies whether FUNC will be
2006 called additionally to the other bound function or whether it will
2007 replace the previous function. See bind for the return value."""
2008 return self._bind((self._w, 'bind', tagOrId),
2009 sequence, func, add)
2010 def canvasx(self, screenx, gridspacing=None):
2011 """Return the canvas x coordinate of pixel position SCREENX rounded
2012 to nearest multiple of GRIDSPACING units."""
2013 return getdouble(self.tk.call(
2014 self._w, 'canvasx', screenx, gridspacing))
2015 def canvasy(self, screeny, gridspacing=None):
2016 """Return the canvas y coordinate of pixel position SCREENY rounded
2017 to nearest multiple of GRIDSPACING units."""
2018 return getdouble(self.tk.call(
2019 self._w, 'canvasy', screeny, gridspacing))
2020 def coords(self, *args):
2021 """Return a list of coordinates for the item given in ARGS."""
2022 # XXX Should use _flatten on args
2023 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00002024 self.tk.splitlist(
Fredrik Lundh06d28152000-08-09 18:03:12 +00002025 self.tk.call((self._w, 'coords') + args)))
2026 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
2027 """Internal function."""
2028 args = _flatten(args)
2029 cnf = args[-1]
2030 if type(cnf) in (DictionaryType, TupleType):
2031 args = args[:-1]
2032 else:
2033 cnf = {}
Raymond Hettingerff41c482003-04-06 09:01:11 +00002034 return getint(self.tk.call(
2035 self._w, 'create', itemType,
2036 *(args + self._options(cnf, kw))))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002037 def create_arc(self, *args, **kw):
2038 """Create arc shaped region with coordinates x1,y1,x2,y2."""
2039 return self._create('arc', args, kw)
2040 def create_bitmap(self, *args, **kw):
2041 """Create bitmap with coordinates x1,y1."""
2042 return self._create('bitmap', args, kw)
2043 def create_image(self, *args, **kw):
2044 """Create image item with coordinates x1,y1."""
2045 return self._create('image', args, kw)
2046 def create_line(self, *args, **kw):
2047 """Create line with coordinates x1,y1,...,xn,yn."""
2048 return self._create('line', args, kw)
2049 def create_oval(self, *args, **kw):
2050 """Create oval with coordinates x1,y1,x2,y2."""
2051 return self._create('oval', args, kw)
2052 def create_polygon(self, *args, **kw):
2053 """Create polygon with coordinates x1,y1,...,xn,yn."""
2054 return self._create('polygon', args, kw)
2055 def create_rectangle(self, *args, **kw):
2056 """Create rectangle with coordinates x1,y1,x2,y2."""
2057 return self._create('rectangle', args, kw)
2058 def create_text(self, *args, **kw):
2059 """Create text with coordinates x1,y1."""
2060 return self._create('text', args, kw)
2061 def create_window(self, *args, **kw):
2062 """Create window with coordinates x1,y1,x2,y2."""
2063 return self._create('window', args, kw)
2064 def dchars(self, *args):
2065 """Delete characters of text items identified by tag or id in ARGS (possibly
2066 several times) from FIRST to LAST character (including)."""
2067 self.tk.call((self._w, 'dchars') + args)
2068 def delete(self, *args):
2069 """Delete items identified by all tag or ids contained in ARGS."""
2070 self.tk.call((self._w, 'delete') + args)
2071 def dtag(self, *args):
2072 """Delete tag or id given as last arguments in ARGS from items
2073 identified by first argument in ARGS."""
2074 self.tk.call((self._w, 'dtag') + args)
2075 def find(self, *args):
2076 """Internal function."""
2077 return self._getints(
2078 self.tk.call((self._w, 'find') + args)) or ()
2079 def find_above(self, tagOrId):
2080 """Return items above TAGORID."""
2081 return self.find('above', tagOrId)
2082 def find_all(self):
2083 """Return all items."""
2084 return self.find('all')
2085 def find_below(self, tagOrId):
2086 """Return all items below TAGORID."""
2087 return self.find('below', tagOrId)
2088 def find_closest(self, x, y, halo=None, start=None):
2089 """Return item which is closest to pixel at X, Y.
2090 If several match take the top-most.
2091 All items closer than HALO are considered overlapping (all are
2092 closests). If START is specified the next below this tag is taken."""
2093 return self.find('closest', x, y, halo, start)
2094 def find_enclosed(self, x1, y1, x2, y2):
2095 """Return all items in rectangle defined
2096 by X1,Y1,X2,Y2."""
2097 return self.find('enclosed', x1, y1, x2, y2)
2098 def find_overlapping(self, x1, y1, x2, y2):
2099 """Return all items which overlap the rectangle
2100 defined by X1,Y1,X2,Y2."""
2101 return self.find('overlapping', x1, y1, x2, y2)
2102 def find_withtag(self, tagOrId):
2103 """Return all items with TAGORID."""
2104 return self.find('withtag', tagOrId)
2105 def focus(self, *args):
2106 """Set focus to the first item specified in ARGS."""
2107 return self.tk.call((self._w, 'focus') + args)
2108 def gettags(self, *args):
2109 """Return tags associated with the first item specified in ARGS."""
2110 return self.tk.splitlist(
2111 self.tk.call((self._w, 'gettags') + args))
2112 def icursor(self, *args):
2113 """Set cursor at position POS in the item identified by TAGORID.
2114 In ARGS TAGORID must be first."""
2115 self.tk.call((self._w, 'icursor') + args)
2116 def index(self, *args):
2117 """Return position of cursor as integer in item specified in ARGS."""
2118 return getint(self.tk.call((self._w, 'index') + args))
2119 def insert(self, *args):
2120 """Insert TEXT in item TAGORID at position POS. ARGS must
2121 be TAGORID POS TEXT."""
2122 self.tk.call((self._w, 'insert') + args)
2123 def itemcget(self, tagOrId, option):
2124 """Return the resource value for an OPTION for item TAGORID."""
2125 return self.tk.call(
2126 (self._w, 'itemcget') + (tagOrId, '-'+option))
2127 def itemconfigure(self, tagOrId, cnf=None, **kw):
2128 """Configure resources of an item TAGORID.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002129
Fredrik Lundh06d28152000-08-09 18:03:12 +00002130 The values for resources are specified as keyword
2131 arguments. To get an overview about
2132 the allowed keyword arguments call the method without arguments.
2133 """
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002134 return self._configure(('itemconfigure', tagOrId), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002135 itemconfig = itemconfigure
2136 # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
2137 # so the preferred name for them is tag_lower, tag_raise
2138 # (similar to tag_bind, and similar to the Text widget);
2139 # unfortunately can't delete the old ones yet (maybe in 1.6)
2140 def tag_lower(self, *args):
2141 """Lower an item TAGORID given in ARGS
2142 (optional below another item)."""
2143 self.tk.call((self._w, 'lower') + args)
2144 lower = tag_lower
2145 def move(self, *args):
2146 """Move an item TAGORID given in ARGS."""
2147 self.tk.call((self._w, 'move') + args)
2148 def postscript(self, cnf={}, **kw):
2149 """Print the contents of the canvas to a postscript
2150 file. Valid options: colormap, colormode, file, fontmap,
2151 height, pageanchor, pageheight, pagewidth, pagex, pagey,
2152 rotate, witdh, x, y."""
2153 return self.tk.call((self._w, 'postscript') +
2154 self._options(cnf, kw))
2155 def tag_raise(self, *args):
2156 """Raise an item TAGORID given in ARGS
2157 (optional above another item)."""
2158 self.tk.call((self._w, 'raise') + args)
2159 lift = tkraise = tag_raise
2160 def scale(self, *args):
2161 """Scale item TAGORID with XORIGIN, YORIGIN, XSCALE, YSCALE."""
2162 self.tk.call((self._w, 'scale') + args)
2163 def scan_mark(self, x, y):
2164 """Remember the current X, Y coordinates."""
2165 self.tk.call(self._w, 'scan', 'mark', x, y)
Neal Norwitze931ed52003-01-10 23:24:32 +00002166 def scan_dragto(self, x, y, gain=10):
2167 """Adjust the view of the canvas to GAIN times the
Fredrik Lundh06d28152000-08-09 18:03:12 +00002168 difference between X and Y and the coordinates given in
2169 scan_mark."""
Neal Norwitze931ed52003-01-10 23:24:32 +00002170 self.tk.call(self._w, 'scan', 'dragto', x, y, gain)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002171 def select_adjust(self, tagOrId, index):
2172 """Adjust the end of the selection near the cursor of an item TAGORID to index."""
2173 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
2174 def select_clear(self):
2175 """Clear the selection if it is in this widget."""
2176 self.tk.call(self._w, 'select', 'clear')
2177 def select_from(self, tagOrId, index):
2178 """Set the fixed end of a selection in item TAGORID to INDEX."""
2179 self.tk.call(self._w, 'select', 'from', tagOrId, index)
2180 def select_item(self):
2181 """Return the item which has the selection."""
Neal Norwitz58b63bf2002-07-23 02:52:58 +00002182 return self.tk.call(self._w, 'select', 'item') or None
Fredrik Lundh06d28152000-08-09 18:03:12 +00002183 def select_to(self, tagOrId, index):
2184 """Set the variable end of a selection in item TAGORID to INDEX."""
2185 self.tk.call(self._w, 'select', 'to', tagOrId, index)
2186 def type(self, tagOrId):
2187 """Return the type of the item TAGORID."""
2188 return self.tk.call(self._w, 'type', tagOrId) or None
2189 def xview(self, *args):
2190 """Query and change horizontal position of the view."""
2191 if not args:
2192 return self._getdoubles(self.tk.call(self._w, 'xview'))
2193 self.tk.call((self._w, 'xview') + args)
2194 def xview_moveto(self, fraction):
2195 """Adjusts the view in the window so that FRACTION of the
2196 total width of the canvas is off-screen to the left."""
2197 self.tk.call(self._w, 'xview', 'moveto', fraction)
2198 def xview_scroll(self, number, what):
2199 """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
2200 self.tk.call(self._w, 'xview', 'scroll', number, what)
2201 def yview(self, *args):
2202 """Query and change vertical position of the view."""
2203 if not args:
2204 return self._getdoubles(self.tk.call(self._w, 'yview'))
2205 self.tk.call((self._w, 'yview') + args)
2206 def yview_moveto(self, fraction):
2207 """Adjusts the view in the window so that FRACTION of the
2208 total height of the canvas is off-screen to the top."""
2209 self.tk.call(self._w, 'yview', 'moveto', fraction)
2210 def yview_scroll(self, number, what):
2211 """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
2212 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00002213
2214class Checkbutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002215 """Checkbutton widget which is either in on- or off-state."""
2216 def __init__(self, master=None, cnf={}, **kw):
2217 """Construct a checkbutton widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002218
Fredrik Lundh06d28152000-08-09 18:03:12 +00002219 Valid resource names: activebackground, activeforeground, anchor,
2220 background, bd, bg, bitmap, borderwidth, command, cursor,
2221 disabledforeground, fg, font, foreground, height,
2222 highlightbackground, highlightcolor, highlightthickness, image,
2223 indicatoron, justify, offvalue, onvalue, padx, pady, relief,
2224 selectcolor, selectimage, state, takefocus, text, textvariable,
2225 underline, variable, width, wraplength."""
2226 Widget.__init__(self, master, 'checkbutton', cnf, kw)
2227 def deselect(self):
2228 """Put the button in off-state."""
2229 self.tk.call(self._w, 'deselect')
2230 def flash(self):
2231 """Flash the button."""
2232 self.tk.call(self._w, 'flash')
2233 def invoke(self):
2234 """Toggle the button and invoke a command if given as resource."""
2235 return self.tk.call(self._w, 'invoke')
2236 def select(self):
2237 """Put the button in on-state."""
2238 self.tk.call(self._w, 'select')
2239 def toggle(self):
2240 """Toggle the button."""
2241 self.tk.call(self._w, 'toggle')
Guido van Rossum18468821994-06-20 07:49:28 +00002242
2243class Entry(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002244 """Entry widget which allows to display simple text."""
2245 def __init__(self, master=None, cnf={}, **kw):
2246 """Construct an entry widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002247
Fredrik Lundh06d28152000-08-09 18:03:12 +00002248 Valid resource names: background, bd, bg, borderwidth, cursor,
2249 exportselection, fg, font, foreground, highlightbackground,
2250 highlightcolor, highlightthickness, insertbackground,
2251 insertborderwidth, insertofftime, insertontime, insertwidth,
2252 invalidcommand, invcmd, justify, relief, selectbackground,
2253 selectborderwidth, selectforeground, show, state, takefocus,
2254 textvariable, validate, validatecommand, vcmd, width,
2255 xscrollcommand."""
2256 Widget.__init__(self, master, 'entry', cnf, kw)
2257 def delete(self, first, last=None):
2258 """Delete text from FIRST to LAST (not included)."""
2259 self.tk.call(self._w, 'delete', first, last)
2260 def get(self):
2261 """Return the text."""
2262 return self.tk.call(self._w, 'get')
2263 def icursor(self, index):
2264 """Insert cursor at INDEX."""
2265 self.tk.call(self._w, 'icursor', index)
2266 def index(self, index):
2267 """Return position of cursor."""
2268 return getint(self.tk.call(
2269 self._w, 'index', index))
2270 def insert(self, index, string):
2271 """Insert STRING at INDEX."""
2272 self.tk.call(self._w, 'insert', index, string)
2273 def scan_mark(self, x):
2274 """Remember the current X, Y coordinates."""
2275 self.tk.call(self._w, 'scan', 'mark', x)
2276 def scan_dragto(self, x):
2277 """Adjust the view of the canvas to 10 times the
2278 difference between X and Y and the coordinates given in
2279 scan_mark."""
2280 self.tk.call(self._w, 'scan', 'dragto', x)
2281 def selection_adjust(self, index):
2282 """Adjust the end of the selection near the cursor to INDEX."""
2283 self.tk.call(self._w, 'selection', 'adjust', index)
2284 select_adjust = selection_adjust
2285 def selection_clear(self):
2286 """Clear the selection if it is in this widget."""
2287 self.tk.call(self._w, 'selection', 'clear')
2288 select_clear = selection_clear
2289 def selection_from(self, index):
2290 """Set the fixed end of a selection to INDEX."""
2291 self.tk.call(self._w, 'selection', 'from', index)
2292 select_from = selection_from
2293 def selection_present(self):
2294 """Return whether the widget has the selection."""
2295 return self.tk.getboolean(
2296 self.tk.call(self._w, 'selection', 'present'))
2297 select_present = selection_present
2298 def selection_range(self, start, end):
2299 """Set the selection from START to END (not included)."""
2300 self.tk.call(self._w, 'selection', 'range', start, end)
2301 select_range = selection_range
2302 def selection_to(self, index):
2303 """Set the variable end of a selection to INDEX."""
2304 self.tk.call(self._w, 'selection', 'to', index)
2305 select_to = selection_to
2306 def xview(self, index):
2307 """Query and change horizontal position of the view."""
2308 self.tk.call(self._w, 'xview', index)
2309 def xview_moveto(self, fraction):
2310 """Adjust the view in the window so that FRACTION of the
2311 total width of the entry is off-screen to the left."""
2312 self.tk.call(self._w, 'xview', 'moveto', fraction)
2313 def xview_scroll(self, number, what):
2314 """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
2315 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00002316
2317class Frame(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002318 """Frame widget which may contain other widgets and can have a 3D border."""
2319 def __init__(self, master=None, cnf={}, **kw):
2320 """Construct a frame widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002321
Fredrik Lundh06d28152000-08-09 18:03:12 +00002322 Valid resource names: background, bd, bg, borderwidth, class,
2323 colormap, container, cursor, height, highlightbackground,
2324 highlightcolor, highlightthickness, relief, takefocus, visual, width."""
2325 cnf = _cnfmerge((cnf, kw))
2326 extra = ()
2327 if cnf.has_key('class_'):
2328 extra = ('-class', cnf['class_'])
2329 del cnf['class_']
2330 elif cnf.has_key('class'):
2331 extra = ('-class', cnf['class'])
2332 del cnf['class']
2333 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00002334
2335class Label(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002336 """Label widget which can display text and bitmaps."""
2337 def __init__(self, master=None, cnf={}, **kw):
2338 """Construct a label widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002339
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002340 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002341
2342 activebackground, activeforeground, anchor,
2343 background, bitmap, borderwidth, cursor,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002344 disabledforeground, font, foreground,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002345 highlightbackground, highlightcolor,
2346 highlightthickness, image, justify,
2347 padx, pady, relief, takefocus, text,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002348 textvariable, underline, wraplength
Raymond Hettingerff41c482003-04-06 09:01:11 +00002349
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002350 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002351
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002352 height, state, width
Raymond Hettingerff41c482003-04-06 09:01:11 +00002353
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002354 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002355 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002356
Guido van Rossum18468821994-06-20 07:49:28 +00002357class Listbox(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002358 """Listbox widget which can display a list of strings."""
2359 def __init__(self, master=None, cnf={}, **kw):
2360 """Construct a listbox widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002361
Fredrik Lundh06d28152000-08-09 18:03:12 +00002362 Valid resource names: background, bd, bg, borderwidth, cursor,
2363 exportselection, fg, font, foreground, height, highlightbackground,
2364 highlightcolor, highlightthickness, relief, selectbackground,
2365 selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
2366 width, xscrollcommand, yscrollcommand, listvariable."""
2367 Widget.__init__(self, master, 'listbox', cnf, kw)
2368 def activate(self, index):
2369 """Activate item identified by INDEX."""
2370 self.tk.call(self._w, 'activate', index)
2371 def bbox(self, *args):
2372 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
2373 which encloses the item identified by index in ARGS."""
2374 return self._getints(
2375 self.tk.call((self._w, 'bbox') + args)) or None
2376 def curselection(self):
2377 """Return list of indices of currently selected item."""
2378 # XXX Ought to apply self._getints()...
2379 return self.tk.splitlist(self.tk.call(
2380 self._w, 'curselection'))
2381 def delete(self, first, last=None):
2382 """Delete items from FIRST to LAST (not included)."""
2383 self.tk.call(self._w, 'delete', first, last)
2384 def get(self, first, last=None):
2385 """Get list of items from FIRST to LAST (not included)."""
2386 if last:
2387 return self.tk.splitlist(self.tk.call(
2388 self._w, 'get', first, last))
2389 else:
2390 return self.tk.call(self._w, 'get', first)
2391 def index(self, index):
2392 """Return index of item identified with INDEX."""
2393 i = self.tk.call(self._w, 'index', index)
2394 if i == 'none': return None
2395 return getint(i)
2396 def insert(self, index, *elements):
2397 """Insert ELEMENTS at INDEX."""
2398 self.tk.call((self._w, 'insert', index) + elements)
2399 def nearest(self, y):
2400 """Get index of item which is nearest to y coordinate Y."""
2401 return getint(self.tk.call(
2402 self._w, 'nearest', y))
2403 def scan_mark(self, x, y):
2404 """Remember the current X, Y coordinates."""
2405 self.tk.call(self._w, 'scan', 'mark', x, y)
2406 def scan_dragto(self, x, y):
2407 """Adjust the view of the listbox to 10 times the
2408 difference between X and Y and the coordinates given in
2409 scan_mark."""
2410 self.tk.call(self._w, 'scan', 'dragto', x, y)
2411 def see(self, index):
2412 """Scroll such that INDEX is visible."""
2413 self.tk.call(self._w, 'see', index)
2414 def selection_anchor(self, index):
2415 """Set the fixed end oft the selection to INDEX."""
2416 self.tk.call(self._w, 'selection', 'anchor', index)
2417 select_anchor = selection_anchor
2418 def selection_clear(self, first, last=None):
2419 """Clear the selection from FIRST to LAST (not included)."""
2420 self.tk.call(self._w,
2421 'selection', 'clear', first, last)
2422 select_clear = selection_clear
2423 def selection_includes(self, index):
2424 """Return 1 if INDEX is part of the selection."""
2425 return self.tk.getboolean(self.tk.call(
2426 self._w, 'selection', 'includes', index))
2427 select_includes = selection_includes
2428 def selection_set(self, first, last=None):
2429 """Set the selection from FIRST to LAST (not included) without
2430 changing the currently selected elements."""
2431 self.tk.call(self._w, 'selection', 'set', first, last)
2432 select_set = selection_set
2433 def size(self):
2434 """Return the number of elements in the listbox."""
2435 return getint(self.tk.call(self._w, 'size'))
2436 def xview(self, *what):
2437 """Query and change horizontal position of the view."""
2438 if not what:
2439 return self._getdoubles(self.tk.call(self._w, 'xview'))
2440 self.tk.call((self._w, 'xview') + what)
2441 def xview_moveto(self, fraction):
2442 """Adjust the view in the window so that FRACTION of the
2443 total width of the entry is off-screen to the left."""
2444 self.tk.call(self._w, 'xview', 'moveto', fraction)
2445 def xview_scroll(self, number, what):
2446 """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
2447 self.tk.call(self._w, 'xview', 'scroll', number, what)
2448 def yview(self, *what):
2449 """Query and change vertical position of the view."""
2450 if not what:
2451 return self._getdoubles(self.tk.call(self._w, 'yview'))
2452 self.tk.call((self._w, 'yview') + what)
2453 def yview_moveto(self, fraction):
2454 """Adjust the view in the window so that FRACTION of the
2455 total width of the entry is off-screen to the top."""
2456 self.tk.call(self._w, 'yview', 'moveto', fraction)
2457 def yview_scroll(self, number, what):
2458 """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
2459 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum09f1ad82001-09-05 19:29:56 +00002460 def itemcget(self, index, option):
2461 """Return the resource value for an ITEM and an OPTION."""
2462 return self.tk.call(
2463 (self._w, 'itemcget') + (index, '-'+option))
Guido van Rossuma0adb922001-09-01 18:29:55 +00002464 def itemconfigure(self, index, cnf=None, **kw):
Guido van Rossum09f1ad82001-09-05 19:29:56 +00002465 """Configure resources of an ITEM.
Guido van Rossuma0adb922001-09-01 18:29:55 +00002466
2467 The values for resources are specified as keyword arguments.
2468 To get an overview about the allowed keyword arguments
2469 call the method without arguments.
2470 Valid resource names: background, bg, foreground, fg,
2471 selectbackground, selectforeground."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002472 return self._configure(('itemconfigure', index), cnf, kw)
Guido van Rossuma0adb922001-09-01 18:29:55 +00002473 itemconfig = itemconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00002474
2475class Menu(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002476 """Menu widget which allows to display menu bars, pull-down menus and pop-up menus."""
2477 def __init__(self, master=None, cnf={}, **kw):
2478 """Construct menu widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002479
Fredrik Lundh06d28152000-08-09 18:03:12 +00002480 Valid resource names: activebackground, activeborderwidth,
2481 activeforeground, background, bd, bg, borderwidth, cursor,
2482 disabledforeground, fg, font, foreground, postcommand, relief,
2483 selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
2484 Widget.__init__(self, master, 'menu', cnf, kw)
2485 def tk_bindForTraversal(self):
2486 pass # obsolete since Tk 4.0
2487 def tk_mbPost(self):
2488 self.tk.call('tk_mbPost', self._w)
2489 def tk_mbUnpost(self):
2490 self.tk.call('tk_mbUnpost')
2491 def tk_traverseToMenu(self, char):
2492 self.tk.call('tk_traverseToMenu', self._w, char)
2493 def tk_traverseWithinMenu(self, char):
2494 self.tk.call('tk_traverseWithinMenu', self._w, char)
2495 def tk_getMenuButtons(self):
2496 return self.tk.call('tk_getMenuButtons', self._w)
2497 def tk_nextMenu(self, count):
2498 self.tk.call('tk_nextMenu', count)
2499 def tk_nextMenuEntry(self, count):
2500 self.tk.call('tk_nextMenuEntry', count)
2501 def tk_invokeMenu(self):
2502 self.tk.call('tk_invokeMenu', self._w)
2503 def tk_firstMenu(self):
2504 self.tk.call('tk_firstMenu', self._w)
2505 def tk_mbButtonDown(self):
2506 self.tk.call('tk_mbButtonDown', self._w)
2507 def tk_popup(self, x, y, entry=""):
2508 """Post the menu at position X,Y with entry ENTRY."""
2509 self.tk.call('tk_popup', self._w, x, y, entry)
2510 def activate(self, index):
2511 """Activate entry at INDEX."""
2512 self.tk.call(self._w, 'activate', index)
2513 def add(self, itemType, cnf={}, **kw):
2514 """Internal function."""
2515 self.tk.call((self._w, 'add', itemType) +
2516 self._options(cnf, kw))
2517 def add_cascade(self, cnf={}, **kw):
2518 """Add hierarchical menu item."""
2519 self.add('cascade', cnf or kw)
2520 def add_checkbutton(self, cnf={}, **kw):
2521 """Add checkbutton menu item."""
2522 self.add('checkbutton', cnf or kw)
2523 def add_command(self, cnf={}, **kw):
2524 """Add command menu item."""
2525 self.add('command', cnf or kw)
2526 def add_radiobutton(self, cnf={}, **kw):
2527 """Addd radio menu item."""
2528 self.add('radiobutton', cnf or kw)
2529 def add_separator(self, cnf={}, **kw):
2530 """Add separator."""
2531 self.add('separator', cnf or kw)
2532 def insert(self, index, itemType, cnf={}, **kw):
2533 """Internal function."""
2534 self.tk.call((self._w, 'insert', index, itemType) +
2535 self._options(cnf, kw))
2536 def insert_cascade(self, index, cnf={}, **kw):
2537 """Add hierarchical menu item at INDEX."""
2538 self.insert(index, 'cascade', cnf or kw)
2539 def insert_checkbutton(self, index, cnf={}, **kw):
2540 """Add checkbutton menu item at INDEX."""
2541 self.insert(index, 'checkbutton', cnf or kw)
2542 def insert_command(self, index, cnf={}, **kw):
2543 """Add command menu item at INDEX."""
2544 self.insert(index, 'command', cnf or kw)
2545 def insert_radiobutton(self, index, cnf={}, **kw):
2546 """Addd radio menu item at INDEX."""
2547 self.insert(index, 'radiobutton', cnf or kw)
2548 def insert_separator(self, index, cnf={}, **kw):
2549 """Add separator at INDEX."""
2550 self.insert(index, 'separator', cnf or kw)
2551 def delete(self, index1, index2=None):
2552 """Delete menu items between INDEX1 and INDEX2 (not included)."""
2553 self.tk.call(self._w, 'delete', index1, index2)
2554 def entrycget(self, index, option):
2555 """Return the resource value of an menu item for OPTION at INDEX."""
2556 return self.tk.call(self._w, 'entrycget', index, '-' + option)
2557 def entryconfigure(self, index, cnf=None, **kw):
2558 """Configure a menu item at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002559 return self._configure(('entryconfigure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002560 entryconfig = entryconfigure
2561 def index(self, index):
2562 """Return the index of a menu item identified by INDEX."""
2563 i = self.tk.call(self._w, 'index', index)
2564 if i == 'none': return None
2565 return getint(i)
2566 def invoke(self, index):
2567 """Invoke a menu item identified by INDEX and execute
2568 the associated command."""
2569 return self.tk.call(self._w, 'invoke', index)
2570 def post(self, x, y):
2571 """Display a menu at position X,Y."""
2572 self.tk.call(self._w, 'post', x, y)
2573 def type(self, index):
2574 """Return the type of the menu item at INDEX."""
2575 return self.tk.call(self._w, 'type', index)
2576 def unpost(self):
2577 """Unmap a menu."""
2578 self.tk.call(self._w, 'unpost')
2579 def yposition(self, index):
2580 """Return the y-position of the topmost pixel of the menu item at INDEX."""
2581 return getint(self.tk.call(
2582 self._w, 'yposition', index))
Guido van Rossum18468821994-06-20 07:49:28 +00002583
2584class Menubutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002585 """Menubutton widget, obsolete since Tk8.0."""
2586 def __init__(self, master=None, cnf={}, **kw):
2587 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002588
2589class Message(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002590 """Message widget to display multiline text. Obsolete since Label does it too."""
2591 def __init__(self, master=None, cnf={}, **kw):
2592 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002593
2594class Radiobutton(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002595 """Radiobutton widget which shows only one of several buttons in on-state."""
2596 def __init__(self, master=None, cnf={}, **kw):
2597 """Construct a radiobutton widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002598
Fredrik Lundh06d28152000-08-09 18:03:12 +00002599 Valid resource names: activebackground, activeforeground, anchor,
2600 background, bd, bg, bitmap, borderwidth, command, cursor,
2601 disabledforeground, fg, font, foreground, height,
2602 highlightbackground, highlightcolor, highlightthickness, image,
2603 indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
2604 state, takefocus, text, textvariable, underline, value, variable,
2605 width, wraplength."""
2606 Widget.__init__(self, master, 'radiobutton', cnf, kw)
2607 def deselect(self):
2608 """Put the button in off-state."""
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002609
Fredrik Lundh06d28152000-08-09 18:03:12 +00002610 self.tk.call(self._w, 'deselect')
2611 def flash(self):
2612 """Flash the button."""
2613 self.tk.call(self._w, 'flash')
2614 def invoke(self):
2615 """Toggle the button and invoke a command if given as resource."""
2616 return self.tk.call(self._w, 'invoke')
2617 def select(self):
2618 """Put the button in on-state."""
2619 self.tk.call(self._w, 'select')
Guido van Rossum18468821994-06-20 07:49:28 +00002620
2621class Scale(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002622 """Scale widget which can display a numerical scale."""
2623 def __init__(self, master=None, cnf={}, **kw):
2624 """Construct a scale widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002625
Fredrik Lundh06d28152000-08-09 18:03:12 +00002626 Valid resource names: activebackground, background, bigincrement, bd,
2627 bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
2628 highlightbackground, highlightcolor, highlightthickness, label,
2629 length, orient, relief, repeatdelay, repeatinterval, resolution,
2630 showvalue, sliderlength, sliderrelief, state, takefocus,
2631 tickinterval, to, troughcolor, variable, width."""
2632 Widget.__init__(self, master, 'scale', cnf, kw)
2633 def get(self):
2634 """Get the current value as integer or float."""
2635 value = self.tk.call(self._w, 'get')
2636 try:
2637 return getint(value)
2638 except ValueError:
2639 return getdouble(value)
2640 def set(self, value):
2641 """Set the value to VALUE."""
2642 self.tk.call(self._w, 'set', value)
2643 def coords(self, value=None):
2644 """Return a tuple (X,Y) of the point along the centerline of the
2645 trough that corresponds to VALUE or the current value if None is
2646 given."""
2647
2648 return self._getints(self.tk.call(self._w, 'coords', value))
2649 def identify(self, x, y):
2650 """Return where the point X,Y lies. Valid return values are "slider",
2651 "though1" and "though2"."""
2652 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00002653
2654class Scrollbar(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002655 """Scrollbar widget which displays a slider at a certain position."""
2656 def __init__(self, master=None, cnf={}, **kw):
2657 """Construct a scrollbar widget with the parent MASTER.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002658
Fredrik Lundh06d28152000-08-09 18:03:12 +00002659 Valid resource names: activebackground, activerelief,
2660 background, bd, bg, borderwidth, command, cursor,
2661 elementborderwidth, highlightbackground,
2662 highlightcolor, highlightthickness, jump, orient,
2663 relief, repeatdelay, repeatinterval, takefocus,
2664 troughcolor, width."""
2665 Widget.__init__(self, master, 'scrollbar', cnf, kw)
2666 def activate(self, index):
2667 """Display the element at INDEX with activebackground and activerelief.
2668 INDEX can be "arrow1","slider" or "arrow2"."""
2669 self.tk.call(self._w, 'activate', index)
2670 def delta(self, deltax, deltay):
2671 """Return the fractional change of the scrollbar setting if it
2672 would be moved by DELTAX or DELTAY pixels."""
2673 return getdouble(
2674 self.tk.call(self._w, 'delta', deltax, deltay))
2675 def fraction(self, x, y):
2676 """Return the fractional value which corresponds to a slider
2677 position of X,Y."""
2678 return getdouble(self.tk.call(self._w, 'fraction', x, y))
2679 def identify(self, x, y):
2680 """Return the element under position X,Y as one of
2681 "arrow1","slider","arrow2" or ""."""
2682 return self.tk.call(self._w, 'identify', x, y)
2683 def get(self):
2684 """Return the current fractional values (upper and lower end)
2685 of the slider position."""
2686 return self._getdoubles(self.tk.call(self._w, 'get'))
2687 def set(self, *args):
2688 """Set the fractional values of the slider position (upper and
2689 lower ends as value between 0 and 1)."""
2690 self.tk.call((self._w, 'set') + args)
Raymond Hettingerff41c482003-04-06 09:01:11 +00002691
2692
2693
Guido van Rossum18468821994-06-20 07:49:28 +00002694class Text(Widget):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002695 """Text widget which can display text in various forms."""
Fredrik Lundh06d28152000-08-09 18:03:12 +00002696 def __init__(self, master=None, cnf={}, **kw):
2697 """Construct a text widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002698
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002699 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002700
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002701 background, borderwidth, cursor,
2702 exportselection, font, foreground,
2703 highlightbackground, highlightcolor,
2704 highlightthickness, insertbackground,
2705 insertborderwidth, insertofftime,
2706 insertontime, insertwidth, padx, pady,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002707 relief, selectbackground,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002708 selectborderwidth, selectforeground,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002709 setgrid, takefocus,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002710 xscrollcommand, yscrollcommand,
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002711
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002712 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00002713
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002714 autoseparators, height, maxundo,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002715 spacing1, spacing2, spacing3,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002716 state, tabs, undo, width, wrap,
Raymond Hettingerff41c482003-04-06 09:01:11 +00002717
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002718 """
Fredrik Lundh06d28152000-08-09 18:03:12 +00002719 Widget.__init__(self, master, 'text', cnf, kw)
2720 def bbox(self, *args):
2721 """Return a tuple of (x,y,width,height) which gives the bounding
2722 box of the visible part of the character at the index in ARGS."""
2723 return self._getints(
2724 self.tk.call((self._w, 'bbox') + args)) or None
2725 def tk_textSelectTo(self, index):
2726 self.tk.call('tk_textSelectTo', self._w, index)
2727 def tk_textBackspace(self):
2728 self.tk.call('tk_textBackspace', self._w)
2729 def tk_textIndexCloser(self, a, b, c):
2730 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
2731 def tk_textResetAnchor(self, index):
2732 self.tk.call('tk_textResetAnchor', self._w, index)
2733 def compare(self, index1, op, index2):
2734 """Return whether between index INDEX1 and index INDEX2 the
2735 relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=."""
2736 return self.tk.getboolean(self.tk.call(
2737 self._w, 'compare', index1, op, index2))
2738 def debug(self, boolean=None):
2739 """Turn on the internal consistency checks of the B-Tree inside the text
2740 widget according to BOOLEAN."""
2741 return self.tk.getboolean(self.tk.call(
2742 self._w, 'debug', boolean))
2743 def delete(self, index1, index2=None):
2744 """Delete the characters between INDEX1 and INDEX2 (not included)."""
2745 self.tk.call(self._w, 'delete', index1, index2)
2746 def dlineinfo(self, index):
2747 """Return tuple (x,y,width,height,baseline) giving the bounding box
2748 and baseline position of the visible part of the line containing
2749 the character at INDEX."""
2750 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum256705b2002-04-23 13:29:43 +00002751 def dump(self, index1, index2=None, command=None, **kw):
2752 """Return the contents of the widget between index1 and index2.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002753
Guido van Rossum256705b2002-04-23 13:29:43 +00002754 The type of contents returned in filtered based on the keyword
2755 parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are
2756 given and true, then the corresponding items are returned. The result
2757 is a list of triples of the form (key, value, index). If none of the
2758 keywords are true then 'all' is used by default.
Raymond Hettingerff41c482003-04-06 09:01:11 +00002759
Guido van Rossum256705b2002-04-23 13:29:43 +00002760 If the 'command' argument is given, it is called once for each element
2761 of the list of triples, with the values of each triple serving as the
2762 arguments to the function. In this case the list is not returned."""
2763 args = []
2764 func_name = None
2765 result = None
2766 if not command:
2767 # Never call the dump command without the -command flag, since the
2768 # output could involve Tcl quoting and would be a pain to parse
2769 # right. Instead just set the command to build a list of triples
2770 # as if we had done the parsing.
2771 result = []
2772 def append_triple(key, value, index, result=result):
2773 result.append((key, value, index))
2774 command = append_triple
2775 try:
2776 if not isinstance(command, str):
2777 func_name = command = self._register(command)
2778 args += ["-command", command]
2779 for key in kw:
2780 if kw[key]: args.append("-" + key)
2781 args.append(index1)
2782 if index2:
2783 args.append(index2)
2784 self.tk.call(self._w, "dump", *args)
2785 return result
2786 finally:
2787 if func_name:
2788 self.deletecommand(func_name)
Raymond Hettingerff41c482003-04-06 09:01:11 +00002789
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002790 ## new in tk8.4
2791 def edit(self, *args):
2792 """Internal method
Raymond Hettingerff41c482003-04-06 09:01:11 +00002793
2794 This method controls the undo mechanism and
2795 the modified flag. The exact behavior of the
2796 command depends on the option argument that
2797 follows the edit argument. The following forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002798 of the command are currently supported:
Raymond Hettingerff41c482003-04-06 09:01:11 +00002799
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002800 edit_modified, edit_redo, edit_reset, edit_separator
2801 and edit_undo
Raymond Hettingerff41c482003-04-06 09:01:11 +00002802
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002803 """
2804 return self._getints(
2805 self.tk.call((self._w, 'edit') + args)) or ()
2806
2807 def edit_modified(self, arg=None):
2808 """Get or Set the modified flag
Raymond Hettingerff41c482003-04-06 09:01:11 +00002809
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002810 If arg is not specified, returns the modified
Raymond Hettingerff41c482003-04-06 09:01:11 +00002811 flag of the widget. The insert, delete, edit undo and
2812 edit redo commands or the user can set or clear the
2813 modified flag. If boolean is specified, sets the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002814 modified flag of the widget to arg.
2815 """
2816 return self.edit("modified", arg)
Raymond Hettingerff41c482003-04-06 09:01:11 +00002817
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002818 def edit_redo(self):
2819 """Redo the last undone edit
Raymond Hettingerff41c482003-04-06 09:01:11 +00002820
2821 When the undo option is true, reapplies the last
2822 undone edits provided no other edits were done since
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002823 then. Generates an error when the redo stack is empty.
2824 Does nothing when the undo option is false.
2825 """
2826 return self.edit("redo")
Raymond Hettingerff41c482003-04-06 09:01:11 +00002827
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002828 def edit_reset(self):
2829 """Clears the undo and redo stacks
2830 """
2831 return self.edit("reset")
Raymond Hettingerff41c482003-04-06 09:01:11 +00002832
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002833 def edit_separator(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00002834 """Inserts a separator (boundary) on the undo stack.
2835
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002836 Does nothing when the undo option is false
2837 """
2838 return self.edit("separator")
Raymond Hettingerff41c482003-04-06 09:01:11 +00002839
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002840 def edit_undo(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00002841 """Undoes the last edit action
2842
2843 If the undo option is true. An edit action is defined
2844 as all the insert and delete commands that are recorded
2845 on the undo stack in between two separators. Generates
2846 an error when the undo stack is empty. Does nothing
Martin v. Löwis2ec36272002-10-13 10:22:08 +00002847 when the undo option is false
2848 """
2849 return self.edit("undo")
Raymond Hettingerff41c482003-04-06 09:01:11 +00002850
Fredrik Lundh06d28152000-08-09 18:03:12 +00002851 def get(self, index1, index2=None):
2852 """Return the text from INDEX1 to INDEX2 (not included)."""
2853 return self.tk.call(self._w, 'get', index1, index2)
2854 # (Image commands are new in 8.0)
2855 def image_cget(self, index, option):
2856 """Return the value of OPTION of an embedded image at INDEX."""
2857 if option[:1] != "-":
2858 option = "-" + option
2859 if option[-1:] == "_":
2860 option = option[:-1]
2861 return self.tk.call(self._w, "image", "cget", index, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002862 def image_configure(self, index, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002863 """Configure an embedded image at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002864 return self._configure(('image', 'configure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002865 def image_create(self, index, cnf={}, **kw):
2866 """Create an embedded image at INDEX."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00002867 return self.tk.call(
2868 self._w, "image", "create", index,
2869 *self._options(cnf, kw))
Fredrik Lundh06d28152000-08-09 18:03:12 +00002870 def image_names(self):
2871 """Return all names of embedded images in this widget."""
2872 return self.tk.call(self._w, "image", "names")
2873 def index(self, index):
2874 """Return the index in the form line.char for INDEX."""
2875 return self.tk.call(self._w, 'index', index)
2876 def insert(self, index, chars, *args):
2877 """Insert CHARS before the characters at INDEX. An additional
2878 tag can be given in ARGS. Additional CHARS and tags can follow in ARGS."""
2879 self.tk.call((self._w, 'insert', index, chars) + args)
2880 def mark_gravity(self, markName, direction=None):
2881 """Change the gravity of a mark MARKNAME to DIRECTION (LEFT or RIGHT).
2882 Return the current value if None is given for DIRECTION."""
2883 return self.tk.call(
2884 (self._w, 'mark', 'gravity', markName, direction))
2885 def mark_names(self):
2886 """Return all mark names."""
2887 return self.tk.splitlist(self.tk.call(
2888 self._w, 'mark', 'names'))
2889 def mark_set(self, markName, index):
2890 """Set mark MARKNAME before the character at INDEX."""
2891 self.tk.call(self._w, 'mark', 'set', markName, index)
2892 def mark_unset(self, *markNames):
2893 """Delete all marks in MARKNAMES."""
2894 self.tk.call((self._w, 'mark', 'unset') + markNames)
2895 def mark_next(self, index):
2896 """Return the name of the next mark after INDEX."""
2897 return self.tk.call(self._w, 'mark', 'next', index) or None
2898 def mark_previous(self, index):
2899 """Return the name of the previous mark before INDEX."""
2900 return self.tk.call(self._w, 'mark', 'previous', index) or None
2901 def scan_mark(self, x, y):
2902 """Remember the current X, Y coordinates."""
2903 self.tk.call(self._w, 'scan', 'mark', x, y)
2904 def scan_dragto(self, x, y):
2905 """Adjust the view of the text to 10 times the
2906 difference between X and Y and the coordinates given in
2907 scan_mark."""
2908 self.tk.call(self._w, 'scan', 'dragto', x, y)
2909 def search(self, pattern, index, stopindex=None,
2910 forwards=None, backwards=None, exact=None,
2911 regexp=None, nocase=None, count=None):
2912 """Search PATTERN beginning from INDEX until STOPINDEX.
2913 Return the index of the first character of a match or an empty string."""
2914 args = [self._w, 'search']
2915 if forwards: args.append('-forwards')
2916 if backwards: args.append('-backwards')
2917 if exact: args.append('-exact')
2918 if regexp: args.append('-regexp')
2919 if nocase: args.append('-nocase')
2920 if count: args.append('-count'); args.append(count)
2921 if pattern[0] == '-': args.append('--')
2922 args.append(pattern)
2923 args.append(index)
2924 if stopindex: args.append(stopindex)
2925 return self.tk.call(tuple(args))
2926 def see(self, index):
2927 """Scroll such that the character at INDEX is visible."""
2928 self.tk.call(self._w, 'see', index)
2929 def tag_add(self, tagName, index1, *args):
2930 """Add tag TAGNAME to all characters between INDEX1 and index2 in ARGS.
2931 Additional pairs of indices may follow in ARGS."""
2932 self.tk.call(
2933 (self._w, 'tag', 'add', tagName, index1) + args)
2934 def tag_unbind(self, tagName, sequence, funcid=None):
2935 """Unbind for all characters with TAGNAME for event SEQUENCE the
2936 function identified with FUNCID."""
2937 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
2938 if funcid:
2939 self.deletecommand(funcid)
2940 def tag_bind(self, tagName, sequence, func, add=None):
2941 """Bind to all characters with TAGNAME at event SEQUENCE a call to function FUNC.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002942
Fredrik Lundh06d28152000-08-09 18:03:12 +00002943 An additional boolean parameter ADD specifies whether FUNC will be
2944 called additionally to the other bound function or whether it will
2945 replace the previous function. See bind for the return value."""
2946 return self._bind((self._w, 'tag', 'bind', tagName),
2947 sequence, func, add)
2948 def tag_cget(self, tagName, option):
2949 """Return the value of OPTION for tag TAGNAME."""
2950 if option[:1] != '-':
2951 option = '-' + option
2952 if option[-1:] == '_':
2953 option = option[:-1]
2954 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002955 def tag_configure(self, tagName, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00002956 """Configure a tag TAGNAME."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00002957 return self._configure(('tag', 'configure', tagName), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00002958 tag_config = tag_configure
2959 def tag_delete(self, *tagNames):
2960 """Delete all tags in TAGNAMES."""
2961 self.tk.call((self._w, 'tag', 'delete') + tagNames)
2962 def tag_lower(self, tagName, belowThis=None):
2963 """Change the priority of tag TAGNAME such that it is lower
2964 than the priority of BELOWTHIS."""
2965 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
2966 def tag_names(self, index=None):
2967 """Return a list of all tag names."""
2968 return self.tk.splitlist(
2969 self.tk.call(self._w, 'tag', 'names', index))
2970 def tag_nextrange(self, tagName, index1, index2=None):
2971 """Return a list of start and end index for the first sequence of
2972 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
2973 The text is searched forward from INDEX1."""
2974 return self.tk.splitlist(self.tk.call(
2975 self._w, 'tag', 'nextrange', tagName, index1, index2))
2976 def tag_prevrange(self, tagName, index1, index2=None):
2977 """Return a list of start and end index for the first sequence of
2978 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
2979 The text is searched backwards from INDEX1."""
2980 return self.tk.splitlist(self.tk.call(
2981 self._w, 'tag', 'prevrange', tagName, index1, index2))
2982 def tag_raise(self, tagName, aboveThis=None):
2983 """Change the priority of tag TAGNAME such that it is higher
2984 than the priority of ABOVETHIS."""
2985 self.tk.call(
2986 self._w, 'tag', 'raise', tagName, aboveThis)
2987 def tag_ranges(self, tagName):
2988 """Return a list of ranges of text which have tag TAGNAME."""
2989 return self.tk.splitlist(self.tk.call(
2990 self._w, 'tag', 'ranges', tagName))
2991 def tag_remove(self, tagName, index1, index2=None):
2992 """Remove tag TAGNAME from all characters between INDEX1 and INDEX2."""
2993 self.tk.call(
2994 self._w, 'tag', 'remove', tagName, index1, index2)
2995 def window_cget(self, index, option):
2996 """Return the value of OPTION of an embedded window at INDEX."""
2997 if option[:1] != '-':
2998 option = '-' + option
2999 if option[-1:] == '_':
3000 option = option[:-1]
3001 return self.tk.call(self._w, 'window', 'cget', index, option)
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003002 def window_configure(self, index, cnf=None, **kw):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003003 """Configure an embedded window at INDEX."""
Martin v. Löwis6ce13152002-10-10 14:36:13 +00003004 return self._configure(('window', 'configure', index), cnf, kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003005 window_config = window_configure
3006 def window_create(self, index, cnf={}, **kw):
3007 """Create a window at INDEX."""
3008 self.tk.call(
3009 (self._w, 'window', 'create', index)
3010 + self._options(cnf, kw))
3011 def window_names(self):
3012 """Return all names of embedded windows in this widget."""
3013 return self.tk.splitlist(
3014 self.tk.call(self._w, 'window', 'names'))
3015 def xview(self, *what):
3016 """Query and change horizontal position of the view."""
3017 if not what:
3018 return self._getdoubles(self.tk.call(self._w, 'xview'))
3019 self.tk.call((self._w, 'xview') + what)
Fredrik Lundh5bd2cd62000-08-09 18:29:51 +00003020 def xview_moveto(self, fraction):
3021 """Adjusts the view in the window so that FRACTION of the
3022 total width of the canvas is off-screen to the left."""
3023 self.tk.call(self._w, 'xview', 'moveto', fraction)
3024 def xview_scroll(self, number, what):
3025 """Shift the x-view according to NUMBER which is measured
3026 in "units" or "pages" (WHAT)."""
3027 self.tk.call(self._w, 'xview', 'scroll', number, what)
Fredrik Lundh8fffa202000-08-09 18:51:01 +00003028 def yview(self, *what):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003029 """Query and change vertical position of the view."""
Fredrik Lundh8fffa202000-08-09 18:51:01 +00003030 if not what:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003031 return self._getdoubles(self.tk.call(self._w, 'yview'))
Fredrik Lundh8fffa202000-08-09 18:51:01 +00003032 self.tk.call((self._w, 'yview') + what)
Fredrik Lundh5bd2cd62000-08-09 18:29:51 +00003033 def yview_moveto(self, fraction):
3034 """Adjusts the view in the window so that FRACTION of the
3035 total height of the canvas is off-screen to the top."""
3036 self.tk.call(self._w, 'yview', 'moveto', fraction)
3037 def yview_scroll(self, number, what):
3038 """Shift the y-view according to NUMBER which is measured
3039 in "units" or "pages" (WHAT)."""
3040 self.tk.call(self._w, 'yview', 'scroll', number, what)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003041 def yview_pickplace(self, *what):
3042 """Obsolete function, use see."""
3043 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00003044
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003045
Guido van Rossum28574b51996-10-21 15:16:51 +00003046class _setit:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003047 """Internal class. It wraps the command in the widget OptionMenu."""
3048 def __init__(self, var, value, callback=None):
3049 self.__value = value
3050 self.__var = var
3051 self.__callback = callback
3052 def __call__(self, *args):
3053 self.__var.set(self.__value)
3054 if self.__callback:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003055 self.__callback(self.__value, *args)
Guido van Rossum28574b51996-10-21 15:16:51 +00003056
3057class OptionMenu(Menubutton):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003058 """OptionMenu which allows the user to select a value from a menu."""
3059 def __init__(self, master, variable, value, *values, **kwargs):
3060 """Construct an optionmenu widget with the parent MASTER, with
3061 the resource textvariable set to VARIABLE, the initially selected
3062 value VALUE, the other menu values VALUES and an additional
3063 keyword argument command."""
3064 kw = {"borderwidth": 2, "textvariable": variable,
3065 "indicatoron": 1, "relief": RAISED, "anchor": "c",
3066 "highlightthickness": 2}
3067 Widget.__init__(self, master, "menubutton", kw)
3068 self.widgetName = 'tk_optionMenu'
3069 menu = self.__menu = Menu(self, name="menu", tearoff=0)
3070 self.menuname = menu._w
3071 # 'command' is the only supported keyword
3072 callback = kwargs.get('command')
3073 if kwargs.has_key('command'):
3074 del kwargs['command']
3075 if kwargs:
3076 raise TclError, 'unknown option -'+kwargs.keys()[0]
3077 menu.add_command(label=value,
3078 command=_setit(variable, value, callback))
3079 for v in values:
3080 menu.add_command(label=v,
3081 command=_setit(variable, v, callback))
3082 self["menu"] = menu
Guido van Rossum28574b51996-10-21 15:16:51 +00003083
Fredrik Lundh06d28152000-08-09 18:03:12 +00003084 def __getitem__(self, name):
3085 if name == 'menu':
3086 return self.__menu
3087 return Widget.__getitem__(self, name)
Guido van Rossum28574b51996-10-21 15:16:51 +00003088
Fredrik Lundh06d28152000-08-09 18:03:12 +00003089 def destroy(self):
3090 """Destroy this widget and the associated menu."""
3091 Menubutton.destroy(self)
3092 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00003093
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003094class Image:
Fredrik Lundh06d28152000-08-09 18:03:12 +00003095 """Base class for images."""
Martin v. Löwis0d8ce612000-09-08 16:28:30 +00003096 _last_id = 0
Fredrik Lundh06d28152000-08-09 18:03:12 +00003097 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
3098 self.name = None
3099 if not master:
3100 master = _default_root
3101 if not master:
3102 raise RuntimeError, 'Too early to create image'
3103 self.tk = master.tk
3104 if not name:
Martin v. Löwis0d8ce612000-09-08 16:28:30 +00003105 Image._last_id += 1
3106 name = "pyimage" +`Image._last_id` # tk itself would use image<x>
Fredrik Lundh06d28152000-08-09 18:03:12 +00003107 # The following is needed for systems where id(x)
3108 # can return a negative number, such as Linux/m68k:
3109 if name[0] == '-': name = '_' + name[1:]
3110 if kw and cnf: cnf = _cnfmerge((cnf, kw))
3111 elif kw: cnf = kw
3112 options = ()
3113 for k, v in cnf.items():
3114 if callable(v):
3115 v = self._register(v)
3116 options = options + ('-'+k, v)
3117 self.tk.call(('image', 'create', imgtype, name,) + options)
3118 self.name = name
3119 def __str__(self): return self.name
3120 def __del__(self):
3121 if self.name:
3122 try:
3123 self.tk.call('image', 'delete', self.name)
3124 except TclError:
3125 # May happen if the root was destroyed
3126 pass
3127 def __setitem__(self, key, value):
3128 self.tk.call(self.name, 'configure', '-'+key, value)
3129 def __getitem__(self, key):
3130 return self.tk.call(self.name, 'configure', '-'+key)
3131 def configure(self, **kw):
3132 """Configure the image."""
3133 res = ()
3134 for k, v in _cnfmerge(kw).items():
3135 if v is not None:
3136 if k[-1] == '_': k = k[:-1]
3137 if callable(v):
3138 v = self._register(v)
3139 res = res + ('-'+k, v)
3140 self.tk.call((self.name, 'config') + res)
3141 config = configure
3142 def height(self):
3143 """Return the height of the image."""
3144 return getint(
3145 self.tk.call('image', 'height', self.name))
3146 def type(self):
3147 """Return the type of the imgage, e.g. "photo" or "bitmap"."""
3148 return self.tk.call('image', 'type', self.name)
3149 def width(self):
3150 """Return the width of the image."""
3151 return getint(
3152 self.tk.call('image', 'width', self.name))
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003153
3154class PhotoImage(Image):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003155 """Widget which can display colored images in GIF, PPM/PGM format."""
3156 def __init__(self, name=None, cnf={}, master=None, **kw):
3157 """Create an image with NAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003158
Fredrik Lundh06d28152000-08-09 18:03:12 +00003159 Valid resource names: data, format, file, gamma, height, palette,
3160 width."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003161 Image.__init__(self, 'photo', name, cnf, master, **kw)
Fredrik Lundh06d28152000-08-09 18:03:12 +00003162 def blank(self):
3163 """Display a transparent image."""
3164 self.tk.call(self.name, 'blank')
3165 def cget(self, option):
3166 """Return the value of OPTION."""
3167 return self.tk.call(self.name, 'cget', '-' + option)
3168 # XXX config
3169 def __getitem__(self, key):
3170 return self.tk.call(self.name, 'cget', '-' + key)
3171 # XXX copy -from, -to, ...?
3172 def copy(self):
3173 """Return a new PhotoImage with the same image as this widget."""
3174 destImage = PhotoImage()
3175 self.tk.call(destImage, 'copy', self.name)
3176 return destImage
3177 def zoom(self,x,y=''):
3178 """Return a new PhotoImage with the same image as this widget
3179 but zoom it with X and Y."""
3180 destImage = PhotoImage()
3181 if y=='': y=x
3182 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
3183 return destImage
3184 def subsample(self,x,y=''):
3185 """Return a new PhotoImage based on the same image as this widget
3186 but use only every Xth or Yth pixel."""
3187 destImage = PhotoImage()
3188 if y=='': y=x
3189 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
3190 return destImage
3191 def get(self, x, y):
3192 """Return the color (red, green, blue) of the pixel at X,Y."""
3193 return self.tk.call(self.name, 'get', x, y)
3194 def put(self, data, to=None):
3195 """Put row formated colors to image starting from
3196 position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))"""
3197 args = (self.name, 'put', data)
3198 if to:
3199 if to[0] == '-to':
3200 to = to[1:]
3201 args = args + ('-to',) + tuple(to)
3202 self.tk.call(args)
3203 # XXX read
3204 def write(self, filename, format=None, from_coords=None):
3205 """Write image to file FILENAME in FORMAT starting from
3206 position FROM_COORDS."""
3207 args = (self.name, 'write', filename)
3208 if format:
3209 args = args + ('-format', format)
3210 if from_coords:
3211 args = args + ('-from',) + tuple(from_coords)
3212 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003213
3214class BitmapImage(Image):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003215 """Widget which can display a bitmap."""
3216 def __init__(self, name=None, cnf={}, master=None, **kw):
3217 """Create a bitmap with NAME.
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003218
Fredrik Lundh06d28152000-08-09 18:03:12 +00003219 Valid resource names: background, data, file, foreground, maskdata, maskfile."""
Raymond Hettingerff41c482003-04-06 09:01:11 +00003220 Image.__init__(self, 'bitmap', name, cnf, master, **kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003221
3222def image_names(): return _default_root.tk.call('image', 'names')
3223def image_types(): return _default_root.tk.call('image', 'types')
3224
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003225
3226class Spinbox(Widget):
3227 """spinbox widget."""
3228 def __init__(self, master=None, cnf={}, **kw):
3229 """Construct a spinbox widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003230
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003231 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003232
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003233 activebackground, background, borderwidth,
3234 cursor, exportselection, font, foreground,
3235 highlightbackground, highlightcolor,
3236 highlightthickness, insertbackground,
3237 insertborderwidth, insertofftime,
Raymond Hettingerff41c482003-04-06 09:01:11 +00003238 insertontime, insertwidth, justify, relief,
3239 repeatdelay, repeatinterval,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003240 selectbackground, selectborderwidth
3241 selectforeground, takefocus, textvariable
3242 xscrollcommand.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003243
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003244 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003245
3246 buttonbackground, buttoncursor,
3247 buttondownrelief, buttonuprelief,
3248 command, disabledbackground,
3249 disabledforeground, format, from,
3250 invalidcommand, increment,
3251 readonlybackground, state, to,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003252 validate, validatecommand values,
3253 width, wrap,
3254 """
3255 Widget.__init__(self, master, 'spinbox', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003256
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003257 def bbox(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003258 """Return a tuple of X1,Y1,X2,Y2 coordinates for a
3259 rectangle which encloses the character given by index.
3260
3261 The first two elements of the list give the x and y
3262 coordinates of the upper-left corner of the screen
3263 area covered by the character (in pixels relative
3264 to the widget) and the last two elements give the
3265 width and height of the character, in pixels. The
3266 bounding box may refer to a region outside the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003267 visible area of the window.
3268 """
3269 return self.tk.call(self._w, 'bbox', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003270
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003271 def delete(self, first, last=None):
3272 """Delete one or more elements of the spinbox.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003273
3274 First is the index of the first character to delete,
3275 and last is the index of the character just after
3276 the last one to delete. If last isn't specified it
3277 defaults to first+1, i.e. a single character is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003278 deleted. This command returns an empty string.
3279 """
3280 return self.tk.call(self._w, 'delete', first, last)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003281
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003282 def get(self):
3283 """Returns the spinbox's string"""
3284 return self.tk.call(self._w, 'get')
Raymond Hettingerff41c482003-04-06 09:01:11 +00003285
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003286 def icursor(self, index):
3287 """Alter the position of the insertion cursor.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003288
3289 The insertion cursor will be displayed just before
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003290 the character given by index. Returns an empty string
3291 """
3292 return self.tk.call(self._w, 'icursor', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003293
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003294 def identify(self, x, y):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003295 """Returns the name of the widget at position x, y
3296
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003297 Return value is one of: none, buttondown, buttonup, entry
3298 """
3299 return self.tk.call(self._w, 'identify', x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003300
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003301 def index(self, index):
3302 """Returns the numerical index corresponding to index
3303 """
3304 return self.tk.call(self._w, 'index', index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003305
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003306 def insert(self, index, s):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003307 """Insert string s at index
3308
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003309 Returns an empty string.
3310 """
3311 return self.tk.call(self._w, 'insert', index, s)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003312
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003313 def invoke(self, element):
3314 """Causes the specified element to be invoked
Raymond Hettingerff41c482003-04-06 09:01:11 +00003315
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003316 The element could be buttondown or buttonup
3317 triggering the action associated with it.
3318 """
3319 return self.tk.call(self._w, 'invoke', element)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003320
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003321 def scan(self, *args):
3322 """Internal function."""
3323 return self._getints(
3324 self.tk.call((self._w, 'scan') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003325
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003326 def scan_mark(self, x):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003327 """Records x and the current view in the spinbox window;
3328
3329 used in conjunction with later scan dragto commands.
3330 Typically this command is associated with a mouse button
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003331 press in the widget. It returns an empty string.
3332 """
3333 return self.scan("mark", x)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003334
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003335 def scan_dragto(self, x):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003336 """Compute the difference between the given x argument
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003337 and the x argument to the last scan mark command
Raymond Hettingerff41c482003-04-06 09:01:11 +00003338
3339 It then adjusts the view left or right by 10 times the
3340 difference in x-coordinates. This command is typically
3341 associated with mouse motion events in the widget, to
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003342 produce the effect of dragging the spinbox at high speed
3343 through the window. The return value is an empty string.
3344 """
3345 return self.scan("dragto", x)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003346
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003347 def selection(self, *args):
3348 """Internal function."""
3349 return self._getints(
3350 self.tk.call((self._w, 'selection') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003351
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003352 def selection_adjust(self, index):
3353 """Locate the end of the selection nearest to the character
Raymond Hettingerff41c482003-04-06 09:01:11 +00003354 given by index,
3355
3356 Then adjust that end of the selection to be at index
3357 (i.e including but not going beyond index). The other
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003358 end of the selection is made the anchor point for future
Raymond Hettingerff41c482003-04-06 09:01:11 +00003359 select to commands. If the selection isn't currently in
3360 the spinbox, then a new selection is created to include
3361 the characters between index and the most recent selection
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003362 anchor point, inclusive. Returns an empty string.
3363 """
3364 return self.selection("adjust", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003365
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003366 def selection_clear(self):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003367 """Clear the selection
3368
3369 If the selection isn't in this widget then the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003370 command has no effect. Returns an empty string.
3371 """
3372 return self.selection("clear")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003373
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003374 def selection_element(self, element=None):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003375 """Sets or gets the currently selected element.
3376
3377 If a spinbutton element is specified, it will be
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003378 displayed depressed
3379 """
3380 return self.selection("element", element)
3381
3382###########################################################################
3383
3384class LabelFrame(Widget):
3385 """labelframe widget."""
3386 def __init__(self, master=None, cnf={}, **kw):
3387 """Construct a labelframe widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003388
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003389 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003390
3391 borderwidth, cursor, font, foreground,
3392 highlightbackground, highlightcolor,
3393 highlightthickness, padx, pady, relief,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003394 takefocus, text
Raymond Hettingerff41c482003-04-06 09:01:11 +00003395
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003396 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003397
3398 background, class, colormap, container,
3399 height, labelanchor, labelwidget,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003400 visual, width
3401 """
3402 Widget.__init__(self, master, 'labelframe', cnf, kw)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003403
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003404########################################################################
3405
3406class PanedWindow(Widget):
3407 """panedwindow widget."""
3408 def __init__(self, master=None, cnf={}, **kw):
3409 """Construct a panedwindow widget with the parent MASTER.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003410
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003411 STANDARD OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003412
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003413 background, borderwidth, cursor, height,
3414 orient, relief, width
Raymond Hettingerff41c482003-04-06 09:01:11 +00003415
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003416 WIDGET-SPECIFIC OPTIONS
Raymond Hettingerff41c482003-04-06 09:01:11 +00003417
3418 handlepad, handlesize, opaqueresize,
3419 sashcursor, sashpad, sashrelief,
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003420 sashwidth, showhandle,
3421 """
3422 Widget.__init__(self, master, 'panedwindow', cnf, kw)
3423
3424 def add(self, child, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003425 """Add a child widget to the panedwindow in a new pane.
3426
3427 The child argument is the name of the child widget
3428 followed by pairs of arguments that specify how to
3429 manage the windows. Options may have any of the values
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003430 accepted by the configure subcommand.
3431 """
3432 self.tk.call((self._w, 'add', child) + self._options(kw))
Raymond Hettingerff41c482003-04-06 09:01:11 +00003433
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003434 def remove(self, child):
3435 """Remove the pane containing child from the panedwindow
Raymond Hettingerff41c482003-04-06 09:01:11 +00003436
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003437 All geometry management options for child will be forgotten.
3438 """
3439 self.tk.call(self._w, 'forget', child)
3440 forget=remove
Raymond Hettingerff41c482003-04-06 09:01:11 +00003441
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003442 def identify(self, x, y):
3443 """Identify the panedwindow component at point x, y
Raymond Hettingerff41c482003-04-06 09:01:11 +00003444
3445 If the point is over a sash or a sash handle, the result
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003446 is a two element list containing the index of the sash or
Raymond Hettingerff41c482003-04-06 09:01:11 +00003447 handle, and a word indicating whether it is over a sash
3448 or a handle, such as {0 sash} or {2 handle}. If the point
3449 is over any other part of the panedwindow, the result is
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003450 an empty list.
3451 """
3452 return self.tk.call(self._w, 'identify', x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003453
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003454 def proxy(self, *args):
3455 """Internal function."""
3456 return self._getints(
Raymond Hettingerff41c482003-04-06 09:01:11 +00003457 self.tk.call((self._w, 'proxy') + args)) or ()
3458
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003459 def proxy_coord(self):
3460 """Return the x and y pair of the most recent proxy location
3461 """
3462 return self.proxy("coord")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003463
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003464 def proxy_forget(self):
3465 """Remove the proxy from the display.
3466 """
3467 return self.proxy("forget")
Raymond Hettingerff41c482003-04-06 09:01:11 +00003468
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003469 def proxy_place(self, x, y):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003470 """Place the proxy at the given x and y coordinates.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003471 """
3472 return self.proxy("place", x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003473
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003474 def sash(self, *args):
3475 """Internal function."""
3476 return self._getints(
3477 self.tk.call((self._w, 'sash') + args)) or ()
Raymond Hettingerff41c482003-04-06 09:01:11 +00003478
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003479 def sash_coord(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003480 """Return the current x and y pair for the sash given by index.
3481
3482 Index must be an integer between 0 and 1 less than the
3483 number of panes in the panedwindow. The coordinates given are
3484 those of the top left corner of the region containing the sash.
3485 pathName sash dragto index x y This command computes the
3486 difference between the given coordinates and the coordinates
3487 given to the last sash coord command for the given sash. It then
3488 moves that sash the computed difference. The return value is the
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003489 empty string.
3490 """
3491 return self.sash("coord", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003492
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003493 def sash_mark(self, index):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003494 """Records x and y for the sash given by index;
3495
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003496 Used in conjunction with later dragto commands to move the sash.
3497 """
3498 return self.sash("mark", index)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003499
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003500 def sash_place(self, index, x, y):
3501 """Place the sash given by index at the given coordinates
3502 """
3503 return self.sash("place", index, x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +00003504
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003505 def panecget(self, child, option):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003506 """Query a management option for window.
3507
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003508 Option may be any value allowed by the paneconfigure subcommand
3509 """
3510 return self.tk.call(
3511 (self._w, 'panecget') + (child, '-'+option))
Raymond Hettingerff41c482003-04-06 09:01:11 +00003512
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003513 def paneconfigure(self, tagOrId, cnf=None, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +00003514 """Query or modify the management options for window.
3515
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003516 If no option is specified, returns a list describing all
Raymond Hettingerff41c482003-04-06 09:01:11 +00003517 of the available options for pathName. If option is
3518 specified with no value, then the command returns a list
3519 describing the one named option (this list will be identical
3520 to the corresponding sublist of the value returned if no
3521 option is specified). If one or more option-value pairs are
3522 specified, then the command modifies the given widget
3523 option(s) to have the given value(s); in this case the
3524 command returns an empty string. The following options
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003525 are supported:
Raymond Hettingerff41c482003-04-06 09:01:11 +00003526
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003527 after window
Raymond Hettingerff41c482003-04-06 09:01:11 +00003528 Insert the window after the window specified. window
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003529 should be the name of a window already managed by pathName.
3530 before window
Raymond Hettingerff41c482003-04-06 09:01:11 +00003531 Insert the window before the window specified. window
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003532 should be the name of a window already managed by pathName.
3533 height size
Raymond Hettingerff41c482003-04-06 09:01:11 +00003534 Specify a height for the window. The height will be the
3535 outer dimension of the window including its border, if
3536 any. If size is an empty string, or if -height is not
3537 specified, then the height requested internally by the
3538 window will be used initially; the height may later be
3539 adjusted by the movement of sashes in the panedwindow.
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003540 Size may be any value accepted by Tk_GetPixels.
3541 minsize n
Raymond Hettingerff41c482003-04-06 09:01:11 +00003542 Specifies that the size of the window cannot be made
3543 less than n. This constraint only affects the size of
3544 the widget in the paned dimension -- the x dimension
3545 for horizontal panedwindows, the y dimension for
3546 vertical panedwindows. May be any value accepted by
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003547 Tk_GetPixels.
3548 padx n
Raymond Hettingerff41c482003-04-06 09:01:11 +00003549 Specifies a non-negative value indicating how much
3550 extra space to leave on each side of the window in
3551 the X-direction. The value may have any of the forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003552 accepted by Tk_GetPixels.
3553 pady n
3554 Specifies a non-negative value indicating how much
Raymond Hettingerff41c482003-04-06 09:01:11 +00003555 extra space to leave on each side of the window in
3556 the Y-direction. The value may have any of the forms
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003557 accepted by Tk_GetPixels.
3558 sticky style
Raymond Hettingerff41c482003-04-06 09:01:11 +00003559 If a window's pane is larger than the requested
3560 dimensions of the window, this option may be used
3561 to position (or stretch) the window within its pane.
3562 Style is a string that contains zero or more of the
3563 characters n, s, e or w. The string can optionally
3564 contains spaces or commas, but they are ignored. Each
3565 letter refers to a side (north, south, east, or west)
3566 that the window will "stick" to. If both n and s
3567 (or e and w) are specified, the window will be
3568 stretched to fill the entire height (or width) of
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003569 its cavity.
3570 width size
Raymond Hettingerff41c482003-04-06 09:01:11 +00003571 Specify a width for the window. The width will be
3572 the outer dimension of the window including its
3573 border, if any. If size is an empty string, or
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003574 if -width is not specified, then the width requested
Raymond Hettingerff41c482003-04-06 09:01:11 +00003575 internally by the window will be used initially; the
3576 width may later be adjusted by the movement of sashes
3577 in the panedwindow. Size may be any value accepted by
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003578 Tk_GetPixels.
Raymond Hettingerff41c482003-04-06 09:01:11 +00003579
Martin v. Löwis2ec36272002-10-13 10:22:08 +00003580 """
3581 if cnf is None and not kw:
3582 cnf = {}
3583 for x in self.tk.split(
3584 self.tk.call(self._w,
3585 'paneconfigure', tagOrId)):
3586 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
3587 return cnf
3588 if type(cnf) == StringType and not kw:
3589 x = self.tk.split(self.tk.call(
3590 self._w, 'paneconfigure', tagOrId, '-'+cnf))
3591 return (x[0][1:],) + x[1:]
3592 self.tk.call((self._w, 'paneconfigure', tagOrId) +
3593 self._options(cnf, kw))
3594 paneconfig = paneconfigure
3595
3596 def panes(self):
3597 """Returns an ordered list of the child panes."""
3598 return self.tk.call(self._w, 'panes')
3599
Guido van Rossumaec5dc91994-06-27 07:55:12 +00003600######################################################################
3601# Extensions:
3602
3603class Studbutton(Button):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003604 def __init__(self, master=None, cnf={}, **kw):
3605 Widget.__init__(self, master, 'studbutton', cnf, kw)
3606 self.bind('<Any-Enter>', self.tkButtonEnter)
3607 self.bind('<Any-Leave>', self.tkButtonLeave)
3608 self.bind('<1>', self.tkButtonDown)
3609 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00003610
3611class Tributton(Button):
Fredrik Lundh06d28152000-08-09 18:03:12 +00003612 def __init__(self, master=None, cnf={}, **kw):
3613 Widget.__init__(self, master, 'tributton', cnf, kw)
3614 self.bind('<Any-Enter>', self.tkButtonEnter)
3615 self.bind('<Any-Leave>', self.tkButtonLeave)
3616 self.bind('<1>', self.tkButtonDown)
3617 self.bind('<ButtonRelease-1>', self.tkButtonUp)
3618 self['fg'] = self['bg']
3619 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00003620
Guido van Rossumc417ef81996-08-21 23:38:59 +00003621######################################################################
3622# Test:
3623
3624def _test():
Fredrik Lundh06d28152000-08-09 18:03:12 +00003625 root = Tk()
3626 text = "This is Tcl/Tk version %s" % TclVersion
3627 if TclVersion >= 8.1:
Fredrik Lundh8fffa202000-08-09 18:51:01 +00003628 try:
3629 text = text + unicode("\nThis should be a cedilla: \347",
3630 "iso-8859-1")
3631 except NameError:
3632 pass # no unicode support
Fredrik Lundh06d28152000-08-09 18:03:12 +00003633 label = Label(root, text=text)
3634 label.pack()
3635 test = Button(root, text="Click me!",
3636 command=lambda root=root: root.test.configure(
3637 text="[%s]" % root.test['text']))
3638 test.pack()
3639 root.test = test
3640 quit = Button(root, text="QUIT", command=root.destroy)
3641 quit.pack()
3642 # The following three commands are needed so the window pops
3643 # up on top on Windows...
3644 root.iconify()
3645 root.update()
3646 root.deiconify()
3647 root.mainloop()
Guido van Rossumc417ef81996-08-21 23:38:59 +00003648
3649if __name__ == '__main__':
Fredrik Lundh06d28152000-08-09 18:03:12 +00003650 _test()