blob: a70e0643c72dd55ab52ead5a9d8676e53dbefc4b [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,
6Checkbutton, Scale, Listbox, Scrollbar, OptionMenu. Properties of the widgets are
7specified with keyword arguments. Keyword arguments have the same
8name as the corresponding resource under Tk.
9
10Widgets are positioned with one of the geometry managers Place, Pack
11or Grid. These managers can be called with methods place, pack, grid
12available in every Widget.
13
14Actions are bound to events by resources (e.g. keyword argument command) or
15with the method bind.
16
17Example (Hello, World):
18import Tkinter
19from Tkconstants import *
20tk = Tkinter.Tk()
21frame = Tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
22frame.pack(fill=BOTH,expand=1)
23label = Tkinter.Label(frame, text="Hello, World")
24label.pack(fill=X, expand=1)
25button = Tkinter.Button(frame,text="Exit",command=tk.destroy)
26button.pack(side=BOTTOM)
27tk.mainloop()
28"""
Guido van Rossum2dcf5291994-07-06 09:23:20 +000029
Guido van Rossum37dcab11996-05-16 16:00:19 +000030__version__ = "$Revision$"
31
Guido van Rossumf8d579c1999-01-04 18:06:45 +000032import sys
33if sys.platform == "win32":
Guido van Rossumc55b0ca1999-02-08 15:26:49 +000034 import FixTk # Attempt to configure Tcl/Tk without requiring PATH
Guido van Rossumf8d579c1999-01-04 18:06:45 +000035import _tkinter # If this fails your Python may not be configured for Tk
Guido van Rossum95806091997-02-15 18:33:24 +000036tkinter = _tkinter # b/w compat for export
37TclError = _tkinter.TclError
Guido van Rossum7e9394a1995-03-17 16:21:33 +000038from types import *
Guido van Rossuma5773dd1995-09-07 19:22:00 +000039from Tkconstants import *
Guido van Rossum37dcab11996-05-16 16:00:19 +000040import string; _string = string; del string
Guido van Rossumf0c891a1998-04-29 21:43:36 +000041try:
42 import MacOS; _MacOS = MacOS; del MacOS
43except ImportError:
44 _MacOS = None
Guido van Rossum18468821994-06-20 07:49:28 +000045
Guido van Rossum95806091997-02-15 18:33:24 +000046TkVersion = _string.atof(_tkinter.TK_VERSION)
47TclVersion = _string.atof(_tkinter.TCL_VERSION)
Guido van Rossum18468821994-06-20 07:49:28 +000048
Guido van Rossumd6615ab1997-08-05 02:35:01 +000049READABLE = _tkinter.READABLE
50WRITABLE = _tkinter.WRITABLE
51EXCEPTION = _tkinter.EXCEPTION
Guido van Rossumf53c86c1997-08-14 14:15:54 +000052
53# These are not always defined, e.g. not on Win32 with Tk 8.0 :-(
54try: _tkinter.createfilehandler
55except AttributeError: _tkinter.createfilehandler = None
56try: _tkinter.deletefilehandler
57except AttributeError: _tkinter.deletefilehandler = None
Guido van Rossum36269991996-05-16 17:11:27 +000058
59
Guido van Rossum2dcf5291994-07-06 09:23:20 +000060def _flatten(tuple):
Guido van Rossum5917ecb2000-06-29 16:30:50 +000061 """Internal function."""
Guido van Rossum2dcf5291994-07-06 09:23:20 +000062 res = ()
63 for item in tuple:
64 if type(item) in (TupleType, ListType):
65 res = res + _flatten(item)
Guido van Rossum35f67fb1995-08-04 03:50:29 +000066 elif item is not None:
Guido van Rossum2dcf5291994-07-06 09:23:20 +000067 res = res + (item,)
68 return res
69
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000070try: _flatten = _tkinter._flatten
71except AttributeError: pass
72
Guido van Rossum2dcf5291994-07-06 09:23:20 +000073def _cnfmerge(cnfs):
Guido van Rossum5917ecb2000-06-29 16:30:50 +000074 """Internal function."""
Guido van Rossum761c5ab1995-07-14 15:29:10 +000075 if type(cnfs) is DictionaryType:
Guido van Rossum761c5ab1995-07-14 15:29:10 +000076 return cnfs
77 elif type(cnfs) in (NoneType, StringType):
Guido van Rossum2dcf5291994-07-06 09:23:20 +000078 return cnfs
79 else:
80 cnf = {}
81 for c in _flatten(cnfs):
Guido van Rossum65c78e11997-07-19 20:02:04 +000082 try:
83 cnf.update(c)
84 except (AttributeError, TypeError), msg:
85 print "_cnfmerge: fallback due to:", msg
86 for k, v in c.items():
87 cnf[k] = v
Guido van Rossum2dcf5291994-07-06 09:23:20 +000088 return cnf
89
Andrew M. Kuchlinge475e702000-06-18 18:45:50 +000090try: _cnfmerge = _tkinter._cnfmerge
91except AttributeError: pass
92
Guido van Rossum2dcf5291994-07-06 09:23:20 +000093class Event:
Guido van Rossum0a3f7972000-07-06 05:34:14 +000094 """Container for the properties of an event.
Guido van Rossum5917ecb2000-06-29 16:30:50 +000095
96 Instances of this type are generated if one of the following events occurs:
97
98 KeyPress, KeyRelease - for keyboard events
99 ButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse events
100 Visibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate,
101 Colormap, Gravity, Reparent, Property, Destroy, Activate,
102 Deactivate - for window events.
103
104 If a callback function for one of these events is registered
105 using bind, bind_all, bind_class, or tag_bind, the callback is
106 called with an Event as first argument. It will have the
107 following attributes (in braces are the event types for which
108 the attribute is valid):
109
110 serial - serial number of event
111 num - mouse button pressed (ButtonPress, ButtonRelease)
112 focus - whether the window has the focus (Enter, Leave)
113 height - height of the exposed window (Configure, Expose)
114 width - width of the exposed window (Configure, Expose)
115 keycode - keycode of the pressed key (KeyPress, KeyRelease)
116 state - state of the event as a number (ButtonPress, ButtonRelease,
117 Enter, KeyPress, KeyRelease,
118 Leave, Motion)
119 state - state as a string (Visibility)
120 time - when the event occured
121 x - x-position of the mouse
122 y - y-position of the mouse
123 x_root - x-position of the mouse on the screen
124 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
125 y_root - y-position of the mouse on the screen
126 (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
127 char - pressed character (KeyPress, KeyRelease)
128 send_event - see X/Windows documentation
129 keysym - keysym of the the event as a string (KeyPress, KeyRelease)
130 keysym_num - keysym of the event as a number (KeyPress, KeyRelease)
131 type - type of the event as a number
132 widget - widget in which the event occured
133 delta - delta of wheel movement (MouseWheel)
134 """
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000135 pass
136
Guido van Rossumc4570481998-03-20 20:45:49 +0000137_support_default_root = 1
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000138_default_root = None
139
Guido van Rossumc4570481998-03-20 20:45:49 +0000140def NoDefaultRoot():
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000141 """Inhibit setting of default root window.
142
143 Call this function to inhibit that the first instance of
144 Tk is used for windows without an explicit parent window.
145 """
Guido van Rossumc4570481998-03-20 20:45:49 +0000146 global _support_default_root
147 _support_default_root = 0
Guido van Rossumda654501998-10-06 19:06:27 +0000148 global _default_root
149 _default_root = None
Guido van Rossumc4570481998-03-20 20:45:49 +0000150 del _default_root
151
Guido van Rossum45853db1994-06-20 12:19:19 +0000152def _tkerror(err):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000153 """Internal function."""
Guido van Rossum18468821994-06-20 07:49:28 +0000154 pass
155
Guido van Rossum97aeca11994-07-07 13:12:12 +0000156def _exit(code='0'):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000157 """Internal function. Calling it will throw the exception SystemExit."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000158 raise SystemExit, code
Guido van Rossum97aeca11994-07-07 13:12:12 +0000159
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000160_varnum = 0
161class Variable:
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000162 """Internal class. Base class to define value holders for e.g. buttons."""
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000163 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000164 def __init__(self, master=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000165 """Construct a variable with an optional MASTER as master widget.
166 The variable is named PY_VAR_number in Tcl.
167 """
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000168 global _varnum
Guido van Rossume2c6e201998-01-14 16:44:34 +0000169 if not master:
170 master = _default_root
171 self._master = master
172 self._tk = master.tk
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000173 self._name = 'PY_VAR' + `_varnum`
174 _varnum = _varnum + 1
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000175 self.set(self._default)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000176 def __del__(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000177 """Unset the variable in Tcl."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000178 self._tk.globalunsetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000179 def __str__(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000180 """Return the name of the variable in Tcl."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000181 return self._name
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000182 def set(self, value):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000183 """Set the variable to VALUE."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000184 return self._tk.globalsetvar(self._name, value)
Guido van Rossume2c6e201998-01-14 16:44:34 +0000185 def trace_variable(self, mode, callback):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000186 """Define a trace callback for the variable.
187
188 MODE is one of "r", "w", "u" for read, write, undefine.
189 CALLBACK must be a function which is called when
190 the variable is read, written or undefined.
191
192 Return the name of the callback.
193 """
Guido van Rossume2c6e201998-01-14 16:44:34 +0000194 cbname = self._master._register(callback)
195 self._tk.call("trace", "variable", self._name, mode, cbname)
196 return cbname
197 trace = trace_variable
198 def trace_vdelete(self, mode, cbname):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000199 """Delete the trace callback for a variable.
200
201 MODE is one of "r", "w", "u" for read, write, undefine.
202 CBNAME is the name of the callback returned from trace_variable or trace.
203 """
Guido van Rossume2c6e201998-01-14 16:44:34 +0000204 self._tk.call("trace", "vdelete", self._name, mode, cbname)
Guido van Rossum0001a111998-02-19 21:20:30 +0000205 self._master.deletecommand(cbname)
Guido van Rossume2c6e201998-01-14 16:44:34 +0000206 def trace_vinfo(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000207 """Return all trace callback information."""
Guido van Rossume2c6e201998-01-14 16:44:34 +0000208 return map(self._tk.split, self._tk.splitlist(
209 self._tk.call("trace", "vinfo", self._name)))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000210
211class StringVar(Variable):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000212 """Value holder for strings variables."""
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000213 _default = ""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000214 def __init__(self, master=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000215 """Construct a string variable.
216
217 MASTER can be given as master widget."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000218 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000219
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000220 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000221 """Return value of variable as string."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000222 return self._tk.globalgetvar(self._name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000223
224class IntVar(Variable):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000225 """Value holder for integer variables."""
Guido van Rossum0b96b941996-12-27 15:30:20 +0000226 _default = 0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000227 def __init__(self, master=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000228 """Construct an integer variable.
229
230 MASTER can be given as master widget."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000231 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000232
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000233 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000234 """Return the value of the variable as an integer."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000235 return getint(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000236
237class DoubleVar(Variable):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000238 """Value holder for float variables."""
Guido van Rossum0b96b941996-12-27 15:30:20 +0000239 _default = 0.0
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000240 def __init__(self, master=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000241 """Construct a float variable.
242
243 MASTER can be given as a master widget."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000244 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000245
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000246 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000247 """Return the value of the variable as a float."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000248 return getdouble(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000249
250class BooleanVar(Variable):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000251 """Value holder for boolean variables."""
Guido van Rossume1a7a3b1996-09-05 16:45:49 +0000252 _default = "false"
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000253 def __init__(self, master=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000254 """Construct a boolean variable.
255
256 MASTER can be given as a master widget."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000257 Variable.__init__(self, master)
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000258
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000259 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000260 """Return the value of the variable as 0 or 1."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000261 return self._tk.getboolean(self._tk.globalgetvar(self._name))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000262
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000263def mainloop(n=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000264 """Run the main loop of Tcl."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000265 _default_root.tk.mainloop(n)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000266
Guido van Rossum0132f691998-04-30 17:50:36 +0000267getint = int
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000268
Guido van Rossum0132f691998-04-30 17:50:36 +0000269getdouble = float
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000270
271def getboolean(s):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000272 """Convert true and false to integer values 1 and 0."""
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000273 return _default_root.tk.getboolean(s)
274
Guido van Rossum368e06b1997-11-07 20:38:49 +0000275# Methods defined on both toplevel and interior widgets
Guido van Rossum18468821994-06-20 07:49:28 +0000276class Misc:
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000277 """Internal class.
278
279 Base class which defines methods common for interior widgets."""
280
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000281 # XXX font command?
Fred Drake526749b1997-05-03 04:16:23 +0000282 _tclCommands = None
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000283 def destroy(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000284 """Internal function.
285
286 Delete all Tcl commands created for
287 this widget in the Tcl interpreter."""
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000288 if self._tclCommands is not None:
289 for name in self._tclCommands:
290 #print '- Tkinter: deleted command', name
291 self.tk.deletecommand(name)
292 self._tclCommands = None
293 def deletecommand(self, name):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000294 """Internal function.
295
296 Delete the Tcl command provided in NAME."""
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000297 #print '- Tkinter: deleted command', name
298 self.tk.deletecommand(name)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000299 try:
300 self._tclCommands.remove(name)
301 except ValueError:
302 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000303 def tk_strictMotif(self, boolean=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000304 """Set Tcl internal variable, whether the look and feel
305 should adhere to Motif.
306
307 A parameter of 1 means adhere to Motif (e.g. no color
308 change if mouse passes over slider).
309 Returns the set value."""
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000310 return self.tk.getboolean(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000311 'set', 'tk_strictMotif', boolean))
Fred Drake3c602d71996-09-27 14:06:54 +0000312 def tk_bisque(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000313 """Change the color scheme to light brown as used in Tk 3.6 and before."""
Fred Drake3c602d71996-09-27 14:06:54 +0000314 self.tk.call('tk_bisque')
315 def tk_setPalette(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000316 """Set a new color scheme for all widget elements.
317
318 A single color as argument will cause that all colors of Tk
319 widget elements are derived from this.
320 Alternatively several keyword parameters and its associated
321 colors can be given. The following keywords are valid:
322 activeBackground, foreground, selectColor,
323 activeForeground, highlightBackground, selectBackground,
324 background, highlightColor, selectForeground,
325 disabledForeground, insertBackground, troughColor."""
Guido van Rossumf9756991998-04-29 21:57:08 +0000326 self.tk.call(('tk_setPalette',)
Fred Drake3faf9b41996-10-04 19:23:04 +0000327 + _flatten(args) + _flatten(kw.items()))
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000328 def tk_menuBar(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000329 """Do not use. Needed in Tk 3.6 and earlier."""
Guido van Rossum688bbfc1996-09-10 12:39:26 +0000330 pass # obsolete since Tk 4.0
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000331 def wait_variable(self, name='PY_VAR'):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000332 """Wait until the variable is modified.
333
334 A parameter of type IntVar, StringVar, DoubleVar or
335 BooleanVar must be given."""
Guido van Rossum18468821994-06-20 07:49:28 +0000336 self.tk.call('tkwait', 'variable', name)
Guido van Rossum2dcf5291994-07-06 09:23:20 +0000337 waitvar = wait_variable # XXX b/w compat
Guido van Rossum9beb9321994-06-27 23:15:31 +0000338 def wait_window(self, window=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000339 """Wait until a WIDGET is destroyed.
340
341 If no parameter is given self is used."""
Guido van Rossum9beb9321994-06-27 23:15:31 +0000342 if window == None:
343 window = self
344 self.tk.call('tkwait', 'window', window._w)
345 def wait_visibility(self, window=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000346 """Wait until the visibility of a WIDGET changes
347 (e.g. it appears).
348
349 If no parameter is given self is used."""
Guido van Rossum9beb9321994-06-27 23:15:31 +0000350 if window == None:
351 window = self
352 self.tk.call('tkwait', 'visibility', window._w)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000353 def setvar(self, name='PY_VAR', value='1'):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000354 """Set Tcl variable NAME to VALUE."""
Guido van Rossum18468821994-06-20 07:49:28 +0000355 self.tk.setvar(name, value)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000356 def getvar(self, name='PY_VAR'):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000357 """Return value of Tcl variable NAME."""
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000358 return self.tk.getvar(name)
Guido van Rossum0132f691998-04-30 17:50:36 +0000359 getint = int
360 getdouble = float
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000361 def getboolean(self, s):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000362 """Return 0 or 1 for Tcl boolean values true and false given as parameter."""
Guido van Rossum1e9e4001994-06-20 09:09:51 +0000363 return self.tk.getboolean(s)
Guido van Rossum45853db1994-06-20 12:19:19 +0000364 def focus_set(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000365 """Direct input focus to this widget.
366
367 If the application currently does not have the focus
368 this widget will get the focus if the application gets
369 the focus through the window manager."""
Guido van Rossum18468821994-06-20 07:49:28 +0000370 self.tk.call('focus', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000371 focus = focus_set # XXX b/w compat?
Fred Drake3c602d71996-09-27 14:06:54 +0000372 def focus_force(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000373 """Direct input focus to this widget even if the
374 application does not have the focus. Use with
375 caution!"""
Fred Drake3c602d71996-09-27 14:06:54 +0000376 self.tk.call('focus', '-force', self._w)
Guido van Rossum45853db1994-06-20 12:19:19 +0000377 def focus_get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000378 """Return the widget which has currently the focus in the
379 application.
380
381 Use focus_displayof to allow working with several
382 displays. Return None if application does not have
383 the focus."""
Guido van Rossum45853db1994-06-20 12:19:19 +0000384 name = self.tk.call('focus')
Guido van Rossum5468a7b1996-08-08 18:31:42 +0000385 if name == 'none' or not name: return None
Guido van Rossum45853db1994-06-20 12:19:19 +0000386 return self._nametowidget(name)
Fred Drake3c602d71996-09-27 14:06:54 +0000387 def focus_displayof(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000388 """Return the widget which has currently the focus on the
389 display where this widget is located.
390
391 Return None if the application does not have the focus."""
Fred Drake3c602d71996-09-27 14:06:54 +0000392 name = self.tk.call('focus', '-displayof', self._w)
393 if name == 'none' or not name: return None
394 return self._nametowidget(name)
395 def focus_lastfor(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000396 """Return the widget which would have the focus if top level
397 for this widget gets the focus from the window manager."""
Fred Drake3c602d71996-09-27 14:06:54 +0000398 name = self.tk.call('focus', '-lastfor', self._w)
399 if name == 'none' or not name: return None
400 return self._nametowidget(name)
401 def tk_focusFollowsMouse(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000402 """The widget under mouse will get automatically focus. Can not
403 be disabled easily."""
Fred Drake3c602d71996-09-27 14:06:54 +0000404 self.tk.call('tk_focusFollowsMouse')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000405 def tk_focusNext(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000406 """Return the next widget in the focus order which follows
407 widget which has currently the focus.
408
409 The focus order first goes to the next child, then to
410 the children of the child recursively and then to the
411 next sibling which is higher in the stacking order. A
412 widget is ommited if it has the takefocus resource set
413 to 0."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000414 name = self.tk.call('tk_focusNext', self._w)
415 if not name: return None
416 return self._nametowidget(name)
417 def tk_focusPrev(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000418 """Return previous widget in the focus order. See tk_focusNext for details."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000419 name = self.tk.call('tk_focusPrev', self._w)
420 if not name: return None
421 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000422 def after(self, ms, func=None, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000423 """Call function once after given time.
424
425 MS specifies the time in milliseconds. FUNC gives the
426 function which shall be called. Additional parameters
427 are given as parameters to the function call. Return
428 identifier to cancel scheduling with after_cancel."""
Guido van Rossum18468821994-06-20 07:49:28 +0000429 if not func:
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000430 # I'd rather use time.sleep(ms*0.001)
Guido van Rossum18468821994-06-20 07:49:28 +0000431 self.tk.call('after', ms)
432 else:
Guido van Rossum08a40381994-06-21 11:44:21 +0000433 # XXX Disgusting hack to clean up after calling func
434 tmp = []
Guido van Rossum103cc6d1997-04-14 13:30:24 +0000435 def callit(func=func, args=args, self=self, tmp=tmp):
Guido van Rossum08a40381994-06-21 11:44:21 +0000436 try:
437 apply(func, args)
438 finally:
Guido van Rossum0c920001998-09-14 19:06:39 +0000439 try:
440 self.deletecommand(tmp[0])
441 except TclError:
442 pass
Guido van Rossum08a40381994-06-21 11:44:21 +0000443 name = self._register(callit)
444 tmp.append(name)
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000445 return self.tk.call('after', ms, name)
446 def after_idle(self, func, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000447 """Call FUNC once if the Tcl main loop has no event to
448 process.
449
450 Return an identifier to cancel the scheduling with
451 after_cancel."""
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000452 return apply(self.after, ('idle', func) + args)
453 def after_cancel(self, id):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000454 """Cancel scheduling of function identified with ID.
455
456 Identifier returned by after or after_idle must be
457 given as first parameter."""
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000458 self.tk.call('after', 'cancel', id)
Fred Drake3c602d71996-09-27 14:06:54 +0000459 def bell(self, displayof=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000460 """Ring a display's bell."""
Guido van Rossumf9756991998-04-29 21:57:08 +0000461 self.tk.call(('bell',) + self._displayof(displayof))
Fred Drake3c602d71996-09-27 14:06:54 +0000462 # Clipboard handling:
463 def clipboard_clear(self, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000464 """Clear the data in the Tk clipboard.
465
466 A widget specified for the optional displayof keyword
467 argument specifies the target display."""
Fred Drake3c602d71996-09-27 14:06:54 +0000468 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000469 self.tk.call(('clipboard', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000470 def clipboard_append(self, string, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000471 """Append STRING to the Tk clipboard.
472
473 A widget specified at the optional displayof keyword
474 argument specifies the target display. The clipboard
475 can be retrieved with selection_get."""
Fred Drake3c602d71996-09-27 14:06:54 +0000476 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000477 self.tk.call(('clipboard', 'append') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000478 + ('--', string))
Guido van Rossum45853db1994-06-20 12:19:19 +0000479 # XXX grab current w/o window argument
480 def grab_current(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000481 """Return widget which has currently the grab in this application
482 or None."""
Guido van Rossum45853db1994-06-20 12:19:19 +0000483 name = self.tk.call('grab', 'current', self._w)
484 if not name: return None
485 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000486 def grab_release(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000487 """Release grab for this widget if currently set."""
Guido van Rossum18468821994-06-20 07:49:28 +0000488 self.tk.call('grab', 'release', self._w)
489 def grab_set(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000490 """Set grab for this widget.
491
492 A grab directs all events to this and descendant
493 widgets in the application."""
Guido van Rossum18468821994-06-20 07:49:28 +0000494 self.tk.call('grab', 'set', self._w)
495 def grab_set_global(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000496 """Set global grab for this widget.
497
498 A global grab directs all events to this and
499 descendant widgets on the display. Use with caution -
500 other applications do not get events anymore."""
Guido van Rossum18468821994-06-20 07:49:28 +0000501 self.tk.call('grab', 'set', '-global', self._w)
502 def grab_status(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000503 """Return None, "local" or "global" if this widget has
504 no, a local or a global grab."""
Guido van Rossum45853db1994-06-20 12:19:19 +0000505 status = self.tk.call('grab', 'status', self._w)
506 if status == 'none': status = None
507 return status
Guido van Rossum18468821994-06-20 07:49:28 +0000508 def lower(self, belowThis=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000509 """Lower this widget in the stacking order."""
Guido van Rossum18468821994-06-20 07:49:28 +0000510 self.tk.call('lower', self._w, belowThis)
Guido van Rossum780044f1994-10-20 22:02:27 +0000511 def option_add(self, pattern, value, priority = None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000512 """Set a VALUE (second parameter) for an option
513 PATTERN (first parameter).
514
515 An optional third parameter gives the numeric priority
516 (defaults to 80)."""
Guido van Rossum96ebbd31995-09-30 17:05:26 +0000517 self.tk.call('option', 'add', pattern, value, priority)
Guido van Rossum780044f1994-10-20 22:02:27 +0000518 def option_clear(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000519 """Clear the option database.
520
521 It will be reloaded if option_add is called."""
Guido van Rossum780044f1994-10-20 22:02:27 +0000522 self.tk.call('option', 'clear')
523 def option_get(self, name, className):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000524 """Return the value for an option NAME for this widget
525 with CLASSNAME.
526
527 Values with higher priority override lower values."""
Guido van Rossum780044f1994-10-20 22:02:27 +0000528 return self.tk.call('option', 'get', self._w, name, className)
529 def option_readfile(self, fileName, priority = None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000530 """Read file FILENAME into the option database.
531
532 An optional second paramter gives the numeric
533 priority."""
Guido van Rossum780044f1994-10-20 22:02:27 +0000534 self.tk.call('option', 'readfile', fileName, priority)
Fred Drake3c602d71996-09-27 14:06:54 +0000535 def selection_clear(self, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000536 """Clear the current X selection."""
Fred Drake3c602d71996-09-27 14:06:54 +0000537 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000538 self.tk.call(('selection', 'clear') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000539 def selection_get(self, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000540 """Return the contents of the current X selection.
541
542 A keyword parameter selection specifies the name of
543 the selection and defaults to PRIMARY. A keyword
544 parameter displayof specifies a widget on the display
545 to use."""
Fred Drake3c602d71996-09-27 14:06:54 +0000546 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000547 return self.tk.call(('selection', 'get') + self._options(kw))
Fred Drake3c602d71996-09-27 14:06:54 +0000548 def selection_handle(self, command, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000549 """Specify a function COMMAND to call if the X
550 selection owned by this widget is queried by another
551 application.
552
553 This function must return the contents of the
554 selection. The function will be called with the
555 arguments OFFSET and LENGTH which allows the chunking
556 of very long selections. The following keyword
557 parameters can be provided:
558 selection - name of the selection (default PRIMARY),
559 type - type of the selection (e.g. STRING, FILE_NAME)."""
Fred Drake3c602d71996-09-27 14:06:54 +0000560 name = self._register(command)
Guido van Rossumf9756991998-04-29 21:57:08 +0000561 self.tk.call(('selection', 'handle') + self._options(kw)
Fred Drake3c602d71996-09-27 14:06:54 +0000562 + (self._w, name))
563 def selection_own(self, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000564 """Become owner of X selection.
565
566 A keyword parameter selection specifies the name of
567 the selection (default PRIMARY)."""
Guido van Rossumf9756991998-04-29 21:57:08 +0000568 self.tk.call(('selection', 'own') +
569 self._options(kw) + (self._w,))
Fred Drake3c602d71996-09-27 14:06:54 +0000570 def selection_own_get(self, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000571 """Return owner of X selection.
572
573 The following keyword parameter can
574 be provided:
575 selection - name of the selection (default PRIMARY),
576 type - type of the selection (e.g. STRING, FILE_NAME)."""
Fred Drake3c602d71996-09-27 14:06:54 +0000577 if not kw.has_key('displayof'): kw['displayof'] = self._w
Guido van Rossumf9756991998-04-29 21:57:08 +0000578 name = self.tk.call(('selection', 'own') + self._options(kw))
Guido van Rossum76f587b1997-01-21 23:22:03 +0000579 if not name: return None
580 return self._nametowidget(name)
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000581 def send(self, interp, cmd, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000582 """Send Tcl command CMD to different interpreter INTERP to be executed."""
Guido van Rossumf9756991998-04-29 21:57:08 +0000583 return self.tk.call(('send', interp, cmd) + args)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000584 def lower(self, belowThis=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000585 """Lower this widget in the stacking order."""
Guido van Rossum6e8ec591996-09-11 14:25:41 +0000586 self.tk.call('lower', self._w, belowThis)
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000587 def tkraise(self, aboveThis=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000588 """Raise this widget in the stacking order."""
Guido van Rossum5e8d3721994-06-20 08:12:01 +0000589 self.tk.call('raise', self._w, aboveThis)
590 lift = tkraise
Guido van Rossum18468821994-06-20 07:49:28 +0000591 def colormodel(self, value=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000592 """Useless. Not implemented in Tk."""
Guido van Rossum18468821994-06-20 07:49:28 +0000593 return self.tk.call('tk', 'colormodel', self._w, value)
Fred Drake3c602d71996-09-27 14:06:54 +0000594 def winfo_atom(self, name, displayof=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000595 """Return integer which represents atom NAME."""
Fred Drake3c602d71996-09-27 14:06:54 +0000596 args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
Guido van Rossum0132f691998-04-30 17:50:36 +0000597 return getint(self.tk.call(args))
Fred Drake3c602d71996-09-27 14:06:54 +0000598 def winfo_atomname(self, id, displayof=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000599 """Return name of atom with identifier ID."""
Fred Drake3c602d71996-09-27 14:06:54 +0000600 args = ('winfo', 'atomname') \
601 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000602 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000603 def winfo_cells(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000604 """Return number of cells in the colormap for this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000605 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000606 self.tk.call('winfo', 'cells', self._w))
Guido van Rossum45853db1994-06-20 12:19:19 +0000607 def winfo_children(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000608 """Return a list of all widgets which are children of this widget."""
Guido van Rossum45853db1994-06-20 12:19:19 +0000609 return map(self._nametowidget,
610 self.tk.splitlist(self.tk.call(
611 'winfo', 'children', self._w)))
Guido van Rossum18468821994-06-20 07:49:28 +0000612 def winfo_class(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000613 """Return window class name of this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +0000614 return self.tk.call('winfo', 'class', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000615 def winfo_colormapfull(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000616 """Return true if at the last color request the colormap was full."""
Fred Drake3c602d71996-09-27 14:06:54 +0000617 return self.tk.getboolean(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000618 self.tk.call('winfo', 'colormapfull', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000619 def winfo_containing(self, rootX, rootY, displayof=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000620 """Return the widget which is at the root coordinates ROOTX, ROOTY."""
Fred Drake3c602d71996-09-27 14:06:54 +0000621 args = ('winfo', 'containing') \
622 + self._displayof(displayof) + (rootX, rootY)
Guido van Rossumf9756991998-04-29 21:57:08 +0000623 name = self.tk.call(args)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000624 if not name: return None
625 return self._nametowidget(name)
Guido van Rossum18468821994-06-20 07:49:28 +0000626 def winfo_depth(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000627 """Return the number of bits per pixel."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000628 return getint(self.tk.call('winfo', 'depth', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000629 def winfo_exists(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000630 """Return true if this widget exists."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000631 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000632 self.tk.call('winfo', 'exists', self._w))
633 def winfo_fpixels(self, number):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000634 """Return the number of pixels for the given distance NUMBER
635 (e.g. "3c") as float."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000636 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +0000637 'winfo', 'fpixels', self._w, number))
638 def winfo_geometry(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000639 """Return geometry string for this widget in the form "widthxheight+X+Y"."""
Guido van Rossum18468821994-06-20 07:49:28 +0000640 return self.tk.call('winfo', 'geometry', self._w)
641 def winfo_height(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000642 """Return heigth of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000643 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000644 self.tk.call('winfo', 'height', self._w))
645 def winfo_id(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000646 """Return identifier ID for this widget."""
Guido van Rossumcef4c841998-06-19 04:35:45 +0000647 return self.tk.getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000648 self.tk.call('winfo', 'id', self._w))
Fred Drake3c602d71996-09-27 14:06:54 +0000649 def winfo_interps(self, displayof=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000650 """Return the name of all Tcl interpreters for this display."""
Fred Drake3c602d71996-09-27 14:06:54 +0000651 args = ('winfo', 'interps') + self._displayof(displayof)
Guido van Rossumf9756991998-04-29 21:57:08 +0000652 return self.tk.splitlist(self.tk.call(args))
Guido van Rossum18468821994-06-20 07:49:28 +0000653 def winfo_ismapped(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000654 """Return true if this widget is mapped."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000655 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000656 self.tk.call('winfo', 'ismapped', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000657 def winfo_manager(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000658 """Return the window mananger name for this widget."""
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000659 return self.tk.call('winfo', 'manager', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000660 def winfo_name(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000661 """Return the name of this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +0000662 return self.tk.call('winfo', 'name', self._w)
663 def winfo_parent(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000664 """Return the name of the parent of this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +0000665 return self.tk.call('winfo', 'parent', self._w)
Fred Drake3c602d71996-09-27 14:06:54 +0000666 def winfo_pathname(self, id, displayof=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000667 """Return the pathname of the widget given by ID."""
Fred Drake3c602d71996-09-27 14:06:54 +0000668 args = ('winfo', 'pathname') \
669 + self._displayof(displayof) + (id,)
Guido van Rossumf9756991998-04-29 21:57:08 +0000670 return self.tk.call(args)
Guido van Rossum18468821994-06-20 07:49:28 +0000671 def winfo_pixels(self, number):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000672 """Rounded integer value of winfo_fpixels."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000673 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000674 self.tk.call('winfo', 'pixels', self._w, number))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000675 def winfo_pointerx(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000676 """Return the x coordinate of the pointer on the root window."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000677 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000678 self.tk.call('winfo', 'pointerx', self._w))
679 def winfo_pointerxy(self):
Guido van Rossum0a3f7972000-07-06 05:34:14 +0000680 """Return a tuple of x and y coordinates of the pointer on the root window."""
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000681 return self._getints(
682 self.tk.call('winfo', 'pointerxy', self._w))
683 def winfo_pointery(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000684 """Return the y coordinate of the pointer on the root window."""
Guido van Rossum0a3f7972000-07-06 05:34:14 +0000685 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000686 self.tk.call('winfo', 'pointery', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000687 def winfo_reqheight(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000688 """Return requested height of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000689 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000690 self.tk.call('winfo', 'reqheight', self._w))
691 def winfo_reqwidth(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000692 """Return requested width of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000693 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000694 self.tk.call('winfo', 'reqwidth', self._w))
695 def winfo_rgb(self, color):
Guido van Rossum0a3f7972000-07-06 05:34:14 +0000696 """Return tuple of decimal values for red, green, blue for
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000697 COLOR in this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +0000698 return self._getints(
699 self.tk.call('winfo', 'rgb', self._w, color))
700 def winfo_rootx(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000701 """Return x coordinate of upper left corner of this widget on the
702 root window."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000703 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000704 self.tk.call('winfo', 'rootx', self._w))
705 def winfo_rooty(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000706 """Return y coordinate of upper left corner of this widget on the
707 root window."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000708 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000709 self.tk.call('winfo', 'rooty', self._w))
710 def winfo_screen(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000711 """Return the screen name of this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +0000712 return self.tk.call('winfo', 'screen', self._w)
713 def winfo_screencells(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000714 """Return the number of the cells in the colormap of the screen
715 of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000716 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000717 self.tk.call('winfo', 'screencells', self._w))
718 def winfo_screendepth(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000719 """Return the number of bits per pixel of the root window of the
720 screen of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000721 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000722 self.tk.call('winfo', 'screendepth', self._w))
723 def winfo_screenheight(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000724 """Return the number of pixels of the height of the screen of this widget
725 in pixel."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000726 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000727 self.tk.call('winfo', 'screenheight', self._w))
728 def winfo_screenmmheight(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000729 """Return the number of pixels of the height of the screen of
730 this widget in mm."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000731 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000732 self.tk.call('winfo', 'screenmmheight', self._w))
733 def winfo_screenmmwidth(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000734 """Return the number of pixels of the width of the screen of
735 this widget in mm."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000736 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000737 self.tk.call('winfo', 'screenmmwidth', self._w))
738 def winfo_screenvisual(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000739 """Return one of the strings directcolor, grayscale, pseudocolor,
740 staticcolor, staticgray, or truecolor for the default
741 colormodel of this screen."""
Guido van Rossum18468821994-06-20 07:49:28 +0000742 return self.tk.call('winfo', 'screenvisual', self._w)
743 def winfo_screenwidth(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000744 """Return the number of pixels of the width of the screen of
745 this widget in pixel."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000746 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000747 self.tk.call('winfo', 'screenwidth', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000748 def winfo_server(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000749 """Return information of the X-Server of the screen of this widget in
750 the form "XmajorRminor vendor vendorVersion"."""
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000751 return self.tk.call('winfo', 'server', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +0000752 def winfo_toplevel(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000753 """Return the toplevel widget of this widget."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +0000754 return self._nametowidget(self.tk.call(
755 'winfo', 'toplevel', self._w))
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000756 def winfo_viewable(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000757 """Return true if the widget and all its higher anchestors are mapped."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000758 return getint(
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000759 self.tk.call('winfo', 'viewable', self._w))
Guido van Rossum18468821994-06-20 07:49:28 +0000760 def winfo_visual(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000761 """Return one of the strings directcolor, grayscale, pseudocolor,
762 staticcolor, staticgray, or truecolor for the
763 colormodel of this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +0000764 return self.tk.call('winfo', 'visual', self._w)
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000765 def winfo_visualid(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000766 """Return the X identifier for the visual for this widget."""
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000767 return self.tk.call('winfo', 'visualid', self._w)
768 def winfo_visualsavailable(self, includeids=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000769 """Return a list of all visuals available for the screen
770 of this widget.
771
772 Each item in the list consists of a visual name (see winfo_visual), a
773 depth and if INCLUDEIDS=1 is given also the X identifier."""
Guido van Rossumc0967cd1996-12-12 16:43:05 +0000774 data = self.tk.split(
775 self.tk.call('winfo', 'visualsavailable', self._w,
776 includeids and 'includeids' or None))
Guido van Rossumf8d8e071999-10-18 22:06:38 +0000777 return map(self.__winfo_parseitem, data)
778 def __winfo_parseitem(self, t):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000779 """Internal function."""
Guido van Rossumf8d8e071999-10-18 22:06:38 +0000780 return t[:1] + tuple(map(self.__winfo_getint, t[1:]))
781 def __winfo_getint(self, x):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000782 """Internal function."""
Guido van Rossumf8d8e071999-10-18 22:06:38 +0000783 return _string.atoi(x, 0)
Guido van Rossum18468821994-06-20 07:49:28 +0000784 def winfo_vrootheight(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000785 """Return the height of the virtual root window associated with this
786 widget in pixels. If there is no virtual root window return the
787 height of the screen."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000788 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000789 self.tk.call('winfo', 'vrootheight', self._w))
790 def winfo_vrootwidth(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000791 """Return the width of the virtual root window associated with this
792 widget in pixel. If there is no virtual root window return the
793 width of the screen."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000794 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000795 self.tk.call('winfo', 'vrootwidth', self._w))
796 def winfo_vrootx(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000797 """Return the x offset of the virtual root relative to the root
798 window of the screen of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000799 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000800 self.tk.call('winfo', 'vrootx', self._w))
801 def winfo_vrooty(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000802 """Return the y offset of the virtual root relative to the root
803 window of the screen of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000804 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000805 self.tk.call('winfo', 'vrooty', self._w))
806 def winfo_width(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000807 """Return the width of this widget."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000808 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000809 self.tk.call('winfo', 'width', self._w))
810 def winfo_x(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000811 """Return the x coordinate of the upper left corner of this widget
812 in the parent."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000813 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000814 self.tk.call('winfo', 'x', self._w))
815 def winfo_y(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000816 """Return the y coordinate of the upper left corner of this widget
817 in the parent."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000818 return getint(
Guido van Rossum18468821994-06-20 07:49:28 +0000819 self.tk.call('winfo', 'y', self._w))
820 def update(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000821 """Enter event loop until all pending events have been processed by Tcl."""
Guido van Rossum18468821994-06-20 07:49:28 +0000822 self.tk.call('update')
823 def update_idletasks(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000824 """Enter event loop until all idle callbacks have been called. This
825 will update the display of windows but not process events caused by
826 the user."""
Guido van Rossum18468821994-06-20 07:49:28 +0000827 self.tk.call('update', 'idletasks')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000828 def bindtags(self, tagList=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000829 """Set or get the list of bindtags for this widget.
830
831 With no argument return the list of all bindtags associated with
832 this widget. With a list of strings as argument the bindtags are
833 set to this list. The bindtags determine in which order events are
834 processed (see bind)."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000835 if tagList is None:
836 return self.tk.splitlist(
837 self.tk.call('bindtags', self._w))
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000838 else:
Guido van Rossum37dcab11996-05-16 16:00:19 +0000839 self.tk.call('bindtags', self._w, tagList)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000840 def _bind(self, what, sequence, func, add, needcleanup=1):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000841 """Internal function."""
Guido van Rossum88b63b81998-06-25 18:54:49 +0000842 if type(func) is StringType:
843 self.tk.call(what + (sequence, func))
844 elif func:
Guido van Rossum117a5a81998-03-27 21:26:51 +0000845 funcid = self._register(func, self._substitute,
846 needcleanup)
Guido van Rossumdc593401998-04-29 22:16:57 +0000847 cmd = ('%sif {"[%s %s]" == "break"} break\n'
848 %
849 (add and '+' or '',
850 funcid,
851 _string.join(self._subst_format)))
Guido van Rossumf9756991998-04-29 21:57:08 +0000852 self.tk.call(what + (sequence, cmd))
Guido van Rossum117a5a81998-03-27 21:26:51 +0000853 return funcid
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000854 elif sequence:
Guido van Rossumf9756991998-04-29 21:57:08 +0000855 return self.tk.call(what + (sequence,))
Guido van Rossumc86b7c61998-08-31 16:54:33 +0000856 else:
857 return self.tk.splitlist(self.tk.call(what))
Guido van Rossum37dcab11996-05-16 16:00:19 +0000858 def bind(self, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000859 """Bind to this widget at event SEQUENCE a call to function FUNC.
860
861 SEQUENCE is a string of concatenated event
862 patterns. An event pattern is of the form
863 <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one
864 of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
865 Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
866 B3, Alt, Button4, B4, Double, Button5, B5 Triple,
867 Mod1, M1. TYPE is one of Activate, Enter, Map,
868 ButtonPress, Button, Expose, Motion, ButtonRelease
869 FocusIn, MouseWheel, Circulate, FocusOut, Property,
870 Colormap, Gravity Reparent, Configure, KeyPress, Key,
871 Unmap, Deactivate, KeyRelease Visibility, Destroy,
872 Leave and DETAIL is the button number for ButtonPress,
873 ButtonRelease and DETAIL is the Keysym for KeyPress and
874 KeyRelease. Examples are
875 <Control-Button-1> for pressing Control and mouse button 1 or
876 <Alt-A> for pressing A and the Alt key (KeyPress can be omitted).
877 An event pattern can also be a virtual event of the form
878 <<AString>> where AString can be arbitrary. This
879 event can be generated by event_generate.
880 If events are concatenated they must appear shortly
881 after each other.
882
883 FUNC will be called if the event sequence occurs with an
884 instance of Event as argument. If the return value of FUNC is
885 "break" no further bound function is invoked.
886
887 An additional boolean parameter ADD specifies whether FUNC will
888 be called additionally to the other bound function or whether
889 it will replace the previous function.
890
891 Bind will return an identifier to allow deletion of the bound function with
892 unbind without memory leak.
893
894 If FUNC or SEQUENCE is omitted the bound function or list
895 of bound events are returned."""
896
Guido van Rossum37dcab11996-05-16 16:00:19 +0000897 return self._bind(('bind', self._w), sequence, func, add)
Guido van Rossum117a5a81998-03-27 21:26:51 +0000898 def unbind(self, sequence, funcid=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000899 """Unbind for this widget for event SEQUENCE the
900 function identified with FUNCID."""
Guido van Rossumef8f8811994-08-08 12:47:33 +0000901 self.tk.call('bind', self._w, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +0000902 if funcid:
903 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +0000904 def bind_all(self, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000905 """Bind to all widgets at an event SEQUENCE a call to function FUNC.
906 An additional boolean parameter ADD specifies whether FUNC will
907 be called additionally to the other bound function or whether
908 it will replace the previous function. See bind for the return value."""
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000909 return self._bind(('bind', 'all'), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000910 def unbind_all(self, sequence):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000911 """Unbind for all widgets for event SEQUENCE all functions."""
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000912 self.tk.call('bind', 'all' , sequence, '')
Guido van Rossum37dcab11996-05-16 16:00:19 +0000913 def bind_class(self, className, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000914
915 """Bind to widgets with bindtag CLASSNAME at event
916 SEQUENCE a call of function FUNC. An additional
917 boolean parameter ADD specifies whether FUNC will be
918 called additionally to the other bound function or
919 whether it will replace the previous function. See bind for
920 the return value."""
921
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000922 return self._bind(('bind', className), sequence, func, add, 0)
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000923 def unbind_class(self, className, sequence):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000924 """Unbind for a all widgets with bindtag CLASSNAME for event SEQUENCE
925 all functions."""
Guido van Rossum761c5ab1995-07-14 15:29:10 +0000926 self.tk.call('bind', className , sequence, '')
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000927 def mainloop(self, n=0):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000928 """Call the mainloop of Tk."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000929 self.tk.mainloop(n)
Guido van Rossum18468821994-06-20 07:49:28 +0000930 def quit(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000931 """Quit the Tcl interpreter. All widgets will be destroyed."""
Guido van Rossum18468821994-06-20 07:49:28 +0000932 self.tk.quit()
Guido van Rossum18468821994-06-20 07:49:28 +0000933 def _getints(self, string):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000934 """Internal function."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000935 if string:
936 return tuple(map(getint, self.tk.splitlist(string)))
Guido van Rossum7e9394a1995-03-17 16:21:33 +0000937 def _getdoubles(self, string):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000938 """Internal function."""
Guido van Rossum0132f691998-04-30 17:50:36 +0000939 if string:
940 return tuple(map(getdouble, self.tk.splitlist(string)))
Guido van Rossum18468821994-06-20 07:49:28 +0000941 def _getboolean(self, string):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000942 """Internal function."""
Guido van Rossum18468821994-06-20 07:49:28 +0000943 if string:
944 return self.tk.getboolean(string)
Fred Drake3c602d71996-09-27 14:06:54 +0000945 def _displayof(self, displayof):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000946 """Internal function."""
Fred Drake3c602d71996-09-27 14:06:54 +0000947 if displayof:
948 return ('-displayof', displayof)
949 if displayof is None:
950 return ('-displayof', self._w)
951 return ()
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000952 def _options(self, cnf, kw = None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000953 """Internal function."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +0000954 if kw:
955 cnf = _cnfmerge((cnf, kw))
956 else:
957 cnf = _cnfmerge(cnf)
Guido van Rossum18468821994-06-20 07:49:28 +0000958 res = ()
959 for k, v in cnf.items():
Fred Drake3c602d71996-09-27 14:06:54 +0000960 if v is not None:
961 if k[-1] == '_': k = k[:-1]
962 if callable(v):
963 v = self._register(v)
964 res = res + ('-'+k, v)
Guido van Rossum18468821994-06-20 07:49:28 +0000965 return res
Guido van Rossum98b9d771997-12-12 00:09:34 +0000966 def nametowidget(self, name):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000967 """Return the Tkinter instance of a widget identified by
968 its Tcl name NAME."""
Guido van Rossum45853db1994-06-20 12:19:19 +0000969 w = self
970 if name[0] == '.':
971 w = w._root()
972 name = name[1:]
Guido van Rossum37dcab11996-05-16 16:00:19 +0000973 find = _string.find
Guido van Rossum45853db1994-06-20 12:19:19 +0000974 while name:
975 i = find(name, '.')
976 if i >= 0:
977 name, tail = name[:i], name[i+1:]
978 else:
979 tail = ''
980 w = w.children[name]
981 name = tail
982 return w
Guido van Rossum98b9d771997-12-12 00:09:34 +0000983 _nametowidget = nametowidget
Guido van Rossum5ac00ac1997-12-11 02:03:55 +0000984 def _register(self, func, subst=None, needcleanup=1):
Guido van Rossum5917ecb2000-06-29 16:30:50 +0000985 """Return a newly created Tcl function. If this
986 function is called, the Python function FUNC will
987 be executed. An optional function SUBST can
988 be given which will be executed before FUNC."""
Guido van Rossum37dcab11996-05-16 16:00:19 +0000989 f = CallWrapper(func, subst, self).__call__
Guido van Rossum18468821994-06-20 07:49:28 +0000990 name = `id(f)`
Guido van Rossum37dcab11996-05-16 16:00:19 +0000991 try:
Guido van Rossum18468821994-06-20 07:49:28 +0000992 func = func.im_func
Guido van Rossum37dcab11996-05-16 16:00:19 +0000993 except AttributeError:
994 pass
995 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +0000996 name = name + func.__name__
Guido van Rossum37dcab11996-05-16 16:00:19 +0000997 except AttributeError:
998 pass
Guido van Rossum18468821994-06-20 07:49:28 +0000999 self.tk.createcommand(name, f)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00001000 if needcleanup:
1001 if self._tclCommands is None:
1002 self._tclCommands = []
Guido van Rossumc4570481998-03-20 20:45:49 +00001003 self._tclCommands.append(name)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001004 #print '+ Tkinter created command', name
Guido van Rossum18468821994-06-20 07:49:28 +00001005 return name
Guido van Rossum9beb9321994-06-27 23:15:31 +00001006 register = _register
Guido van Rossum45853db1994-06-20 12:19:19 +00001007 def _root(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001008 """Internal function."""
Guido van Rossum45853db1994-06-20 12:19:19 +00001009 w = self
1010 while w.master: w = w.master
1011 return w
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001012 _subst_format = ('%#', '%b', '%f', '%h', '%k',
Guido van Rossum45853db1994-06-20 12:19:19 +00001013 '%s', '%t', '%w', '%x', '%y',
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001014 '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D')
Guido van Rossum45853db1994-06-20 12:19:19 +00001015 def _substitute(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001016 """Internal function."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001017 if len(args) != len(self._subst_format): return args
Guido van Rossum0132f691998-04-30 17:50:36 +00001018 getboolean = self.tk.getboolean
1019 getint = int
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001020 nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args
Guido van Rossum45853db1994-06-20 12:19:19 +00001021 # Missing: (a, c, d, m, o, v, B, R)
1022 e = Event()
Guido van Rossum0132f691998-04-30 17:50:36 +00001023 e.serial = getint(nsign)
1024 e.num = getint(b)
1025 try: e.focus = getboolean(f)
Guido van Rossum45853db1994-06-20 12:19:19 +00001026 except TclError: pass
Guido van Rossum0132f691998-04-30 17:50:36 +00001027 e.height = getint(h)
1028 e.keycode = getint(k)
Guido van Rossum36269991996-05-16 17:11:27 +00001029 # For Visibility events, event state is a string and
1030 # not an integer:
1031 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00001032 e.state = getint(s)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00001033 except ValueError:
Guido van Rossum36269991996-05-16 17:11:27 +00001034 e.state = s
Guido van Rossum0132f691998-04-30 17:50:36 +00001035 e.time = getint(t)
1036 e.width = getint(w)
1037 e.x = getint(x)
1038 e.y = getint(y)
Guido van Rossum45853db1994-06-20 12:19:19 +00001039 e.char = A
Guido van Rossum0132f691998-04-30 17:50:36 +00001040 try: e.send_event = getboolean(E)
Guido van Rossum45853db1994-06-20 12:19:19 +00001041 except TclError: pass
1042 e.keysym = K
Guido van Rossum0132f691998-04-30 17:50:36 +00001043 e.keysym_num = getint(N)
Guido van Rossum45853db1994-06-20 12:19:19 +00001044 e.type = T
Guido van Rossume86271a1998-04-27 19:32:59 +00001045 try:
1046 e.widget = self._nametowidget(W)
1047 except KeyError:
1048 e.widget = W
Guido van Rossum0132f691998-04-30 17:50:36 +00001049 e.x_root = getint(X)
1050 e.y_root = getint(Y)
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001051 e.delta = getint(D)
Guido van Rossum45853db1994-06-20 12:19:19 +00001052 return (e,)
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001053 def _report_exception(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001054 """Internal function."""
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001055 import sys
1056 exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
1057 root = self._root()
1058 root.report_callback_exception(exc, val, tb)
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001059 # These used to be defined in Widget:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001060 def configure(self, cnf=None, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001061 """Configure resources of a widget.
1062
1063 The values for resources are specified as keyword
1064 arguments. To get an overview about
1065 the allowed keyword arguments call the method keys.
1066 """
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001067 # XXX ought to generalize this so tag_config etc. can use it
1068 if kw:
1069 cnf = _cnfmerge((cnf, kw))
1070 elif cnf:
1071 cnf = _cnfmerge(cnf)
1072 if cnf is None:
1073 cnf = {}
1074 for x in self.tk.split(
1075 self.tk.call(self._w, 'configure')):
1076 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
1077 return cnf
1078 if type(cnf) is StringType:
1079 x = self.tk.split(self.tk.call(
1080 self._w, 'configure', '-'+cnf))
1081 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00001082 self.tk.call((self._w, 'configure')
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001083 + self._options(cnf))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001084 config = configure
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001085 def cget(self, key):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001086 """Return the resource value for a KEY given as string."""
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001087 return self.tk.call(self._w, 'cget', '-' + key)
1088 __getitem__ = cget
1089 def __setitem__(self, key, value):
Guido van Rossum368e06b1997-11-07 20:38:49 +00001090 self.configure({key: value})
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001091 def keys(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001092 """Return a list of all resource names of this widget."""
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001093 return map(lambda x: x[0][1:],
1094 self.tk.split(self.tk.call(self._w, 'configure')))
1095 def __str__(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001096 """Return the window path name of this widget."""
Guido van Rossum83bd9a91997-09-29 23:24:52 +00001097 return self._w
Guido van Rossum368e06b1997-11-07 20:38:49 +00001098 # Pack methods that apply to the master
1099 _noarg_ = ['_noarg_']
1100 def pack_propagate(self, flag=_noarg_):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001101 """Set or get the status for propagation of geometry information.
1102
1103 A boolean argument specifies whether the geometry information
1104 of the slaves will determine the size of this widget. If no argument
1105 is given the current setting will be returned.
1106 """
Guido van Rossum368e06b1997-11-07 20:38:49 +00001107 if flag is Misc._noarg_:
1108 return self._getboolean(self.tk.call(
1109 'pack', 'propagate', self._w))
1110 else:
1111 self.tk.call('pack', 'propagate', self._w, flag)
1112 propagate = pack_propagate
1113 def pack_slaves(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001114 """Return a list of all slaves of this widget
1115 in its packing order."""
Guido van Rossum368e06b1997-11-07 20:38:49 +00001116 return map(self._nametowidget,
1117 self.tk.splitlist(
1118 self.tk.call('pack', 'slaves', self._w)))
1119 slaves = pack_slaves
1120 # Place method that applies to the master
1121 def place_slaves(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001122 """Return a list of all slaves of this widget
1123 in its packing order."""
Guido van Rossum368e06b1997-11-07 20:38:49 +00001124 return map(self._nametowidget,
1125 self.tk.splitlist(
1126 self.tk.call(
1127 'place', 'slaves', self._w)))
1128 # Grid methods that apply to the master
Barry Warsaw107e6231998-12-15 00:44:15 +00001129 def grid_bbox(self, column=None, row=None, col2=None, row2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001130 """Return a tuple of integer coordinates for the bounding
1131 box of this widget controlled by the geometry manager grid.
1132
1133 If COLUMN, ROW is given the bounding box applies from
1134 the cell with row and column 0 to the specified
1135 cell. If COL2 and ROW2 are given the bounding box
1136 starts at that cell.
1137
1138 The returned integers specify the offset of the uppler left
1139 corner in the master widget and the width and height.
1140 """
Barry Warsaw107e6231998-12-15 00:44:15 +00001141 args = ('grid', 'bbox', self._w)
1142 if column is not None and row is not None:
1143 args = args + (column, row)
1144 if col2 is not None and row2 is not None:
1145 args = args + (col2, row2)
1146 return self._getints(apply(self.tk.call, args)) or None
1147
Guido van Rossum368e06b1997-11-07 20:38:49 +00001148 bbox = grid_bbox
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001149 def _grid_configure(self, command, index, cnf, kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001150 """Internal function."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001151 if type(cnf) is StringType and not kw:
1152 if cnf[-1:] == '_':
1153 cnf = cnf[:-1]
1154 if cnf[:1] != '-':
1155 cnf = '-'+cnf
1156 options = (cnf,)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001157 else:
1158 options = self._options(cnf, kw)
1159 if not options:
1160 res = self.tk.call('grid',
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001161 command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001162 words = self.tk.splitlist(res)
1163 dict = {}
1164 for i in range(0, len(words), 2):
1165 key = words[i][1:]
1166 value = words[i+1]
1167 if not value:
1168 value = None
1169 elif '.' in value:
Guido van Rossum0132f691998-04-30 17:50:36 +00001170 value = getdouble(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001171 else:
Guido van Rossum0132f691998-04-30 17:50:36 +00001172 value = getint(value)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001173 dict[key] = value
1174 return dict
Guido van Rossumf9756991998-04-29 21:57:08 +00001175 res = self.tk.call(
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001176 ('grid', command, self._w, index)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001177 + options)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001178 if len(options) == 1:
1179 if not res: return None
1180 # In Tk 7.5, -width can be a float
Guido van Rossum0132f691998-04-30 17:50:36 +00001181 if '.' in res: return getdouble(res)
1182 return getint(res)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001183 def grid_columnconfigure(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001184 """Configure column INDEX of a grid.
1185
1186 Valid resources are minsize (minimum size of the column),
1187 weight (how much does additional space propagate to this column)
1188 and pad (how much space to let additionally)."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001189 return self._grid_configure('columnconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001190 columnconfigure = grid_columnconfigure
1191 def grid_propagate(self, flag=_noarg_):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001192 """Set or get the status for propagation of geometry information.
1193
1194 A boolean argument specifies whether the geometry information
1195 of the slaves will determine the size of this widget. If no argument
1196 is given, the current setting will be returned.
1197 """
Guido van Rossum368e06b1997-11-07 20:38:49 +00001198 if flag is Misc._noarg_:
1199 return self._getboolean(self.tk.call(
1200 'grid', 'propagate', self._w))
1201 else:
1202 self.tk.call('grid', 'propagate', self._w, flag)
1203 def grid_rowconfigure(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001204 """Configure row INDEX of a grid.
1205
1206 Valid resources are minsize (minimum size of the row),
1207 weight (how much does additional space propagate to this row)
1208 and pad (how much space to let additionally)."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001209 return self._grid_configure('rowconfigure', index, cnf, kw)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001210 rowconfigure = grid_rowconfigure
1211 def grid_size(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001212 """Return a tuple of the number of column and rows in the grid."""
Guido van Rossum368e06b1997-11-07 20:38:49 +00001213 return self._getints(
1214 self.tk.call('grid', 'size', self._w)) or None
1215 size = grid_size
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001216 def grid_slaves(self, row=None, column=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001217 """Return a list of all slaves of this widget
1218 in its packing order."""
Guido van Rossum1cd6a451997-12-30 04:07:19 +00001219 args = ()
Guido van Rossumeb354b31999-03-16 21:54:50 +00001220 if row is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001221 args = args + ('-row', row)
Guido van Rossumeb354b31999-03-16 21:54:50 +00001222 if column is not None:
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001223 args = args + ('-column', column)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001224 return map(self._nametowidget,
Guido van Rossumf9756991998-04-29 21:57:08 +00001225 self.tk.splitlist(self.tk.call(
1226 ('grid', 'slaves', self._w) + args)))
Guido van Rossum18468821994-06-20 07:49:28 +00001227
Guido van Rossum80f8be81997-12-02 19:51:39 +00001228 # Support for the "event" command, new in Tk 4.2.
1229 # By Case Roole.
1230
Guido van Rossum56c04b81998-04-06 03:10:03 +00001231 def event_add(self, virtual, *sequences):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001232 """Bind a virtual event VIRTUAL (of the form <<Name>>)
1233 to an event SEQUENCE such that the virtual event is triggered
1234 whenever SEQUENCE occurs."""
Guido van Rossum80f8be81997-12-02 19:51:39 +00001235 args = ('event', 'add', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +00001236 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +00001237
Guido van Rossum56c04b81998-04-06 03:10:03 +00001238 def event_delete(self, virtual, *sequences):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001239 """Unbind a virtual event VIRTUAL from SEQUENCE."""
Guido van Rossum80f8be81997-12-02 19:51:39 +00001240 args = ('event', 'delete', virtual) + sequences
Guido van Rossumf9756991998-04-29 21:57:08 +00001241 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +00001242
1243 def event_generate(self, sequence, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001244 """Generate an event SEQUENCE. Additional
1245 keyword arguments specify parameter of the event
1246 (e.g. x, y, rootx, rooty)."""
Guido van Rossum80f8be81997-12-02 19:51:39 +00001247 args = ('event', 'generate', self._w, sequence)
Guido van Rossum56c04b81998-04-06 03:10:03 +00001248 for k, v in kw.items():
1249 args = args + ('-%s' % k, str(v))
Guido van Rossumf9756991998-04-29 21:57:08 +00001250 self.tk.call(args)
Guido van Rossum80f8be81997-12-02 19:51:39 +00001251
Guido van Rossum56c04b81998-04-06 03:10:03 +00001252 def event_info(self, virtual=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001253 """Return a list of all virtual events or the information
1254 about the SEQUENCE bound to the virtual event VIRTUAL."""
Guido van Rossum56c04b81998-04-06 03:10:03 +00001255 return self.tk.splitlist(
1256 self.tk.call('event', 'info', virtual))
Guido van Rossum80f8be81997-12-02 19:51:39 +00001257
Guido van Rossumc2966511998-04-10 19:16:10 +00001258 # Image related commands
1259
1260 def image_names(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001261 """Return a list of all existing image names."""
Guido van Rossumc2966511998-04-10 19:16:10 +00001262 return self.tk.call('image', 'names')
1263
1264 def image_types(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001265 """Return a list of all available image types (e.g. phote bitmap)."""
Guido van Rossumc2966511998-04-10 19:16:10 +00001266 return self.tk.call('image', 'types')
1267
Guido van Rossum80f8be81997-12-02 19:51:39 +00001268
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001269class CallWrapper:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001270 """Internal class. Stores function to call when some user
1271 defined Tcl function is called e.g. after an event occured."""
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001272 def __init__(self, func, subst, widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001273 """Store FUNC, SUBST and WIDGET as members."""
Guido van Rossum18468821994-06-20 07:49:28 +00001274 self.func = func
1275 self.subst = subst
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001276 self.widget = widget
Guido van Rossum18468821994-06-20 07:49:28 +00001277 def __call__(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001278 """Apply first function SUBST to arguments, than FUNC."""
Guido van Rossum18468821994-06-20 07:49:28 +00001279 try:
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001280 if self.subst:
1281 args = apply(self.subst, args)
1282 return apply(self.func, args)
Guido van Rossum45853db1994-06-20 12:19:19 +00001283 except SystemExit, msg:
1284 raise SystemExit, msg
Guido van Rossum18468821994-06-20 07:49:28 +00001285 except:
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001286 self.widget._report_exception()
Guido van Rossum18468821994-06-20 07:49:28 +00001287
Guido van Rossume365a591998-05-01 19:48:20 +00001288
Guido van Rossum18468821994-06-20 07:49:28 +00001289class Wm:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001290 """Provides functions for the communication with the window manager."""
1291 def wm_aspect(self,
1292 minNumer=None, minDenom=None,
1293 maxNumer=None, maxDenom=None):
1294 """Instruct the window manager to set the aspect ratio (width/height)
1295 of this widget to be between MINNUMER/MINDENOM and MAXNUMER/MAXDENOM. Return a tuple
1296 of the actual values if no argument is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001297 return self._getints(
1298 self.tk.call('wm', 'aspect', self._w,
1299 minNumer, minDenom,
1300 maxNumer, maxDenom))
Guido van Rossume365a591998-05-01 19:48:20 +00001301 aspect = wm_aspect
1302 def wm_client(self, name=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001303 """Store NAME in WM_CLIENT_MACHINE property of this widget. Return
1304 current value."""
Guido van Rossum18468821994-06-20 07:49:28 +00001305 return self.tk.call('wm', 'client', self._w, name)
Guido van Rossume365a591998-05-01 19:48:20 +00001306 client = wm_client
1307 def wm_colormapwindows(self, *wlist):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001308 """Store list of window names (WLIST) into WM_COLORMAPWINDOWS property
1309 of this widget. This list contains windows whose colormaps differ from their
1310 parents. Return current list of widgets if WLIST is empty."""
Guido van Rossum8fa42af1999-10-20 12:29:56 +00001311 if len(wlist) > 1:
1312 wlist = (wlist,) # Tk needs a list of windows here
1313 args = ('wm', 'colormapwindows', self._w) + wlist
Guido van Rossumf9756991998-04-29 21:57:08 +00001314 return map(self._nametowidget, self.tk.call(args))
Guido van Rossume365a591998-05-01 19:48:20 +00001315 colormapwindows = wm_colormapwindows
1316 def wm_command(self, value=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001317 """Store VALUE in WM_COMMAND property. It is the command
1318 which shall be used to invoke the application. Return current
1319 commmand if VALUE is None."""
Guido van Rossum18468821994-06-20 07:49:28 +00001320 return self.tk.call('wm', 'command', self._w, value)
Guido van Rossume365a591998-05-01 19:48:20 +00001321 command = wm_command
1322 def wm_deiconify(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001323 """Deiconify this widget. If it was never mapped it will not be mapped.
1324 On Windows it will raise this widget and give it the focus."""
Guido van Rossum18468821994-06-20 07:49:28 +00001325 return self.tk.call('wm', 'deiconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +00001326 deiconify = wm_deiconify
1327 def wm_focusmodel(self, model=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001328 """Set focus model to MODEL. "active" means that this widget will claim
1329 the focus itself, "passive" means that the window manager shall give
1330 the focus. Return current focus model if MODEL is None."""
Guido van Rossum18468821994-06-20 07:49:28 +00001331 return self.tk.call('wm', 'focusmodel', self._w, model)
Guido van Rossume365a591998-05-01 19:48:20 +00001332 focusmodel = wm_focusmodel
1333 def wm_frame(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001334 """Return identifier for decorative frame of this widget if present."""
Guido van Rossum18468821994-06-20 07:49:28 +00001335 return self.tk.call('wm', 'frame', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +00001336 frame = wm_frame
1337 def wm_geometry(self, newGeometry=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001338 """Set geometry to NEWGEOMETRY of the form =widthxheigth+x+y. Return
1339 current value if None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001340 return self.tk.call('wm', 'geometry', self._w, newGeometry)
Guido van Rossume365a591998-05-01 19:48:20 +00001341 geometry = wm_geometry
1342 def wm_grid(self,
Guido van Rossum21df8f51998-02-24 23:26:18 +00001343 baseWidth=None, baseHeight=None,
Guido van Rossum18468821994-06-20 07:49:28 +00001344 widthInc=None, heightInc=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001345 """Instruct the window manager that this widget shall only be
1346 resized on grid boundaries. WIDTHINC and HEIGHTINC are the width and
1347 height of a grid unit in pixels. BASEWIDTH and BASEHEIGHT are the
1348 number of grid units requested in Tk_GeometryRequest."""
Guido van Rossum18468821994-06-20 07:49:28 +00001349 return self._getints(self.tk.call(
1350 'wm', 'grid', self._w,
Guido van Rossum4d9d3f11997-12-27 15:14:43 +00001351 baseWidth, baseHeight, widthInc, heightInc))
Guido van Rossume365a591998-05-01 19:48:20 +00001352 grid = wm_grid
1353 def wm_group(self, pathName=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001354 """Set the group leader widgets for related widgets to PATHNAME. Return
1355 the group leader of this widget if None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001356 return self.tk.call('wm', 'group', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +00001357 group = wm_group
1358 def wm_iconbitmap(self, bitmap=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001359 """Set bitmap for the iconified widget to BITMAP. Return
1360 the bitmap if None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001361 return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +00001362 iconbitmap = wm_iconbitmap
1363 def wm_iconify(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001364 """Display widget as icon."""
Guido van Rossum18468821994-06-20 07:49:28 +00001365 return self.tk.call('wm', 'iconify', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +00001366 iconify = wm_iconify
1367 def wm_iconmask(self, bitmap=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001368 """Set mask for the icon bitmap of this widget. Return the
1369 mask if None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001370 return self.tk.call('wm', 'iconmask', self._w, bitmap)
Guido van Rossume365a591998-05-01 19:48:20 +00001371 iconmask = wm_iconmask
1372 def wm_iconname(self, newName=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001373 """Set the name of the icon for this widget. Return the name if
1374 None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001375 return self.tk.call('wm', 'iconname', self._w, newName)
Guido van Rossume365a591998-05-01 19:48:20 +00001376 iconname = wm_iconname
1377 def wm_iconposition(self, x=None, y=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001378 """Set the position of the icon of this widget to X and Y. Return
1379 a tuple of the current values of X and X if None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001380 return self._getints(self.tk.call(
1381 'wm', 'iconposition', self._w, x, y))
Guido van Rossume365a591998-05-01 19:48:20 +00001382 iconposition = wm_iconposition
1383 def wm_iconwindow(self, pathName=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001384 """Set widget PATHNAME to be displayed instead of icon. Return the current
1385 value if None is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001386 return self.tk.call('wm', 'iconwindow', self._w, pathName)
Guido van Rossume365a591998-05-01 19:48:20 +00001387 iconwindow = wm_iconwindow
1388 def wm_maxsize(self, width=None, height=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001389 """Set max WIDTH and HEIGHT for this widget. If the window is gridded
1390 the values are given in grid units. Return the current values if None
1391 is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001392 return self._getints(self.tk.call(
1393 'wm', 'maxsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +00001394 maxsize = wm_maxsize
1395 def wm_minsize(self, width=None, height=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001396 """Set min WIDTH and HEIGHT for this widget. If the window is gridded
1397 the values are given in grid units. Return the current values if None
1398 is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001399 return self._getints(self.tk.call(
1400 'wm', 'minsize', self._w, width, height))
Guido van Rossume365a591998-05-01 19:48:20 +00001401 minsize = wm_minsize
1402 def wm_overrideredirect(self, boolean=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001403 """Instruct the window manager to ignore this widget
1404 if BOOLEAN is given with 1. Return the current value if None
1405 is given."""
Guido van Rossum18468821994-06-20 07:49:28 +00001406 return self._getboolean(self.tk.call(
1407 'wm', 'overrideredirect', self._w, boolean))
Guido van Rossume365a591998-05-01 19:48:20 +00001408 overrideredirect = wm_overrideredirect
1409 def wm_positionfrom(self, who=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001410 """Instruct the window manager that the position of this widget shall
1411 be defined by the user if WHO is "user", and by its own policy if WHO is
1412 "program"."""
Guido van Rossum18468821994-06-20 07:49:28 +00001413 return self.tk.call('wm', 'positionfrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +00001414 positionfrom = wm_positionfrom
1415 def wm_protocol(self, name=None, func=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001416 """Bind function FUNC to command NAME for this widget.
1417 Return the function bound to NAME if None is given. NAME could be
1418 e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW"."""
Guido van Rossumc4570481998-03-20 20:45:49 +00001419 if callable(func):
Guido van Rossum18468821994-06-20 07:49:28 +00001420 command = self._register(func)
1421 else:
1422 command = func
1423 return self.tk.call(
1424 'wm', 'protocol', self._w, name, command)
Guido van Rossume365a591998-05-01 19:48:20 +00001425 protocol = wm_protocol
1426 def wm_resizable(self, width=None, height=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001427 """Instruct the window manager whether this width can be resized
1428 in WIDTH or HEIGHT. Both values are boolean values."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00001429 return self.tk.call('wm', 'resizable', self._w, width, height)
Guido van Rossume365a591998-05-01 19:48:20 +00001430 resizable = wm_resizable
1431 def wm_sizefrom(self, who=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001432 """Instruct the window manager that the size of this widget shall
1433 be defined by the user if WHO is "user", and by its own policy if WHO is
1434 "program"."""
Guido van Rossum18468821994-06-20 07:49:28 +00001435 return self.tk.call('wm', 'sizefrom', self._w, who)
Guido van Rossume365a591998-05-01 19:48:20 +00001436 sizefrom = wm_sizefrom
1437 def wm_state(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001438 """Return the state of this widget as one of normal,
1439 icon, iconic (see wm_iconwindow) and withdrawn."""
Guido van Rossum18468821994-06-20 07:49:28 +00001440 return self.tk.call('wm', 'state', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +00001441 state = wm_state
1442 def wm_title(self, string=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001443 """Set the title of this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +00001444 return self.tk.call('wm', 'title', self._w, string)
Guido van Rossume365a591998-05-01 19:48:20 +00001445 title = wm_title
1446 def wm_transient(self, master=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001447 """Instruct the window manager that this widget is transient
1448 with regard to widget MASTER."""
Guido van Rossum18468821994-06-20 07:49:28 +00001449 return self.tk.call('wm', 'transient', self._w, master)
Guido van Rossume365a591998-05-01 19:48:20 +00001450 transient = wm_transient
1451 def wm_withdraw(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001452 """Withdraw this widget from the screen such that it is unmapped
1453 and forgotten by the window manager. Re-draw it with wm_deiconify."""
Guido van Rossum18468821994-06-20 07:49:28 +00001454 return self.tk.call('wm', 'withdraw', self._w)
Guido van Rossume365a591998-05-01 19:48:20 +00001455 withdraw = wm_withdraw
1456
Guido van Rossum18468821994-06-20 07:49:28 +00001457
1458class Tk(Misc, Wm):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001459 """Toplevel widget of Tk which represents mostly the main window
1460 of an appliation. It has an associated Tcl interpreter."""
Guido van Rossum18468821994-06-20 07:49:28 +00001461 _w = '.'
1462 def __init__(self, screenName=None, baseName=None, className='Tk'):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001463 """Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
1464 be created. BASENAME will be used for the identification of the profile file (see
1465 readprofile).
1466 It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME
1467 is the name of the widget class."""
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001468 global _default_root
Guido van Rossum45853db1994-06-20 12:19:19 +00001469 self.master = None
1470 self.children = {}
Guido van Rossum18468821994-06-20 07:49:28 +00001471 if baseName is None:
1472 import sys, os
1473 baseName = os.path.basename(sys.argv[0])
Fred Drakecab3c3b1996-10-06 17:55:20 +00001474 baseName, ext = os.path.splitext(baseName)
Fred Drake182c5901998-07-15 04:36:56 +00001475 if ext not in ('.py', '.pyc', '.pyo'):
1476 baseName = baseName + ext
Guido van Rossum95806091997-02-15 18:33:24 +00001477 self.tk = _tkinter.create(screenName, baseName, className)
Guido van Rossumf0c891a1998-04-29 21:43:36 +00001478 if _MacOS:
Guido van Rossum37dcab11996-05-16 16:00:19 +00001479 # Disable event scanning except for Command-Period
Guido van Rossumf0c891a1998-04-29 21:43:36 +00001480 _MacOS.SchedParams(1, 0)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001481 # Work around nasty MacTk bug
Guido van Rossumf0c891a1998-04-29 21:43:36 +00001482 # XXX Is this one still needed?
Guido van Rossum37dcab11996-05-16 16:00:19 +00001483 self.update()
Guido van Rossumf7f79ac1995-10-07 19:08:37 +00001484 # Version sanity checks
1485 tk_version = self.tk.getvar('tk_version')
Guido van Rossum95806091997-02-15 18:33:24 +00001486 if tk_version != _tkinter.TK_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +00001487 raise RuntimeError, \
1488 "tk.h version (%s) doesn't match libtk.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +00001489 % (_tkinter.TK_VERSION, tk_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +00001490 tcl_version = self.tk.getvar('tcl_version')
Guido van Rossum95806091997-02-15 18:33:24 +00001491 if tcl_version != _tkinter.TCL_VERSION:
Guido van Rossumf7f79ac1995-10-07 19:08:37 +00001492 raise RuntimeError, \
1493 "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
Guido van Rossum95806091997-02-15 18:33:24 +00001494 % (_tkinter.TCL_VERSION, tcl_version)
Guido van Rossumf7f79ac1995-10-07 19:08:37 +00001495 if TkVersion < 4.0:
Guido van Rossum37dcab11996-05-16 16:00:19 +00001496 raise RuntimeError, \
1497 "Tk 4.0 or higher is required; found Tk %s" \
1498 % str(TkVersion)
Guido van Rossum45853db1994-06-20 12:19:19 +00001499 self.tk.createcommand('tkerror', _tkerror)
Guido van Rossum97aeca11994-07-07 13:12:12 +00001500 self.tk.createcommand('exit', _exit)
Guido van Rossum27b77a41994-07-12 15:52:32 +00001501 self.readprofile(baseName, className)
Guido van Rossumc4570481998-03-20 20:45:49 +00001502 if _support_default_root and not _default_root:
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001503 _default_root = self
Guido van Rossume61e98d1999-08-20 18:26:06 +00001504 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum45853db1994-06-20 12:19:19 +00001505 def destroy(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001506 """Destroy this and all descendants widgets. This will
1507 end the application of this Tcl interpreter."""
Guido van Rossum45853db1994-06-20 12:19:19 +00001508 for c in self.children.values(): c.destroy()
Guido van Rossum45853db1994-06-20 12:19:19 +00001509 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001510 Misc.destroy(self)
Guido van Rossumd6615ab1997-08-05 02:35:01 +00001511 global _default_root
Guido van Rossumc4570481998-03-20 20:45:49 +00001512 if _support_default_root and _default_root is self:
Guido van Rossumd6615ab1997-08-05 02:35:01 +00001513 _default_root = None
Guido van Rossum27b77a41994-07-12 15:52:32 +00001514 def readprofile(self, baseName, className):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001515 """Internal function. It reads BASENAME.tcl and CLASSNAME.tcl into
1516 the Tcl Interpreter and calls execfile on BASENAME.py and CLASSNAME.py if
1517 such a file exists in the home directory."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00001518 import os
Guido van Rossum27b77a41994-07-12 15:52:32 +00001519 if os.environ.has_key('HOME'): home = os.environ['HOME']
1520 else: home = os.curdir
1521 class_tcl = os.path.join(home, '.%s.tcl' % className)
1522 class_py = os.path.join(home, '.%s.py' % className)
1523 base_tcl = os.path.join(home, '.%s.tcl' % baseName)
1524 base_py = os.path.join(home, '.%s.py' % baseName)
1525 dir = {'self': self}
1526 exec 'from Tkinter import *' in dir
1527 if os.path.isfile(class_tcl):
1528 print 'source', `class_tcl`
1529 self.tk.call('source', class_tcl)
1530 if os.path.isfile(class_py):
1531 print 'execfile', `class_py`
1532 execfile(class_py, dir)
1533 if os.path.isfile(base_tcl):
1534 print 'source', `base_tcl`
1535 self.tk.call('source', base_tcl)
1536 if os.path.isfile(base_py):
1537 print 'execfile', `base_py`
1538 execfile(base_py, dir)
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001539 def report_callback_exception(self, exc, val, tb):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001540 """Internal function. It reports exception on sys.stderr."""
Guido van Rossumda654501998-10-06 19:06:27 +00001541 import traceback, sys
1542 sys.stderr.write("Exception in Tkinter callback\n")
Guido van Rossum9f1292d1998-10-13 20:02:39 +00001543 sys.last_type = exc
1544 sys.last_value = val
1545 sys.last_traceback = tb
Guido van Rossuma5773dd1995-09-07 19:22:00 +00001546 traceback.print_exception(exc, val, tb)
Guido van Rossum18468821994-06-20 07:49:28 +00001547
Guido van Rossum368e06b1997-11-07 20:38:49 +00001548# Ideally, the classes Pack, Place and Grid disappear, the
1549# pack/place/grid methods are defined on the Widget class, and
1550# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,
1551# ...), with pack(), place() and grid() being short for
1552# pack_configure(), place_configure() and grid_columnconfigure(), and
1553# forget() being short for pack_forget(). As a practical matter, I'm
1554# afraid that there is too much code out there that may be using the
1555# Pack, Place or Grid class, so I leave them intact -- but only as
1556# backwards compatibility features. Also note that those methods that
1557# take a master as argument (e.g. pack_propagate) have been moved to
1558# the Misc class (which now incorporates all methods common between
1559# toplevel and interior widgets). Again, for compatibility, these are
1560# copied into the Pack, Place or Grid class.
1561
Guido van Rossum18468821994-06-20 07:49:28 +00001562class Pack:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001563 """Geometry manager Pack.
1564
1565 Base class to use the methods pack_* in every widget."""
Guido van Rossum368e06b1997-11-07 20:38:49 +00001566 def pack_configure(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001567 """Pack a widget in the parent widget. Use as options:
1568 after=widget - pack it after you have packed widget
1569 anchor=NSEW (or subset) - position widget according to
1570 given direction
1571 before=widget - pack it before you will pack widget
1572 expand=1 or 0 - expand widget if parent size grows
1573 fill=NONE or X or Y or BOTH - fill widget if widget grows
1574 in=master - use master to contain this widget
1575 ipadx=amount - add internal padding in x direction
1576 ipady=amount - add internal padding in y direction
1577 padx=amount - add padding in x direction
1578 pady=amount - add padding in y direction
1579 side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
1580 """
Guido van Rossumf9756991998-04-29 21:57:08 +00001581 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001582 ('pack', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001583 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001584 pack = configure = config = pack_configure
1585 def pack_forget(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001586 """Unmap this widget and do not use it for the packing order."""
Guido van Rossum18468821994-06-20 07:49:28 +00001587 self.tk.call('pack', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001588 forget = pack_forget
1589 def pack_info(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001590 """Return information about the packing options
1591 for this widget."""
Guido van Rossum69170c51994-07-11 15:21:31 +00001592 words = self.tk.splitlist(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001593 self.tk.call('pack', 'info', self._w))
Guido van Rossum69170c51994-07-11 15:21:31 +00001594 dict = {}
1595 for i in range(0, len(words), 2):
1596 key = words[i][1:]
1597 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001598 if value[:1] == '.':
Guido van Rossum69170c51994-07-11 15:21:31 +00001599 value = self._nametowidget(value)
1600 dict[key] = value
1601 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001602 info = pack_info
1603 propagate = pack_propagate = Misc.pack_propagate
1604 slaves = pack_slaves = Misc.pack_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001605
1606class Place:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001607 """Geometry manager Place.
1608
1609 Base class to use the methods place_* in every widget."""
Guido van Rossum368e06b1997-11-07 20:38:49 +00001610 def place_configure(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001611 """Place a widget in the parent widget. Use as options:
1612 in=master - master relative to which the widget is placed.
1613 x=amount - locate anchor of this widget at position x of master
1614 y=amount - locate anchor of this widget at position y of master
1615 relx=amount - locate anchor of this widget between 0.0 and 1.0
1616 relative to width of master (1.0 is right edge)
1617 rely=amount - locate anchor of this widget between 0.0 and 1.0
1618 relative to height of master (1.0 is bottom edge)
1619 anchor=NSEW (or subset) - position anchor according to given direction
1620 width=amount - width of this widget in pixel
1621 height=amount - height of this widget in pixel
1622 relwidth=amount - width of this widget between 0.0 and 1.0
1623 relative to width of master (1.0 is the same width
1624 as the master)
1625 relheight=amount - height of this widget between 0.0 and 1.0
1626 relative to heigth of master (1.0 is the same
1627 height as the master)
1628 bordermode="inside" or "outside" - whether to take border width of master widget
1629 into account
1630 """
Guido van Rossum37dcab11996-05-16 16:00:19 +00001631 for k in ['in_']:
1632 if kw.has_key(k):
1633 kw[k[:-1]] = kw[k]
1634 del kw[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001635 self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001636 ('place', 'configure', self._w)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001637 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001638 place = configure = config = place_configure
1639 def place_forget(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001640 """Unmap this widget."""
Guido van Rossum18468821994-06-20 07:49:28 +00001641 self.tk.call('place', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001642 forget = place_forget
1643 def place_info(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001644 """Return information about the placing options
1645 for this widget."""
Guido van Rossum63e39ae1996-05-16 17:53:48 +00001646 words = self.tk.splitlist(
1647 self.tk.call('place', 'info', self._w))
1648 dict = {}
1649 for i in range(0, len(words), 2):
1650 key = words[i][1:]
1651 value = words[i+1]
1652 if value[:1] == '.':
1653 value = self._nametowidget(value)
1654 dict[key] = value
1655 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001656 info = place_info
1657 slaves = place_slaves = Misc.place_slaves
Guido van Rossum18468821994-06-20 07:49:28 +00001658
Guido van Rossum37dcab11996-05-16 16:00:19 +00001659class Grid:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001660 """Geometry manager Grid.
1661
1662 Base class to use the methods grid_* in every widget."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00001663 # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001664 def grid_configure(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001665 """Position a widget in the parent widget in a grid. Use as options:
1666 column=number - use cell identified with given column (starting with 0)
1667 columnspan=number - this widget will span several columns
1668 in=master - use master to contain this widget
1669 ipadx=amount - add internal padding in x direction
1670 ipady=amount - add internal padding in y direction
1671 padx=amount - add padding in x direction
1672 pady=amount - add padding in y direction
1673 row=number - use cell identified with given row (starting with 0)
1674 rowspan=number - this widget will span several rows
1675 sticky=NSEW - if cell is larger on which sides will this
1676 widget stick to the cell boundary
1677 """
Guido van Rossumf9756991998-04-29 21:57:08 +00001678 self.tk.call(
Guido van Rossum37dcab11996-05-16 16:00:19 +00001679 ('grid', 'configure', self._w)
1680 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00001681 grid = configure = config = grid_configure
1682 bbox = grid_bbox = Misc.grid_bbox
1683 columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
1684 def grid_forget(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001685 """Unmap this widget."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00001686 self.tk.call('grid', 'forget', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001687 forget = grid_forget
Guido van Rossum8e407591999-05-05 23:03:48 +00001688 def grid_remove(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001689 """Unmap this widget but remember the grid options."""
Guido van Rossum8e407591999-05-05 23:03:48 +00001690 self.tk.call('grid', 'remove', self._w)
Guido van Rossum368e06b1997-11-07 20:38:49 +00001691 def grid_info(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001692 """Return information about the options
1693 for positioning this widget in a grid."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00001694 words = self.tk.splitlist(
1695 self.tk.call('grid', 'info', self._w))
1696 dict = {}
1697 for i in range(0, len(words), 2):
1698 key = words[i][1:]
1699 value = words[i+1]
Guido van Rossuma5f875f1996-05-16 17:50:07 +00001700 if value[:1] == '.':
Guido van Rossum37dcab11996-05-16 16:00:19 +00001701 value = self._nametowidget(value)
1702 dict[key] = value
1703 return dict
Guido van Rossum368e06b1997-11-07 20:38:49 +00001704 info = grid_info
1705 def grid_location(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001706 """Return a tuple of column and row which identify the cell
1707 at which the pixel at position X and Y inside the master
1708 widget is located."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00001709 return self._getints(
1710 self.tk.call(
1711 'grid', 'location', self._w, x, y)) or None
Guido van Rossum368e06b1997-11-07 20:38:49 +00001712 location = grid_location
1713 propagate = grid_propagate = Misc.grid_propagate
1714 rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
1715 size = grid_size = Misc.grid_size
1716 slaves = grid_slaves = Misc.grid_slaves
Guido van Rossum37dcab11996-05-16 16:00:19 +00001717
Guido van Rossum368e06b1997-11-07 20:38:49 +00001718class BaseWidget(Misc):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001719 """Internal class."""
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001720 def _setup(self, master, cnf):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001721 """Internal function. Sets up information about children."""
Guido van Rossumc4570481998-03-20 20:45:49 +00001722 if _support_default_root:
1723 global _default_root
1724 if not master:
1725 if not _default_root:
1726 _default_root = Tk()
1727 master = _default_root
Guido van Rossum18468821994-06-20 07:49:28 +00001728 self.master = master
1729 self.tk = master.tk
Fred Drakec8296db1997-05-27 22:45:10 +00001730 name = None
Guido van Rossum18468821994-06-20 07:49:28 +00001731 if cnf.has_key('name'):
1732 name = cnf['name']
1733 del cnf['name']
Fred Drakec8296db1997-05-27 22:45:10 +00001734 if not name:
Guido van Rossum18468821994-06-20 07:49:28 +00001735 name = `id(self)`
Guido van Rossum45853db1994-06-20 12:19:19 +00001736 self._name = name
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001737 if master._w=='.':
Guido van Rossum18468821994-06-20 07:49:28 +00001738 self._w = '.' + name
1739 else:
1740 self._w = master._w + '.' + name
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001741 self.children = {}
1742 if self.master.children.has_key(self._name):
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001743 self.master.children[self._name].destroy()
Guido van Rossumaec5dc91994-06-27 07:55:12 +00001744 self.master.children[self._name] = self
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001745 def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001746 """Construct a widget with the parent widget MASTER, a name WIDGETNAME
1747 and appropriate options."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001748 if kw:
1749 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001750 self.widgetName = widgetName
Guido van Rossum368e06b1997-11-07 20:38:49 +00001751 BaseWidget._setup(self, master, cnf)
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001752 classes = []
1753 for k in cnf.keys():
1754 if type(k) is ClassType:
1755 classes.append((k, cnf[k]))
1756 del cnf[k]
Guido van Rossumf9756991998-04-29 21:57:08 +00001757 self.tk.call(
1758 (widgetName, self._w) + extra + self._options(cnf))
Guido van Rossumad8b3ba1996-07-21 03:05:05 +00001759 for k, v in classes:
Guido van Rossum368e06b1997-11-07 20:38:49 +00001760 k.configure(self, v)
Guido van Rossum45853db1994-06-20 12:19:19 +00001761 def destroy(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001762 """Destroy this and all descendants widgets."""
Guido van Rossum45853db1994-06-20 12:19:19 +00001763 for c in self.children.values(): c.destroy()
Guido van Rossumf023ab01994-08-30 12:13:44 +00001764 if self.master.children.has_key(self._name):
1765 del self.master.children[self._name]
Guido van Rossum18468821994-06-20 07:49:28 +00001766 self.tk.call('destroy', self._w)
Guido van Rossum103cc6d1997-04-14 13:30:24 +00001767 Misc.destroy(self)
Guido van Rossum18468821994-06-20 07:49:28 +00001768 def _do(self, name, args=()):
Guido van Rossum0bd54331998-05-19 21:18:13 +00001769 # XXX Obsolete -- better use self.tk.call directly!
Guido van Rossumf9756991998-04-29 21:57:08 +00001770 return self.tk.call((self._w, name) + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001771
Guido van Rossum368e06b1997-11-07 20:38:49 +00001772class Widget(BaseWidget, Pack, Place, Grid):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001773 """Internal class.
1774
1775 Base class for a widget which can be positioned with the geometry managers
1776 Pack, Place or Grid."""
Guido van Rossum368e06b1997-11-07 20:38:49 +00001777 pass
1778
1779class Toplevel(BaseWidget, Wm):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001780 """Toplevel widget, e.g. for dialogs."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001781 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001782 """Construct a toplevel widget with the parent MASTER.
1783
1784 Valid resource names: background, bd, bg, borderwidth, class,
1785 colormap, container, cursor, height, highlightbackground,
1786 highlightcolor, highlightthickness, menu, relief, screen, takefocus,
1787 use, visual, width."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001788 if kw:
1789 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00001790 extra = ()
Guido van Rossum37dcab11996-05-16 16:00:19 +00001791 for wmkey in ['screen', 'class_', 'class', 'visual',
1792 'colormap']:
1793 if cnf.has_key(wmkey):
1794 val = cnf[wmkey]
1795 # TBD: a hack needed because some keys
1796 # are not valid as keyword arguments
1797 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
1798 else: opt = '-'+wmkey
1799 extra = extra + (opt, val)
1800 del cnf[wmkey]
Guido van Rossum368e06b1997-11-07 20:38:49 +00001801 BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
Guido van Rossum45853db1994-06-20 12:19:19 +00001802 root = self._root()
1803 self.iconname(root.iconname())
1804 self.title(root.title())
Guido van Rossume61e98d1999-08-20 18:26:06 +00001805 self.protocol("WM_DELETE_WINDOW", self.destroy)
Guido van Rossum18468821994-06-20 07:49:28 +00001806
1807class Button(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001808 """Button widget."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001809 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001810 """Construct a button widget with the parent MASTER.
1811
1812 Valid resource names: activebackground, activeforeground, anchor,
1813 background, bd, bg, bitmap, borderwidth, command, cursor, default,
1814 disabledforeground, fg, font, foreground, height,
1815 highlightbackground, highlightcolor, highlightthickness, image,
1816 justify, padx, pady, relief, state, takefocus, text, textvariable,
1817 underline, width, wraplength."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001818 Widget.__init__(self, master, 'button', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00001819 def tkButtonEnter(self, *dummy):
1820 self.tk.call('tkButtonEnter', self._w)
1821 def tkButtonLeave(self, *dummy):
1822 self.tk.call('tkButtonLeave', self._w)
1823 def tkButtonDown(self, *dummy):
1824 self.tk.call('tkButtonDown', self._w)
1825 def tkButtonUp(self, *dummy):
1826 self.tk.call('tkButtonUp', self._w)
Guido van Rossum36269991996-05-16 17:11:27 +00001827 def tkButtonInvoke(self, *dummy):
1828 self.tk.call('tkButtonInvoke', self._w)
Guido van Rossum18468821994-06-20 07:49:28 +00001829 def flash(self):
1830 self.tk.call(self._w, 'flash')
1831 def invoke(self):
Guido van Rossum9fd41e31997-12-29 19:59:33 +00001832 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00001833
1834# Indices:
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001835# XXX I don't like these -- take them away
Guido van Rossum18468821994-06-20 07:49:28 +00001836def AtEnd():
1837 return 'end'
Guido van Rossum1e9e4001994-06-20 09:09:51 +00001838def AtInsert(*args):
1839 s = 'insert'
1840 for a in args:
1841 if a: s = s + (' ' + a)
1842 return s
Guido van Rossum18468821994-06-20 07:49:28 +00001843def AtSelFirst():
1844 return 'sel.first'
1845def AtSelLast():
1846 return 'sel.last'
1847def At(x, y=None):
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001848 if y is None:
1849 return '@' + `x`
Guido van Rossum18468821994-06-20 07:49:28 +00001850 else:
Guido van Rossum5e0c25b1994-07-12 09:04:41 +00001851 return '@' + `x` + ',' + `y`
Guido van Rossum18468821994-06-20 07:49:28 +00001852
1853class Canvas(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001854 """Canvas widget to display graphical elements like lines or text."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001855 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001856 """Construct a canvas widget with the parent MASTER.
1857
1858 Valid resource names: background, bd, bg, borderwidth, closeenough,
1859 confine, cursor, height, highlightbackground, highlightcolor,
1860 highlightthickness, insertbackground, insertborderwidth,
1861 insertofftime, insertontime, insertwidth, offset, relief,
1862 scrollregion, selectbackground, selectborderwidth, selectforeground,
1863 state, takefocus, width, xscrollcommand, xscrollincrement,
1864 yscrollcommand, yscrollincrement."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001865 Widget.__init__(self, master, 'canvas', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001866 def addtag(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001867 """Internal function."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00001868 self.tk.call((self._w, 'addtag') + args)
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001869 def addtag_above(self, newtag, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001870 """Add tag NEWTAG to all items above TAGORID."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001871 self.addtag(newtag, 'above', tagOrId)
1872 def addtag_all(self, newtag):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001873 """Add tag NEWTAG to all items."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001874 self.addtag(newtag, 'all')
1875 def addtag_below(self, newtag, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001876 """Add tag NEWTAG to all items below TAGORID."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001877 self.addtag(newtag, 'below', tagOrId)
1878 def addtag_closest(self, newtag, x, y, halo=None, start=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001879 """Add tag NEWTAG to item which is closest to pixel at X, Y.
1880 If several match take the top-most.
1881 All items closer than HALO are considered overlapping (all are
1882 closests). If START is specified the next below this tag is taken."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001883 self.addtag(newtag, 'closest', x, y, halo, start)
1884 def addtag_enclosed(self, newtag, x1, y1, x2, y2):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001885 """Add tag NEWTAG to all items in the rectangle defined
1886 by X1,Y1,X2,Y2."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001887 self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
1888 def addtag_overlapping(self, newtag, x1, y1, x2, y2):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001889 """Add tag NEWTAG to all items which overlap the rectangle
1890 defined by X1,Y1,X2,Y2."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001891 self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
1892 def addtag_withtag(self, newtag, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001893 """Add tag NEWTAG to all items with TAGORID."""
Guido van Rossum5c8c91b1996-08-22 23:18:09 +00001894 self.addtag(newtag, 'withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00001895 def bbox(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001896 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
1897 which encloses all items with tags specified as arguments."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00001898 return self._getints(
1899 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum117a5a81998-03-27 21:26:51 +00001900 def tag_unbind(self, tagOrId, sequence, funcid=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001901 """Unbind for all items with TAGORID for event SEQUENCE the
1902 function identified with FUNCID."""
Guido van Rossumef8f8811994-08-08 12:47:33 +00001903 self.tk.call(self._w, 'bind', tagOrId, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00001904 if funcid:
1905 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00001906 def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001907 """Bind to all items with TAGORID at event SEQUENCE a call to function FUNC.
1908
1909 An additional boolean parameter ADD specifies whether FUNC will be
1910 called additionally to the other bound function or whether it will
1911 replace the previous function. See bind for the return value."""
Guido van Rossum42b334d1999-07-30 12:22:12 +00001912 return self._bind((self._w, 'bind', tagOrId),
1913 sequence, func, add)
Guido van Rossum18468821994-06-20 07:49:28 +00001914 def canvasx(self, screenx, gridspacing=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001915 """Return the canvas x coordinate of pixel position SCREENX rounded
1916 to nearest muliple of GRIDSPACING units."""
Guido van Rossum0132f691998-04-30 17:50:36 +00001917 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001918 self._w, 'canvasx', screenx, gridspacing))
1919 def canvasy(self, screeny, gridspacing=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001920 """Return the canvas y coordinate of pixel position SCREENY rounded
1921 to nearest muliple of GRIDSPACING units."""
Guido van Rossum0132f691998-04-30 17:50:36 +00001922 return getdouble(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00001923 self._w, 'canvasy', screeny, gridspacing))
1924 def coords(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001925 """Return a list of coordinates for the item given in ARGS."""
Guido van Rossum0001a111998-02-19 21:20:30 +00001926 # XXX Should use _flatten on args
Guido van Rossum0132f691998-04-30 17:50:36 +00001927 return map(getdouble,
Guido van Rossum0bd54331998-05-19 21:18:13 +00001928 self.tk.splitlist(
1929 self.tk.call((self._w, 'coords') + args)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00001930 def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001931 """Internal function."""
Guido van Rossum08a40381994-06-21 11:44:21 +00001932 args = _flatten(args)
Guido van Rossum18468821994-06-20 07:49:28 +00001933 cnf = args[-1]
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001934 if type(cnf) in (DictionaryType, TupleType):
Guido van Rossum18468821994-06-20 07:49:28 +00001935 args = args[:-1]
1936 else:
1937 cnf = {}
Guido van Rossum0132f691998-04-30 17:50:36 +00001938 return getint(apply(
Guido van Rossum2dcf5291994-07-06 09:23:20 +00001939 self.tk.call,
1940 (self._w, 'create', itemType)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001941 + args + self._options(cnf, kw)))
1942 def create_arc(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001943 """Create arc shaped region with coordinates x1,y1,x2,y2."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001944 return self._create('arc', args, kw)
1945 def create_bitmap(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001946 """Create bitmap with coordinates x1,y1."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001947 return self._create('bitmap', args, kw)
1948 def create_image(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001949 """Create image item with coordinates x1,y1."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001950 return self._create('image', args, kw)
1951 def create_line(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001952 """Create line with coordinates x1,y1,...,xn,yn."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001953 return self._create('line', args, kw)
1954 def create_oval(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001955 """Create oval with coordinates x1,y1,x2,y2."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001956 return self._create('oval', args, kw)
1957 def create_polygon(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001958 """Create polygon with coordinates x1,y1,...,xn,yn."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001959 return self._create('polygon', args, kw)
1960 def create_rectangle(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001961 """Create rectangle with coordinates x1,y1,x2,y2."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001962 return self._create('rectangle', args, kw)
1963 def create_text(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001964 """Create text with coordinates x1,y1."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001965 return self._create('text', args, kw)
1966 def create_window(self, *args, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001967 """Create window with coordinates x1,y1,x2,y2."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00001968 return self._create('window', args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00001969 def dchars(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001970 """Delete characters of text items identified by tag or id in ARGS (possibly
1971 several times) from FIRST to LAST character (including)."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00001972 self.tk.call((self._w, 'dchars') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001973 def delete(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001974 """Delete items identified by all tag or ids contained in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00001975 self.tk.call((self._w, 'delete') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001976 def dtag(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001977 """Delete tag or id given as last arguments in ARGS from items
1978 identified by first argument in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00001979 self.tk.call((self._w, 'dtag') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00001980 def find(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001981 """Internal function."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00001982 return self._getints(
1983 self.tk.call((self._w, 'find') + args)) or ()
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001984 def find_above(self, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001985 """Return items above TAGORID."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001986 return self.find('above', tagOrId)
1987 def find_all(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001988 """Return all items."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001989 return self.find('all')
1990 def find_below(self, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001991 """Return all items below TAGORID."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001992 return self.find('below', tagOrId)
1993 def find_closest(self, x, y, halo=None, start=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00001994 """Return item which is closest to pixel at X, Y.
1995 If several match take the top-most.
1996 All items closer than HALO are considered overlapping (all are
1997 closests). If START is specified the next below this tag is taken."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00001998 return self.find('closest', x, y, halo, start)
1999 def find_enclosed(self, x1, y1, x2, y2):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002000 """Return all items in rectangle defined
2001 by X1,Y1,X2,Y2."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00002002 return self.find('enclosed', x1, y1, x2, y2)
2003 def find_overlapping(self, x1, y1, x2, y2):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002004 """Return all items which overlap the rectangle
2005 defined by X1,Y1,X2,Y2."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00002006 return self.find('overlapping', x1, y1, x2, y2)
2007 def find_withtag(self, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002008 """Return all items with TAGORID."""
Guido van Rossum9b68fd91994-06-23 07:40:14 +00002009 return self.find('withtag', tagOrId)
Guido van Rossum18468821994-06-20 07:49:28 +00002010 def focus(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002011 """Set focus to the first item specified in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002012 return self.tk.call((self._w, 'focus') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00002013 def gettags(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002014 """Return tags associated with the first item specified in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002015 return self.tk.splitlist(
2016 self.tk.call((self._w, 'gettags') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00002017 def icursor(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002018 """Set cursor at position POS in the item identified by TAGORID.
2019 In ARGS TAGORID must be first."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002020 self.tk.call((self._w, 'icursor') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00002021 def index(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002022 """Return position of cursor as integer in item specified in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002023 return getint(self.tk.call((self._w, 'index') + args))
Guido van Rossum18468821994-06-20 07:49:28 +00002024 def insert(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002025 """Insert TEXT in item TAGORID at position POS. ARGS must
2026 be TAGORID POS TEXT."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002027 self.tk.call((self._w, 'insert') + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002028 def itemcget(self, tagOrId, option):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002029 """Return the resource value for an OPTION for item TAGORID."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002030 return self.tk.call(
2031 (self._w, 'itemcget') + (tagOrId, '-'+option))
Guido van Rossum368e06b1997-11-07 20:38:49 +00002032 def itemconfigure(self, tagOrId, cnf=None, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002033 """Configure resources of an item TAGORID.
2034
2035 The values for resources are specified as keyword
2036 arguments. To get an overview about
2037 the allowed keyword arguments call the method without arguments.
2038 """
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002039 if cnf is None and not kw:
Guido van Rossum9b68fd91994-06-23 07:40:14 +00002040 cnf = {}
2041 for x in self.tk.split(
Guido van Rossum0bd54331998-05-19 21:18:13 +00002042 self.tk.call(self._w,
2043 'itemconfigure', tagOrId)):
Guido van Rossum9b68fd91994-06-23 07:40:14 +00002044 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
2045 return cnf
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002046 if type(cnf) == StringType and not kw:
Guido van Rossum0bd54331998-05-19 21:18:13 +00002047 x = self.tk.split(self.tk.call(
2048 self._w, 'itemconfigure', tagOrId, '-'+cnf))
Guido van Rossum9b68fd91994-06-23 07:40:14 +00002049 return (x[0][1:],) + x[1:]
Guido van Rossum0bd54331998-05-19 21:18:13 +00002050 self.tk.call((self._w, 'itemconfigure', tagOrId) +
2051 self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00002052 itemconfig = itemconfigure
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00002053 # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
2054 # so the preferred name for them is tag_lower, tag_raise
2055 # (similar to tag_bind, and similar to the Text widget);
2056 # unfortunately can't delete the old ones yet (maybe in 1.6)
2057 def tag_lower(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002058 """Lower an item TAGORID given in ARGS
2059 (optional below another item)."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002060 self.tk.call((self._w, 'lower') + args)
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00002061 lower = tag_lower
Guido van Rossum18468821994-06-20 07:49:28 +00002062 def move(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002063 """Move an item TAGORID given in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002064 self.tk.call((self._w, 'move') + args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002065 def postscript(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002066 """Print the contents of the canvas to a postscript
2067 file. Valid options: colormap, colormode, file, fontmap,
2068 height, pageanchor, pageheight, pagewidth, pagex, pagey,
2069 rotate, witdh, x, y."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002070 return self.tk.call((self._w, 'postscript') +
2071 self._options(cnf, kw))
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00002072 def tag_raise(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002073 """Raise an item TAGORID given in ARGS
2074 (optional above another item)."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002075 self.tk.call((self._w, 'raise') + args)
Guido van Rossum0d8d3dd1999-04-07 16:25:54 +00002076 lift = tkraise = tag_raise
Guido van Rossum18468821994-06-20 07:49:28 +00002077 def scale(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002078 """Scale item TAGORID with XORIGIN, YORIGIN, XSCALE, YSCALE."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002079 self.tk.call((self._w, 'scale') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00002080 def scan_mark(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002081 """Remember the current X, Y coordinates."""
Guido van Rossum18468821994-06-20 07:49:28 +00002082 self.tk.call(self._w, 'scan', 'mark', x, y)
2083 def scan_dragto(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002084 """Adjust the view of the canvas to 10 times the
2085 difference between X and Y and the coordinates given in
2086 scan_mark."""
Guido van Rossum18468821994-06-20 07:49:28 +00002087 self.tk.call(self._w, 'scan', 'dragto', x, y)
2088 def select_adjust(self, tagOrId, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002089 """Adjust the end of the selection near the cursor of an item TAGORID to index."""
Guido van Rossum08a40381994-06-21 11:44:21 +00002090 self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00002091 def select_clear(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002092 """Clear the selection if it is in this widget."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002093 self.tk.call(self._w, 'select', 'clear')
Guido van Rossum18468821994-06-20 07:49:28 +00002094 def select_from(self, tagOrId, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002095 """Set the fixed end of a selection in item TAGORID to INDEX."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00002096 self.tk.call(self._w, 'select', 'from', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00002097 def select_item(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002098 """Return the item which has the selection."""
Guido van Rossum18468821994-06-20 07:49:28 +00002099 self.tk.call(self._w, 'select', 'item')
2100 def select_to(self, tagOrId, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002101 """Set the variable end of a selection in item TAGORID to INDEX."""
Guido van Rossum08a40381994-06-21 11:44:21 +00002102 self.tk.call(self._w, 'select', 'to', tagOrId, index)
Guido van Rossum18468821994-06-20 07:49:28 +00002103 def type(self, tagOrId):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002104 """Return the type of the item TAGORID."""
Guido van Rossum08a40381994-06-21 11:44:21 +00002105 return self.tk.call(self._w, 'type', tagOrId) or None
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002106 def xview(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002107 """Query and change horizontal position of the view."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002108 if not args:
2109 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00002110 self.tk.call((self._w, 'xview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00002111 def xview_moveto(self, fraction):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002112 """Adjusts the view in the window so that FRACTION of the
2113 total width of the canvas is off-screen to the left."""
Barry Warsaw4eaadf01998-10-13 19:01:10 +00002114 self.tk.call(self._w, 'xview', 'moveto', fraction)
2115 def xview_scroll(self, number, what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002116 """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
Barry Warsaw4eaadf01998-10-13 19:01:10 +00002117 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002118 def yview(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002119 """Query and change vertical position of the view."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002120 if not args:
2121 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00002122 self.tk.call((self._w, 'yview') + args)
Barry Warsaw4eaadf01998-10-13 19:01:10 +00002123 def yview_moveto(self, fraction):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002124 """Adjusts the view in the window so that FRACTION of the
2125 total height of the canvas is off-screen to the top."""
Barry Warsaw4eaadf01998-10-13 19:01:10 +00002126 self.tk.call(self._w, 'yview', 'moveto', fraction)
2127 def yview_scroll(self, number, what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002128 """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
Barry Warsaw4eaadf01998-10-13 19:01:10 +00002129 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00002130
2131class Checkbutton(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002132 """Checkbutton widget which is either in on- or off-state."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002133 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002134 """Construct a checkbutton widget with the parent MASTER.
2135
2136 Valid resource names: activebackground, activeforeground, anchor,
2137 background, bd, bg, bitmap, borderwidth, command, cursor,
2138 disabledforeground, fg, font, foreground, height,
2139 highlightbackground, highlightcolor, highlightthickness, image,
2140 indicatoron, justify, offvalue, onvalue, padx, pady, relief,
2141 selectcolor, selectimage, state, takefocus, text, textvariable,
2142 underline, variable, width, wraplength."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002143 Widget.__init__(self, master, 'checkbutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002144 def deselect(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002145 """Put the button in off-state."""
Guido van Rossum18468821994-06-20 07:49:28 +00002146 self.tk.call(self._w, 'deselect')
2147 def flash(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002148 """Flash the button."""
Guido van Rossum18468821994-06-20 07:49:28 +00002149 self.tk.call(self._w, 'flash')
2150 def invoke(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002151 """Toggle the button and invoke a command if given as resource."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00002152 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00002153 def select(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002154 """Put the button in on-state."""
Guido van Rossum18468821994-06-20 07:49:28 +00002155 self.tk.call(self._w, 'select')
2156 def toggle(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002157 """Toggle the button."""
Guido van Rossum18468821994-06-20 07:49:28 +00002158 self.tk.call(self._w, 'toggle')
2159
2160class Entry(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002161 """Entry widget which allows to display simple text."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002162 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002163 """Construct an entry widget with the parent MASTER.
2164
2165 Valid resource names: background, bd, bg, borderwidth, cursor,
2166 exportselection, fg, font, foreground, highlightbackground,
2167 highlightcolor, highlightthickness, insertbackground,
2168 insertborderwidth, insertofftime, insertontime, insertwidth,
2169 invalidcommand, invcmd, justify, relief, selectbackground,
2170 selectborderwidth, selectforeground, show, state, takefocus,
2171 textvariable, validate, validatecommand, vcmd, width,
2172 xscrollcommand."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002173 Widget.__init__(self, master, 'entry', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002174 def delete(self, first, last=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002175 """Delete text from FIRST to LAST (not included)."""
Guido van Rossum18468821994-06-20 07:49:28 +00002176 self.tk.call(self._w, 'delete', first, last)
2177 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002178 """Return the text."""
Guido van Rossum18468821994-06-20 07:49:28 +00002179 return self.tk.call(self._w, 'get')
2180 def icursor(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002181 """Insert cursor at INDEX."""
Guido van Rossum18468821994-06-20 07:49:28 +00002182 self.tk.call(self._w, 'icursor', index)
2183 def index(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002184 """Return position of cursor."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002185 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00002186 self._w, 'index', index))
2187 def insert(self, index, string):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002188 """Insert STRING at INDEX."""
Guido van Rossum18468821994-06-20 07:49:28 +00002189 self.tk.call(self._w, 'insert', index, string)
2190 def scan_mark(self, x):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002191 """Remember the current X, Y coordinates."""
Guido van Rossum18468821994-06-20 07:49:28 +00002192 self.tk.call(self._w, 'scan', 'mark', x)
2193 def scan_dragto(self, x):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002194 """Adjust the view of the canvas to 10 times the
2195 difference between X and Y and the coordinates given in
2196 scan_mark."""
Guido van Rossum18468821994-06-20 07:49:28 +00002197 self.tk.call(self._w, 'scan', 'dragto', x)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002198 def selection_adjust(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002199 """Adjust the end of the selection near the cursor to INDEX."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002200 self.tk.call(self._w, 'selection', 'adjust', index)
2201 select_adjust = selection_adjust
2202 def selection_clear(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002203 """Clear the selection if it is in this widget."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002204 self.tk.call(self._w, 'selection', 'clear')
2205 select_clear = selection_clear
2206 def selection_from(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002207 """Set the fixed end of a selection to INDEX."""
Guido van Rossum42b78e61996-09-06 14:20:23 +00002208 self.tk.call(self._w, 'selection', 'from', index)
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002209 select_from = selection_from
2210 def selection_present(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002211 """Return whether the widget has the selection."""
Guido van Rossum1d59df21995-08-11 14:21:06 +00002212 return self.tk.getboolean(
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002213 self.tk.call(self._w, 'selection', 'present'))
2214 select_present = selection_present
2215 def selection_range(self, start, end):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002216 """Set the selection from START to END (not included)."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002217 self.tk.call(self._w, 'selection', 'range', start, end)
2218 select_range = selection_range
2219 def selection_to(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002220 """Set the variable end of a selection to INDEX."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002221 self.tk.call(self._w, 'selection', 'to', index)
2222 select_to = selection_to
2223 def xview(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002224 """Query and change horizontal position of the view."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002225 self.tk.call(self._w, 'xview', index)
2226 def xview_moveto(self, fraction):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002227 """Adjust the view in the window so that FRACTION of the
2228 total width of the entry is off-screen to the left."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002229 self.tk.call(self._w, 'xview', 'moveto', fraction)
2230 def xview_scroll(self, number, what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002231 """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
Guido van Rossum422cc7f1996-05-21 20:30:07 +00002232 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00002233
2234class Frame(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002235 """Frame widget which may contain other widgets and can have a 3D border."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002236 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002237 """Construct a frame widget with the parent MASTER.
2238
2239 Valid resource names: background, bd, bg, borderwidth, class,
2240 colormap, container, cursor, height, highlightbackground,
2241 highlightcolor, highlightthickness, relief, takefocus, visual, width."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002242 cnf = _cnfmerge((cnf, kw))
Guido van Rossum18468821994-06-20 07:49:28 +00002243 extra = ()
Fred Drake41dc09d1997-01-10 15:13:12 +00002244 if cnf.has_key('class_'):
2245 extra = ('-class', cnf['class_'])
2246 del cnf['class_']
2247 elif cnf.has_key('class'):
Guido van Rossum18468821994-06-20 07:49:28 +00002248 extra = ('-class', cnf['class'])
2249 del cnf['class']
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002250 Widget.__init__(self, master, 'frame', cnf, {}, extra)
Guido van Rossum18468821994-06-20 07:49:28 +00002251
2252class Label(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002253 """Label widget which can display text and bitmaps."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002254 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002255 """Construct a label widget with the parent MASTER.
2256
2257 Valid resource names: anchor, background, bd, bg, bitmap,
2258 borderwidth, cursor, fg, font, foreground, height,
2259 highlightbackground, highlightcolor, highlightthickness, image,
2260 justify, padx, pady, relief, takefocus, text, textvariable,
2261 underline, width, wraplength."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002262 Widget.__init__(self, master, 'label', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002263
Guido van Rossum18468821994-06-20 07:49:28 +00002264class Listbox(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002265 """Listbox widget which can display a list of strings."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002266 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002267 """Construct a listbox widget with the parent MASTER.
2268
2269 Valid resource names: background, bd, bg, borderwidth, cursor,
2270 exportselection, fg, font, foreground, height, highlightbackground,
2271 highlightcolor, highlightthickness, relief, selectbackground,
2272 selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
2273 width, xscrollcommand, yscrollcommand, listvariable."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002274 Widget.__init__(self, master, 'listbox', cnf, kw)
Guido van Rossum46f92d21995-10-11 17:41:00 +00002275 def activate(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002276 """Activate item identified by INDEX."""
Guido van Rossum46f92d21995-10-11 17:41:00 +00002277 self.tk.call(self._w, 'activate', index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002278 def bbox(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002279 """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
2280 which encloses the item identified by index in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002281 return self._getints(
2282 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00002283 def curselection(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002284 """Return list of indices of currently selected item."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002285 # XXX Ought to apply self._getints()...
Guido van Rossum18468821994-06-20 07:49:28 +00002286 return self.tk.splitlist(self.tk.call(
2287 self._w, 'curselection'))
2288 def delete(self, first, last=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002289 """Delete items from FIRST to LAST (not included)."""
Guido van Rossum18468821994-06-20 07:49:28 +00002290 self.tk.call(self._w, 'delete', first, last)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002291 def get(self, first, last=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002292 """Get list of items from FIRST to LAST (not included)."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002293 if last:
2294 return self.tk.splitlist(self.tk.call(
2295 self._w, 'get', first, last))
2296 else:
2297 return self.tk.call(self._w, 'get', first)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002298 def index(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002299 """Return index of item identified with INDEX."""
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002300 i = self.tk.call(self._w, 'index', index)
2301 if i == 'none': return None
2302 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00002303 def insert(self, index, *elements):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002304 """Insert ELEMENTS at INDEX."""
Guido van Rossumf9756991998-04-29 21:57:08 +00002305 self.tk.call((self._w, 'insert', index) + elements)
Guido van Rossum18468821994-06-20 07:49:28 +00002306 def nearest(self, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002307 """Get index of item which is nearest to y coordinate Y."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002308 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00002309 self._w, 'nearest', y))
2310 def scan_mark(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002311 """Remember the current X, Y coordinates."""
Guido van Rossum18468821994-06-20 07:49:28 +00002312 self.tk.call(self._w, 'scan', 'mark', x, y)
2313 def scan_dragto(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002314 """Adjust the view of the listbox to 10 times the
2315 difference between X and Y and the coordinates given in
2316 scan_mark."""
Guido van Rossum18468821994-06-20 07:49:28 +00002317 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002318 def see(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002319 """Scroll such that INDEX is visible."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002320 self.tk.call(self._w, 'see', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002321 def selection_anchor(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002322 """Set the fixed end oft the selection to INDEX."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002323 self.tk.call(self._w, 'selection', 'anchor', index)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002324 select_anchor = selection_anchor
2325 def selection_clear(self, first, last=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002326 """Clear the selection from FIRST to LAST (not included)."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002327 self.tk.call(self._w,
2328 'selection', 'clear', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002329 select_clear = selection_clear
2330 def selection_includes(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002331 """Return 1 if INDEX is part of the selection."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002332 return self.tk.getboolean(self.tk.call(
2333 self._w, 'selection', 'includes', index))
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002334 select_includes = selection_includes
2335 def selection_set(self, first, last=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002336 """Set the selection from FIRST to LAST (not included) without
2337 changing the currently selected elements."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002338 self.tk.call(self._w, 'selection', 'set', first, last)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002339 select_set = selection_set
Guido van Rossum18468821994-06-20 07:49:28 +00002340 def size(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002341 """Return the number of elements in the listbox."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002342 return getint(self.tk.call(self._w, 'size'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002343 def xview(self, *what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002344 """Query and change horizontal position of the view."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002345 if not what:
2346 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00002347 self.tk.call((self._w, 'xview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002348 def xview_moveto(self, fraction):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002349 """Adjust the view in the window so that FRACTION of the
2350 total width of the entry is off-screen to the left."""
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002351 self.tk.call(self._w, 'xview', 'moveto', fraction)
2352 def xview_scroll(self, number, what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002353 """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002354 self.tk.call(self._w, 'xview', 'scroll', number, what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002355 def yview(self, *what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002356 """Query and change vertical position of the view."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002357 if not what:
2358 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00002359 self.tk.call((self._w, 'yview') + what)
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002360 def yview_moveto(self, fraction):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002361 """Adjust the view in the window so that FRACTION of the
2362 total width of the entry is off-screen to the top."""
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002363 self.tk.call(self._w, 'yview', 'moveto', fraction)
2364 def yview_scroll(self, number, what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002365 """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT)."""
Guido van Rossum243ac4f1998-10-13 13:37:30 +00002366 self.tk.call(self._w, 'yview', 'scroll', number, what)
Guido van Rossum18468821994-06-20 07:49:28 +00002367
2368class Menu(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002369 """Menu widget which allows to display menu bars, pull-down menus and pop-up menus."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002370 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002371 """Construct menu widget with the parent MASTER.
2372
2373 Valid resource names: activebackground, activeborderwidth,
2374 activeforeground, background, bd, bg, borderwidth, cursor,
2375 disabledforeground, fg, font, foreground, postcommand, relief,
2376 selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002377 Widget.__init__(self, master, 'menu', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002378 def tk_bindForTraversal(self):
Guido van Rossum688bbfc1996-09-10 12:39:26 +00002379 pass # obsolete since Tk 4.0
Guido van Rossum18468821994-06-20 07:49:28 +00002380 def tk_mbPost(self):
2381 self.tk.call('tk_mbPost', self._w)
2382 def tk_mbUnpost(self):
2383 self.tk.call('tk_mbUnpost')
2384 def tk_traverseToMenu(self, char):
2385 self.tk.call('tk_traverseToMenu', self._w, char)
2386 def tk_traverseWithinMenu(self, char):
2387 self.tk.call('tk_traverseWithinMenu', self._w, char)
2388 def tk_getMenuButtons(self):
2389 return self.tk.call('tk_getMenuButtons', self._w)
2390 def tk_nextMenu(self, count):
2391 self.tk.call('tk_nextMenu', count)
2392 def tk_nextMenuEntry(self, count):
2393 self.tk.call('tk_nextMenuEntry', count)
2394 def tk_invokeMenu(self):
2395 self.tk.call('tk_invokeMenu', self._w)
2396 def tk_firstMenu(self):
2397 self.tk.call('tk_firstMenu', self._w)
2398 def tk_mbButtonDown(self):
2399 self.tk.call('tk_mbButtonDown', self._w)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002400 def tk_popup(self, x, y, entry=""):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002401 """Post the menu at position X,Y with entry ENTRY."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002402 self.tk.call('tk_popup', self._w, x, y, entry)
Guido van Rossum18468821994-06-20 07:49:28 +00002403 def activate(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002404 """Activate entry at INDEX."""
Guido van Rossum18468821994-06-20 07:49:28 +00002405 self.tk.call(self._w, 'activate', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002406 def add(self, itemType, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002407 """Internal function."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002408 self.tk.call((self._w, 'add', itemType) +
2409 self._options(cnf, kw))
Guido van Rossuma1db48b1995-10-09 22:37:28 +00002410 def add_cascade(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002411 """Add hierarchical menu item."""
Guido van Rossuma1db48b1995-10-09 22:37:28 +00002412 self.add('cascade', cnf or kw)
2413 def add_checkbutton(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002414 """Add checkbutton menu item."""
Guido van Rossuma1db48b1995-10-09 22:37:28 +00002415 self.add('checkbutton', cnf or kw)
2416 def add_command(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002417 """Add command menu item."""
Guido van Rossuma1db48b1995-10-09 22:37:28 +00002418 self.add('command', cnf or kw)
2419 def add_radiobutton(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002420 """Addd radio menu item."""
Guido van Rossuma1db48b1995-10-09 22:37:28 +00002421 self.add('radiobutton', cnf or kw)
2422 def add_separator(self, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002423 """Add separator."""
Guido van Rossuma1db48b1995-10-09 22:37:28 +00002424 self.add('separator', cnf or kw)
Guido van Rossum2caac731996-09-05 16:46:31 +00002425 def insert(self, index, itemType, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002426 """Internal function."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002427 self.tk.call((self._w, 'insert', index, itemType) +
2428 self._options(cnf, kw))
Guido van Rossum2caac731996-09-05 16:46:31 +00002429 def insert_cascade(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002430 """Add hierarchical menu item at INDEX."""
Guido van Rossum2caac731996-09-05 16:46:31 +00002431 self.insert(index, 'cascade', cnf or kw)
2432 def insert_checkbutton(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002433 """Add checkbutton menu item at INDEX."""
Guido van Rossum2caac731996-09-05 16:46:31 +00002434 self.insert(index, 'checkbutton', cnf or kw)
2435 def insert_command(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002436 """Add command menu item at INDEX."""
Guido van Rossum2caac731996-09-05 16:46:31 +00002437 self.insert(index, 'command', cnf or kw)
2438 def insert_radiobutton(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002439 """Addd radio menu item at INDEX."""
Guido van Rossum2caac731996-09-05 16:46:31 +00002440 self.insert(index, 'radiobutton', cnf or kw)
2441 def insert_separator(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002442 """Add separator at INDEX."""
Guido van Rossum2caac731996-09-05 16:46:31 +00002443 self.insert(index, 'separator', cnf or kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002444 def delete(self, index1, index2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002445 """Delete menu items between INDEX1 and INDEX2 (not included)."""
Guido van Rossum18468821994-06-20 07:49:28 +00002446 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00002447 def entrycget(self, index, option):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002448 """Return the resource value of an menu item for OPTION at INDEX."""
Guido van Rossum1cd6a451997-12-30 04:07:19 +00002449 return self.tk.call(self._w, 'entrycget', index, '-' + option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00002450 def entryconfigure(self, index, cnf=None, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002451 """Configure a menu item at INDEX."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002452 if cnf is None and not kw:
2453 cnf = {}
Guido van Rossumf9756991998-04-29 21:57:08 +00002454 for x in self.tk.split(self.tk.call(
2455 (self._w, 'entryconfigure', index))):
Guido van Rossum37dcab11996-05-16 16:00:19 +00002456 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
2457 return cnf
2458 if type(cnf) == StringType and not kw:
Guido van Rossumf9756991998-04-29 21:57:08 +00002459 x = self.tk.split(self.tk.call(
2460 (self._w, 'entryconfigure', index, '-'+cnf)))
Guido van Rossum37dcab11996-05-16 16:00:19 +00002461 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00002462 self.tk.call((self._w, 'entryconfigure', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002463 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00002464 entryconfig = entryconfigure
Guido van Rossum18468821994-06-20 07:49:28 +00002465 def index(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002466 """Return the index of a menu item identified by INDEX."""
Guido van Rossum535cf0c1994-06-27 07:55:59 +00002467 i = self.tk.call(self._w, 'index', index)
2468 if i == 'none': return None
Guido van Rossum0132f691998-04-30 17:50:36 +00002469 return getint(i)
Guido van Rossum18468821994-06-20 07:49:28 +00002470 def invoke(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002471 """Invoke a menu item identified by INDEX and execute
2472 the associated command."""
Guido van Rossum18468821994-06-20 07:49:28 +00002473 return self.tk.call(self._w, 'invoke', index)
2474 def post(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002475 """Display a menu at position X,Y."""
Guido van Rossum18468821994-06-20 07:49:28 +00002476 self.tk.call(self._w, 'post', x, y)
Guido van Rossum9fd41e31997-12-29 19:59:33 +00002477 def type(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002478 """Return the type of the menu item at INDEX."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00002479 return self.tk.call(self._w, 'type', index)
Guido van Rossum18468821994-06-20 07:49:28 +00002480 def unpost(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002481 """Unmap a menu."""
Guido van Rossum18468821994-06-20 07:49:28 +00002482 self.tk.call(self._w, 'unpost')
2483 def yposition(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002484 """Return the y-position of the topmost pixel of the menu item at INDEX."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002485 return getint(self.tk.call(
Guido van Rossum18468821994-06-20 07:49:28 +00002486 self._w, 'yposition', index))
2487
2488class Menubutton(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002489 """Menubutton widget, obsolete since Tk8.0."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002490 def __init__(self, master=None, cnf={}, **kw):
2491 Widget.__init__(self, master, 'menubutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002492
2493class Message(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002494 """Message widget to display multiline text. Obsolete since Label does it too."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002495 def __init__(self, master=None, cnf={}, **kw):
2496 Widget.__init__(self, master, 'message', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002497
2498class Radiobutton(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002499 """Radiobutton widget which shows only one of several buttons in on-state."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002500 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002501 """Construct a radiobutton widget with the parent MASTER.
2502
2503 Valid resource names: activebackground, activeforeground, anchor,
2504 background, bd, bg, bitmap, borderwidth, command, cursor,
2505 disabledforeground, fg, font, foreground, height,
2506 highlightbackground, highlightcolor, highlightthickness, image,
2507 indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
2508 state, takefocus, text, textvariable, underline, value, variable,
2509 width, wraplength."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002510 Widget.__init__(self, master, 'radiobutton', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002511 def deselect(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002512 """Put the button in off-state."""
2513
Guido van Rossum18468821994-06-20 07:49:28 +00002514 self.tk.call(self._w, 'deselect')
2515 def flash(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002516 """Flash the button."""
Guido van Rossum18468821994-06-20 07:49:28 +00002517 self.tk.call(self._w, 'flash')
2518 def invoke(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002519 """Toggle the button and invoke a command if given as resource."""
Guido van Rossum9fd41e31997-12-29 19:59:33 +00002520 return self.tk.call(self._w, 'invoke')
Guido van Rossum18468821994-06-20 07:49:28 +00002521 def select(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002522 """Put the button in on-state."""
Guido van Rossum18468821994-06-20 07:49:28 +00002523 self.tk.call(self._w, 'select')
2524
2525class Scale(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002526 """Scale widget which can display a numerical scale."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002527 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002528 """Construct a scale widget with the parent MASTER.
2529
2530 Valid resource names: activebackground, background, bigincrement, bd,
2531 bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
2532 highlightbackground, highlightcolor, highlightthickness, label,
2533 length, orient, relief, repeatdelay, repeatinterval, resolution,
2534 showvalue, sliderlength, sliderrelief, state, takefocus,
2535 tickinterval, to, troughcolor, variable, width."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002536 Widget.__init__(self, master, 'scale', cnf, kw)
Guido van Rossum18468821994-06-20 07:49:28 +00002537 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002538 """Get the current value as integer or float."""
Guido van Rossum14957471996-10-23 14:16:28 +00002539 value = self.tk.call(self._w, 'get')
2540 try:
Guido van Rossum0132f691998-04-30 17:50:36 +00002541 return getint(value)
Guido van Rossumfe02efd1998-06-09 02:37:45 +00002542 except ValueError:
Guido van Rossum0132f691998-04-30 17:50:36 +00002543 return getdouble(value)
Guido van Rossum18468821994-06-20 07:49:28 +00002544 def set(self, value):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002545 """Set the value to VALUE."""
Guido van Rossum18468821994-06-20 07:49:28 +00002546 self.tk.call(self._w, 'set', value)
Guido van Rossumb4750db1998-08-11 19:07:14 +00002547 def coords(self, value=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002548 """Return a tuple (X,Y) of the point along the centerline of the
2549 trough that corresponds to VALUE or the current value if None is
2550 given."""
2551
Guido van Rossumb4750db1998-08-11 19:07:14 +00002552 return self._getints(self.tk.call(self._w, 'coords', value))
2553 def identify(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002554 """Return where the point X,Y lies. Valid return values are "slider",
2555 "though1" and "though2"."""
Guido van Rossumb4750db1998-08-11 19:07:14 +00002556 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00002557
2558class Scrollbar(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002559 """Scrollbar widget which displays a slider at a certain position."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002560 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002561 """Construct a scrollbar widget with the parent MASTER.
2562
2563 Valid resource names: activebackground, activerelief,
2564 background, bd, bg, borderwidth, command, cursor,
2565 elementborderwidth, highlightbackground,
2566 highlightcolor, highlightthickness, jump, orient,
2567 relief, repeatdelay, repeatinterval, takefocus,
2568 troughcolor, width."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002569 Widget.__init__(self, master, 'scrollbar', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002570 def activate(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002571 """Display the element at INDEX with activebackground and activerelief.
2572 INDEX can be "arrow1","slider" or "arrow2"."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002573 self.tk.call(self._w, 'activate', index)
2574 def delta(self, deltax, deltay):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002575 """Return the fractional change of the scrollbar setting if it
2576 would be moved by DELTAX or DELTAY pixels."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002577 return getdouble(
2578 self.tk.call(self._w, 'delta', deltax, deltay))
Guido van Rossum37dcab11996-05-16 16:00:19 +00002579 def fraction(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002580 """Return the fractional value which corresponds to a slider
2581 position of X,Y."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002582 return getdouble(self.tk.call(self._w, 'fraction', x, y))
Guido van Rossum37dcab11996-05-16 16:00:19 +00002583 def identify(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002584 """Return the element under position X,Y as one of
2585 "arrow1","slider","arrow2" or ""."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002586 return self.tk.call(self._w, 'identify', x, y)
Guido van Rossum18468821994-06-20 07:49:28 +00002587 def get(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002588 """Return the current fractional values (upper and lower end)
2589 of the slider position."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002590 return self._getdoubles(self.tk.call(self._w, 'get'))
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002591 def set(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002592 """Set the fractional values of the slider position (upper and
2593 lower ends as value between 0 and 1)."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002594 self.tk.call((self._w, 'set') + args)
Guido van Rossum18468821994-06-20 07:49:28 +00002595
2596class Text(Widget):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002597 """Text widget which can display text in various forms."""
Guido van Rossumdfaac4d1998-12-21 18:25:03 +00002598 # XXX Add dump()
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002599 def __init__(self, master=None, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002600 """Construct a text widget with the parent MASTER.
2601
2602 Valid resource names: background, bd, bg, borderwidth, cursor,
2603 exportselection, fg, font, foreground, height,
2604 highlightbackground, highlightcolor, highlightthickness,
2605 insertbackground, insertborderwidth, insertofftime,
2606 insertontime, insertwidth, padx, pady, relief,
2607 selectbackground, selectborderwidth, selectforeground,
2608 setgrid, spacing1, spacing2, spacing3, state, tabs, takefocus,
2609 width, wrap, xscrollcommand, yscrollcommand."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002610 Widget.__init__(self, master, 'text', cnf, kw)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002611 def bbox(self, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002612 """Return a tuple of (x,y,width,heigth) which gives the bounding
2613 box of the visible part of the character at the index in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002614 return self._getints(
2615 self.tk.call((self._w, 'bbox') + args)) or None
Guido van Rossum18468821994-06-20 07:49:28 +00002616 def tk_textSelectTo(self, index):
2617 self.tk.call('tk_textSelectTo', self._w, index)
2618 def tk_textBackspace(self):
2619 self.tk.call('tk_textBackspace', self._w)
2620 def tk_textIndexCloser(self, a, b, c):
2621 self.tk.call('tk_textIndexCloser', self._w, a, b, c)
2622 def tk_textResetAnchor(self, index):
2623 self.tk.call('tk_textResetAnchor', self._w, index)
2624 def compare(self, index1, op, index2):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002625 """Return whether between index INDEX1 and index INDEX2 the
2626 relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=."""
Guido van Rossum18468821994-06-20 07:49:28 +00002627 return self.tk.getboolean(self.tk.call(
2628 self._w, 'compare', index1, op, index2))
2629 def debug(self, boolean=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002630 """Turn on the internal consistency checks of the B-Tree inside the text
2631 widget according to BOOLEAN."""
Guido van Rossum18468821994-06-20 07:49:28 +00002632 return self.tk.getboolean(self.tk.call(
2633 self._w, 'debug', boolean))
2634 def delete(self, index1, index2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002635 """Delete the characters between INDEX1 and INDEX2 (not included)."""
Guido van Rossum18468821994-06-20 07:49:28 +00002636 self.tk.call(self._w, 'delete', index1, index2)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002637 def dlineinfo(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002638 """Return tuple (x,y,width,height,baseline) giving the bounding box
2639 and baseline position of the visible part of the line containing
2640 the character at INDEX."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002641 return self._getints(self.tk.call(self._w, 'dlineinfo', index))
Guido van Rossum18468821994-06-20 07:49:28 +00002642 def get(self, index1, index2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002643 """Return the text from INDEX1 to INDEX2 (not included)."""
Guido van Rossum18468821994-06-20 07:49:28 +00002644 return self.tk.call(self._w, 'get', index1, index2)
Guido van Rossumc86b7c61998-08-31 16:54:33 +00002645 # (Image commands are new in 8.0)
2646 def image_cget(self, index, option):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002647 """Return the value of OPTION of an embedded image at INDEX."""
Guido van Rossumc86b7c61998-08-31 16:54:33 +00002648 if option[:1] != "-":
2649 option = "-" + option
2650 if option[-1:] == "_":
2651 option = option[:-1]
2652 return self.tk.call(self._w, "image", "cget", index, option)
2653 def image_configure(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002654 """Configure an embedded image at INDEX."""
Guido van Rossumc86b7c61998-08-31 16:54:33 +00002655 if not cnf and not kw:
2656 cnf = {}
2657 for x in self.tk.split(
2658 self.tk.call(
2659 self._w, "image", "configure", index)):
2660 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
2661 return cnf
2662 apply(self.tk.call,
2663 (self._w, "image", "configure", index)
2664 + self._options(cnf, kw))
2665 def image_create(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002666 """Create an embedded image at INDEX."""
Guido van Rossumc86b7c61998-08-31 16:54:33 +00002667 return apply(self.tk.call,
2668 (self._w, "image", "create", index)
2669 + self._options(cnf, kw))
2670 def image_names(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002671 """Return all names of embedded images in this widget."""
Guido van Rossumc86b7c61998-08-31 16:54:33 +00002672 return self.tk.call(self._w, "image", "names")
Guido van Rossum18468821994-06-20 07:49:28 +00002673 def index(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002674 """Return the index in the form line.char for INDEX."""
Guido van Rossum18468821994-06-20 07:49:28 +00002675 return self.tk.call(self._w, 'index', index)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002676 def insert(self, index, chars, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002677 """Insert CHARS before the charaters at INDEX. An additional
2678 tag can be given in ARGS. Additional CHARS and tags can follow in ARGS."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002679 self.tk.call((self._w, 'insert', index, chars) + args)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002680 def mark_gravity(self, markName, direction=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002681 """Change the gravity of a mark MARKNAME to DIRECTION (LEFT or RIGHT).
2682 Return the current value if None is given for DIRECTION."""
Guido van Rossumf9756991998-04-29 21:57:08 +00002683 return self.tk.call(
2684 (self._w, 'mark', 'gravity', markName, direction))
Guido van Rossum18468821994-06-20 07:49:28 +00002685 def mark_names(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002686 """Return all mark names."""
Guido van Rossum18468821994-06-20 07:49:28 +00002687 return self.tk.splitlist(self.tk.call(
2688 self._w, 'mark', 'names'))
2689 def mark_set(self, markName, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002690 """Set mark MARKNAME before the character at INDEX."""
Guido van Rossum18468821994-06-20 07:49:28 +00002691 self.tk.call(self._w, 'mark', 'set', markName, index)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002692 def mark_unset(self, *markNames):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002693 """Delete all marks in MARKNAMES."""
Guido van Rossumf9756991998-04-29 21:57:08 +00002694 self.tk.call((self._w, 'mark', 'unset') + markNames)
Guido van Rossum1a03cf51999-06-21 14:13:30 +00002695 def mark_next(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002696 """Return the name of the next mark after INDEX."""
Guido van Rossum1a03cf51999-06-21 14:13:30 +00002697 return self.tk.call(self._w, 'mark', 'next', index) or None
2698 def mark_previous(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002699 """Return the name of the previous mark before INDEX."""
Guido van Rossum1a03cf51999-06-21 14:13:30 +00002700 return self.tk.call(self._w, 'mark', 'previous', index) or None
Guido van Rossum37dcab11996-05-16 16:00:19 +00002701 def scan_mark(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002702 """Remember the current X, Y coordinates."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002703 self.tk.call(self._w, 'scan', 'mark', x, y)
2704 def scan_dragto(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002705 """Adjust the view of the text to 10 times the
2706 difference between X and Y and the coordinates given in
2707 scan_mark."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002708 self.tk.call(self._w, 'scan', 'dragto', x, y)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002709 def search(self, pattern, index, stopindex=None,
2710 forwards=None, backwards=None, exact=None,
2711 regexp=None, nocase=None, count=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002712 """Search PATTERN beginning from INDEX until STOPINDEX.
2713 Return the index of the first character of a match or an empty string."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002714 args = [self._w, 'search']
2715 if forwards: args.append('-forwards')
2716 if backwards: args.append('-backwards')
2717 if exact: args.append('-exact')
2718 if regexp: args.append('-regexp')
2719 if nocase: args.append('-nocase')
2720 if count: args.append('-count'); args.append(count)
2721 if pattern[0] == '-': args.append('--')
2722 args.append(pattern)
2723 args.append(index)
2724 if stopindex: args.append(stopindex)
Guido van Rossumf9756991998-04-29 21:57:08 +00002725 return self.tk.call(tuple(args))
Guido van Rossum37dcab11996-05-16 16:00:19 +00002726 def see(self, index):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002727 """Scroll such that the character at INDEX is visible."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002728 self.tk.call(self._w, 'see', index)
Guido van Rossumfa8c3ea1999-06-01 13:57:15 +00002729 def tag_add(self, tagName, index1, *args):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002730 """Add tag TAGNAME to all characters between INDEX1 and index2 in ARGS.
2731 Addtional pairs of indices may follow in ARGS."""
Guido van Rossum18468821994-06-20 07:49:28 +00002732 self.tk.call(
Guido van Rossumfa8c3ea1999-06-01 13:57:15 +00002733 (self._w, 'tag', 'add', tagName, index1) + args)
Guido van Rossum117a5a81998-03-27 21:26:51 +00002734 def tag_unbind(self, tagName, sequence, funcid=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002735 """Unbind for all characters with TAGNAME for event SEQUENCE the
2736 function identified with FUNCID."""
Guido van Rossumef8f8811994-08-08 12:47:33 +00002737 self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
Guido van Rossum117a5a81998-03-27 21:26:51 +00002738 if funcid:
2739 self.deletecommand(funcid)
Guido van Rossum37dcab11996-05-16 16:00:19 +00002740 def tag_bind(self, tagName, sequence, func, add=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002741 """Bind to all characters with TAGNAME at event SEQUENCE a call to function FUNC.
2742
2743 An additional boolean parameter ADD specifies whether FUNC will be
2744 called additionally to the other bound function or whether it will
2745 replace the previous function. See bind for the return value."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002746 return self._bind((self._w, 'tag', 'bind', tagName),
2747 sequence, func, add)
2748 def tag_cget(self, tagName, option):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002749 """Return the value of OPTION for tag TAGNAME."""
Guido van Rossum73eba251996-11-11 19:10:58 +00002750 if option[:1] != '-':
2751 option = '-' + option
2752 if option[-1:] == '_':
2753 option = option[:-1]
Guido van Rossum37dcab11996-05-16 16:00:19 +00002754 return self.tk.call(self._w, 'tag', 'cget', tagName, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00002755 def tag_configure(self, tagName, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002756 """Configure a tag TAGNAME."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002757 if type(cnf) == StringType:
2758 x = self.tk.split(self.tk.call(
2759 self._w, 'tag', 'configure', tagName, '-'+cnf))
2760 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00002761 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002762 (self._w, 'tag', 'configure', tagName)
2763 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00002764 tag_config = tag_configure
Guido van Rossum2dcf5291994-07-06 09:23:20 +00002765 def tag_delete(self, *tagNames):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002766 """Delete all tags in TAGNAMES."""
Guido van Rossumf9756991998-04-29 21:57:08 +00002767 self.tk.call((self._w, 'tag', 'delete') + tagNames)
Guido van Rossum18468821994-06-20 07:49:28 +00002768 def tag_lower(self, tagName, belowThis=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002769 """Change the priority of tag TAGNAME such that it is lower
2770 than the priority of BELOWTHIS."""
Guido van Rossum97aeca11994-07-07 13:12:12 +00002771 self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
Guido van Rossum18468821994-06-20 07:49:28 +00002772 def tag_names(self, index=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002773 """Return a list of all tag names."""
Guido van Rossum18468821994-06-20 07:49:28 +00002774 return self.tk.splitlist(
2775 self.tk.call(self._w, 'tag', 'names', index))
2776 def tag_nextrange(self, tagName, index1, index2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002777 """Return a list of start and end index for the first sequence of
2778 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
2779 The text is searched forward from INDEX1."""
Guido van Rossum18468821994-06-20 07:49:28 +00002780 return self.tk.splitlist(self.tk.call(
Guido van Rossum903abee1995-03-20 15:09:13 +00002781 self._w, 'tag', 'nextrange', tagName, index1, index2))
Guido van Rossumf0413d41997-12-15 17:31:52 +00002782 def tag_prevrange(self, tagName, index1, index2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002783 """Return a list of start and end index for the first sequence of
2784 characters between INDEX1 and INDEX2 which all have tag TAGNAME.
2785 The text is searched backwards from INDEX1."""
Guido van Rossumf0413d41997-12-15 17:31:52 +00002786 return self.tk.splitlist(self.tk.call(
2787 self._w, 'tag', 'prevrange', tagName, index1, index2))
Guido van Rossum18468821994-06-20 07:49:28 +00002788 def tag_raise(self, tagName, aboveThis=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002789 """Change the priority of tag TAGNAME such that it is higher
2790 than the priority of ABOVETHIS."""
Guido van Rossum18468821994-06-20 07:49:28 +00002791 self.tk.call(
2792 self._w, 'tag', 'raise', tagName, aboveThis)
2793 def tag_ranges(self, tagName):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002794 """Return a list of ranges of text which have tag TAGNAME."""
Guido van Rossum18468821994-06-20 07:49:28 +00002795 return self.tk.splitlist(self.tk.call(
2796 self._w, 'tag', 'ranges', tagName))
2797 def tag_remove(self, tagName, index1, index2=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002798 """Remove tag TAGNAME from all characters between INDEX1 and INDEX2."""
Guido van Rossum18468821994-06-20 07:49:28 +00002799 self.tk.call(
Guido van Rossum51135691994-07-06 21:16:58 +00002800 self._w, 'tag', 'remove', tagName, index1, index2)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002801 def window_cget(self, index, option):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002802 """Return the value of OPTION of an embedded window at INDEX."""
Guido van Rossum7814ea61997-12-11 17:08:52 +00002803 if option[:1] != '-':
2804 option = '-' + option
2805 if option[-1:] == '_':
2806 option = option[:-1]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002807 return self.tk.call(self._w, 'window', 'cget', index, option)
Guido van Rossum368e06b1997-11-07 20:38:49 +00002808 def window_configure(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002809 """Configure an embedded window at INDEX."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002810 if type(cnf) == StringType:
2811 x = self.tk.split(self.tk.call(
2812 self._w, 'window', 'configure',
2813 index, '-'+cnf))
2814 return (x[0][1:],) + x[1:]
Guido van Rossumf9756991998-04-29 21:57:08 +00002815 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002816 (self._w, 'window', 'configure', index)
2817 + self._options(cnf, kw))
Guido van Rossum368e06b1997-11-07 20:38:49 +00002818 window_config = window_configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002819 def window_create(self, index, cnf={}, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002820 """Create a window at INDEX."""
Guido van Rossumf9756991998-04-29 21:57:08 +00002821 self.tk.call(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002822 (self._w, 'window', 'create', index)
2823 + self._options(cnf, kw))
2824 def window_names(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002825 """Return all names of embedded windows in this widget."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002826 return self.tk.splitlist(
2827 self.tk.call(self._w, 'window', 'names'))
Guido van Rossum37dcab11996-05-16 16:00:19 +00002828 def xview(self, *what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002829 """Query and change horizontal position of the view."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002830 if not what:
2831 return self._getdoubles(self.tk.call(self._w, 'xview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00002832 self.tk.call((self._w, 'xview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002833 def yview(self, *what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002834 """Query and change vertical position of the view."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002835 if not what:
2836 return self._getdoubles(self.tk.call(self._w, 'yview'))
Guido van Rossum0bd54331998-05-19 21:18:13 +00002837 self.tk.call((self._w, 'yview') + what)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00002838 def yview_pickplace(self, *what):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002839 """Obsolete function, use see."""
Guido van Rossum0bd54331998-05-19 21:18:13 +00002840 self.tk.call((self._w, 'yview', '-pickplace') + what)
Guido van Rossum18468821994-06-20 07:49:28 +00002841
Guido van Rossum28574b51996-10-21 15:16:51 +00002842class _setit:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002843 """Internal class. It wraps the command in the widget OptionMenu."""
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002844 def __init__(self, var, value, callback=None):
Guido van Rossum28574b51996-10-21 15:16:51 +00002845 self.__value = value
2846 self.__var = var
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002847 self.__callback = callback
Guido van Rossum28574b51996-10-21 15:16:51 +00002848 def __call__(self, *args):
Fred Drake0c373691996-10-21 17:09:31 +00002849 self.__var.set(self.__value)
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002850 if self.__callback:
2851 apply(self.__callback, (self.__value,)+args)
Guido van Rossum28574b51996-10-21 15:16:51 +00002852
2853class OptionMenu(Menubutton):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002854 """OptionMenu which allows the user to select a value from a menu."""
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002855 def __init__(self, master, variable, value, *values, **kwargs):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002856 """Construct an optionmenu widget with the parent MASTER, with
2857 the resource textvariable set to VARIABLE, the initially selected
2858 value VALUE, the other menu values VALUES and an additional
2859 keyword argument command."""
Guido van Rossum28574b51996-10-21 15:16:51 +00002860 kw = {"borderwidth": 2, "textvariable": variable,
2861 "indicatoron": 1, "relief": RAISED, "anchor": "c",
2862 "highlightthickness": 2}
2863 Widget.__init__(self, master, "menubutton", kw)
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00002864 self.widgetName = 'tk_optionMenu'
Guido van Rossum28574b51996-10-21 15:16:51 +00002865 menu = self.__menu = Menu(self, name="menu", tearoff=0)
2866 self.menuname = menu._w
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002867 # 'command' is the only supported keyword
2868 callback = kwargs.get('command')
Guido van Rossum0ba33002000-02-27 15:35:47 +00002869 if kwargs.has_key('command'):
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002870 del kwargs['command']
2871 if kwargs:
2872 raise TclError, 'unknown option -'+kwargs.keys()[0]
2873 menu.add_command(label=value,
2874 command=_setit(variable, value, callback))
Guido van Rossum28574b51996-10-21 15:16:51 +00002875 for v in values:
Barry Warsaw7d3f27c2000-02-25 21:54:19 +00002876 menu.add_command(label=v,
2877 command=_setit(variable, v, callback))
Guido van Rossum28574b51996-10-21 15:16:51 +00002878 self["menu"] = menu
2879
2880 def __getitem__(self, name):
2881 if name == 'menu':
2882 return self.__menu
2883 return Widget.__getitem__(self, name)
2884
2885 def destroy(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002886 """Destroy this widget and the associated menu."""
Guido van Rossum28574b51996-10-21 15:16:51 +00002887 Menubutton.destroy(self)
2888 self.__menu = None
Guido van Rossumbf4d8f91995-09-01 20:35:37 +00002889
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002890class Image:
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002891 """Base class for images."""
Guido van Rossumc4570481998-03-20 20:45:49 +00002892 def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002893 self.name = None
Guido van Rossumc4570481998-03-20 20:45:49 +00002894 if not master:
2895 master = _default_root
2896 if not master:
2897 raise RuntimeError, 'Too early to create image'
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002898 self.tk = master.tk
Guido van Rossum58103d31996-11-20 22:17:38 +00002899 if not name:
2900 name = `id(self)`
2901 # The following is needed for systems where id(x)
2902 # can return a negative number, such as Linux/m68k:
2903 if name[0] == '-': name = '_' + name[1:]
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002904 if kw and cnf: cnf = _cnfmerge((cnf, kw))
2905 elif kw: cnf = kw
2906 options = ()
2907 for k, v in cnf.items():
Guido van Rossum37dcab11996-05-16 16:00:19 +00002908 if callable(v):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002909 v = self._register(v)
2910 options = options + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00002911 self.tk.call(('image', 'create', imgtype, name,) + options)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002912 self.name = name
2913 def __str__(self): return self.name
2914 def __del__(self):
2915 if self.name:
Guido van Rossumc55b0ca1999-02-08 15:26:49 +00002916 try:
2917 self.tk.call('image', 'delete', self.name)
2918 except TclError:
2919 # May happen if the root was destroyed
2920 pass
Guido van Rossum71b1a901995-09-18 21:54:35 +00002921 def __setitem__(self, key, value):
2922 self.tk.call(self.name, 'configure', '-'+key, value)
2923 def __getitem__(self, key):
2924 return self.tk.call(self.name, 'configure', '-'+key)
Guido van Rossum368e06b1997-11-07 20:38:49 +00002925 def configure(self, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002926 """Configure the image."""
Guido van Rossum83710131996-12-27 15:33:17 +00002927 res = ()
2928 for k, v in _cnfmerge(kw).items():
2929 if v is not None:
2930 if k[-1] == '_': k = k[:-1]
2931 if callable(v):
2932 v = self._register(v)
2933 res = res + ('-'+k, v)
Guido van Rossumf9756991998-04-29 21:57:08 +00002934 self.tk.call((self.name, 'config') + res)
Guido van Rossum368e06b1997-11-07 20:38:49 +00002935 config = configure
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002936 def height(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002937 """Return the height of the image."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002938 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002939 self.tk.call('image', 'height', self.name))
2940 def type(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002941 """Return the type of the imgage, e.g. "photo" or "bitmap"."""
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002942 return self.tk.call('image', 'type', self.name)
2943 def width(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002944 """Return the width of the image."""
Guido van Rossum0132f691998-04-30 17:50:36 +00002945 return getint(
Guido van Rossum35f67fb1995-08-04 03:50:29 +00002946 self.tk.call('image', 'width', self.name))
2947
2948class PhotoImage(Image):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002949 """Widget which can display colored images in GIF, PPM/PGM format."""
Guido van Rossumc4570481998-03-20 20:45:49 +00002950 def __init__(self, name=None, cnf={}, master=None, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002951 """Create an image with NAME.
2952
2953 Valid resource names: data, format, file, gamma, height, palette,
2954 width."""
Guido van Rossumc4570481998-03-20 20:45:49 +00002955 apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002956 def blank(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002957 """Display a transparent image."""
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002958 self.tk.call(self.name, 'blank')
Guido van Rossum37dcab11996-05-16 16:00:19 +00002959 def cget(self, option):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002960 """Return the value of OPTION."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002961 return self.tk.call(self.name, 'cget', '-' + option)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002962 # XXX config
Guido van Rossum37dcab11996-05-16 16:00:19 +00002963 def __getitem__(self, key):
2964 return self.tk.call(self.name, 'cget', '-' + key)
Guido van Rossum5ac00ac1997-12-11 02:03:55 +00002965 # XXX copy -from, -to, ...?
Guido van Rossum37dcab11996-05-16 16:00:19 +00002966 def copy(self):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002967 """Return a new PhotoImage with the same image as this widget."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002968 destImage = PhotoImage()
2969 self.tk.call(destImage, 'copy', self.name)
2970 return destImage
2971 def zoom(self,x,y=''):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002972 """Return a new PhotoImage with the same image as this widget
2973 but zoom it with X and Y."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002974 destImage = PhotoImage()
2975 if y=='': y=x
2976 self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
2977 return destImage
2978 def subsample(self,x,y=''):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002979 """Return a new PhotoImage based on the same image as this widget
2980 but use only every Xth or Yth pixel."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00002981 destImage = PhotoImage()
2982 if y=='': y=x
2983 self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
2984 return destImage
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002985 def get(self, x, y):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002986 """Return the color (red, green, blue) of the pixel at X,Y."""
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002987 return self.tk.call(self.name, 'get', x, y)
2988 def put(self, data, to=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002989 """Put row formated colors to image starting from
2990 position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))"""
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002991 args = (self.name, 'put', data)
2992 if to:
Fred Drakeb5323991997-12-16 15:03:43 +00002993 if to[0] == '-to':
2994 to = to[1:]
2995 args = args + ('-to',) + tuple(to)
Guido van Rossumf9756991998-04-29 21:57:08 +00002996 self.tk.call(args)
Guido van Rossum96ebbd31995-09-30 17:05:26 +00002997 # XXX read
Guido van Rossum37dcab11996-05-16 16:00:19 +00002998 def write(self, filename, format=None, from_coords=None):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00002999 """Write image to file FILENAME in FORMAT starting from
3000 position FROM_COORDS."""
Guido van Rossum37dcab11996-05-16 16:00:19 +00003001 args = (self.name, 'write', filename)
3002 if format:
3003 args = args + ('-format', format)
3004 if from_coords:
3005 args = args + ('-from',) + tuple(from_coords)
Guido van Rossumf9756991998-04-29 21:57:08 +00003006 self.tk.call(args)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003007
3008class BitmapImage(Image):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003009 """Widget which can display a bitmap."""
Guido van Rossumc4570481998-03-20 20:45:49 +00003010 def __init__(self, name=None, cnf={}, master=None, **kw):
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003011 """Create a bitmap with NAME.
3012
3013 Valid resource names: background, data, file, foreground, maskdata, maskfile."""
Guido van Rossumc4570481998-03-20 20:45:49 +00003014 apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003015
3016def image_names(): return _default_root.tk.call('image', 'names')
3017def image_types(): return _default_root.tk.call('image', 'types')
3018
Guido van Rossumaec5dc91994-06-27 07:55:12 +00003019######################################################################
3020# Extensions:
3021
3022class Studbutton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003023 def __init__(self, master=None, cnf={}, **kw):
3024 Widget.__init__(self, master, 'studbutton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00003025 self.bind('<Any-Enter>', self.tkButtonEnter)
3026 self.bind('<Any-Leave>', self.tkButtonLeave)
3027 self.bind('<1>', self.tkButtonDown)
3028 self.bind('<ButtonRelease-1>', self.tkButtonUp)
Guido van Rossumaec5dc91994-06-27 07:55:12 +00003029
3030class Tributton(Button):
Guido van Rossum35f67fb1995-08-04 03:50:29 +00003031 def __init__(self, master=None, cnf={}, **kw):
3032 Widget.__init__(self, master, 'tributton', cnf, kw)
Guido van Rossum761c5ab1995-07-14 15:29:10 +00003033 self.bind('<Any-Enter>', self.tkButtonEnter)
3034 self.bind('<Any-Leave>', self.tkButtonLeave)
3035 self.bind('<1>', self.tkButtonDown)
3036 self.bind('<ButtonRelease-1>', self.tkButtonUp)
3037 self['fg'] = self['bg']
3038 self['activebackground'] = self['bg']
Guido van Rossum37dcab11996-05-16 16:00:19 +00003039
Guido van Rossumc417ef81996-08-21 23:38:59 +00003040######################################################################
3041# Test:
3042
3043def _test():
3044 root = Tk()
Guido van Rossum2ab90822000-03-30 23:19:44 +00003045 text = "This is Tcl/Tk version %s" % TclVersion
3046 if TclVersion >= 8.1:
3047 text = text + u"\nThis should be a cedilla: \347"
3048 label = Label(root, text=text)
Guido van Rossumc417ef81996-08-21 23:38:59 +00003049 label.pack()
3050 test = Button(root, text="Click me!",
Guido van Rossum368e06b1997-11-07 20:38:49 +00003051 command=lambda root=root: root.test.configure(
Guido van Rossumc417ef81996-08-21 23:38:59 +00003052 text="[%s]" % root.test['text']))
3053 test.pack()
3054 root.test = test
3055 quit = Button(root, text="QUIT", command=root.destroy)
3056 quit.pack()
Guido van Rossum268824e1998-06-19 04:34:19 +00003057 # The following three commands are needed so the window pops
3058 # up on top on Windows...
3059 root.iconify()
3060 root.update()
3061 root.deiconify()
Guido van Rossumc417ef81996-08-21 23:38:59 +00003062 root.mainloop()
3063
3064if __name__ == '__main__':
3065 _test()
Guido van Rossum5917ecb2000-06-29 16:30:50 +00003066