Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1 | """Wrapper functions for Tcl/Tk. |
| 2 | |
| 3 | Tkinter provides classes which allow the display, positioning and |
| 4 | control of widgets. Toplevel widgets are Tk and Toplevel. Other |
| 5 | widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 6 | Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 7 | LabelFrame and PanedWindow. |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 9 | Properties of the widgets are specified with keyword arguments. |
| 10 | Keyword arguments have the same name as the corresponding resource |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 11 | under Tk. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 12 | |
| 13 | Widgets are positioned with one of the geometry managers Place, Pack |
| 14 | or Grid. These managers can be called with methods place, pack, grid |
| 15 | available in every Widget. |
| 16 | |
Guido van Rossum | a0adb92 | 2001-09-01 18:29:55 +0000 | [diff] [blame] | 17 | Actions are bound to events by resources (e.g. keyword argument |
| 18 | command) or with the method bind. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 19 | |
| 20 | Example (Hello, World): |
| 21 | import Tkinter |
| 22 | from Tkconstants import * |
| 23 | tk = Tkinter.Tk() |
| 24 | frame = Tkinter.Frame(tk, relief=RIDGE, borderwidth=2) |
| 25 | frame.pack(fill=BOTH,expand=1) |
| 26 | label = Tkinter.Label(frame, text="Hello, World") |
| 27 | label.pack(fill=X, expand=1) |
| 28 | button = Tkinter.Button(frame,text="Exit",command=tk.destroy) |
| 29 | button.pack(side=BOTTOM) |
| 30 | tk.mainloop() |
| 31 | """ |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 33 | __version__ = "$Revision$" |
| 34 | |
Guido van Rossum | f8d579c | 1999-01-04 18:06:45 +0000 | [diff] [blame] | 35 | import sys |
| 36 | if sys.platform == "win32": |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 37 | import FixTk # Attempt to configure Tcl/Tk without requiring PATH |
Guido van Rossum | f8d579c | 1999-01-04 18:06:45 +0000 | [diff] [blame] | 38 | import _tkinter # If this fails your Python may not be configured for Tk |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 39 | tkinter = _tkinter # b/w compat for export |
| 40 | TclError = _tkinter.TclError |
Guido van Rossum | 7e9394a | 1995-03-17 16:21:33 +0000 | [diff] [blame] | 41 | from types import * |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 42 | from Tkconstants import * |
Guido van Rossum | f0c891a | 1998-04-29 21:43:36 +0000 | [diff] [blame] | 43 | try: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 44 | import MacOS; _MacOS = MacOS; del MacOS |
Guido van Rossum | f0c891a | 1998-04-29 21:43:36 +0000 | [diff] [blame] | 45 | except ImportError: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 46 | _MacOS = None |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 8c8aa5d | 2002-11-26 21:39:48 +0000 | [diff] [blame] | 48 | wantobjects = 1 |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 49 | |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 50 | TkVersion = float(_tkinter.TK_VERSION) |
| 51 | TclVersion = float(_tkinter.TCL_VERSION) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d6615ab | 1997-08-05 02:35:01 +0000 | [diff] [blame] | 53 | READABLE = _tkinter.READABLE |
| 54 | WRITABLE = _tkinter.WRITABLE |
| 55 | EXCEPTION = _tkinter.EXCEPTION |
Guido van Rossum | f53c86c | 1997-08-14 14:15:54 +0000 | [diff] [blame] | 56 | |
| 57 | # These are not always defined, e.g. not on Win32 with Tk 8.0 :-( |
| 58 | try: _tkinter.createfilehandler |
| 59 | except AttributeError: _tkinter.createfilehandler = None |
| 60 | try: _tkinter.deletefilehandler |
| 61 | except AttributeError: _tkinter.deletefilehandler = None |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 62 | |
| 63 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 64 | def _flatten(tuple): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 65 | """Internal function.""" |
| 66 | res = () |
| 67 | for item in tuple: |
| 68 | if type(item) in (TupleType, ListType): |
| 69 | res = res + _flatten(item) |
| 70 | elif item is not None: |
| 71 | res = res + (item,) |
| 72 | return res |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 73 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 74 | try: _flatten = _tkinter._flatten |
| 75 | except AttributeError: pass |
| 76 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 77 | def _cnfmerge(cnfs): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 78 | """Internal function.""" |
| 79 | if type(cnfs) is DictionaryType: |
| 80 | return cnfs |
| 81 | elif type(cnfs) in (NoneType, StringType): |
| 82 | return cnfs |
| 83 | else: |
| 84 | cnf = {} |
| 85 | for c in _flatten(cnfs): |
| 86 | try: |
| 87 | cnf.update(c) |
| 88 | except (AttributeError, TypeError), msg: |
| 89 | print "_cnfmerge: fallback due to:", msg |
| 90 | for k, v in c.items(): |
| 91 | cnf[k] = v |
| 92 | return cnf |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 93 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 94 | try: _cnfmerge = _tkinter._cnfmerge |
| 95 | except AttributeError: pass |
| 96 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 97 | class Event: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 98 | """Container for the properties of an event. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 99 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 100 | Instances of this type are generated if one of the following events occurs: |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 101 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 102 | KeyPress, KeyRelease - for keyboard events |
| 103 | ButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse events |
| 104 | Visibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate, |
| 105 | Colormap, Gravity, Reparent, Property, Destroy, Activate, |
| 106 | Deactivate - for window events. |
| 107 | |
| 108 | If a callback function for one of these events is registered |
| 109 | using bind, bind_all, bind_class, or tag_bind, the callback is |
| 110 | called with an Event as first argument. It will have the |
| 111 | following attributes (in braces are the event types for which |
| 112 | the attribute is valid): |
| 113 | |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 114 | serial - serial number of event |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 115 | num - mouse button pressed (ButtonPress, ButtonRelease) |
| 116 | focus - whether the window has the focus (Enter, Leave) |
| 117 | height - height of the exposed window (Configure, Expose) |
| 118 | width - width of the exposed window (Configure, Expose) |
| 119 | keycode - keycode of the pressed key (KeyPress, KeyRelease) |
| 120 | state - state of the event as a number (ButtonPress, ButtonRelease, |
| 121 | Enter, KeyPress, KeyRelease, |
| 122 | Leave, Motion) |
| 123 | state - state as a string (Visibility) |
| 124 | time - when the event occurred |
| 125 | x - x-position of the mouse |
| 126 | y - y-position of the mouse |
| 127 | x_root - x-position of the mouse on the screen |
| 128 | (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion) |
| 129 | y_root - y-position of the mouse on the screen |
| 130 | (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion) |
| 131 | char - pressed character (KeyPress, KeyRelease) |
| 132 | send_event - see X/Windows documentation |
Walter Dörwald | 966c264 | 2005-11-09 17:12:43 +0000 | [diff] [blame] | 133 | keysym - keysym of the event as a string (KeyPress, KeyRelease) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 134 | keysym_num - keysym of the event as a number (KeyPress, KeyRelease) |
| 135 | type - type of the event as a number |
| 136 | widget - widget in which the event occurred |
| 137 | delta - delta of wheel movement (MouseWheel) |
| 138 | """ |
| 139 | pass |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 141 | _support_default_root = 1 |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 142 | _default_root = None |
| 143 | |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 144 | def NoDefaultRoot(): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 145 | """Inhibit setting of default root window. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 146 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 147 | Call this function to inhibit that the first instance of |
| 148 | Tk is used for windows without an explicit parent window. |
| 149 | """ |
| 150 | global _support_default_root |
| 151 | _support_default_root = 0 |
| 152 | global _default_root |
| 153 | _default_root = None |
| 154 | del _default_root |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 156 | def _tkerror(err): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 157 | """Internal function.""" |
| 158 | pass |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 160 | def _exit(code='0'): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 161 | """Internal function. Calling it will throw the exception SystemExit.""" |
| 162 | raise SystemExit, code |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 164 | _varnum = 0 |
| 165 | class Variable: |
Guido van Rossum | 2cd0a65 | 2003-04-16 20:10:03 +0000 | [diff] [blame] | 166 | """Class to define value holders for e.g. buttons. |
| 167 | |
| 168 | Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializations |
| 169 | that constrain the type of the value returned from get().""" |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 170 | _default = "" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 171 | def __init__(self, master=None, value=None, name=None): |
| 172 | """Construct a variable |
| 173 | |
| 174 | MASTER can be given as master widget. |
| 175 | VALUE is an optional value (defaults to "") |
| 176 | NAME is an optional Tcl name (defaults to PY_VARnum). |
| 177 | |
| 178 | If NAME matches an existing variable and VALUE is omitted |
| 179 | then the existing value is retained. |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 180 | """ |
| 181 | global _varnum |
| 182 | if not master: |
| 183 | master = _default_root |
| 184 | self._master = master |
| 185 | self._tk = master.tk |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 186 | if name: |
| 187 | self._name = name |
| 188 | else: |
| 189 | self._name = 'PY_VAR' + repr(_varnum) |
| 190 | _varnum += 1 |
| 191 | if value != None: |
| 192 | self.set(value) |
| 193 | elif not self._tk.call("info", "exists", self._name): |
| 194 | self.set(self._default) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 195 | def __del__(self): |
| 196 | """Unset the variable in Tcl.""" |
| 197 | self._tk.globalunsetvar(self._name) |
| 198 | def __str__(self): |
| 199 | """Return the name of the variable in Tcl.""" |
| 200 | return self._name |
| 201 | def set(self, value): |
| 202 | """Set the variable to VALUE.""" |
| 203 | return self._tk.globalsetvar(self._name, value) |
Guido van Rossum | 2cd0a65 | 2003-04-16 20:10:03 +0000 | [diff] [blame] | 204 | def get(self): |
| 205 | """Return value of variable.""" |
| 206 | return self._tk.globalgetvar(self._name) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 207 | def trace_variable(self, mode, callback): |
| 208 | """Define a trace callback for the variable. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 209 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 210 | MODE is one of "r", "w", "u" for read, write, undefine. |
| 211 | CALLBACK must be a function which is called when |
| 212 | the variable is read, written or undefined. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 213 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 214 | Return the name of the callback. |
| 215 | """ |
| 216 | cbname = self._master._register(callback) |
| 217 | self._tk.call("trace", "variable", self._name, mode, cbname) |
| 218 | return cbname |
| 219 | trace = trace_variable |
| 220 | def trace_vdelete(self, mode, cbname): |
| 221 | """Delete the trace callback for a variable. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 222 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 223 | MODE is one of "r", "w", "u" for read, write, undefine. |
| 224 | CBNAME is the name of the callback returned from trace_variable or trace. |
| 225 | """ |
| 226 | self._tk.call("trace", "vdelete", self._name, mode, cbname) |
| 227 | self._master.deletecommand(cbname) |
| 228 | def trace_vinfo(self): |
| 229 | """Return all trace callback information.""" |
| 230 | return map(self._tk.split, self._tk.splitlist( |
| 231 | self._tk.call("trace", "vinfo", self._name))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 232 | def __eq__(self, other): |
| 233 | """Comparison for equality (==). |
| 234 | |
| 235 | Note: if the Variable's master matters to behavior |
| 236 | also compare self._master == other._master |
| 237 | """ |
| 238 | return self.__class__.__name__ == other.__class__.__name__ \ |
| 239 | and self._name == other._name |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 240 | |
| 241 | class StringVar(Variable): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 242 | """Value holder for strings variables.""" |
| 243 | _default = "" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 244 | def __init__(self, master=None, value=None, name=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 245 | """Construct a string variable. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 246 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 247 | MASTER can be given as master widget. |
| 248 | VALUE is an optional value (defaults to "") |
| 249 | NAME is an optional Tcl name (defaults to PY_VARnum). |
| 250 | |
| 251 | If NAME matches an existing variable and VALUE is omitted |
| 252 | then the existing value is retained. |
| 253 | """ |
| 254 | Variable.__init__(self, master, value, name) |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 255 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 256 | def get(self): |
| 257 | """Return value of variable as string.""" |
Martin v. Löwis | bfe175c | 2003-04-16 19:42:51 +0000 | [diff] [blame] | 258 | value = self._tk.globalgetvar(self._name) |
| 259 | if isinstance(value, basestring): |
| 260 | return value |
| 261 | return str(value) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 262 | |
| 263 | class IntVar(Variable): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 264 | """Value holder for integer variables.""" |
| 265 | _default = 0 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 266 | def __init__(self, master=None, value=None, name=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 267 | """Construct an integer variable. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 268 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 269 | MASTER can be given as master widget. |
| 270 | VALUE is an optional value (defaults to 0) |
| 271 | NAME is an optional Tcl name (defaults to PY_VARnum). |
| 272 | |
| 273 | If NAME matches an existing variable and VALUE is omitted |
| 274 | then the existing value is retained. |
| 275 | """ |
| 276 | Variable.__init__(self, master, value, name) |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 277 | |
Martin v. Löwis | 70c3dda | 2003-01-22 09:17:38 +0000 | [diff] [blame] | 278 | def set(self, value): |
| 279 | """Set the variable to value, converting booleans to integers.""" |
| 280 | if isinstance(value, bool): |
| 281 | value = int(value) |
| 282 | return Variable.set(self, value) |
| 283 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 284 | def get(self): |
| 285 | """Return the value of the variable as an integer.""" |
| 286 | return getint(self._tk.globalgetvar(self._name)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 287 | |
| 288 | class DoubleVar(Variable): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 289 | """Value holder for float variables.""" |
| 290 | _default = 0.0 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | def __init__(self, master=None, value=None, name=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 292 | """Construct a float variable. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 293 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | MASTER can be given as master widget. |
| 295 | VALUE is an optional value (defaults to 0.0) |
| 296 | NAME is an optional Tcl name (defaults to PY_VARnum). |
| 297 | |
| 298 | If NAME matches an existing variable and VALUE is omitted |
| 299 | then the existing value is retained. |
| 300 | """ |
| 301 | Variable.__init__(self, master, value, name) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 302 | |
| 303 | def get(self): |
| 304 | """Return the value of the variable as a float.""" |
| 305 | return getdouble(self._tk.globalgetvar(self._name)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 306 | |
| 307 | class BooleanVar(Variable): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 308 | """Value holder for boolean variables.""" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 309 | _default = False |
| 310 | def __init__(self, master=None, value=None, name=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 311 | """Construct a boolean variable. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 312 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 313 | MASTER can be given as master widget. |
| 314 | VALUE is an optional value (defaults to False) |
| 315 | NAME is an optional Tcl name (defaults to PY_VARnum). |
| 316 | |
| 317 | If NAME matches an existing variable and VALUE is omitted |
| 318 | then the existing value is retained. |
| 319 | """ |
| 320 | Variable.__init__(self, master, value, name) |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 321 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 322 | def get(self): |
Martin v. Löwis | bfe175c | 2003-04-16 19:42:51 +0000 | [diff] [blame] | 323 | """Return the value of the variable as a bool.""" |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 324 | return self._tk.getboolean(self._tk.globalgetvar(self._name)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 326 | def mainloop(n=0): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 327 | """Run the main loop of Tcl.""" |
| 328 | _default_root.tk.mainloop(n) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 329 | |
Guido van Rossum | 0132f69 | 1998-04-30 17:50:36 +0000 | [diff] [blame] | 330 | getint = int |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 331 | |
Guido van Rossum | 0132f69 | 1998-04-30 17:50:36 +0000 | [diff] [blame] | 332 | getdouble = float |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 333 | |
| 334 | def getboolean(s): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 335 | """Convert true and false to integer values 1 and 0.""" |
| 336 | return _default_root.tk.getboolean(s) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 338 | # Methods defined on both toplevel and interior widgets |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 339 | class Misc: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 340 | """Internal class. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 341 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 342 | Base class which defines methods common for interior widgets.""" |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 343 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 344 | # XXX font command? |
| 345 | _tclCommands = None |
| 346 | def destroy(self): |
| 347 | """Internal function. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 348 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 349 | Delete all Tcl commands created for |
| 350 | this widget in the Tcl interpreter.""" |
| 351 | if self._tclCommands is not None: |
| 352 | for name in self._tclCommands: |
| 353 | #print '- Tkinter: deleted command', name |
| 354 | self.tk.deletecommand(name) |
| 355 | self._tclCommands = None |
| 356 | def deletecommand(self, name): |
| 357 | """Internal function. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 358 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 359 | Delete the Tcl command provided in NAME.""" |
| 360 | #print '- Tkinter: deleted command', name |
| 361 | self.tk.deletecommand(name) |
| 362 | try: |
| 363 | self._tclCommands.remove(name) |
| 364 | except ValueError: |
| 365 | pass |
| 366 | def tk_strictMotif(self, boolean=None): |
| 367 | """Set Tcl internal variable, whether the look and feel |
| 368 | should adhere to Motif. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 369 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 370 | A parameter of 1 means adhere to Motif (e.g. no color |
| 371 | change if mouse passes over slider). |
| 372 | Returns the set value.""" |
| 373 | return self.tk.getboolean(self.tk.call( |
| 374 | 'set', 'tk_strictMotif', boolean)) |
| 375 | def tk_bisque(self): |
| 376 | """Change the color scheme to light brown as used in Tk 3.6 and before.""" |
| 377 | self.tk.call('tk_bisque') |
| 378 | def tk_setPalette(self, *args, **kw): |
| 379 | """Set a new color scheme for all widget elements. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 380 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 381 | A single color as argument will cause that all colors of Tk |
| 382 | widget elements are derived from this. |
| 383 | Alternatively several keyword parameters and its associated |
| 384 | colors can be given. The following keywords are valid: |
| 385 | activeBackground, foreground, selectColor, |
| 386 | activeForeground, highlightBackground, selectBackground, |
| 387 | background, highlightColor, selectForeground, |
| 388 | disabledForeground, insertBackground, troughColor.""" |
| 389 | self.tk.call(('tk_setPalette',) |
| 390 | + _flatten(args) + _flatten(kw.items())) |
| 391 | def tk_menuBar(self, *args): |
| 392 | """Do not use. Needed in Tk 3.6 and earlier.""" |
| 393 | pass # obsolete since Tk 4.0 |
| 394 | def wait_variable(self, name='PY_VAR'): |
| 395 | """Wait until the variable is modified. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 396 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 397 | A parameter of type IntVar, StringVar, DoubleVar or |
| 398 | BooleanVar must be given.""" |
| 399 | self.tk.call('tkwait', 'variable', name) |
| 400 | waitvar = wait_variable # XXX b/w compat |
| 401 | def wait_window(self, window=None): |
| 402 | """Wait until a WIDGET is destroyed. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 403 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 404 | If no parameter is given self is used.""" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 405 | if window is None: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 406 | window = self |
| 407 | self.tk.call('tkwait', 'window', window._w) |
| 408 | def wait_visibility(self, window=None): |
| 409 | """Wait until the visibility of a WIDGET changes |
| 410 | (e.g. it appears). |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 411 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 412 | If no parameter is given self is used.""" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 413 | if window is None: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 414 | window = self |
| 415 | self.tk.call('tkwait', 'visibility', window._w) |
| 416 | def setvar(self, name='PY_VAR', value='1'): |
| 417 | """Set Tcl variable NAME to VALUE.""" |
| 418 | self.tk.setvar(name, value) |
| 419 | def getvar(self, name='PY_VAR'): |
| 420 | """Return value of Tcl variable NAME.""" |
| 421 | return self.tk.getvar(name) |
| 422 | getint = int |
| 423 | getdouble = float |
| 424 | def getboolean(self, s): |
Neal Norwitz | 6e5be22 | 2003-04-17 13:13:55 +0000 | [diff] [blame] | 425 | """Return a boolean value for Tcl boolean values true and false given as parameter.""" |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 426 | return self.tk.getboolean(s) |
| 427 | def focus_set(self): |
| 428 | """Direct input focus to this widget. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 429 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 430 | If the application currently does not have the focus |
| 431 | this widget will get the focus if the application gets |
| 432 | the focus through the window manager.""" |
| 433 | self.tk.call('focus', self._w) |
| 434 | focus = focus_set # XXX b/w compat? |
| 435 | def focus_force(self): |
| 436 | """Direct input focus to this widget even if the |
| 437 | application does not have the focus. Use with |
| 438 | caution!""" |
| 439 | self.tk.call('focus', '-force', self._w) |
| 440 | def focus_get(self): |
| 441 | """Return the widget which has currently the focus in the |
| 442 | application. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 443 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 444 | Use focus_displayof to allow working with several |
| 445 | displays. Return None if application does not have |
| 446 | the focus.""" |
| 447 | name = self.tk.call('focus') |
| 448 | if name == 'none' or not name: return None |
| 449 | return self._nametowidget(name) |
| 450 | def focus_displayof(self): |
| 451 | """Return the widget which has currently the focus on the |
| 452 | display where this widget is located. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 453 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 454 | Return None if the application does not have the focus.""" |
| 455 | name = self.tk.call('focus', '-displayof', self._w) |
| 456 | if name == 'none' or not name: return None |
| 457 | return self._nametowidget(name) |
| 458 | def focus_lastfor(self): |
| 459 | """Return the widget which would have the focus if top level |
| 460 | for this widget gets the focus from the window manager.""" |
| 461 | name = self.tk.call('focus', '-lastfor', self._w) |
| 462 | if name == 'none' or not name: return None |
| 463 | return self._nametowidget(name) |
| 464 | def tk_focusFollowsMouse(self): |
| 465 | """The widget under mouse will get automatically focus. Can not |
| 466 | be disabled easily.""" |
| 467 | self.tk.call('tk_focusFollowsMouse') |
| 468 | def tk_focusNext(self): |
| 469 | """Return the next widget in the focus order which follows |
| 470 | widget which has currently the focus. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 471 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 472 | The focus order first goes to the next child, then to |
| 473 | the children of the child recursively and then to the |
| 474 | next sibling which is higher in the stacking order. A |
| 475 | widget is omitted if it has the takefocus resource set |
| 476 | to 0.""" |
| 477 | name = self.tk.call('tk_focusNext', self._w) |
| 478 | if not name: return None |
| 479 | return self._nametowidget(name) |
| 480 | def tk_focusPrev(self): |
| 481 | """Return previous widget in the focus order. See tk_focusNext for details.""" |
| 482 | name = self.tk.call('tk_focusPrev', self._w) |
| 483 | if not name: return None |
| 484 | return self._nametowidget(name) |
| 485 | def after(self, ms, func=None, *args): |
| 486 | """Call function once after given time. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 487 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 488 | MS specifies the time in milliseconds. FUNC gives the |
| 489 | function which shall be called. Additional parameters |
| 490 | are given as parameters to the function call. Return |
| 491 | identifier to cancel scheduling with after_cancel.""" |
| 492 | if not func: |
| 493 | # I'd rather use time.sleep(ms*0.001) |
| 494 | self.tk.call('after', ms) |
| 495 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 496 | def callit(): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 497 | try: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 498 | func(*args) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 499 | finally: |
| 500 | try: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 501 | self.deletecommand(name) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 502 | except TclError: |
| 503 | pass |
| 504 | name = self._register(callit) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 505 | return self.tk.call('after', ms, name) |
| 506 | def after_idle(self, func, *args): |
| 507 | """Call FUNC once if the Tcl main loop has no event to |
| 508 | process. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 509 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 510 | Return an identifier to cancel the scheduling with |
| 511 | after_cancel.""" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 512 | return self.after('idle', func, *args) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 513 | def after_cancel(self, id): |
| 514 | """Cancel scheduling of function identified with ID. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 515 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 516 | Identifier returned by after or after_idle must be |
| 517 | given as first parameter.""" |
Martin v. Löwis | 0f9e525 | 2003-06-07 19:52:38 +0000 | [diff] [blame] | 518 | try: |
Neal Norwitz | 3c0f2c9 | 2003-07-01 21:12:47 +0000 | [diff] [blame] | 519 | data = self.tk.call('after', 'info', id) |
| 520 | # In Tk 8.3, splitlist returns: (script, type) |
| 521 | # In Tk 8.4, splitlist may return (script, type) or (script,) |
| 522 | script = self.tk.splitlist(data)[0] |
Martin v. Löwis | 0f9e525 | 2003-06-07 19:52:38 +0000 | [diff] [blame] | 523 | self.deletecommand(script) |
| 524 | except TclError: |
| 525 | pass |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 526 | self.tk.call('after', 'cancel', id) |
| 527 | def bell(self, displayof=0): |
| 528 | """Ring a display's bell.""" |
| 529 | self.tk.call(('bell',) + self._displayof(displayof)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 530 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 531 | # Clipboard handling: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 532 | def clipboard_get(self, **kw): |
| 533 | """Retrieve data from the clipboard on window's display. |
| 534 | |
| 535 | The window keyword defaults to the root window of the Tkinter |
| 536 | application. |
| 537 | |
| 538 | The type keyword specifies the form in which the data is |
| 539 | to be returned and should be an atom name such as STRING |
| 540 | or FILE_NAME. Type defaults to STRING. |
| 541 | |
| 542 | This command is equivalent to: |
| 543 | |
| 544 | selection_get(CLIPBOARD) |
| 545 | """ |
| 546 | return self.tk.call(('clipboard', 'get') + self._options(kw)) |
| 547 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 548 | def clipboard_clear(self, **kw): |
| 549 | """Clear the data in the Tk clipboard. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 550 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 551 | A widget specified for the optional displayof keyword |
| 552 | argument specifies the target display.""" |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 553 | if 'displayof' not in kw: kw['displayof'] = self._w |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 554 | self.tk.call(('clipboard', 'clear') + self._options(kw)) |
| 555 | def clipboard_append(self, string, **kw): |
| 556 | """Append STRING to the Tk clipboard. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 557 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 558 | A widget specified at the optional displayof keyword |
| 559 | argument specifies the target display. The clipboard |
| 560 | can be retrieved with selection_get.""" |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 561 | if 'displayof' not in kw: kw['displayof'] = self._w |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 562 | self.tk.call(('clipboard', 'append') + self._options(kw) |
| 563 | + ('--', string)) |
| 564 | # XXX grab current w/o window argument |
| 565 | def grab_current(self): |
| 566 | """Return widget which has currently the grab in this application |
| 567 | or None.""" |
| 568 | name = self.tk.call('grab', 'current', self._w) |
| 569 | if not name: return None |
| 570 | return self._nametowidget(name) |
| 571 | def grab_release(self): |
| 572 | """Release grab for this widget if currently set.""" |
| 573 | self.tk.call('grab', 'release', self._w) |
| 574 | def grab_set(self): |
| 575 | """Set grab for this widget. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 576 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 577 | A grab directs all events to this and descendant |
| 578 | widgets in the application.""" |
| 579 | self.tk.call('grab', 'set', self._w) |
| 580 | def grab_set_global(self): |
| 581 | """Set global grab for this widget. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 582 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 583 | A global grab directs all events to this and |
| 584 | descendant widgets on the display. Use with caution - |
| 585 | other applications do not get events anymore.""" |
| 586 | self.tk.call('grab', 'set', '-global', self._w) |
| 587 | def grab_status(self): |
| 588 | """Return None, "local" or "global" if this widget has |
| 589 | no, a local or a global grab.""" |
| 590 | status = self.tk.call('grab', 'status', self._w) |
| 591 | if status == 'none': status = None |
| 592 | return status |
| 593 | def lower(self, belowThis=None): |
| 594 | """Lower this widget in the stacking order.""" |
| 595 | self.tk.call('lower', self._w, belowThis) |
| 596 | def option_add(self, pattern, value, priority = None): |
| 597 | """Set a VALUE (second parameter) for an option |
| 598 | PATTERN (first parameter). |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 599 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 600 | An optional third parameter gives the numeric priority |
| 601 | (defaults to 80).""" |
| 602 | self.tk.call('option', 'add', pattern, value, priority) |
| 603 | def option_clear(self): |
| 604 | """Clear the option database. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 605 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 606 | It will be reloaded if option_add is called.""" |
| 607 | self.tk.call('option', 'clear') |
| 608 | def option_get(self, name, className): |
| 609 | """Return the value for an option NAME for this widget |
| 610 | with CLASSNAME. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 611 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 612 | Values with higher priority override lower values.""" |
| 613 | return self.tk.call('option', 'get', self._w, name, className) |
| 614 | def option_readfile(self, fileName, priority = None): |
| 615 | """Read file FILENAME into the option database. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 616 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 617 | An optional second parameter gives the numeric |
| 618 | priority.""" |
| 619 | self.tk.call('option', 'readfile', fileName, priority) |
| 620 | def selection_clear(self, **kw): |
| 621 | """Clear the current X selection.""" |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 622 | if 'displayof' not in kw: kw['displayof'] = self._w |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 623 | self.tk.call(('selection', 'clear') + self._options(kw)) |
| 624 | def selection_get(self, **kw): |
| 625 | """Return the contents of the current X selection. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 626 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 627 | A keyword parameter selection specifies the name of |
| 628 | the selection and defaults to PRIMARY. A keyword |
| 629 | parameter displayof specifies a widget on the display |
| 630 | to use.""" |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 631 | if 'displayof' not in kw: kw['displayof'] = self._w |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 632 | return self.tk.call(('selection', 'get') + self._options(kw)) |
| 633 | def selection_handle(self, command, **kw): |
| 634 | """Specify a function COMMAND to call if the X |
| 635 | selection owned by this widget is queried by another |
| 636 | application. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 637 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 638 | This function must return the contents of the |
| 639 | selection. The function will be called with the |
| 640 | arguments OFFSET and LENGTH which allows the chunking |
| 641 | of very long selections. The following keyword |
| 642 | parameters can be provided: |
| 643 | selection - name of the selection (default PRIMARY), |
| 644 | type - type of the selection (e.g. STRING, FILE_NAME).""" |
| 645 | name = self._register(command) |
| 646 | self.tk.call(('selection', 'handle') + self._options(kw) |
| 647 | + (self._w, name)) |
| 648 | def selection_own(self, **kw): |
| 649 | """Become owner of X selection. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 650 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 651 | A keyword parameter selection specifies the name of |
| 652 | the selection (default PRIMARY).""" |
| 653 | self.tk.call(('selection', 'own') + |
| 654 | self._options(kw) + (self._w,)) |
| 655 | def selection_own_get(self, **kw): |
| 656 | """Return owner of X selection. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 657 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 658 | The following keyword parameter can |
| 659 | be provided: |
| 660 | selection - name of the selection (default PRIMARY), |
| 661 | type - type of the selection (e.g. STRING, FILE_NAME).""" |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 662 | if 'displayof' not in kw: kw['displayof'] = self._w |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 663 | name = self.tk.call(('selection', 'own') + self._options(kw)) |
| 664 | if not name: return None |
| 665 | return self._nametowidget(name) |
| 666 | def send(self, interp, cmd, *args): |
| 667 | """Send Tcl command CMD to different interpreter INTERP to be executed.""" |
| 668 | return self.tk.call(('send', interp, cmd) + args) |
| 669 | def lower(self, belowThis=None): |
| 670 | """Lower this widget in the stacking order.""" |
| 671 | self.tk.call('lower', self._w, belowThis) |
| 672 | def tkraise(self, aboveThis=None): |
| 673 | """Raise this widget in the stacking order.""" |
| 674 | self.tk.call('raise', self._w, aboveThis) |
| 675 | lift = tkraise |
| 676 | def colormodel(self, value=None): |
| 677 | """Useless. Not implemented in Tk.""" |
| 678 | return self.tk.call('tk', 'colormodel', self._w, value) |
| 679 | def winfo_atom(self, name, displayof=0): |
| 680 | """Return integer which represents atom NAME.""" |
| 681 | args = ('winfo', 'atom') + self._displayof(displayof) + (name,) |
| 682 | return getint(self.tk.call(args)) |
| 683 | def winfo_atomname(self, id, displayof=0): |
| 684 | """Return name of atom with identifier ID.""" |
| 685 | args = ('winfo', 'atomname') \ |
| 686 | + self._displayof(displayof) + (id,) |
| 687 | return self.tk.call(args) |
| 688 | def winfo_cells(self): |
| 689 | """Return number of cells in the colormap for this widget.""" |
| 690 | return getint( |
| 691 | self.tk.call('winfo', 'cells', self._w)) |
| 692 | def winfo_children(self): |
| 693 | """Return a list of all widgets which are children of this widget.""" |
Martin v. Löwis | f2041b8 | 2002-03-27 17:15:57 +0000 | [diff] [blame] | 694 | result = [] |
| 695 | for child in self.tk.splitlist( |
| 696 | self.tk.call('winfo', 'children', self._w)): |
| 697 | try: |
| 698 | # Tcl sometimes returns extra windows, e.g. for |
| 699 | # menus; those need to be skipped |
| 700 | result.append(self._nametowidget(child)) |
| 701 | except KeyError: |
| 702 | pass |
| 703 | return result |
| 704 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 705 | def winfo_class(self): |
| 706 | """Return window class name of this widget.""" |
| 707 | return self.tk.call('winfo', 'class', self._w) |
| 708 | def winfo_colormapfull(self): |
| 709 | """Return true if at the last color request the colormap was full.""" |
| 710 | return self.tk.getboolean( |
| 711 | self.tk.call('winfo', 'colormapfull', self._w)) |
| 712 | def winfo_containing(self, rootX, rootY, displayof=0): |
| 713 | """Return the widget which is at the root coordinates ROOTX, ROOTY.""" |
| 714 | args = ('winfo', 'containing') \ |
| 715 | + self._displayof(displayof) + (rootX, rootY) |
| 716 | name = self.tk.call(args) |
| 717 | if not name: return None |
| 718 | return self._nametowidget(name) |
| 719 | def winfo_depth(self): |
| 720 | """Return the number of bits per pixel.""" |
| 721 | return getint(self.tk.call('winfo', 'depth', self._w)) |
| 722 | def winfo_exists(self): |
| 723 | """Return true if this widget exists.""" |
| 724 | return getint( |
| 725 | self.tk.call('winfo', 'exists', self._w)) |
| 726 | def winfo_fpixels(self, number): |
| 727 | """Return the number of pixels for the given distance NUMBER |
| 728 | (e.g. "3c") as float.""" |
| 729 | return getdouble(self.tk.call( |
| 730 | 'winfo', 'fpixels', self._w, number)) |
| 731 | def winfo_geometry(self): |
| 732 | """Return geometry string for this widget in the form "widthxheight+X+Y".""" |
| 733 | return self.tk.call('winfo', 'geometry', self._w) |
| 734 | def winfo_height(self): |
| 735 | """Return height of this widget.""" |
| 736 | return getint( |
| 737 | self.tk.call('winfo', 'height', self._w)) |
| 738 | def winfo_id(self): |
| 739 | """Return identifier ID for this widget.""" |
| 740 | return self.tk.getint( |
| 741 | self.tk.call('winfo', 'id', self._w)) |
| 742 | def winfo_interps(self, displayof=0): |
| 743 | """Return the name of all Tcl interpreters for this display.""" |
| 744 | args = ('winfo', 'interps') + self._displayof(displayof) |
| 745 | return self.tk.splitlist(self.tk.call(args)) |
| 746 | def winfo_ismapped(self): |
| 747 | """Return true if this widget is mapped.""" |
| 748 | return getint( |
| 749 | self.tk.call('winfo', 'ismapped', self._w)) |
| 750 | def winfo_manager(self): |
| 751 | """Return the window mananger name for this widget.""" |
| 752 | return self.tk.call('winfo', 'manager', self._w) |
| 753 | def winfo_name(self): |
| 754 | """Return the name of this widget.""" |
| 755 | return self.tk.call('winfo', 'name', self._w) |
| 756 | def winfo_parent(self): |
| 757 | """Return the name of the parent of this widget.""" |
| 758 | return self.tk.call('winfo', 'parent', self._w) |
| 759 | def winfo_pathname(self, id, displayof=0): |
| 760 | """Return the pathname of the widget given by ID.""" |
| 761 | args = ('winfo', 'pathname') \ |
| 762 | + self._displayof(displayof) + (id,) |
| 763 | return self.tk.call(args) |
| 764 | def winfo_pixels(self, number): |
| 765 | """Rounded integer value of winfo_fpixels.""" |
| 766 | return getint( |
| 767 | self.tk.call('winfo', 'pixels', self._w, number)) |
| 768 | def winfo_pointerx(self): |
| 769 | """Return the x coordinate of the pointer on the root window.""" |
| 770 | return getint( |
| 771 | self.tk.call('winfo', 'pointerx', self._w)) |
| 772 | def winfo_pointerxy(self): |
| 773 | """Return a tuple of x and y coordinates of the pointer on the root window.""" |
| 774 | return self._getints( |
| 775 | self.tk.call('winfo', 'pointerxy', self._w)) |
| 776 | def winfo_pointery(self): |
| 777 | """Return the y coordinate of the pointer on the root window.""" |
| 778 | return getint( |
| 779 | self.tk.call('winfo', 'pointery', self._w)) |
| 780 | def winfo_reqheight(self): |
| 781 | """Return requested height of this widget.""" |
| 782 | return getint( |
| 783 | self.tk.call('winfo', 'reqheight', self._w)) |
| 784 | def winfo_reqwidth(self): |
| 785 | """Return requested width of this widget.""" |
| 786 | return getint( |
| 787 | self.tk.call('winfo', 'reqwidth', self._w)) |
| 788 | def winfo_rgb(self, color): |
| 789 | """Return tuple of decimal values for red, green, blue for |
| 790 | COLOR in this widget.""" |
| 791 | return self._getints( |
| 792 | self.tk.call('winfo', 'rgb', self._w, color)) |
| 793 | def winfo_rootx(self): |
| 794 | """Return x coordinate of upper left corner of this widget on the |
| 795 | root window.""" |
| 796 | return getint( |
| 797 | self.tk.call('winfo', 'rootx', self._w)) |
| 798 | def winfo_rooty(self): |
| 799 | """Return y coordinate of upper left corner of this widget on the |
| 800 | root window.""" |
| 801 | return getint( |
| 802 | self.tk.call('winfo', 'rooty', self._w)) |
| 803 | def winfo_screen(self): |
| 804 | """Return the screen name of this widget.""" |
| 805 | return self.tk.call('winfo', 'screen', self._w) |
| 806 | def winfo_screencells(self): |
| 807 | """Return the number of the cells in the colormap of the screen |
| 808 | of this widget.""" |
| 809 | return getint( |
| 810 | self.tk.call('winfo', 'screencells', self._w)) |
| 811 | def winfo_screendepth(self): |
| 812 | """Return the number of bits per pixel of the root window of the |
| 813 | screen of this widget.""" |
| 814 | return getint( |
| 815 | self.tk.call('winfo', 'screendepth', self._w)) |
| 816 | def winfo_screenheight(self): |
| 817 | """Return the number of pixels of the height of the screen of this widget |
| 818 | in pixel.""" |
| 819 | return getint( |
| 820 | self.tk.call('winfo', 'screenheight', self._w)) |
| 821 | def winfo_screenmmheight(self): |
| 822 | """Return the number of pixels of the height of the screen of |
| 823 | this widget in mm.""" |
| 824 | return getint( |
| 825 | self.tk.call('winfo', 'screenmmheight', self._w)) |
| 826 | def winfo_screenmmwidth(self): |
| 827 | """Return the number of pixels of the width of the screen of |
| 828 | this widget in mm.""" |
| 829 | return getint( |
| 830 | self.tk.call('winfo', 'screenmmwidth', self._w)) |
| 831 | def winfo_screenvisual(self): |
| 832 | """Return one of the strings directcolor, grayscale, pseudocolor, |
| 833 | staticcolor, staticgray, or truecolor for the default |
| 834 | colormodel of this screen.""" |
| 835 | return self.tk.call('winfo', 'screenvisual', self._w) |
| 836 | def winfo_screenwidth(self): |
| 837 | """Return the number of pixels of the width of the screen of |
| 838 | this widget in pixel.""" |
| 839 | return getint( |
| 840 | self.tk.call('winfo', 'screenwidth', self._w)) |
| 841 | def winfo_server(self): |
| 842 | """Return information of the X-Server of the screen of this widget in |
| 843 | the form "XmajorRminor vendor vendorVersion".""" |
| 844 | return self.tk.call('winfo', 'server', self._w) |
| 845 | def winfo_toplevel(self): |
| 846 | """Return the toplevel widget of this widget.""" |
| 847 | return self._nametowidget(self.tk.call( |
| 848 | 'winfo', 'toplevel', self._w)) |
| 849 | def winfo_viewable(self): |
| 850 | """Return true if the widget and all its higher ancestors are mapped.""" |
| 851 | return getint( |
| 852 | self.tk.call('winfo', 'viewable', self._w)) |
| 853 | def winfo_visual(self): |
| 854 | """Return one of the strings directcolor, grayscale, pseudocolor, |
| 855 | staticcolor, staticgray, or truecolor for the |
| 856 | colormodel of this widget.""" |
| 857 | return self.tk.call('winfo', 'visual', self._w) |
| 858 | def winfo_visualid(self): |
| 859 | """Return the X identifier for the visual for this widget.""" |
| 860 | return self.tk.call('winfo', 'visualid', self._w) |
| 861 | def winfo_visualsavailable(self, includeids=0): |
| 862 | """Return a list of all visuals available for the screen |
| 863 | of this widget. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 864 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 865 | Each item in the list consists of a visual name (see winfo_visual), a |
| 866 | depth and if INCLUDEIDS=1 is given also the X identifier.""" |
| 867 | data = self.tk.split( |
| 868 | self.tk.call('winfo', 'visualsavailable', self._w, |
| 869 | includeids and 'includeids' or None)) |
Fredrik Lundh | 24037f7 | 2000-08-09 19:26:47 +0000 | [diff] [blame] | 870 | if type(data) is StringType: |
| 871 | data = [self.tk.split(data)] |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 872 | return map(self.__winfo_parseitem, data) |
| 873 | def __winfo_parseitem(self, t): |
| 874 | """Internal function.""" |
| 875 | return t[:1] + tuple(map(self.__winfo_getint, t[1:])) |
| 876 | def __winfo_getint(self, x): |
| 877 | """Internal function.""" |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 878 | return int(x, 0) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 879 | def winfo_vrootheight(self): |
| 880 | """Return the height of the virtual root window associated with this |
| 881 | widget in pixels. If there is no virtual root window return the |
| 882 | height of the screen.""" |
| 883 | return getint( |
| 884 | self.tk.call('winfo', 'vrootheight', self._w)) |
| 885 | def winfo_vrootwidth(self): |
| 886 | """Return the width of the virtual root window associated with this |
| 887 | widget in pixel. If there is no virtual root window return the |
| 888 | width of the screen.""" |
| 889 | return getint( |
| 890 | self.tk.call('winfo', 'vrootwidth', self._w)) |
| 891 | def winfo_vrootx(self): |
| 892 | """Return the x offset of the virtual root relative to the root |
| 893 | window of the screen of this widget.""" |
| 894 | return getint( |
| 895 | self.tk.call('winfo', 'vrootx', self._w)) |
| 896 | def winfo_vrooty(self): |
| 897 | """Return the y offset of the virtual root relative to the root |
| 898 | window of the screen of this widget.""" |
| 899 | return getint( |
| 900 | self.tk.call('winfo', 'vrooty', self._w)) |
| 901 | def winfo_width(self): |
| 902 | """Return the width of this widget.""" |
| 903 | return getint( |
| 904 | self.tk.call('winfo', 'width', self._w)) |
| 905 | def winfo_x(self): |
| 906 | """Return the x coordinate of the upper left corner of this widget |
| 907 | in the parent.""" |
| 908 | return getint( |
| 909 | self.tk.call('winfo', 'x', self._w)) |
| 910 | def winfo_y(self): |
| 911 | """Return the y coordinate of the upper left corner of this widget |
| 912 | in the parent.""" |
| 913 | return getint( |
| 914 | self.tk.call('winfo', 'y', self._w)) |
| 915 | def update(self): |
| 916 | """Enter event loop until all pending events have been processed by Tcl.""" |
| 917 | self.tk.call('update') |
| 918 | def update_idletasks(self): |
| 919 | """Enter event loop until all idle callbacks have been called. This |
| 920 | will update the display of windows but not process events caused by |
| 921 | the user.""" |
| 922 | self.tk.call('update', 'idletasks') |
| 923 | def bindtags(self, tagList=None): |
| 924 | """Set or get the list of bindtags for this widget. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 925 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 926 | With no argument return the list of all bindtags associated with |
| 927 | this widget. With a list of strings as argument the bindtags are |
| 928 | set to this list. The bindtags determine in which order events are |
| 929 | processed (see bind).""" |
| 930 | if tagList is None: |
| 931 | return self.tk.splitlist( |
| 932 | self.tk.call('bindtags', self._w)) |
| 933 | else: |
| 934 | self.tk.call('bindtags', self._w, tagList) |
| 935 | def _bind(self, what, sequence, func, add, needcleanup=1): |
| 936 | """Internal function.""" |
| 937 | if type(func) is StringType: |
| 938 | self.tk.call(what + (sequence, func)) |
| 939 | elif func: |
| 940 | funcid = self._register(func, self._substitute, |
| 941 | needcleanup) |
| 942 | cmd = ('%sif {"[%s %s]" == "break"} break\n' |
| 943 | % |
| 944 | (add and '+' or '', |
Martin v. Löwis | c8718c1 | 2001-08-09 16:57:33 +0000 | [diff] [blame] | 945 | funcid, self._subst_format_str)) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 946 | self.tk.call(what + (sequence, cmd)) |
| 947 | return funcid |
| 948 | elif sequence: |
| 949 | return self.tk.call(what + (sequence,)) |
| 950 | else: |
| 951 | return self.tk.splitlist(self.tk.call(what)) |
| 952 | def bind(self, sequence=None, func=None, add=None): |
| 953 | """Bind to this widget at event SEQUENCE a call to function FUNC. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 954 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 955 | SEQUENCE is a string of concatenated event |
| 956 | patterns. An event pattern is of the form |
| 957 | <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one |
| 958 | of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4, |
| 959 | Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3, |
| 960 | B3, Alt, Button4, B4, Double, Button5, B5 Triple, |
| 961 | Mod1, M1. TYPE is one of Activate, Enter, Map, |
| 962 | ButtonPress, Button, Expose, Motion, ButtonRelease |
| 963 | FocusIn, MouseWheel, Circulate, FocusOut, Property, |
| 964 | Colormap, Gravity Reparent, Configure, KeyPress, Key, |
| 965 | Unmap, Deactivate, KeyRelease Visibility, Destroy, |
| 966 | Leave and DETAIL is the button number for ButtonPress, |
| 967 | ButtonRelease and DETAIL is the Keysym for KeyPress and |
| 968 | KeyRelease. Examples are |
| 969 | <Control-Button-1> for pressing Control and mouse button 1 or |
| 970 | <Alt-A> for pressing A and the Alt key (KeyPress can be omitted). |
| 971 | An event pattern can also be a virtual event of the form |
| 972 | <<AString>> where AString can be arbitrary. This |
| 973 | event can be generated by event_generate. |
| 974 | If events are concatenated they must appear shortly |
| 975 | after each other. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 976 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 977 | FUNC will be called if the event sequence occurs with an |
| 978 | instance of Event as argument. If the return value of FUNC is |
| 979 | "break" no further bound function is invoked. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 980 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 981 | An additional boolean parameter ADD specifies whether FUNC will |
| 982 | be called additionally to the other bound function or whether |
| 983 | it will replace the previous function. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 984 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 985 | Bind will return an identifier to allow deletion of the bound function with |
| 986 | unbind without memory leak. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 987 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 988 | If FUNC or SEQUENCE is omitted the bound function or list |
| 989 | of bound events are returned.""" |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 990 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 991 | return self._bind(('bind', self._w), sequence, func, add) |
| 992 | def unbind(self, sequence, funcid=None): |
| 993 | """Unbind for this widget for event SEQUENCE the |
| 994 | function identified with FUNCID.""" |
| 995 | self.tk.call('bind', self._w, sequence, '') |
| 996 | if funcid: |
| 997 | self.deletecommand(funcid) |
| 998 | def bind_all(self, sequence=None, func=None, add=None): |
| 999 | """Bind to all widgets at an event SEQUENCE a call to function FUNC. |
| 1000 | An additional boolean parameter ADD specifies whether FUNC will |
| 1001 | be called additionally to the other bound function or whether |
| 1002 | it will replace the previous function. See bind for the return value.""" |
| 1003 | return self._bind(('bind', 'all'), sequence, func, add, 0) |
| 1004 | def unbind_all(self, sequence): |
| 1005 | """Unbind for all widgets for event SEQUENCE all functions.""" |
| 1006 | self.tk.call('bind', 'all' , sequence, '') |
| 1007 | def bind_class(self, className, sequence=None, func=None, add=None): |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1008 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1009 | """Bind to widgets with bindtag CLASSNAME at event |
| 1010 | SEQUENCE a call of function FUNC. An additional |
| 1011 | boolean parameter ADD specifies whether FUNC will be |
| 1012 | called additionally to the other bound function or |
| 1013 | whether it will replace the previous function. See bind for |
| 1014 | the return value.""" |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1015 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1016 | return self._bind(('bind', className), sequence, func, add, 0) |
| 1017 | def unbind_class(self, className, sequence): |
| 1018 | """Unbind for a all widgets with bindtag CLASSNAME for event SEQUENCE |
| 1019 | all functions.""" |
| 1020 | self.tk.call('bind', className , sequence, '') |
| 1021 | def mainloop(self, n=0): |
| 1022 | """Call the mainloop of Tk.""" |
| 1023 | self.tk.mainloop(n) |
| 1024 | def quit(self): |
| 1025 | """Quit the Tcl interpreter. All widgets will be destroyed.""" |
| 1026 | self.tk.quit() |
| 1027 | def _getints(self, string): |
| 1028 | """Internal function.""" |
| 1029 | if string: |
| 1030 | return tuple(map(getint, self.tk.splitlist(string))) |
| 1031 | def _getdoubles(self, string): |
| 1032 | """Internal function.""" |
| 1033 | if string: |
| 1034 | return tuple(map(getdouble, self.tk.splitlist(string))) |
| 1035 | def _getboolean(self, string): |
| 1036 | """Internal function.""" |
| 1037 | if string: |
| 1038 | return self.tk.getboolean(string) |
| 1039 | def _displayof(self, displayof): |
| 1040 | """Internal function.""" |
| 1041 | if displayof: |
| 1042 | return ('-displayof', displayof) |
| 1043 | if displayof is None: |
| 1044 | return ('-displayof', self._w) |
| 1045 | return () |
| 1046 | def _options(self, cnf, kw = None): |
| 1047 | """Internal function.""" |
| 1048 | if kw: |
| 1049 | cnf = _cnfmerge((cnf, kw)) |
| 1050 | else: |
| 1051 | cnf = _cnfmerge(cnf) |
| 1052 | res = () |
| 1053 | for k, v in cnf.items(): |
| 1054 | if v is not None: |
| 1055 | if k[-1] == '_': k = k[:-1] |
| 1056 | if callable(v): |
| 1057 | v = self._register(v) |
| 1058 | res = res + ('-'+k, v) |
| 1059 | return res |
| 1060 | def nametowidget(self, name): |
| 1061 | """Return the Tkinter instance of a widget identified by |
| 1062 | its Tcl name NAME.""" |
| 1063 | w = self |
| 1064 | if name[0] == '.': |
| 1065 | w = w._root() |
| 1066 | name = name[1:] |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1067 | while name: |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 1068 | i = name.find('.') |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1069 | if i >= 0: |
| 1070 | name, tail = name[:i], name[i+1:] |
| 1071 | else: |
| 1072 | tail = '' |
| 1073 | w = w.children[name] |
| 1074 | name = tail |
| 1075 | return w |
| 1076 | _nametowidget = nametowidget |
| 1077 | def _register(self, func, subst=None, needcleanup=1): |
| 1078 | """Return a newly created Tcl function. If this |
| 1079 | function is called, the Python function FUNC will |
| 1080 | be executed. An optional function SUBST can |
| 1081 | be given which will be executed before FUNC.""" |
| 1082 | f = CallWrapper(func, subst, self).__call__ |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1083 | name = repr(id(f)) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1084 | try: |
| 1085 | func = func.im_func |
| 1086 | except AttributeError: |
| 1087 | pass |
| 1088 | try: |
| 1089 | name = name + func.__name__ |
| 1090 | except AttributeError: |
| 1091 | pass |
| 1092 | self.tk.createcommand(name, f) |
| 1093 | if needcleanup: |
| 1094 | if self._tclCommands is None: |
| 1095 | self._tclCommands = [] |
| 1096 | self._tclCommands.append(name) |
| 1097 | #print '+ Tkinter created command', name |
| 1098 | return name |
| 1099 | register = _register |
| 1100 | def _root(self): |
| 1101 | """Internal function.""" |
| 1102 | w = self |
| 1103 | while w.master: w = w.master |
| 1104 | return w |
| 1105 | _subst_format = ('%#', '%b', '%f', '%h', '%k', |
| 1106 | '%s', '%t', '%w', '%x', '%y', |
| 1107 | '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D') |
Martin v. Löwis | c8718c1 | 2001-08-09 16:57:33 +0000 | [diff] [blame] | 1108 | _subst_format_str = " ".join(_subst_format) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1109 | def _substitute(self, *args): |
| 1110 | """Internal function.""" |
| 1111 | if len(args) != len(self._subst_format): return args |
| 1112 | getboolean = self.tk.getboolean |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1113 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1114 | getint = int |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1115 | def getint_event(s): |
| 1116 | """Tk changed behavior in 8.4.2, returning "??" rather more often.""" |
| 1117 | try: |
| 1118 | return int(s) |
| 1119 | except ValueError: |
| 1120 | return s |
| 1121 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1122 | nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args |
| 1123 | # Missing: (a, c, d, m, o, v, B, R) |
| 1124 | e = Event() |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1125 | # serial field: valid vor all events |
| 1126 | # number of button: ButtonPress and ButtonRelease events only |
| 1127 | # height field: Configure, ConfigureRequest, Create, |
| 1128 | # ResizeRequest, and Expose events only |
| 1129 | # keycode field: KeyPress and KeyRelease events only |
| 1130 | # time field: "valid for events that contain a time field" |
| 1131 | # width field: Configure, ConfigureRequest, Create, ResizeRequest, |
| 1132 | # and Expose events only |
| 1133 | # x field: "valid for events that contain a x field" |
| 1134 | # y field: "valid for events that contain a y field" |
| 1135 | # keysym as decimal: KeyPress and KeyRelease events only |
| 1136 | # x_root, y_root fields: ButtonPress, ButtonRelease, KeyPress, |
| 1137 | # KeyRelease,and Motion events |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1138 | e.serial = getint(nsign) |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1139 | e.num = getint_event(b) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1140 | try: e.focus = getboolean(f) |
| 1141 | except TclError: pass |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1142 | e.height = getint_event(h) |
| 1143 | e.keycode = getint_event(k) |
| 1144 | e.state = getint_event(s) |
| 1145 | e.time = getint_event(t) |
| 1146 | e.width = getint_event(w) |
| 1147 | e.x = getint_event(x) |
| 1148 | e.y = getint_event(y) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1149 | e.char = A |
| 1150 | try: e.send_event = getboolean(E) |
| 1151 | except TclError: pass |
| 1152 | e.keysym = K |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1153 | e.keysym_num = getint_event(N) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1154 | e.type = T |
| 1155 | try: |
| 1156 | e.widget = self._nametowidget(W) |
| 1157 | except KeyError: |
| 1158 | e.widget = W |
Martin v. Löwis | 043bbc7 | 2003-03-29 09:47:21 +0000 | [diff] [blame] | 1159 | e.x_root = getint_event(X) |
| 1160 | e.y_root = getint_event(Y) |
Fredrik Lundh | a249f16 | 2000-09-07 15:05:09 +0000 | [diff] [blame] | 1161 | try: |
| 1162 | e.delta = getint(D) |
| 1163 | except ValueError: |
| 1164 | e.delta = 0 |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1165 | return (e,) |
| 1166 | def _report_exception(self): |
| 1167 | """Internal function.""" |
| 1168 | import sys |
Neal Norwitz | ac3625f | 2006-03-17 05:49:33 +0000 | [diff] [blame] | 1169 | exc, val, tb = sys.exc_info() |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1170 | root = self._root() |
| 1171 | root.report_callback_exception(exc, val, tb) |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 1172 | def _configure(self, cmd, cnf, kw): |
| 1173 | """Internal function.""" |
| 1174 | if kw: |
| 1175 | cnf = _cnfmerge((cnf, kw)) |
| 1176 | elif cnf: |
| 1177 | cnf = _cnfmerge(cnf) |
| 1178 | if cnf is None: |
| 1179 | cnf = {} |
| 1180 | for x in self.tk.split( |
| 1181 | self.tk.call(_flatten((self._w, cmd)))): |
| 1182 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 1183 | return cnf |
| 1184 | if type(cnf) is StringType: |
| 1185 | x = self.tk.split( |
| 1186 | self.tk.call(_flatten((self._w, cmd, '-'+cnf)))) |
| 1187 | return (x[0][1:],) + x[1:] |
| 1188 | self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1189 | # These used to be defined in Widget: |
| 1190 | def configure(self, cnf=None, **kw): |
| 1191 | """Configure resources of a widget. |
Barry Warsaw | 107e623 | 1998-12-15 00:44:15 +0000 | [diff] [blame] | 1192 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1193 | The values for resources are specified as keyword |
| 1194 | arguments. To get an overview about |
| 1195 | the allowed keyword arguments call the method keys. |
| 1196 | """ |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 1197 | return self._configure('configure', cnf, kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1198 | config = configure |
| 1199 | def cget(self, key): |
| 1200 | """Return the resource value for a KEY given as string.""" |
| 1201 | return self.tk.call(self._w, 'cget', '-' + key) |
| 1202 | __getitem__ = cget |
| 1203 | def __setitem__(self, key, value): |
| 1204 | self.configure({key: value}) |
| 1205 | def keys(self): |
| 1206 | """Return a list of all resource names of this widget.""" |
| 1207 | return map(lambda x: x[0][1:], |
| 1208 | self.tk.split(self.tk.call(self._w, 'configure'))) |
| 1209 | def __str__(self): |
| 1210 | """Return the window path name of this widget.""" |
| 1211 | return self._w |
| 1212 | # Pack methods that apply to the master |
| 1213 | _noarg_ = ['_noarg_'] |
| 1214 | def pack_propagate(self, flag=_noarg_): |
| 1215 | """Set or get the status for propagation of geometry information. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1216 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1217 | A boolean argument specifies whether the geometry information |
| 1218 | of the slaves will determine the size of this widget. If no argument |
| 1219 | is given the current setting will be returned. |
| 1220 | """ |
| 1221 | if flag is Misc._noarg_: |
| 1222 | return self._getboolean(self.tk.call( |
| 1223 | 'pack', 'propagate', self._w)) |
| 1224 | else: |
| 1225 | self.tk.call('pack', 'propagate', self._w, flag) |
| 1226 | propagate = pack_propagate |
| 1227 | def pack_slaves(self): |
| 1228 | """Return a list of all slaves of this widget |
| 1229 | in its packing order.""" |
| 1230 | return map(self._nametowidget, |
| 1231 | self.tk.splitlist( |
| 1232 | self.tk.call('pack', 'slaves', self._w))) |
| 1233 | slaves = pack_slaves |
| 1234 | # Place method that applies to the master |
| 1235 | def place_slaves(self): |
| 1236 | """Return a list of all slaves of this widget |
| 1237 | in its packing order.""" |
| 1238 | return map(self._nametowidget, |
| 1239 | self.tk.splitlist( |
| 1240 | self.tk.call( |
| 1241 | 'place', 'slaves', self._w))) |
| 1242 | # Grid methods that apply to the master |
| 1243 | def grid_bbox(self, column=None, row=None, col2=None, row2=None): |
| 1244 | """Return a tuple of integer coordinates for the bounding |
| 1245 | box of this widget controlled by the geometry manager grid. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1246 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1247 | If COLUMN, ROW is given the bounding box applies from |
| 1248 | the cell with row and column 0 to the specified |
| 1249 | cell. If COL2 and ROW2 are given the bounding box |
| 1250 | starts at that cell. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1251 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1252 | The returned integers specify the offset of the upper left |
| 1253 | corner in the master widget and the width and height. |
| 1254 | """ |
| 1255 | args = ('grid', 'bbox', self._w) |
| 1256 | if column is not None and row is not None: |
| 1257 | args = args + (column, row) |
| 1258 | if col2 is not None and row2 is not None: |
| 1259 | args = args + (col2, row2) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1260 | return self._getints(self.tk.call(*args)) or None |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1261 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1262 | bbox = grid_bbox |
| 1263 | def _grid_configure(self, command, index, cnf, kw): |
| 1264 | """Internal function.""" |
| 1265 | if type(cnf) is StringType and not kw: |
| 1266 | if cnf[-1:] == '_': |
| 1267 | cnf = cnf[:-1] |
| 1268 | if cnf[:1] != '-': |
| 1269 | cnf = '-'+cnf |
| 1270 | options = (cnf,) |
| 1271 | else: |
| 1272 | options = self._options(cnf, kw) |
| 1273 | if not options: |
| 1274 | res = self.tk.call('grid', |
| 1275 | command, self._w, index) |
| 1276 | words = self.tk.splitlist(res) |
| 1277 | dict = {} |
| 1278 | for i in range(0, len(words), 2): |
| 1279 | key = words[i][1:] |
| 1280 | value = words[i+1] |
| 1281 | if not value: |
| 1282 | value = None |
| 1283 | elif '.' in value: |
| 1284 | value = getdouble(value) |
| 1285 | else: |
| 1286 | value = getint(value) |
| 1287 | dict[key] = value |
| 1288 | return dict |
| 1289 | res = self.tk.call( |
| 1290 | ('grid', command, self._w, index) |
| 1291 | + options) |
| 1292 | if len(options) == 1: |
| 1293 | if not res: return None |
| 1294 | # In Tk 7.5, -width can be a float |
| 1295 | if '.' in res: return getdouble(res) |
| 1296 | return getint(res) |
| 1297 | def grid_columnconfigure(self, index, cnf={}, **kw): |
| 1298 | """Configure column INDEX of a grid. |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 1299 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1300 | Valid resources are minsize (minimum size of the column), |
| 1301 | weight (how much does additional space propagate to this column) |
| 1302 | and pad (how much space to let additionally).""" |
| 1303 | return self._grid_configure('columnconfigure', index, cnf, kw) |
| 1304 | columnconfigure = grid_columnconfigure |
Martin v. Löwis | dc57909 | 2001-10-13 09:33:51 +0000 | [diff] [blame] | 1305 | def grid_location(self, x, y): |
| 1306 | """Return a tuple of column and row which identify the cell |
| 1307 | at which the pixel at position X and Y inside the master |
| 1308 | widget is located.""" |
| 1309 | return self._getints( |
| 1310 | self.tk.call( |
| 1311 | 'grid', 'location', self._w, x, y)) or None |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1312 | def grid_propagate(self, flag=_noarg_): |
| 1313 | """Set or get the status for propagation of geometry information. |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 1314 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1315 | A boolean argument specifies whether the geometry information |
| 1316 | of the slaves will determine the size of this widget. If no argument |
| 1317 | is given, the current setting will be returned. |
| 1318 | """ |
| 1319 | if flag is Misc._noarg_: |
| 1320 | return self._getboolean(self.tk.call( |
| 1321 | 'grid', 'propagate', self._w)) |
| 1322 | else: |
| 1323 | self.tk.call('grid', 'propagate', self._w, flag) |
| 1324 | def grid_rowconfigure(self, index, cnf={}, **kw): |
| 1325 | """Configure row INDEX of a grid. |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 1326 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1327 | Valid resources are minsize (minimum size of the row), |
| 1328 | weight (how much does additional space propagate to this row) |
| 1329 | and pad (how much space to let additionally).""" |
| 1330 | return self._grid_configure('rowconfigure', index, cnf, kw) |
| 1331 | rowconfigure = grid_rowconfigure |
| 1332 | def grid_size(self): |
| 1333 | """Return a tuple of the number of column and rows in the grid.""" |
| 1334 | return self._getints( |
| 1335 | self.tk.call('grid', 'size', self._w)) or None |
| 1336 | size = grid_size |
| 1337 | def grid_slaves(self, row=None, column=None): |
| 1338 | """Return a list of all slaves of this widget |
| 1339 | in its packing order.""" |
| 1340 | args = () |
| 1341 | if row is not None: |
| 1342 | args = args + ('-row', row) |
| 1343 | if column is not None: |
| 1344 | args = args + ('-column', column) |
| 1345 | return map(self._nametowidget, |
| 1346 | self.tk.splitlist(self.tk.call( |
| 1347 | ('grid', 'slaves', self._w) + args))) |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 1348 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1349 | # Support for the "event" command, new in Tk 4.2. |
| 1350 | # By Case Roole. |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 1351 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1352 | def event_add(self, virtual, *sequences): |
| 1353 | """Bind a virtual event VIRTUAL (of the form <<Name>>) |
| 1354 | to an event SEQUENCE such that the virtual event is triggered |
| 1355 | whenever SEQUENCE occurs.""" |
| 1356 | args = ('event', 'add', virtual) + sequences |
| 1357 | self.tk.call(args) |
Guido van Rossum | c296651 | 1998-04-10 19:16:10 +0000 | [diff] [blame] | 1358 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1359 | def event_delete(self, virtual, *sequences): |
| 1360 | """Unbind a virtual event VIRTUAL from SEQUENCE.""" |
| 1361 | args = ('event', 'delete', virtual) + sequences |
| 1362 | self.tk.call(args) |
Guido van Rossum | c296651 | 1998-04-10 19:16:10 +0000 | [diff] [blame] | 1363 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1364 | def event_generate(self, sequence, **kw): |
| 1365 | """Generate an event SEQUENCE. Additional |
| 1366 | keyword arguments specify parameter of the event |
| 1367 | (e.g. x, y, rootx, rooty).""" |
| 1368 | args = ('event', 'generate', self._w, sequence) |
| 1369 | for k, v in kw.items(): |
| 1370 | args = args + ('-%s' % k, str(v)) |
| 1371 | self.tk.call(args) |
| 1372 | |
| 1373 | def event_info(self, virtual=None): |
| 1374 | """Return a list of all virtual events or the information |
| 1375 | about the SEQUENCE bound to the virtual event VIRTUAL.""" |
| 1376 | return self.tk.splitlist( |
| 1377 | self.tk.call('event', 'info', virtual)) |
| 1378 | |
| 1379 | # Image related commands |
| 1380 | |
| 1381 | def image_names(self): |
| 1382 | """Return a list of all existing image names.""" |
| 1383 | return self.tk.call('image', 'names') |
| 1384 | |
| 1385 | def image_types(self): |
| 1386 | """Return a list of all available image types (e.g. phote bitmap).""" |
| 1387 | return self.tk.call('image', 'types') |
Guido van Rossum | c296651 | 1998-04-10 19:16:10 +0000 | [diff] [blame] | 1388 | |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 1389 | |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 1390 | class CallWrapper: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1391 | """Internal class. Stores function to call when some user |
| 1392 | defined Tcl function is called e.g. after an event occurred.""" |
| 1393 | def __init__(self, func, subst, widget): |
| 1394 | """Store FUNC, SUBST and WIDGET as members.""" |
| 1395 | self.func = func |
| 1396 | self.subst = subst |
| 1397 | self.widget = widget |
| 1398 | def __call__(self, *args): |
| 1399 | """Apply first function SUBST to arguments, than FUNC.""" |
| 1400 | try: |
| 1401 | if self.subst: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1402 | args = self.subst(*args) |
| 1403 | return self.func(*args) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1404 | except SystemExit, msg: |
| 1405 | raise SystemExit, msg |
| 1406 | except: |
| 1407 | self.widget._report_exception() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1408 | |
Guido van Rossum | e365a59 | 1998-05-01 19:48:20 +0000 | [diff] [blame] | 1409 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1410 | class Wm: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1411 | """Provides functions for the communication with the window manager.""" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1412 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1413 | def wm_aspect(self, |
| 1414 | minNumer=None, minDenom=None, |
| 1415 | maxNumer=None, maxDenom=None): |
| 1416 | """Instruct the window manager to set the aspect ratio (width/height) |
| 1417 | of this widget to be between MINNUMER/MINDENOM and MAXNUMER/MAXDENOM. Return a tuple |
| 1418 | of the actual values if no argument is given.""" |
| 1419 | return self._getints( |
| 1420 | self.tk.call('wm', 'aspect', self._w, |
| 1421 | minNumer, minDenom, |
| 1422 | maxNumer, maxDenom)) |
| 1423 | aspect = wm_aspect |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1424 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1425 | def wm_attributes(self, *args): |
| 1426 | """This subcommand returns or sets platform specific attributes |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1427 | |
| 1428 | The first form returns a list of the platform specific flags and |
| 1429 | their values. The second form returns the value for the specific |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1430 | option. The third form sets one or more of the values. The values |
| 1431 | are as follows: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1432 | |
| 1433 | On Windows, -disabled gets or sets whether the window is in a |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1434 | disabled state. -toolwindow gets or sets the style of the window |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1435 | to toolwindow (as defined in the MSDN). -topmost gets or sets |
| 1436 | whether this is a topmost window (displays above all other |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1437 | windows). |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1438 | |
| 1439 | On Macintosh, XXXXX |
| 1440 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1441 | On Unix, there are currently no special attribute values. |
| 1442 | """ |
| 1443 | args = ('wm', 'attributes', self._w) + args |
| 1444 | return self.tk.call(args) |
| 1445 | attributes=wm_attributes |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1446 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1447 | def wm_client(self, name=None): |
| 1448 | """Store NAME in WM_CLIENT_MACHINE property of this widget. Return |
| 1449 | current value.""" |
| 1450 | return self.tk.call('wm', 'client', self._w, name) |
| 1451 | client = wm_client |
| 1452 | def wm_colormapwindows(self, *wlist): |
| 1453 | """Store list of window names (WLIST) into WM_COLORMAPWINDOWS property |
| 1454 | of this widget. This list contains windows whose colormaps differ from their |
| 1455 | parents. Return current list of widgets if WLIST is empty.""" |
| 1456 | if len(wlist) > 1: |
| 1457 | wlist = (wlist,) # Tk needs a list of windows here |
| 1458 | args = ('wm', 'colormapwindows', self._w) + wlist |
| 1459 | return map(self._nametowidget, self.tk.call(args)) |
| 1460 | colormapwindows = wm_colormapwindows |
| 1461 | def wm_command(self, value=None): |
| 1462 | """Store VALUE in WM_COMMAND property. It is the command |
| 1463 | which shall be used to invoke the application. Return current |
| 1464 | command if VALUE is None.""" |
| 1465 | return self.tk.call('wm', 'command', self._w, value) |
| 1466 | command = wm_command |
| 1467 | def wm_deiconify(self): |
| 1468 | """Deiconify this widget. If it was never mapped it will not be mapped. |
| 1469 | On Windows it will raise this widget and give it the focus.""" |
| 1470 | return self.tk.call('wm', 'deiconify', self._w) |
| 1471 | deiconify = wm_deiconify |
| 1472 | def wm_focusmodel(self, model=None): |
| 1473 | """Set focus model to MODEL. "active" means that this widget will claim |
| 1474 | the focus itself, "passive" means that the window manager shall give |
| 1475 | the focus. Return current focus model if MODEL is None.""" |
| 1476 | return self.tk.call('wm', 'focusmodel', self._w, model) |
| 1477 | focusmodel = wm_focusmodel |
| 1478 | def wm_frame(self): |
| 1479 | """Return identifier for decorative frame of this widget if present.""" |
| 1480 | return self.tk.call('wm', 'frame', self._w) |
| 1481 | frame = wm_frame |
| 1482 | def wm_geometry(self, newGeometry=None): |
| 1483 | """Set geometry to NEWGEOMETRY of the form =widthxheight+x+y. Return |
| 1484 | current value if None is given.""" |
| 1485 | return self.tk.call('wm', 'geometry', self._w, newGeometry) |
| 1486 | geometry = wm_geometry |
| 1487 | def wm_grid(self, |
| 1488 | baseWidth=None, baseHeight=None, |
| 1489 | widthInc=None, heightInc=None): |
| 1490 | """Instruct the window manager that this widget shall only be |
| 1491 | resized on grid boundaries. WIDTHINC and HEIGHTINC are the width and |
| 1492 | height of a grid unit in pixels. BASEWIDTH and BASEHEIGHT are the |
| 1493 | number of grid units requested in Tk_GeometryRequest.""" |
| 1494 | return self._getints(self.tk.call( |
| 1495 | 'wm', 'grid', self._w, |
| 1496 | baseWidth, baseHeight, widthInc, heightInc)) |
| 1497 | grid = wm_grid |
| 1498 | def wm_group(self, pathName=None): |
| 1499 | """Set the group leader widgets for related widgets to PATHNAME. Return |
| 1500 | the group leader of this widget if None is given.""" |
| 1501 | return self.tk.call('wm', 'group', self._w, pathName) |
| 1502 | group = wm_group |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1503 | def wm_iconbitmap(self, bitmap=None, default=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1504 | """Set bitmap for the iconified widget to BITMAP. Return |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1505 | the bitmap if None is given. |
| 1506 | |
| 1507 | Under Windows, the DEFAULT parameter can be used to set the icon |
| 1508 | for the widget and any descendents that don't have an icon set |
| 1509 | explicitly. DEFAULT can be the relative path to a .ico file |
| 1510 | (example: root.iconbitmap(default='myicon.ico') ). See Tk |
| 1511 | documentation for more information.""" |
| 1512 | if default: |
| 1513 | return self.tk.call('wm', 'iconbitmap', self._w, '-default', default) |
| 1514 | else: |
| 1515 | return self.tk.call('wm', 'iconbitmap', self._w, bitmap) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1516 | iconbitmap = wm_iconbitmap |
| 1517 | def wm_iconify(self): |
| 1518 | """Display widget as icon.""" |
| 1519 | return self.tk.call('wm', 'iconify', self._w) |
| 1520 | iconify = wm_iconify |
| 1521 | def wm_iconmask(self, bitmap=None): |
| 1522 | """Set mask for the icon bitmap of this widget. Return the |
| 1523 | mask if None is given.""" |
| 1524 | return self.tk.call('wm', 'iconmask', self._w, bitmap) |
| 1525 | iconmask = wm_iconmask |
| 1526 | def wm_iconname(self, newName=None): |
| 1527 | """Set the name of the icon for this widget. Return the name if |
| 1528 | None is given.""" |
| 1529 | return self.tk.call('wm', 'iconname', self._w, newName) |
| 1530 | iconname = wm_iconname |
| 1531 | def wm_iconposition(self, x=None, y=None): |
| 1532 | """Set the position of the icon of this widget to X and Y. Return |
| 1533 | a tuple of the current values of X and X if None is given.""" |
| 1534 | return self._getints(self.tk.call( |
| 1535 | 'wm', 'iconposition', self._w, x, y)) |
| 1536 | iconposition = wm_iconposition |
| 1537 | def wm_iconwindow(self, pathName=None): |
| 1538 | """Set widget PATHNAME to be displayed instead of icon. Return the current |
| 1539 | value if None is given.""" |
| 1540 | return self.tk.call('wm', 'iconwindow', self._w, pathName) |
| 1541 | iconwindow = wm_iconwindow |
| 1542 | def wm_maxsize(self, width=None, height=None): |
| 1543 | """Set max WIDTH and HEIGHT for this widget. If the window is gridded |
| 1544 | the values are given in grid units. Return the current values if None |
| 1545 | is given.""" |
| 1546 | return self._getints(self.tk.call( |
| 1547 | 'wm', 'maxsize', self._w, width, height)) |
| 1548 | maxsize = wm_maxsize |
| 1549 | def wm_minsize(self, width=None, height=None): |
| 1550 | """Set min WIDTH and HEIGHT for this widget. If the window is gridded |
| 1551 | the values are given in grid units. Return the current values if None |
| 1552 | is given.""" |
| 1553 | return self._getints(self.tk.call( |
| 1554 | 'wm', 'minsize', self._w, width, height)) |
| 1555 | minsize = wm_minsize |
| 1556 | def wm_overrideredirect(self, boolean=None): |
| 1557 | """Instruct the window manager to ignore this widget |
| 1558 | if BOOLEAN is given with 1. Return the current value if None |
| 1559 | is given.""" |
| 1560 | return self._getboolean(self.tk.call( |
| 1561 | 'wm', 'overrideredirect', self._w, boolean)) |
| 1562 | overrideredirect = wm_overrideredirect |
| 1563 | def wm_positionfrom(self, who=None): |
| 1564 | """Instruct the window manager that the position of this widget shall |
| 1565 | be defined by the user if WHO is "user", and by its own policy if WHO is |
| 1566 | "program".""" |
| 1567 | return self.tk.call('wm', 'positionfrom', self._w, who) |
| 1568 | positionfrom = wm_positionfrom |
| 1569 | def wm_protocol(self, name=None, func=None): |
| 1570 | """Bind function FUNC to command NAME for this widget. |
| 1571 | Return the function bound to NAME if None is given. NAME could be |
| 1572 | e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW".""" |
| 1573 | if callable(func): |
| 1574 | command = self._register(func) |
| 1575 | else: |
| 1576 | command = func |
| 1577 | return self.tk.call( |
| 1578 | 'wm', 'protocol', self._w, name, command) |
| 1579 | protocol = wm_protocol |
| 1580 | def wm_resizable(self, width=None, height=None): |
| 1581 | """Instruct the window manager whether this width can be resized |
| 1582 | in WIDTH or HEIGHT. Both values are boolean values.""" |
| 1583 | return self.tk.call('wm', 'resizable', self._w, width, height) |
| 1584 | resizable = wm_resizable |
| 1585 | def wm_sizefrom(self, who=None): |
| 1586 | """Instruct the window manager that the size of this widget shall |
| 1587 | be defined by the user if WHO is "user", and by its own policy if WHO is |
| 1588 | "program".""" |
| 1589 | return self.tk.call('wm', 'sizefrom', self._w, who) |
| 1590 | sizefrom = wm_sizefrom |
Fredrik Lundh | 289ad8f | 2000-08-09 19:11:59 +0000 | [diff] [blame] | 1591 | def wm_state(self, newstate=None): |
| 1592 | """Query or set the state of this widget as one of normal, icon, |
| 1593 | iconic (see wm_iconwindow), withdrawn, or zoomed (Windows only).""" |
| 1594 | return self.tk.call('wm', 'state', self._w, newstate) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1595 | state = wm_state |
| 1596 | def wm_title(self, string=None): |
| 1597 | """Set the title of this widget.""" |
| 1598 | return self.tk.call('wm', 'title', self._w, string) |
| 1599 | title = wm_title |
| 1600 | def wm_transient(self, master=None): |
| 1601 | """Instruct the window manager that this widget is transient |
| 1602 | with regard to widget MASTER.""" |
| 1603 | return self.tk.call('wm', 'transient', self._w, master) |
| 1604 | transient = wm_transient |
| 1605 | def wm_withdraw(self): |
| 1606 | """Withdraw this widget from the screen such that it is unmapped |
| 1607 | and forgotten by the window manager. Re-draw it with wm_deiconify.""" |
| 1608 | return self.tk.call('wm', 'withdraw', self._w) |
| 1609 | withdraw = wm_withdraw |
Guido van Rossum | e365a59 | 1998-05-01 19:48:20 +0000 | [diff] [blame] | 1610 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1611 | |
| 1612 | class Tk(Misc, Wm): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1613 | """Toplevel widget of Tk which represents mostly the main window |
| 1614 | of an appliation. It has an associated Tcl interpreter.""" |
| 1615 | _w = '.' |
Martin v. Löwis | 9441c07 | 2004-08-03 18:36:25 +0000 | [diff] [blame] | 1616 | def __init__(self, screenName=None, baseName=None, className='Tk', |
| 1617 | useTk=1, sync=0, use=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1618 | """Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will |
| 1619 | be created. BASENAME will be used for the identification of the profile file (see |
| 1620 | readprofile). |
| 1621 | It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME |
| 1622 | is the name of the widget class.""" |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1623 | self.master = None |
| 1624 | self.children = {} |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1625 | self._tkloaded = 0 |
| 1626 | # to avoid recursions in the getattr code in case of failure, we |
| 1627 | # ensure that self.tk is always _something_. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 1628 | self.tk = None |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1629 | if baseName is None: |
| 1630 | import sys, os |
| 1631 | baseName = os.path.basename(sys.argv[0]) |
| 1632 | baseName, ext = os.path.splitext(baseName) |
| 1633 | if ext not in ('.py', '.pyc', '.pyo'): |
| 1634 | baseName = baseName + ext |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1635 | interactive = 0 |
Martin v. Löwis | 9441c07 | 2004-08-03 18:36:25 +0000 | [diff] [blame] | 1636 | self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1637 | if useTk: |
| 1638 | self._loadtk() |
| 1639 | self.readprofile(baseName, className) |
| 1640 | def loadtk(self): |
| 1641 | if not self._tkloaded: |
| 1642 | self.tk.loadtk() |
| 1643 | self._loadtk() |
| 1644 | def _loadtk(self): |
| 1645 | self._tkloaded = 1 |
| 1646 | global _default_root |
Jack Jansen | be92af0 | 2001-08-23 13:25:59 +0000 | [diff] [blame] | 1647 | if _MacOS and hasattr(_MacOS, 'SchedParams'): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1648 | # Disable event scanning except for Command-Period |
| 1649 | _MacOS.SchedParams(1, 0) |
| 1650 | # Work around nasty MacTk bug |
| 1651 | # XXX Is this one still needed? |
| 1652 | self.update() |
| 1653 | # Version sanity checks |
| 1654 | tk_version = self.tk.getvar('tk_version') |
| 1655 | if tk_version != _tkinter.TK_VERSION: |
| 1656 | raise RuntimeError, \ |
| 1657 | "tk.h version (%s) doesn't match libtk.a version (%s)" \ |
| 1658 | % (_tkinter.TK_VERSION, tk_version) |
Martin v. Löwis | 5489597 | 2003-05-24 11:37:15 +0000 | [diff] [blame] | 1659 | # Under unknown circumstances, tcl_version gets coerced to float |
| 1660 | tcl_version = str(self.tk.getvar('tcl_version')) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1661 | if tcl_version != _tkinter.TCL_VERSION: |
| 1662 | raise RuntimeError, \ |
| 1663 | "tcl.h version (%s) doesn't match libtcl.a version (%s)" \ |
| 1664 | % (_tkinter.TCL_VERSION, tcl_version) |
| 1665 | if TkVersion < 4.0: |
| 1666 | raise RuntimeError, \ |
| 1667 | "Tk 4.0 or higher is required; found Tk %s" \ |
| 1668 | % str(TkVersion) |
Martin v. Löwis | 4afe154 | 2005-03-01 08:09:28 +0000 | [diff] [blame] | 1669 | # Create and register the tkerror and exit commands |
| 1670 | # We need to inline parts of _register here, _ register |
| 1671 | # would register differently-named commands. |
| 1672 | if self._tclCommands is None: |
| 1673 | self._tclCommands = [] |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1674 | self.tk.createcommand('tkerror', _tkerror) |
| 1675 | self.tk.createcommand('exit', _exit) |
Martin v. Löwis | 4afe154 | 2005-03-01 08:09:28 +0000 | [diff] [blame] | 1676 | self._tclCommands.append('tkerror') |
| 1677 | self._tclCommands.append('exit') |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1678 | if _support_default_root and not _default_root: |
| 1679 | _default_root = self |
| 1680 | self.protocol("WM_DELETE_WINDOW", self.destroy) |
| 1681 | def destroy(self): |
| 1682 | """Destroy this and all descendants widgets. This will |
| 1683 | end the application of this Tcl interpreter.""" |
| 1684 | for c in self.children.values(): c.destroy() |
| 1685 | self.tk.call('destroy', self._w) |
| 1686 | Misc.destroy(self) |
| 1687 | global _default_root |
| 1688 | if _support_default_root and _default_root is self: |
| 1689 | _default_root = None |
| 1690 | def readprofile(self, baseName, className): |
| 1691 | """Internal function. It reads BASENAME.tcl and CLASSNAME.tcl into |
| 1692 | the Tcl Interpreter and calls execfile on BASENAME.py and CLASSNAME.py if |
| 1693 | such a file exists in the home directory.""" |
| 1694 | import os |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 1695 | if 'HOME' in os.environ: home = os.environ['HOME'] |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1696 | else: home = os.curdir |
| 1697 | class_tcl = os.path.join(home, '.%s.tcl' % className) |
| 1698 | class_py = os.path.join(home, '.%s.py' % className) |
| 1699 | base_tcl = os.path.join(home, '.%s.tcl' % baseName) |
| 1700 | base_py = os.path.join(home, '.%s.py' % baseName) |
| 1701 | dir = {'self': self} |
| 1702 | exec 'from Tkinter import *' in dir |
| 1703 | if os.path.isfile(class_tcl): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1704 | self.tk.call('source', class_tcl) |
| 1705 | if os.path.isfile(class_py): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1706 | execfile(class_py, dir) |
| 1707 | if os.path.isfile(base_tcl): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1708 | self.tk.call('source', base_tcl) |
| 1709 | if os.path.isfile(base_py): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1710 | execfile(base_py, dir) |
| 1711 | def report_callback_exception(self, exc, val, tb): |
| 1712 | """Internal function. It reports exception on sys.stderr.""" |
| 1713 | import traceback, sys |
| 1714 | sys.stderr.write("Exception in Tkinter callback\n") |
| 1715 | sys.last_type = exc |
| 1716 | sys.last_value = val |
| 1717 | sys.last_traceback = tb |
| 1718 | traceback.print_exception(exc, val, tb) |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1719 | def __getattr__(self, attr): |
| 1720 | "Delegate attribute access to the interpreter object" |
| 1721 | return getattr(self.tk, attr) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1723 | # Ideally, the classes Pack, Place and Grid disappear, the |
| 1724 | # pack/place/grid methods are defined on the Widget class, and |
| 1725 | # everybody uses w.pack_whatever(...) instead of Pack.whatever(w, |
| 1726 | # ...), with pack(), place() and grid() being short for |
| 1727 | # pack_configure(), place_configure() and grid_columnconfigure(), and |
| 1728 | # forget() being short for pack_forget(). As a practical matter, I'm |
| 1729 | # afraid that there is too much code out there that may be using the |
| 1730 | # Pack, Place or Grid class, so I leave them intact -- but only as |
| 1731 | # backwards compatibility features. Also note that those methods that |
| 1732 | # take a master as argument (e.g. pack_propagate) have been moved to |
| 1733 | # the Misc class (which now incorporates all methods common between |
| 1734 | # toplevel and interior widgets). Again, for compatibility, these are |
| 1735 | # copied into the Pack, Place or Grid class. |
| 1736 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1737 | |
| 1738 | def Tcl(screenName=None, baseName=None, className='Tk', useTk=0): |
| 1739 | return Tk(screenName, baseName, className, useTk) |
| 1740 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1741 | class Pack: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1742 | """Geometry manager Pack. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1743 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1744 | Base class to use the methods pack_* in every widget.""" |
| 1745 | def pack_configure(self, cnf={}, **kw): |
| 1746 | """Pack a widget in the parent widget. Use as options: |
| 1747 | after=widget - pack it after you have packed widget |
| 1748 | anchor=NSEW (or subset) - position widget according to |
| 1749 | given direction |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1750 | before=widget - pack it before you will pack widget |
Martin v. Löwis | bfe175c | 2003-04-16 19:42:51 +0000 | [diff] [blame] | 1751 | expand=bool - expand widget if parent size grows |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1752 | fill=NONE or X or Y or BOTH - fill widget if widget grows |
| 1753 | in=master - use master to contain this widget |
| 1754 | ipadx=amount - add internal padding in x direction |
| 1755 | ipady=amount - add internal padding in y direction |
| 1756 | padx=amount - add padding in x direction |
| 1757 | pady=amount - add padding in y direction |
| 1758 | side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget. |
| 1759 | """ |
| 1760 | self.tk.call( |
| 1761 | ('pack', 'configure', self._w) |
| 1762 | + self._options(cnf, kw)) |
| 1763 | pack = configure = config = pack_configure |
| 1764 | def pack_forget(self): |
| 1765 | """Unmap this widget and do not use it for the packing order.""" |
| 1766 | self.tk.call('pack', 'forget', self._w) |
| 1767 | forget = pack_forget |
| 1768 | def pack_info(self): |
| 1769 | """Return information about the packing options |
| 1770 | for this widget.""" |
| 1771 | words = self.tk.splitlist( |
| 1772 | self.tk.call('pack', 'info', self._w)) |
| 1773 | dict = {} |
| 1774 | for i in range(0, len(words), 2): |
| 1775 | key = words[i][1:] |
| 1776 | value = words[i+1] |
| 1777 | if value[:1] == '.': |
| 1778 | value = self._nametowidget(value) |
| 1779 | dict[key] = value |
| 1780 | return dict |
| 1781 | info = pack_info |
| 1782 | propagate = pack_propagate = Misc.pack_propagate |
| 1783 | slaves = pack_slaves = Misc.pack_slaves |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1784 | |
| 1785 | class Place: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1786 | """Geometry manager Place. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1787 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1788 | Base class to use the methods place_* in every widget.""" |
| 1789 | def place_configure(self, cnf={}, **kw): |
| 1790 | """Place a widget in the parent widget. Use as options: |
| 1791 | in=master - master relative to which the widget is placed. |
| 1792 | x=amount - locate anchor of this widget at position x of master |
| 1793 | y=amount - locate anchor of this widget at position y of master |
| 1794 | relx=amount - locate anchor of this widget between 0.0 and 1.0 |
| 1795 | relative to width of master (1.0 is right edge) |
| 1796 | rely=amount - locate anchor of this widget between 0.0 and 1.0 |
| 1797 | relative to height of master (1.0 is bottom edge) |
| 1798 | anchor=NSEW (or subset) - position anchor according to given direction |
| 1799 | width=amount - width of this widget in pixel |
| 1800 | height=amount - height of this widget in pixel |
| 1801 | relwidth=amount - width of this widget between 0.0 and 1.0 |
| 1802 | relative to width of master (1.0 is the same width |
| 1803 | as the master) |
| 1804 | relheight=amount - height of this widget between 0.0 and 1.0 |
| 1805 | relative to height of master (1.0 is the same |
| 1806 | height as the master) |
| 1807 | bordermode="inside" or "outside" - whether to take border width of master widget |
| 1808 | into account |
| 1809 | """ |
| 1810 | for k in ['in_']: |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 1811 | if k in kw: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1812 | kw[k[:-1]] = kw[k] |
| 1813 | del kw[k] |
| 1814 | self.tk.call( |
| 1815 | ('place', 'configure', self._w) |
| 1816 | + self._options(cnf, kw)) |
| 1817 | place = configure = config = place_configure |
| 1818 | def place_forget(self): |
| 1819 | """Unmap this widget.""" |
| 1820 | self.tk.call('place', 'forget', self._w) |
| 1821 | forget = place_forget |
| 1822 | def place_info(self): |
| 1823 | """Return information about the placing options |
| 1824 | for this widget.""" |
| 1825 | words = self.tk.splitlist( |
| 1826 | self.tk.call('place', 'info', self._w)) |
| 1827 | dict = {} |
| 1828 | for i in range(0, len(words), 2): |
| 1829 | key = words[i][1:] |
| 1830 | value = words[i+1] |
| 1831 | if value[:1] == '.': |
| 1832 | value = self._nametowidget(value) |
| 1833 | dict[key] = value |
| 1834 | return dict |
| 1835 | info = place_info |
| 1836 | slaves = place_slaves = Misc.place_slaves |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1837 | |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1838 | class Grid: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1839 | """Geometry manager Grid. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1840 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1841 | Base class to use the methods grid_* in every widget.""" |
| 1842 | # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu) |
| 1843 | def grid_configure(self, cnf={}, **kw): |
| 1844 | """Position a widget in the parent widget in a grid. Use as options: |
| 1845 | column=number - use cell identified with given column (starting with 0) |
| 1846 | columnspan=number - this widget will span several columns |
| 1847 | in=master - use master to contain this widget |
| 1848 | ipadx=amount - add internal padding in x direction |
| 1849 | ipady=amount - add internal padding in y direction |
| 1850 | padx=amount - add padding in x direction |
| 1851 | pady=amount - add padding in y direction |
| 1852 | row=number - use cell identified with given row (starting with 0) |
| 1853 | rowspan=number - this widget will span several rows |
| 1854 | sticky=NSEW - if cell is larger on which sides will this |
| 1855 | widget stick to the cell boundary |
| 1856 | """ |
| 1857 | self.tk.call( |
| 1858 | ('grid', 'configure', self._w) |
| 1859 | + self._options(cnf, kw)) |
| 1860 | grid = configure = config = grid_configure |
| 1861 | bbox = grid_bbox = Misc.grid_bbox |
| 1862 | columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure |
| 1863 | def grid_forget(self): |
| 1864 | """Unmap this widget.""" |
| 1865 | self.tk.call('grid', 'forget', self._w) |
| 1866 | forget = grid_forget |
| 1867 | def grid_remove(self): |
| 1868 | """Unmap this widget but remember the grid options.""" |
| 1869 | self.tk.call('grid', 'remove', self._w) |
| 1870 | def grid_info(self): |
| 1871 | """Return information about the options |
| 1872 | for positioning this widget in a grid.""" |
| 1873 | words = self.tk.splitlist( |
| 1874 | self.tk.call('grid', 'info', self._w)) |
| 1875 | dict = {} |
| 1876 | for i in range(0, len(words), 2): |
| 1877 | key = words[i][1:] |
| 1878 | value = words[i+1] |
| 1879 | if value[:1] == '.': |
| 1880 | value = self._nametowidget(value) |
| 1881 | dict[key] = value |
| 1882 | return dict |
| 1883 | info = grid_info |
Martin v. Löwis | dc57909 | 2001-10-13 09:33:51 +0000 | [diff] [blame] | 1884 | location = grid_location = Misc.grid_location |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1885 | propagate = grid_propagate = Misc.grid_propagate |
| 1886 | rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure |
| 1887 | size = grid_size = Misc.grid_size |
| 1888 | slaves = grid_slaves = Misc.grid_slaves |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1889 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1890 | class BaseWidget(Misc): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1891 | """Internal class.""" |
| 1892 | def _setup(self, master, cnf): |
| 1893 | """Internal function. Sets up information about children.""" |
| 1894 | if _support_default_root: |
| 1895 | global _default_root |
| 1896 | if not master: |
| 1897 | if not _default_root: |
| 1898 | _default_root = Tk() |
| 1899 | master = _default_root |
| 1900 | self.master = master |
| 1901 | self.tk = master.tk |
| 1902 | name = None |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 1903 | if 'name' in cnf: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1904 | name = cnf['name'] |
| 1905 | del cnf['name'] |
| 1906 | if not name: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1907 | name = repr(id(self)) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1908 | self._name = name |
| 1909 | if master._w=='.': |
| 1910 | self._w = '.' + name |
| 1911 | else: |
| 1912 | self._w = master._w + '.' + name |
| 1913 | self.children = {} |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 1914 | if self._name in self.master.children: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1915 | self.master.children[self._name].destroy() |
| 1916 | self.master.children[self._name] = self |
| 1917 | def __init__(self, master, widgetName, cnf={}, kw={}, extra=()): |
| 1918 | """Construct a widget with the parent widget MASTER, a name WIDGETNAME |
| 1919 | and appropriate options.""" |
| 1920 | if kw: |
| 1921 | cnf = _cnfmerge((cnf, kw)) |
| 1922 | self.widgetName = widgetName |
| 1923 | BaseWidget._setup(self, master, cnf) |
| 1924 | classes = [] |
| 1925 | for k in cnf.keys(): |
| 1926 | if type(k) is ClassType: |
| 1927 | classes.append((k, cnf[k])) |
| 1928 | del cnf[k] |
| 1929 | self.tk.call( |
| 1930 | (widgetName, self._w) + extra + self._options(cnf)) |
| 1931 | for k, v in classes: |
| 1932 | k.configure(self, v) |
| 1933 | def destroy(self): |
| 1934 | """Destroy this and all descendants widgets.""" |
| 1935 | for c in self.children.values(): c.destroy() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1936 | self.tk.call('destroy', self._w) |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 1937 | if self._name in self.master.children: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1938 | del self.master.children[self._name] |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1939 | Misc.destroy(self) |
| 1940 | def _do(self, name, args=()): |
| 1941 | # XXX Obsolete -- better use self.tk.call directly! |
| 1942 | return self.tk.call((self._w, name) + args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1943 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1944 | class Widget(BaseWidget, Pack, Place, Grid): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1945 | """Internal class. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1946 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1947 | Base class for a widget which can be positioned with the geometry managers |
| 1948 | Pack, Place or Grid.""" |
| 1949 | pass |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1950 | |
| 1951 | class Toplevel(BaseWidget, Wm): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1952 | """Toplevel widget, e.g. for dialogs.""" |
| 1953 | def __init__(self, master=None, cnf={}, **kw): |
| 1954 | """Construct a toplevel widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 1955 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1956 | Valid resource names: background, bd, bg, borderwidth, class, |
| 1957 | colormap, container, cursor, height, highlightbackground, |
| 1958 | highlightcolor, highlightthickness, menu, relief, screen, takefocus, |
| 1959 | use, visual, width.""" |
| 1960 | if kw: |
| 1961 | cnf = _cnfmerge((cnf, kw)) |
| 1962 | extra = () |
| 1963 | for wmkey in ['screen', 'class_', 'class', 'visual', |
| 1964 | 'colormap']: |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 1965 | if wmkey in cnf: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1966 | val = cnf[wmkey] |
| 1967 | # TBD: a hack needed because some keys |
| 1968 | # are not valid as keyword arguments |
| 1969 | if wmkey[-1] == '_': opt = '-'+wmkey[:-1] |
| 1970 | else: opt = '-'+wmkey |
| 1971 | extra = extra + (opt, val) |
| 1972 | del cnf[wmkey] |
| 1973 | BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) |
| 1974 | root = self._root() |
| 1975 | self.iconname(root.iconname()) |
| 1976 | self.title(root.title()) |
| 1977 | self.protocol("WM_DELETE_WINDOW", self.destroy) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1978 | |
| 1979 | class Button(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 1980 | """Button widget.""" |
| 1981 | def __init__(self, master=None, cnf={}, **kw): |
| 1982 | """Construct a button widget with the parent MASTER. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1983 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1984 | STANDARD OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1985 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1986 | activebackground, activeforeground, anchor, |
| 1987 | background, bitmap, borderwidth, cursor, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1988 | disabledforeground, font, foreground |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1989 | highlightbackground, highlightcolor, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1990 | highlightthickness, image, justify, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1991 | padx, pady, relief, repeatdelay, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1992 | repeatinterval, takefocus, text, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1993 | textvariable, underline, wraplength |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1994 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1995 | WIDGET-SPECIFIC OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 1996 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 1997 | command, compound, default, height, |
| 1998 | overrelief, state, width |
| 1999 | """ |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2000 | Widget.__init__(self, master, 'button', cnf, kw) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2001 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2002 | def tkButtonEnter(self, *dummy): |
| 2003 | self.tk.call('tkButtonEnter', self._w) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2004 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2005 | def tkButtonLeave(self, *dummy): |
| 2006 | self.tk.call('tkButtonLeave', self._w) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2007 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2008 | def tkButtonDown(self, *dummy): |
| 2009 | self.tk.call('tkButtonDown', self._w) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2010 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2011 | def tkButtonUp(self, *dummy): |
| 2012 | self.tk.call('tkButtonUp', self._w) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2013 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2014 | def tkButtonInvoke(self, *dummy): |
| 2015 | self.tk.call('tkButtonInvoke', self._w) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2016 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2017 | def flash(self): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2018 | """Flash the button. |
| 2019 | |
| 2020 | This is accomplished by redisplaying |
| 2021 | the button several times, alternating between active and |
| 2022 | normal colors. At the end of the flash the button is left |
| 2023 | in the same normal/active state as when the command was |
| 2024 | invoked. This command is ignored if the button's state is |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2025 | disabled. |
| 2026 | """ |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2027 | self.tk.call(self._w, 'flash') |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2028 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2029 | def invoke(self): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2030 | """Invoke the command associated with the button. |
| 2031 | |
| 2032 | The return value is the return value from the command, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2033 | or an empty string if there is no command associated with |
| 2034 | the button. This command is ignored if the button's state |
| 2035 | is disabled. |
| 2036 | """ |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2037 | return self.tk.call(self._w, 'invoke') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2038 | |
| 2039 | # Indices: |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 2040 | # XXX I don't like these -- take them away |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2041 | def AtEnd(): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2042 | return 'end' |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame] | 2043 | def AtInsert(*args): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2044 | s = 'insert' |
| 2045 | for a in args: |
| 2046 | if a: s = s + (' ' + a) |
| 2047 | return s |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2048 | def AtSelFirst(): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2049 | return 'sel.first' |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2050 | def AtSelLast(): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2051 | return 'sel.last' |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2052 | def At(x, y=None): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2053 | if y is None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 2054 | return '@%r' % (x,) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2055 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 2056 | return '@%r,%r' % (x, y) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2057 | |
| 2058 | class Canvas(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2059 | """Canvas widget to display graphical elements like lines or text.""" |
| 2060 | def __init__(self, master=None, cnf={}, **kw): |
| 2061 | """Construct a canvas widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2062 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2063 | Valid resource names: background, bd, bg, borderwidth, closeenough, |
| 2064 | confine, cursor, height, highlightbackground, highlightcolor, |
| 2065 | highlightthickness, insertbackground, insertborderwidth, |
| 2066 | insertofftime, insertontime, insertwidth, offset, relief, |
| 2067 | scrollregion, selectbackground, selectborderwidth, selectforeground, |
| 2068 | state, takefocus, width, xscrollcommand, xscrollincrement, |
| 2069 | yscrollcommand, yscrollincrement.""" |
| 2070 | Widget.__init__(self, master, 'canvas', cnf, kw) |
| 2071 | def addtag(self, *args): |
| 2072 | """Internal function.""" |
| 2073 | self.tk.call((self._w, 'addtag') + args) |
| 2074 | def addtag_above(self, newtag, tagOrId): |
| 2075 | """Add tag NEWTAG to all items above TAGORID.""" |
| 2076 | self.addtag(newtag, 'above', tagOrId) |
| 2077 | def addtag_all(self, newtag): |
| 2078 | """Add tag NEWTAG to all items.""" |
| 2079 | self.addtag(newtag, 'all') |
| 2080 | def addtag_below(self, newtag, tagOrId): |
| 2081 | """Add tag NEWTAG to all items below TAGORID.""" |
| 2082 | self.addtag(newtag, 'below', tagOrId) |
| 2083 | def addtag_closest(self, newtag, x, y, halo=None, start=None): |
| 2084 | """Add tag NEWTAG to item which is closest to pixel at X, Y. |
| 2085 | If several match take the top-most. |
| 2086 | All items closer than HALO are considered overlapping (all are |
| 2087 | closests). If START is specified the next below this tag is taken.""" |
| 2088 | self.addtag(newtag, 'closest', x, y, halo, start) |
| 2089 | def addtag_enclosed(self, newtag, x1, y1, x2, y2): |
| 2090 | """Add tag NEWTAG to all items in the rectangle defined |
| 2091 | by X1,Y1,X2,Y2.""" |
| 2092 | self.addtag(newtag, 'enclosed', x1, y1, x2, y2) |
| 2093 | def addtag_overlapping(self, newtag, x1, y1, x2, y2): |
| 2094 | """Add tag NEWTAG to all items which overlap the rectangle |
| 2095 | defined by X1,Y1,X2,Y2.""" |
| 2096 | self.addtag(newtag, 'overlapping', x1, y1, x2, y2) |
| 2097 | def addtag_withtag(self, newtag, tagOrId): |
| 2098 | """Add tag NEWTAG to all items with TAGORID.""" |
| 2099 | self.addtag(newtag, 'withtag', tagOrId) |
| 2100 | def bbox(self, *args): |
| 2101 | """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle |
| 2102 | which encloses all items with tags specified as arguments.""" |
| 2103 | return self._getints( |
| 2104 | self.tk.call((self._w, 'bbox') + args)) or None |
| 2105 | def tag_unbind(self, tagOrId, sequence, funcid=None): |
| 2106 | """Unbind for all items with TAGORID for event SEQUENCE the |
| 2107 | function identified with FUNCID.""" |
| 2108 | self.tk.call(self._w, 'bind', tagOrId, sequence, '') |
| 2109 | if funcid: |
| 2110 | self.deletecommand(funcid) |
| 2111 | def tag_bind(self, tagOrId, sequence=None, func=None, add=None): |
| 2112 | """Bind to all items with TAGORID at event SEQUENCE a call to function FUNC. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2113 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2114 | An additional boolean parameter ADD specifies whether FUNC will be |
| 2115 | called additionally to the other bound function or whether it will |
| 2116 | replace the previous function. See bind for the return value.""" |
| 2117 | return self._bind((self._w, 'bind', tagOrId), |
| 2118 | sequence, func, add) |
| 2119 | def canvasx(self, screenx, gridspacing=None): |
| 2120 | """Return the canvas x coordinate of pixel position SCREENX rounded |
| 2121 | to nearest multiple of GRIDSPACING units.""" |
| 2122 | return getdouble(self.tk.call( |
| 2123 | self._w, 'canvasx', screenx, gridspacing)) |
| 2124 | def canvasy(self, screeny, gridspacing=None): |
| 2125 | """Return the canvas y coordinate of pixel position SCREENY rounded |
| 2126 | to nearest multiple of GRIDSPACING units.""" |
| 2127 | return getdouble(self.tk.call( |
| 2128 | self._w, 'canvasy', screeny, gridspacing)) |
| 2129 | def coords(self, *args): |
| 2130 | """Return a list of coordinates for the item given in ARGS.""" |
| 2131 | # XXX Should use _flatten on args |
| 2132 | return map(getdouble, |
Guido van Rossum | 0bd5433 | 1998-05-19 21:18:13 +0000 | [diff] [blame] | 2133 | self.tk.splitlist( |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2134 | self.tk.call((self._w, 'coords') + args))) |
| 2135 | def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={}) |
| 2136 | """Internal function.""" |
| 2137 | args = _flatten(args) |
| 2138 | cnf = args[-1] |
| 2139 | if type(cnf) in (DictionaryType, TupleType): |
| 2140 | args = args[:-1] |
| 2141 | else: |
| 2142 | cnf = {} |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2143 | return getint(self.tk.call( |
| 2144 | self._w, 'create', itemType, |
| 2145 | *(args + self._options(cnf, kw)))) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2146 | def create_arc(self, *args, **kw): |
| 2147 | """Create arc shaped region with coordinates x1,y1,x2,y2.""" |
| 2148 | return self._create('arc', args, kw) |
| 2149 | def create_bitmap(self, *args, **kw): |
| 2150 | """Create bitmap with coordinates x1,y1.""" |
| 2151 | return self._create('bitmap', args, kw) |
| 2152 | def create_image(self, *args, **kw): |
| 2153 | """Create image item with coordinates x1,y1.""" |
| 2154 | return self._create('image', args, kw) |
| 2155 | def create_line(self, *args, **kw): |
| 2156 | """Create line with coordinates x1,y1,...,xn,yn.""" |
| 2157 | return self._create('line', args, kw) |
| 2158 | def create_oval(self, *args, **kw): |
| 2159 | """Create oval with coordinates x1,y1,x2,y2.""" |
| 2160 | return self._create('oval', args, kw) |
| 2161 | def create_polygon(self, *args, **kw): |
| 2162 | """Create polygon with coordinates x1,y1,...,xn,yn.""" |
| 2163 | return self._create('polygon', args, kw) |
| 2164 | def create_rectangle(self, *args, **kw): |
| 2165 | """Create rectangle with coordinates x1,y1,x2,y2.""" |
| 2166 | return self._create('rectangle', args, kw) |
| 2167 | def create_text(self, *args, **kw): |
| 2168 | """Create text with coordinates x1,y1.""" |
| 2169 | return self._create('text', args, kw) |
| 2170 | def create_window(self, *args, **kw): |
| 2171 | """Create window with coordinates x1,y1,x2,y2.""" |
| 2172 | return self._create('window', args, kw) |
| 2173 | def dchars(self, *args): |
| 2174 | """Delete characters of text items identified by tag or id in ARGS (possibly |
| 2175 | several times) from FIRST to LAST character (including).""" |
| 2176 | self.tk.call((self._w, 'dchars') + args) |
| 2177 | def delete(self, *args): |
| 2178 | """Delete items identified by all tag or ids contained in ARGS.""" |
| 2179 | self.tk.call((self._w, 'delete') + args) |
| 2180 | def dtag(self, *args): |
| 2181 | """Delete tag or id given as last arguments in ARGS from items |
| 2182 | identified by first argument in ARGS.""" |
| 2183 | self.tk.call((self._w, 'dtag') + args) |
| 2184 | def find(self, *args): |
| 2185 | """Internal function.""" |
| 2186 | return self._getints( |
| 2187 | self.tk.call((self._w, 'find') + args)) or () |
| 2188 | def find_above(self, tagOrId): |
| 2189 | """Return items above TAGORID.""" |
| 2190 | return self.find('above', tagOrId) |
| 2191 | def find_all(self): |
| 2192 | """Return all items.""" |
| 2193 | return self.find('all') |
| 2194 | def find_below(self, tagOrId): |
| 2195 | """Return all items below TAGORID.""" |
| 2196 | return self.find('below', tagOrId) |
| 2197 | def find_closest(self, x, y, halo=None, start=None): |
| 2198 | """Return item which is closest to pixel at X, Y. |
| 2199 | If several match take the top-most. |
| 2200 | All items closer than HALO are considered overlapping (all are |
| 2201 | closests). If START is specified the next below this tag is taken.""" |
| 2202 | return self.find('closest', x, y, halo, start) |
| 2203 | def find_enclosed(self, x1, y1, x2, y2): |
| 2204 | """Return all items in rectangle defined |
| 2205 | by X1,Y1,X2,Y2.""" |
| 2206 | return self.find('enclosed', x1, y1, x2, y2) |
| 2207 | def find_overlapping(self, x1, y1, x2, y2): |
| 2208 | """Return all items which overlap the rectangle |
| 2209 | defined by X1,Y1,X2,Y2.""" |
| 2210 | return self.find('overlapping', x1, y1, x2, y2) |
| 2211 | def find_withtag(self, tagOrId): |
| 2212 | """Return all items with TAGORID.""" |
| 2213 | return self.find('withtag', tagOrId) |
| 2214 | def focus(self, *args): |
| 2215 | """Set focus to the first item specified in ARGS.""" |
| 2216 | return self.tk.call((self._w, 'focus') + args) |
| 2217 | def gettags(self, *args): |
| 2218 | """Return tags associated with the first item specified in ARGS.""" |
| 2219 | return self.tk.splitlist( |
| 2220 | self.tk.call((self._w, 'gettags') + args)) |
| 2221 | def icursor(self, *args): |
| 2222 | """Set cursor at position POS in the item identified by TAGORID. |
| 2223 | In ARGS TAGORID must be first.""" |
| 2224 | self.tk.call((self._w, 'icursor') + args) |
| 2225 | def index(self, *args): |
| 2226 | """Return position of cursor as integer in item specified in ARGS.""" |
| 2227 | return getint(self.tk.call((self._w, 'index') + args)) |
| 2228 | def insert(self, *args): |
| 2229 | """Insert TEXT in item TAGORID at position POS. ARGS must |
| 2230 | be TAGORID POS TEXT.""" |
| 2231 | self.tk.call((self._w, 'insert') + args) |
| 2232 | def itemcget(self, tagOrId, option): |
| 2233 | """Return the resource value for an OPTION for item TAGORID.""" |
| 2234 | return self.tk.call( |
| 2235 | (self._w, 'itemcget') + (tagOrId, '-'+option)) |
| 2236 | def itemconfigure(self, tagOrId, cnf=None, **kw): |
| 2237 | """Configure resources of an item TAGORID. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2238 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2239 | The values for resources are specified as keyword |
| 2240 | arguments. To get an overview about |
| 2241 | the allowed keyword arguments call the method without arguments. |
| 2242 | """ |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 2243 | return self._configure(('itemconfigure', tagOrId), cnf, kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2244 | itemconfig = itemconfigure |
| 2245 | # lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift, |
| 2246 | # so the preferred name for them is tag_lower, tag_raise |
| 2247 | # (similar to tag_bind, and similar to the Text widget); |
| 2248 | # unfortunately can't delete the old ones yet (maybe in 1.6) |
| 2249 | def tag_lower(self, *args): |
| 2250 | """Lower an item TAGORID given in ARGS |
| 2251 | (optional below another item).""" |
| 2252 | self.tk.call((self._w, 'lower') + args) |
| 2253 | lower = tag_lower |
| 2254 | def move(self, *args): |
| 2255 | """Move an item TAGORID given in ARGS.""" |
| 2256 | self.tk.call((self._w, 'move') + args) |
| 2257 | def postscript(self, cnf={}, **kw): |
| 2258 | """Print the contents of the canvas to a postscript |
| 2259 | file. Valid options: colormap, colormode, file, fontmap, |
| 2260 | height, pageanchor, pageheight, pagewidth, pagex, pagey, |
| 2261 | rotate, witdh, x, y.""" |
| 2262 | return self.tk.call((self._w, 'postscript') + |
| 2263 | self._options(cnf, kw)) |
| 2264 | def tag_raise(self, *args): |
| 2265 | """Raise an item TAGORID given in ARGS |
| 2266 | (optional above another item).""" |
| 2267 | self.tk.call((self._w, 'raise') + args) |
| 2268 | lift = tkraise = tag_raise |
| 2269 | def scale(self, *args): |
| 2270 | """Scale item TAGORID with XORIGIN, YORIGIN, XSCALE, YSCALE.""" |
| 2271 | self.tk.call((self._w, 'scale') + args) |
| 2272 | def scan_mark(self, x, y): |
| 2273 | """Remember the current X, Y coordinates.""" |
| 2274 | self.tk.call(self._w, 'scan', 'mark', x, y) |
Neal Norwitz | e931ed5 | 2003-01-10 23:24:32 +0000 | [diff] [blame] | 2275 | def scan_dragto(self, x, y, gain=10): |
| 2276 | """Adjust the view of the canvas to GAIN times the |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2277 | difference between X and Y and the coordinates given in |
| 2278 | scan_mark.""" |
Neal Norwitz | e931ed5 | 2003-01-10 23:24:32 +0000 | [diff] [blame] | 2279 | self.tk.call(self._w, 'scan', 'dragto', x, y, gain) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2280 | def select_adjust(self, tagOrId, index): |
| 2281 | """Adjust the end of the selection near the cursor of an item TAGORID to index.""" |
| 2282 | self.tk.call(self._w, 'select', 'adjust', tagOrId, index) |
| 2283 | def select_clear(self): |
| 2284 | """Clear the selection if it is in this widget.""" |
| 2285 | self.tk.call(self._w, 'select', 'clear') |
| 2286 | def select_from(self, tagOrId, index): |
| 2287 | """Set the fixed end of a selection in item TAGORID to INDEX.""" |
| 2288 | self.tk.call(self._w, 'select', 'from', tagOrId, index) |
| 2289 | def select_item(self): |
| 2290 | """Return the item which has the selection.""" |
Neal Norwitz | 58b63bf | 2002-07-23 02:52:58 +0000 | [diff] [blame] | 2291 | return self.tk.call(self._w, 'select', 'item') or None |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2292 | def select_to(self, tagOrId, index): |
| 2293 | """Set the variable end of a selection in item TAGORID to INDEX.""" |
| 2294 | self.tk.call(self._w, 'select', 'to', tagOrId, index) |
| 2295 | def type(self, tagOrId): |
| 2296 | """Return the type of the item TAGORID.""" |
| 2297 | return self.tk.call(self._w, 'type', tagOrId) or None |
| 2298 | def xview(self, *args): |
| 2299 | """Query and change horizontal position of the view.""" |
| 2300 | if not args: |
| 2301 | return self._getdoubles(self.tk.call(self._w, 'xview')) |
| 2302 | self.tk.call((self._w, 'xview') + args) |
| 2303 | def xview_moveto(self, fraction): |
| 2304 | """Adjusts the view in the window so that FRACTION of the |
| 2305 | total width of the canvas is off-screen to the left.""" |
| 2306 | self.tk.call(self._w, 'xview', 'moveto', fraction) |
| 2307 | def xview_scroll(self, number, what): |
| 2308 | """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT).""" |
| 2309 | self.tk.call(self._w, 'xview', 'scroll', number, what) |
| 2310 | def yview(self, *args): |
| 2311 | """Query and change vertical position of the view.""" |
| 2312 | if not args: |
| 2313 | return self._getdoubles(self.tk.call(self._w, 'yview')) |
| 2314 | self.tk.call((self._w, 'yview') + args) |
| 2315 | def yview_moveto(self, fraction): |
| 2316 | """Adjusts the view in the window so that FRACTION of the |
| 2317 | total height of the canvas is off-screen to the top.""" |
| 2318 | self.tk.call(self._w, 'yview', 'moveto', fraction) |
| 2319 | def yview_scroll(self, number, what): |
| 2320 | """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT).""" |
| 2321 | self.tk.call(self._w, 'yview', 'scroll', number, what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2322 | |
| 2323 | class Checkbutton(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2324 | """Checkbutton widget which is either in on- or off-state.""" |
| 2325 | def __init__(self, master=None, cnf={}, **kw): |
| 2326 | """Construct a checkbutton widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2327 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2328 | Valid resource names: activebackground, activeforeground, anchor, |
| 2329 | background, bd, bg, bitmap, borderwidth, command, cursor, |
| 2330 | disabledforeground, fg, font, foreground, height, |
| 2331 | highlightbackground, highlightcolor, highlightthickness, image, |
| 2332 | indicatoron, justify, offvalue, onvalue, padx, pady, relief, |
| 2333 | selectcolor, selectimage, state, takefocus, text, textvariable, |
| 2334 | underline, variable, width, wraplength.""" |
| 2335 | Widget.__init__(self, master, 'checkbutton', cnf, kw) |
| 2336 | def deselect(self): |
| 2337 | """Put the button in off-state.""" |
| 2338 | self.tk.call(self._w, 'deselect') |
| 2339 | def flash(self): |
| 2340 | """Flash the button.""" |
| 2341 | self.tk.call(self._w, 'flash') |
| 2342 | def invoke(self): |
| 2343 | """Toggle the button and invoke a command if given as resource.""" |
| 2344 | return self.tk.call(self._w, 'invoke') |
| 2345 | def select(self): |
| 2346 | """Put the button in on-state.""" |
| 2347 | self.tk.call(self._w, 'select') |
| 2348 | def toggle(self): |
| 2349 | """Toggle the button.""" |
| 2350 | self.tk.call(self._w, 'toggle') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2351 | |
| 2352 | class Entry(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2353 | """Entry widget which allows to display simple text.""" |
| 2354 | def __init__(self, master=None, cnf={}, **kw): |
| 2355 | """Construct an entry widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2356 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2357 | Valid resource names: background, bd, bg, borderwidth, cursor, |
| 2358 | exportselection, fg, font, foreground, highlightbackground, |
| 2359 | highlightcolor, highlightthickness, insertbackground, |
| 2360 | insertborderwidth, insertofftime, insertontime, insertwidth, |
| 2361 | invalidcommand, invcmd, justify, relief, selectbackground, |
| 2362 | selectborderwidth, selectforeground, show, state, takefocus, |
| 2363 | textvariable, validate, validatecommand, vcmd, width, |
| 2364 | xscrollcommand.""" |
| 2365 | Widget.__init__(self, master, 'entry', cnf, kw) |
| 2366 | def delete(self, first, last=None): |
| 2367 | """Delete text from FIRST to LAST (not included).""" |
| 2368 | self.tk.call(self._w, 'delete', first, last) |
| 2369 | def get(self): |
| 2370 | """Return the text.""" |
| 2371 | return self.tk.call(self._w, 'get') |
| 2372 | def icursor(self, index): |
| 2373 | """Insert cursor at INDEX.""" |
| 2374 | self.tk.call(self._w, 'icursor', index) |
| 2375 | def index(self, index): |
| 2376 | """Return position of cursor.""" |
| 2377 | return getint(self.tk.call( |
| 2378 | self._w, 'index', index)) |
| 2379 | def insert(self, index, string): |
| 2380 | """Insert STRING at INDEX.""" |
| 2381 | self.tk.call(self._w, 'insert', index, string) |
| 2382 | def scan_mark(self, x): |
| 2383 | """Remember the current X, Y coordinates.""" |
| 2384 | self.tk.call(self._w, 'scan', 'mark', x) |
| 2385 | def scan_dragto(self, x): |
| 2386 | """Adjust the view of the canvas to 10 times the |
| 2387 | difference between X and Y and the coordinates given in |
| 2388 | scan_mark.""" |
| 2389 | self.tk.call(self._w, 'scan', 'dragto', x) |
| 2390 | def selection_adjust(self, index): |
| 2391 | """Adjust the end of the selection near the cursor to INDEX.""" |
| 2392 | self.tk.call(self._w, 'selection', 'adjust', index) |
| 2393 | select_adjust = selection_adjust |
| 2394 | def selection_clear(self): |
| 2395 | """Clear the selection if it is in this widget.""" |
| 2396 | self.tk.call(self._w, 'selection', 'clear') |
| 2397 | select_clear = selection_clear |
| 2398 | def selection_from(self, index): |
| 2399 | """Set the fixed end of a selection to INDEX.""" |
| 2400 | self.tk.call(self._w, 'selection', 'from', index) |
| 2401 | select_from = selection_from |
| 2402 | def selection_present(self): |
| 2403 | """Return whether the widget has the selection.""" |
| 2404 | return self.tk.getboolean( |
| 2405 | self.tk.call(self._w, 'selection', 'present')) |
| 2406 | select_present = selection_present |
| 2407 | def selection_range(self, start, end): |
| 2408 | """Set the selection from START to END (not included).""" |
| 2409 | self.tk.call(self._w, 'selection', 'range', start, end) |
| 2410 | select_range = selection_range |
| 2411 | def selection_to(self, index): |
| 2412 | """Set the variable end of a selection to INDEX.""" |
| 2413 | self.tk.call(self._w, 'selection', 'to', index) |
| 2414 | select_to = selection_to |
| 2415 | def xview(self, index): |
| 2416 | """Query and change horizontal position of the view.""" |
| 2417 | self.tk.call(self._w, 'xview', index) |
| 2418 | def xview_moveto(self, fraction): |
| 2419 | """Adjust the view in the window so that FRACTION of the |
| 2420 | total width of the entry is off-screen to the left.""" |
| 2421 | self.tk.call(self._w, 'xview', 'moveto', fraction) |
| 2422 | def xview_scroll(self, number, what): |
| 2423 | """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT).""" |
| 2424 | self.tk.call(self._w, 'xview', 'scroll', number, what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2425 | |
| 2426 | class Frame(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2427 | """Frame widget which may contain other widgets and can have a 3D border.""" |
| 2428 | def __init__(self, master=None, cnf={}, **kw): |
| 2429 | """Construct a frame widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2430 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2431 | Valid resource names: background, bd, bg, borderwidth, class, |
| 2432 | colormap, container, cursor, height, highlightbackground, |
| 2433 | highlightcolor, highlightthickness, relief, takefocus, visual, width.""" |
| 2434 | cnf = _cnfmerge((cnf, kw)) |
| 2435 | extra = () |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 2436 | if 'class_' in cnf: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2437 | extra = ('-class', cnf['class_']) |
| 2438 | del cnf['class_'] |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 2439 | elif 'class' in cnf: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2440 | extra = ('-class', cnf['class']) |
| 2441 | del cnf['class'] |
| 2442 | Widget.__init__(self, master, 'frame', cnf, {}, extra) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2443 | |
| 2444 | class Label(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2445 | """Label widget which can display text and bitmaps.""" |
| 2446 | def __init__(self, master=None, cnf={}, **kw): |
| 2447 | """Construct a label widget with the parent MASTER. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2448 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2449 | STANDARD OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2450 | |
| 2451 | activebackground, activeforeground, anchor, |
| 2452 | background, bitmap, borderwidth, cursor, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2453 | disabledforeground, font, foreground, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2454 | highlightbackground, highlightcolor, |
| 2455 | highlightthickness, image, justify, |
| 2456 | padx, pady, relief, takefocus, text, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2457 | textvariable, underline, wraplength |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2458 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2459 | WIDGET-SPECIFIC OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2460 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2461 | height, state, width |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2462 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2463 | """ |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2464 | Widget.__init__(self, master, 'label', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 2465 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2466 | class Listbox(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2467 | """Listbox widget which can display a list of strings.""" |
| 2468 | def __init__(self, master=None, cnf={}, **kw): |
| 2469 | """Construct a listbox widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2470 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2471 | Valid resource names: background, bd, bg, borderwidth, cursor, |
| 2472 | exportselection, fg, font, foreground, height, highlightbackground, |
| 2473 | highlightcolor, highlightthickness, relief, selectbackground, |
| 2474 | selectborderwidth, selectforeground, selectmode, setgrid, takefocus, |
| 2475 | width, xscrollcommand, yscrollcommand, listvariable.""" |
| 2476 | Widget.__init__(self, master, 'listbox', cnf, kw) |
| 2477 | def activate(self, index): |
| 2478 | """Activate item identified by INDEX.""" |
| 2479 | self.tk.call(self._w, 'activate', index) |
| 2480 | def bbox(self, *args): |
| 2481 | """Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle |
| 2482 | which encloses the item identified by index in ARGS.""" |
| 2483 | return self._getints( |
| 2484 | self.tk.call((self._w, 'bbox') + args)) or None |
| 2485 | def curselection(self): |
| 2486 | """Return list of indices of currently selected item.""" |
| 2487 | # XXX Ought to apply self._getints()... |
| 2488 | return self.tk.splitlist(self.tk.call( |
| 2489 | self._w, 'curselection')) |
| 2490 | def delete(self, first, last=None): |
| 2491 | """Delete items from FIRST to LAST (not included).""" |
| 2492 | self.tk.call(self._w, 'delete', first, last) |
| 2493 | def get(self, first, last=None): |
| 2494 | """Get list of items from FIRST to LAST (not included).""" |
| 2495 | if last: |
| 2496 | return self.tk.splitlist(self.tk.call( |
| 2497 | self._w, 'get', first, last)) |
| 2498 | else: |
| 2499 | return self.tk.call(self._w, 'get', first) |
| 2500 | def index(self, index): |
| 2501 | """Return index of item identified with INDEX.""" |
| 2502 | i = self.tk.call(self._w, 'index', index) |
| 2503 | if i == 'none': return None |
| 2504 | return getint(i) |
| 2505 | def insert(self, index, *elements): |
| 2506 | """Insert ELEMENTS at INDEX.""" |
| 2507 | self.tk.call((self._w, 'insert', index) + elements) |
| 2508 | def nearest(self, y): |
| 2509 | """Get index of item which is nearest to y coordinate Y.""" |
| 2510 | return getint(self.tk.call( |
| 2511 | self._w, 'nearest', y)) |
| 2512 | def scan_mark(self, x, y): |
| 2513 | """Remember the current X, Y coordinates.""" |
| 2514 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 2515 | def scan_dragto(self, x, y): |
| 2516 | """Adjust the view of the listbox to 10 times the |
| 2517 | difference between X and Y and the coordinates given in |
| 2518 | scan_mark.""" |
| 2519 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 2520 | def see(self, index): |
| 2521 | """Scroll such that INDEX is visible.""" |
| 2522 | self.tk.call(self._w, 'see', index) |
| 2523 | def selection_anchor(self, index): |
| 2524 | """Set the fixed end oft the selection to INDEX.""" |
| 2525 | self.tk.call(self._w, 'selection', 'anchor', index) |
| 2526 | select_anchor = selection_anchor |
| 2527 | def selection_clear(self, first, last=None): |
| 2528 | """Clear the selection from FIRST to LAST (not included).""" |
| 2529 | self.tk.call(self._w, |
| 2530 | 'selection', 'clear', first, last) |
| 2531 | select_clear = selection_clear |
| 2532 | def selection_includes(self, index): |
| 2533 | """Return 1 if INDEX is part of the selection.""" |
| 2534 | return self.tk.getboolean(self.tk.call( |
| 2535 | self._w, 'selection', 'includes', index)) |
| 2536 | select_includes = selection_includes |
| 2537 | def selection_set(self, first, last=None): |
| 2538 | """Set the selection from FIRST to LAST (not included) without |
| 2539 | changing the currently selected elements.""" |
| 2540 | self.tk.call(self._w, 'selection', 'set', first, last) |
| 2541 | select_set = selection_set |
| 2542 | def size(self): |
| 2543 | """Return the number of elements in the listbox.""" |
| 2544 | return getint(self.tk.call(self._w, 'size')) |
| 2545 | def xview(self, *what): |
| 2546 | """Query and change horizontal position of the view.""" |
| 2547 | if not what: |
| 2548 | return self._getdoubles(self.tk.call(self._w, 'xview')) |
| 2549 | self.tk.call((self._w, 'xview') + what) |
| 2550 | def xview_moveto(self, fraction): |
| 2551 | """Adjust the view in the window so that FRACTION of the |
| 2552 | total width of the entry is off-screen to the left.""" |
| 2553 | self.tk.call(self._w, 'xview', 'moveto', fraction) |
| 2554 | def xview_scroll(self, number, what): |
| 2555 | """Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT).""" |
| 2556 | self.tk.call(self._w, 'xview', 'scroll', number, what) |
| 2557 | def yview(self, *what): |
| 2558 | """Query and change vertical position of the view.""" |
| 2559 | if not what: |
| 2560 | return self._getdoubles(self.tk.call(self._w, 'yview')) |
| 2561 | self.tk.call((self._w, 'yview') + what) |
| 2562 | def yview_moveto(self, fraction): |
| 2563 | """Adjust the view in the window so that FRACTION of the |
| 2564 | total width of the entry is off-screen to the top.""" |
| 2565 | self.tk.call(self._w, 'yview', 'moveto', fraction) |
| 2566 | def yview_scroll(self, number, what): |
| 2567 | """Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT).""" |
| 2568 | self.tk.call(self._w, 'yview', 'scroll', number, what) |
Guido van Rossum | 09f1ad8 | 2001-09-05 19:29:56 +0000 | [diff] [blame] | 2569 | def itemcget(self, index, option): |
| 2570 | """Return the resource value for an ITEM and an OPTION.""" |
| 2571 | return self.tk.call( |
| 2572 | (self._w, 'itemcget') + (index, '-'+option)) |
Guido van Rossum | a0adb92 | 2001-09-01 18:29:55 +0000 | [diff] [blame] | 2573 | def itemconfigure(self, index, cnf=None, **kw): |
Guido van Rossum | 09f1ad8 | 2001-09-05 19:29:56 +0000 | [diff] [blame] | 2574 | """Configure resources of an ITEM. |
Guido van Rossum | a0adb92 | 2001-09-01 18:29:55 +0000 | [diff] [blame] | 2575 | |
| 2576 | The values for resources are specified as keyword arguments. |
| 2577 | To get an overview about the allowed keyword arguments |
| 2578 | call the method without arguments. |
| 2579 | Valid resource names: background, bg, foreground, fg, |
| 2580 | selectbackground, selectforeground.""" |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 2581 | return self._configure(('itemconfigure', index), cnf, kw) |
Guido van Rossum | a0adb92 | 2001-09-01 18:29:55 +0000 | [diff] [blame] | 2582 | itemconfig = itemconfigure |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2583 | |
| 2584 | class Menu(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2585 | """Menu widget which allows to display menu bars, pull-down menus and pop-up menus.""" |
| 2586 | def __init__(self, master=None, cnf={}, **kw): |
| 2587 | """Construct menu widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2588 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2589 | Valid resource names: activebackground, activeborderwidth, |
| 2590 | activeforeground, background, bd, bg, borderwidth, cursor, |
| 2591 | disabledforeground, fg, font, foreground, postcommand, relief, |
| 2592 | selectcolor, takefocus, tearoff, tearoffcommand, title, type.""" |
| 2593 | Widget.__init__(self, master, 'menu', cnf, kw) |
| 2594 | def tk_bindForTraversal(self): |
| 2595 | pass # obsolete since Tk 4.0 |
| 2596 | def tk_mbPost(self): |
| 2597 | self.tk.call('tk_mbPost', self._w) |
| 2598 | def tk_mbUnpost(self): |
| 2599 | self.tk.call('tk_mbUnpost') |
| 2600 | def tk_traverseToMenu(self, char): |
| 2601 | self.tk.call('tk_traverseToMenu', self._w, char) |
| 2602 | def tk_traverseWithinMenu(self, char): |
| 2603 | self.tk.call('tk_traverseWithinMenu', self._w, char) |
| 2604 | def tk_getMenuButtons(self): |
| 2605 | return self.tk.call('tk_getMenuButtons', self._w) |
| 2606 | def tk_nextMenu(self, count): |
| 2607 | self.tk.call('tk_nextMenu', count) |
| 2608 | def tk_nextMenuEntry(self, count): |
| 2609 | self.tk.call('tk_nextMenuEntry', count) |
| 2610 | def tk_invokeMenu(self): |
| 2611 | self.tk.call('tk_invokeMenu', self._w) |
| 2612 | def tk_firstMenu(self): |
| 2613 | self.tk.call('tk_firstMenu', self._w) |
| 2614 | def tk_mbButtonDown(self): |
| 2615 | self.tk.call('tk_mbButtonDown', self._w) |
| 2616 | def tk_popup(self, x, y, entry=""): |
| 2617 | """Post the menu at position X,Y with entry ENTRY.""" |
| 2618 | self.tk.call('tk_popup', self._w, x, y, entry) |
| 2619 | def activate(self, index): |
| 2620 | """Activate entry at INDEX.""" |
| 2621 | self.tk.call(self._w, 'activate', index) |
| 2622 | def add(self, itemType, cnf={}, **kw): |
| 2623 | """Internal function.""" |
| 2624 | self.tk.call((self._w, 'add', itemType) + |
| 2625 | self._options(cnf, kw)) |
| 2626 | def add_cascade(self, cnf={}, **kw): |
| 2627 | """Add hierarchical menu item.""" |
| 2628 | self.add('cascade', cnf or kw) |
| 2629 | def add_checkbutton(self, cnf={}, **kw): |
| 2630 | """Add checkbutton menu item.""" |
| 2631 | self.add('checkbutton', cnf or kw) |
| 2632 | def add_command(self, cnf={}, **kw): |
| 2633 | """Add command menu item.""" |
| 2634 | self.add('command', cnf or kw) |
| 2635 | def add_radiobutton(self, cnf={}, **kw): |
| 2636 | """Addd radio menu item.""" |
| 2637 | self.add('radiobutton', cnf or kw) |
| 2638 | def add_separator(self, cnf={}, **kw): |
| 2639 | """Add separator.""" |
| 2640 | self.add('separator', cnf or kw) |
| 2641 | def insert(self, index, itemType, cnf={}, **kw): |
| 2642 | """Internal function.""" |
| 2643 | self.tk.call((self._w, 'insert', index, itemType) + |
| 2644 | self._options(cnf, kw)) |
| 2645 | def insert_cascade(self, index, cnf={}, **kw): |
| 2646 | """Add hierarchical menu item at INDEX.""" |
| 2647 | self.insert(index, 'cascade', cnf or kw) |
| 2648 | def insert_checkbutton(self, index, cnf={}, **kw): |
| 2649 | """Add checkbutton menu item at INDEX.""" |
| 2650 | self.insert(index, 'checkbutton', cnf or kw) |
| 2651 | def insert_command(self, index, cnf={}, **kw): |
| 2652 | """Add command menu item at INDEX.""" |
| 2653 | self.insert(index, 'command', cnf or kw) |
| 2654 | def insert_radiobutton(self, index, cnf={}, **kw): |
| 2655 | """Addd radio menu item at INDEX.""" |
| 2656 | self.insert(index, 'radiobutton', cnf or kw) |
| 2657 | def insert_separator(self, index, cnf={}, **kw): |
| 2658 | """Add separator at INDEX.""" |
| 2659 | self.insert(index, 'separator', cnf or kw) |
| 2660 | def delete(self, index1, index2=None): |
| 2661 | """Delete menu items between INDEX1 and INDEX2 (not included).""" |
| 2662 | self.tk.call(self._w, 'delete', index1, index2) |
| 2663 | def entrycget(self, index, option): |
| 2664 | """Return the resource value of an menu item for OPTION at INDEX.""" |
| 2665 | return self.tk.call(self._w, 'entrycget', index, '-' + option) |
| 2666 | def entryconfigure(self, index, cnf=None, **kw): |
| 2667 | """Configure a menu item at INDEX.""" |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 2668 | return self._configure(('entryconfigure', index), cnf, kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2669 | entryconfig = entryconfigure |
| 2670 | def index(self, index): |
| 2671 | """Return the index of a menu item identified by INDEX.""" |
| 2672 | i = self.tk.call(self._w, 'index', index) |
| 2673 | if i == 'none': return None |
| 2674 | return getint(i) |
| 2675 | def invoke(self, index): |
| 2676 | """Invoke a menu item identified by INDEX and execute |
| 2677 | the associated command.""" |
| 2678 | return self.tk.call(self._w, 'invoke', index) |
| 2679 | def post(self, x, y): |
| 2680 | """Display a menu at position X,Y.""" |
| 2681 | self.tk.call(self._w, 'post', x, y) |
| 2682 | def type(self, index): |
| 2683 | """Return the type of the menu item at INDEX.""" |
| 2684 | return self.tk.call(self._w, 'type', index) |
| 2685 | def unpost(self): |
| 2686 | """Unmap a menu.""" |
| 2687 | self.tk.call(self._w, 'unpost') |
| 2688 | def yposition(self, index): |
| 2689 | """Return the y-position of the topmost pixel of the menu item at INDEX.""" |
| 2690 | return getint(self.tk.call( |
| 2691 | self._w, 'yposition', index)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2692 | |
| 2693 | class Menubutton(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2694 | """Menubutton widget, obsolete since Tk8.0.""" |
| 2695 | def __init__(self, master=None, cnf={}, **kw): |
| 2696 | Widget.__init__(self, master, 'menubutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2697 | |
| 2698 | class Message(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2699 | """Message widget to display multiline text. Obsolete since Label does it too.""" |
| 2700 | def __init__(self, master=None, cnf={}, **kw): |
| 2701 | Widget.__init__(self, master, 'message', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2702 | |
| 2703 | class Radiobutton(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2704 | """Radiobutton widget which shows only one of several buttons in on-state.""" |
| 2705 | def __init__(self, master=None, cnf={}, **kw): |
| 2706 | """Construct a radiobutton widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2707 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2708 | Valid resource names: activebackground, activeforeground, anchor, |
| 2709 | background, bd, bg, bitmap, borderwidth, command, cursor, |
| 2710 | disabledforeground, fg, font, foreground, height, |
| 2711 | highlightbackground, highlightcolor, highlightthickness, image, |
| 2712 | indicatoron, justify, padx, pady, relief, selectcolor, selectimage, |
| 2713 | state, takefocus, text, textvariable, underline, value, variable, |
| 2714 | width, wraplength.""" |
| 2715 | Widget.__init__(self, master, 'radiobutton', cnf, kw) |
| 2716 | def deselect(self): |
| 2717 | """Put the button in off-state.""" |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2718 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2719 | self.tk.call(self._w, 'deselect') |
| 2720 | def flash(self): |
| 2721 | """Flash the button.""" |
| 2722 | self.tk.call(self._w, 'flash') |
| 2723 | def invoke(self): |
| 2724 | """Toggle the button and invoke a command if given as resource.""" |
| 2725 | return self.tk.call(self._w, 'invoke') |
| 2726 | def select(self): |
| 2727 | """Put the button in on-state.""" |
| 2728 | self.tk.call(self._w, 'select') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2729 | |
| 2730 | class Scale(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2731 | """Scale widget which can display a numerical scale.""" |
| 2732 | def __init__(self, master=None, cnf={}, **kw): |
| 2733 | """Construct a scale widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2734 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2735 | Valid resource names: activebackground, background, bigincrement, bd, |
| 2736 | bg, borderwidth, command, cursor, digits, fg, font, foreground, from, |
| 2737 | highlightbackground, highlightcolor, highlightthickness, label, |
| 2738 | length, orient, relief, repeatdelay, repeatinterval, resolution, |
| 2739 | showvalue, sliderlength, sliderrelief, state, takefocus, |
| 2740 | tickinterval, to, troughcolor, variable, width.""" |
| 2741 | Widget.__init__(self, master, 'scale', cnf, kw) |
| 2742 | def get(self): |
| 2743 | """Get the current value as integer or float.""" |
| 2744 | value = self.tk.call(self._w, 'get') |
| 2745 | try: |
| 2746 | return getint(value) |
| 2747 | except ValueError: |
| 2748 | return getdouble(value) |
| 2749 | def set(self, value): |
| 2750 | """Set the value to VALUE.""" |
| 2751 | self.tk.call(self._w, 'set', value) |
| 2752 | def coords(self, value=None): |
| 2753 | """Return a tuple (X,Y) of the point along the centerline of the |
| 2754 | trough that corresponds to VALUE or the current value if None is |
| 2755 | given.""" |
| 2756 | |
| 2757 | return self._getints(self.tk.call(self._w, 'coords', value)) |
| 2758 | def identify(self, x, y): |
| 2759 | """Return where the point X,Y lies. Valid return values are "slider", |
| 2760 | "though1" and "though2".""" |
| 2761 | return self.tk.call(self._w, 'identify', x, y) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2762 | |
| 2763 | class Scrollbar(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2764 | """Scrollbar widget which displays a slider at a certain position.""" |
| 2765 | def __init__(self, master=None, cnf={}, **kw): |
| 2766 | """Construct a scrollbar widget with the parent MASTER. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2767 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2768 | Valid resource names: activebackground, activerelief, |
| 2769 | background, bd, bg, borderwidth, command, cursor, |
| 2770 | elementborderwidth, highlightbackground, |
| 2771 | highlightcolor, highlightthickness, jump, orient, |
| 2772 | relief, repeatdelay, repeatinterval, takefocus, |
| 2773 | troughcolor, width.""" |
| 2774 | Widget.__init__(self, master, 'scrollbar', cnf, kw) |
| 2775 | def activate(self, index): |
| 2776 | """Display the element at INDEX with activebackground and activerelief. |
| 2777 | INDEX can be "arrow1","slider" or "arrow2".""" |
| 2778 | self.tk.call(self._w, 'activate', index) |
| 2779 | def delta(self, deltax, deltay): |
| 2780 | """Return the fractional change of the scrollbar setting if it |
| 2781 | would be moved by DELTAX or DELTAY pixels.""" |
| 2782 | return getdouble( |
| 2783 | self.tk.call(self._w, 'delta', deltax, deltay)) |
| 2784 | def fraction(self, x, y): |
| 2785 | """Return the fractional value which corresponds to a slider |
| 2786 | position of X,Y.""" |
| 2787 | return getdouble(self.tk.call(self._w, 'fraction', x, y)) |
| 2788 | def identify(self, x, y): |
| 2789 | """Return the element under position X,Y as one of |
| 2790 | "arrow1","slider","arrow2" or "".""" |
| 2791 | return self.tk.call(self._w, 'identify', x, y) |
| 2792 | def get(self): |
| 2793 | """Return the current fractional values (upper and lower end) |
| 2794 | of the slider position.""" |
| 2795 | return self._getdoubles(self.tk.call(self._w, 'get')) |
| 2796 | def set(self, *args): |
| 2797 | """Set the fractional values of the slider position (upper and |
| 2798 | lower ends as value between 0 and 1).""" |
| 2799 | self.tk.call((self._w, 'set') + args) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2800 | |
| 2801 | |
| 2802 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2803 | class Text(Widget): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2804 | """Text widget which can display text in various forms.""" |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2805 | def __init__(self, master=None, cnf={}, **kw): |
| 2806 | """Construct a text widget with the parent MASTER. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2807 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2808 | STANDARD OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2809 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2810 | background, borderwidth, cursor, |
| 2811 | exportselection, font, foreground, |
| 2812 | highlightbackground, highlightcolor, |
| 2813 | highlightthickness, insertbackground, |
| 2814 | insertborderwidth, insertofftime, |
| 2815 | insertontime, insertwidth, padx, pady, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2816 | relief, selectbackground, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2817 | selectborderwidth, selectforeground, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2818 | setgrid, takefocus, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2819 | xscrollcommand, yscrollcommand, |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 2820 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2821 | WIDGET-SPECIFIC OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2822 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2823 | autoseparators, height, maxundo, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2824 | spacing1, spacing2, spacing3, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2825 | state, tabs, undo, width, wrap, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2826 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2827 | """ |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2828 | Widget.__init__(self, master, 'text', cnf, kw) |
| 2829 | def bbox(self, *args): |
| 2830 | """Return a tuple of (x,y,width,height) which gives the bounding |
| 2831 | box of the visible part of the character at the index in ARGS.""" |
| 2832 | return self._getints( |
| 2833 | self.tk.call((self._w, 'bbox') + args)) or None |
| 2834 | def tk_textSelectTo(self, index): |
| 2835 | self.tk.call('tk_textSelectTo', self._w, index) |
| 2836 | def tk_textBackspace(self): |
| 2837 | self.tk.call('tk_textBackspace', self._w) |
| 2838 | def tk_textIndexCloser(self, a, b, c): |
| 2839 | self.tk.call('tk_textIndexCloser', self._w, a, b, c) |
| 2840 | def tk_textResetAnchor(self, index): |
| 2841 | self.tk.call('tk_textResetAnchor', self._w, index) |
| 2842 | def compare(self, index1, op, index2): |
| 2843 | """Return whether between index INDEX1 and index INDEX2 the |
| 2844 | relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=.""" |
| 2845 | return self.tk.getboolean(self.tk.call( |
| 2846 | self._w, 'compare', index1, op, index2)) |
| 2847 | def debug(self, boolean=None): |
| 2848 | """Turn on the internal consistency checks of the B-Tree inside the text |
| 2849 | widget according to BOOLEAN.""" |
| 2850 | return self.tk.getboolean(self.tk.call( |
| 2851 | self._w, 'debug', boolean)) |
| 2852 | def delete(self, index1, index2=None): |
| 2853 | """Delete the characters between INDEX1 and INDEX2 (not included).""" |
| 2854 | self.tk.call(self._w, 'delete', index1, index2) |
| 2855 | def dlineinfo(self, index): |
| 2856 | """Return tuple (x,y,width,height,baseline) giving the bounding box |
| 2857 | and baseline position of the visible part of the line containing |
| 2858 | the character at INDEX.""" |
| 2859 | return self._getints(self.tk.call(self._w, 'dlineinfo', index)) |
Guido van Rossum | 256705b | 2002-04-23 13:29:43 +0000 | [diff] [blame] | 2860 | def dump(self, index1, index2=None, command=None, **kw): |
| 2861 | """Return the contents of the widget between index1 and index2. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2862 | |
Guido van Rossum | 256705b | 2002-04-23 13:29:43 +0000 | [diff] [blame] | 2863 | The type of contents returned in filtered based on the keyword |
| 2864 | parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are |
| 2865 | given and true, then the corresponding items are returned. The result |
| 2866 | is a list of triples of the form (key, value, index). If none of the |
| 2867 | keywords are true then 'all' is used by default. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2868 | |
Guido van Rossum | 256705b | 2002-04-23 13:29:43 +0000 | [diff] [blame] | 2869 | If the 'command' argument is given, it is called once for each element |
| 2870 | of the list of triples, with the values of each triple serving as the |
| 2871 | arguments to the function. In this case the list is not returned.""" |
| 2872 | args = [] |
| 2873 | func_name = None |
| 2874 | result = None |
| 2875 | if not command: |
| 2876 | # Never call the dump command without the -command flag, since the |
| 2877 | # output could involve Tcl quoting and would be a pain to parse |
| 2878 | # right. Instead just set the command to build a list of triples |
| 2879 | # as if we had done the parsing. |
| 2880 | result = [] |
| 2881 | def append_triple(key, value, index, result=result): |
| 2882 | result.append((key, value, index)) |
| 2883 | command = append_triple |
| 2884 | try: |
| 2885 | if not isinstance(command, str): |
| 2886 | func_name = command = self._register(command) |
| 2887 | args += ["-command", command] |
| 2888 | for key in kw: |
| 2889 | if kw[key]: args.append("-" + key) |
| 2890 | args.append(index1) |
| 2891 | if index2: |
| 2892 | args.append(index2) |
| 2893 | self.tk.call(self._w, "dump", *args) |
| 2894 | return result |
| 2895 | finally: |
| 2896 | if func_name: |
| 2897 | self.deletecommand(func_name) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2898 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2899 | ## new in tk8.4 |
| 2900 | def edit(self, *args): |
| 2901 | """Internal method |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2902 | |
| 2903 | This method controls the undo mechanism and |
| 2904 | the modified flag. The exact behavior of the |
| 2905 | command depends on the option argument that |
| 2906 | follows the edit argument. The following forms |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2907 | of the command are currently supported: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2908 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2909 | edit_modified, edit_redo, edit_reset, edit_separator |
| 2910 | and edit_undo |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2911 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2912 | """ |
| 2913 | return self._getints( |
| 2914 | self.tk.call((self._w, 'edit') + args)) or () |
| 2915 | |
| 2916 | def edit_modified(self, arg=None): |
| 2917 | """Get or Set the modified flag |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2918 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2919 | If arg is not specified, returns the modified |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2920 | flag of the widget. The insert, delete, edit undo and |
| 2921 | edit redo commands or the user can set or clear the |
| 2922 | modified flag. If boolean is specified, sets the |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2923 | modified flag of the widget to arg. |
| 2924 | """ |
| 2925 | return self.edit("modified", arg) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2926 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2927 | def edit_redo(self): |
| 2928 | """Redo the last undone edit |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2929 | |
| 2930 | When the undo option is true, reapplies the last |
| 2931 | undone edits provided no other edits were done since |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2932 | then. Generates an error when the redo stack is empty. |
| 2933 | Does nothing when the undo option is false. |
| 2934 | """ |
| 2935 | return self.edit("redo") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2936 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2937 | def edit_reset(self): |
| 2938 | """Clears the undo and redo stacks |
| 2939 | """ |
| 2940 | return self.edit("reset") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2941 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2942 | def edit_separator(self): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2943 | """Inserts a separator (boundary) on the undo stack. |
| 2944 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2945 | Does nothing when the undo option is false |
| 2946 | """ |
| 2947 | return self.edit("separator") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2948 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2949 | def edit_undo(self): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2950 | """Undoes the last edit action |
| 2951 | |
| 2952 | If the undo option is true. An edit action is defined |
| 2953 | as all the insert and delete commands that are recorded |
| 2954 | on the undo stack in between two separators. Generates |
| 2955 | an error when the undo stack is empty. Does nothing |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 2956 | when the undo option is false |
| 2957 | """ |
| 2958 | return self.edit("undo") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2959 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2960 | def get(self, index1, index2=None): |
| 2961 | """Return the text from INDEX1 to INDEX2 (not included).""" |
| 2962 | return self.tk.call(self._w, 'get', index1, index2) |
| 2963 | # (Image commands are new in 8.0) |
| 2964 | def image_cget(self, index, option): |
| 2965 | """Return the value of OPTION of an embedded image at INDEX.""" |
| 2966 | if option[:1] != "-": |
| 2967 | option = "-" + option |
| 2968 | if option[-1:] == "_": |
| 2969 | option = option[:-1] |
| 2970 | return self.tk.call(self._w, "image", "cget", index, option) |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 2971 | def image_configure(self, index, cnf=None, **kw): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2972 | """Configure an embedded image at INDEX.""" |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 2973 | return self._configure(('image', 'configure', index), cnf, kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2974 | def image_create(self, index, cnf={}, **kw): |
| 2975 | """Create an embedded image at INDEX.""" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 2976 | return self.tk.call( |
| 2977 | self._w, "image", "create", index, |
| 2978 | *self._options(cnf, kw)) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 2979 | def image_names(self): |
| 2980 | """Return all names of embedded images in this widget.""" |
| 2981 | return self.tk.call(self._w, "image", "names") |
| 2982 | def index(self, index): |
| 2983 | """Return the index in the form line.char for INDEX.""" |
| 2984 | return self.tk.call(self._w, 'index', index) |
| 2985 | def insert(self, index, chars, *args): |
| 2986 | """Insert CHARS before the characters at INDEX. An additional |
| 2987 | tag can be given in ARGS. Additional CHARS and tags can follow in ARGS.""" |
| 2988 | self.tk.call((self._w, 'insert', index, chars) + args) |
| 2989 | def mark_gravity(self, markName, direction=None): |
| 2990 | """Change the gravity of a mark MARKNAME to DIRECTION (LEFT or RIGHT). |
| 2991 | Return the current value if None is given for DIRECTION.""" |
| 2992 | return self.tk.call( |
| 2993 | (self._w, 'mark', 'gravity', markName, direction)) |
| 2994 | def mark_names(self): |
| 2995 | """Return all mark names.""" |
| 2996 | return self.tk.splitlist(self.tk.call( |
| 2997 | self._w, 'mark', 'names')) |
| 2998 | def mark_set(self, markName, index): |
| 2999 | """Set mark MARKNAME before the character at INDEX.""" |
| 3000 | self.tk.call(self._w, 'mark', 'set', markName, index) |
| 3001 | def mark_unset(self, *markNames): |
| 3002 | """Delete all marks in MARKNAMES.""" |
| 3003 | self.tk.call((self._w, 'mark', 'unset') + markNames) |
| 3004 | def mark_next(self, index): |
| 3005 | """Return the name of the next mark after INDEX.""" |
| 3006 | return self.tk.call(self._w, 'mark', 'next', index) or None |
| 3007 | def mark_previous(self, index): |
| 3008 | """Return the name of the previous mark before INDEX.""" |
| 3009 | return self.tk.call(self._w, 'mark', 'previous', index) or None |
| 3010 | def scan_mark(self, x, y): |
| 3011 | """Remember the current X, Y coordinates.""" |
| 3012 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 3013 | def scan_dragto(self, x, y): |
| 3014 | """Adjust the view of the text to 10 times the |
| 3015 | difference between X and Y and the coordinates given in |
| 3016 | scan_mark.""" |
| 3017 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 3018 | def search(self, pattern, index, stopindex=None, |
| 3019 | forwards=None, backwards=None, exact=None, |
| 3020 | regexp=None, nocase=None, count=None): |
| 3021 | """Search PATTERN beginning from INDEX until STOPINDEX. |
| 3022 | Return the index of the first character of a match or an empty string.""" |
| 3023 | args = [self._w, 'search'] |
| 3024 | if forwards: args.append('-forwards') |
| 3025 | if backwards: args.append('-backwards') |
| 3026 | if exact: args.append('-exact') |
| 3027 | if regexp: args.append('-regexp') |
| 3028 | if nocase: args.append('-nocase') |
| 3029 | if count: args.append('-count'); args.append(count) |
| 3030 | if pattern[0] == '-': args.append('--') |
| 3031 | args.append(pattern) |
| 3032 | args.append(index) |
| 3033 | if stopindex: args.append(stopindex) |
| 3034 | return self.tk.call(tuple(args)) |
| 3035 | def see(self, index): |
| 3036 | """Scroll such that the character at INDEX is visible.""" |
| 3037 | self.tk.call(self._w, 'see', index) |
| 3038 | def tag_add(self, tagName, index1, *args): |
| 3039 | """Add tag TAGNAME to all characters between INDEX1 and index2 in ARGS. |
| 3040 | Additional pairs of indices may follow in ARGS.""" |
| 3041 | self.tk.call( |
| 3042 | (self._w, 'tag', 'add', tagName, index1) + args) |
| 3043 | def tag_unbind(self, tagName, sequence, funcid=None): |
| 3044 | """Unbind for all characters with TAGNAME for event SEQUENCE the |
| 3045 | function identified with FUNCID.""" |
| 3046 | self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '') |
| 3047 | if funcid: |
| 3048 | self.deletecommand(funcid) |
| 3049 | def tag_bind(self, tagName, sequence, func, add=None): |
| 3050 | """Bind to all characters with TAGNAME at event SEQUENCE a call to function FUNC. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 3051 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3052 | An additional boolean parameter ADD specifies whether FUNC will be |
| 3053 | called additionally to the other bound function or whether it will |
| 3054 | replace the previous function. See bind for the return value.""" |
| 3055 | return self._bind((self._w, 'tag', 'bind', tagName), |
| 3056 | sequence, func, add) |
| 3057 | def tag_cget(self, tagName, option): |
| 3058 | """Return the value of OPTION for tag TAGNAME.""" |
| 3059 | if option[:1] != '-': |
| 3060 | option = '-' + option |
| 3061 | if option[-1:] == '_': |
| 3062 | option = option[:-1] |
| 3063 | return self.tk.call(self._w, 'tag', 'cget', tagName, option) |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 3064 | def tag_configure(self, tagName, cnf=None, **kw): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3065 | """Configure a tag TAGNAME.""" |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 3066 | return self._configure(('tag', 'configure', tagName), cnf, kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3067 | tag_config = tag_configure |
| 3068 | def tag_delete(self, *tagNames): |
| 3069 | """Delete all tags in TAGNAMES.""" |
| 3070 | self.tk.call((self._w, 'tag', 'delete') + tagNames) |
| 3071 | def tag_lower(self, tagName, belowThis=None): |
| 3072 | """Change the priority of tag TAGNAME such that it is lower |
| 3073 | than the priority of BELOWTHIS.""" |
| 3074 | self.tk.call(self._w, 'tag', 'lower', tagName, belowThis) |
| 3075 | def tag_names(self, index=None): |
| 3076 | """Return a list of all tag names.""" |
| 3077 | return self.tk.splitlist( |
| 3078 | self.tk.call(self._w, 'tag', 'names', index)) |
| 3079 | def tag_nextrange(self, tagName, index1, index2=None): |
| 3080 | """Return a list of start and end index for the first sequence of |
| 3081 | characters between INDEX1 and INDEX2 which all have tag TAGNAME. |
| 3082 | The text is searched forward from INDEX1.""" |
| 3083 | return self.tk.splitlist(self.tk.call( |
| 3084 | self._w, 'tag', 'nextrange', tagName, index1, index2)) |
| 3085 | def tag_prevrange(self, tagName, index1, index2=None): |
| 3086 | """Return a list of start and end index for the first sequence of |
| 3087 | characters between INDEX1 and INDEX2 which all have tag TAGNAME. |
| 3088 | The text is searched backwards from INDEX1.""" |
| 3089 | return self.tk.splitlist(self.tk.call( |
| 3090 | self._w, 'tag', 'prevrange', tagName, index1, index2)) |
| 3091 | def tag_raise(self, tagName, aboveThis=None): |
| 3092 | """Change the priority of tag TAGNAME such that it is higher |
| 3093 | than the priority of ABOVETHIS.""" |
| 3094 | self.tk.call( |
| 3095 | self._w, 'tag', 'raise', tagName, aboveThis) |
| 3096 | def tag_ranges(self, tagName): |
| 3097 | """Return a list of ranges of text which have tag TAGNAME.""" |
| 3098 | return self.tk.splitlist(self.tk.call( |
| 3099 | self._w, 'tag', 'ranges', tagName)) |
| 3100 | def tag_remove(self, tagName, index1, index2=None): |
| 3101 | """Remove tag TAGNAME from all characters between INDEX1 and INDEX2.""" |
| 3102 | self.tk.call( |
| 3103 | self._w, 'tag', 'remove', tagName, index1, index2) |
| 3104 | def window_cget(self, index, option): |
| 3105 | """Return the value of OPTION of an embedded window at INDEX.""" |
| 3106 | if option[:1] != '-': |
| 3107 | option = '-' + option |
| 3108 | if option[-1:] == '_': |
| 3109 | option = option[:-1] |
| 3110 | return self.tk.call(self._w, 'window', 'cget', index, option) |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 3111 | def window_configure(self, index, cnf=None, **kw): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3112 | """Configure an embedded window at INDEX.""" |
Martin v. Löwis | 6ce1315 | 2002-10-10 14:36:13 +0000 | [diff] [blame] | 3113 | return self._configure(('window', 'configure', index), cnf, kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3114 | window_config = window_configure |
| 3115 | def window_create(self, index, cnf={}, **kw): |
| 3116 | """Create a window at INDEX.""" |
| 3117 | self.tk.call( |
| 3118 | (self._w, 'window', 'create', index) |
| 3119 | + self._options(cnf, kw)) |
| 3120 | def window_names(self): |
| 3121 | """Return all names of embedded windows in this widget.""" |
| 3122 | return self.tk.splitlist( |
| 3123 | self.tk.call(self._w, 'window', 'names')) |
| 3124 | def xview(self, *what): |
| 3125 | """Query and change horizontal position of the view.""" |
| 3126 | if not what: |
| 3127 | return self._getdoubles(self.tk.call(self._w, 'xview')) |
| 3128 | self.tk.call((self._w, 'xview') + what) |
Fredrik Lundh | 5bd2cd6 | 2000-08-09 18:29:51 +0000 | [diff] [blame] | 3129 | def xview_moveto(self, fraction): |
| 3130 | """Adjusts the view in the window so that FRACTION of the |
| 3131 | total width of the canvas is off-screen to the left.""" |
| 3132 | self.tk.call(self._w, 'xview', 'moveto', fraction) |
| 3133 | def xview_scroll(self, number, what): |
| 3134 | """Shift the x-view according to NUMBER which is measured |
| 3135 | in "units" or "pages" (WHAT).""" |
| 3136 | self.tk.call(self._w, 'xview', 'scroll', number, what) |
Fredrik Lundh | 8fffa20 | 2000-08-09 18:51:01 +0000 | [diff] [blame] | 3137 | def yview(self, *what): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3138 | """Query and change vertical position of the view.""" |
Fredrik Lundh | 8fffa20 | 2000-08-09 18:51:01 +0000 | [diff] [blame] | 3139 | if not what: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3140 | return self._getdoubles(self.tk.call(self._w, 'yview')) |
Fredrik Lundh | 8fffa20 | 2000-08-09 18:51:01 +0000 | [diff] [blame] | 3141 | self.tk.call((self._w, 'yview') + what) |
Fredrik Lundh | 5bd2cd6 | 2000-08-09 18:29:51 +0000 | [diff] [blame] | 3142 | def yview_moveto(self, fraction): |
| 3143 | """Adjusts the view in the window so that FRACTION of the |
| 3144 | total height of the canvas is off-screen to the top.""" |
| 3145 | self.tk.call(self._w, 'yview', 'moveto', fraction) |
| 3146 | def yview_scroll(self, number, what): |
| 3147 | """Shift the y-view according to NUMBER which is measured |
| 3148 | in "units" or "pages" (WHAT).""" |
| 3149 | self.tk.call(self._w, 'yview', 'scroll', number, what) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3150 | def yview_pickplace(self, *what): |
| 3151 | """Obsolete function, use see.""" |
| 3152 | self.tk.call((self._w, 'yview', '-pickplace') + what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3153 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3154 | |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 3155 | class _setit: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3156 | """Internal class. It wraps the command in the widget OptionMenu.""" |
| 3157 | def __init__(self, var, value, callback=None): |
| 3158 | self.__value = value |
| 3159 | self.__var = var |
| 3160 | self.__callback = callback |
| 3161 | def __call__(self, *args): |
| 3162 | self.__var.set(self.__value) |
| 3163 | if self.__callback: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3164 | self.__callback(self.__value, *args) |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 3165 | |
| 3166 | class OptionMenu(Menubutton): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3167 | """OptionMenu which allows the user to select a value from a menu.""" |
| 3168 | def __init__(self, master, variable, value, *values, **kwargs): |
| 3169 | """Construct an optionmenu widget with the parent MASTER, with |
| 3170 | the resource textvariable set to VARIABLE, the initially selected |
| 3171 | value VALUE, the other menu values VALUES and an additional |
| 3172 | keyword argument command.""" |
| 3173 | kw = {"borderwidth": 2, "textvariable": variable, |
| 3174 | "indicatoron": 1, "relief": RAISED, "anchor": "c", |
| 3175 | "highlightthickness": 2} |
| 3176 | Widget.__init__(self, master, "menubutton", kw) |
| 3177 | self.widgetName = 'tk_optionMenu' |
| 3178 | menu = self.__menu = Menu(self, name="menu", tearoff=0) |
| 3179 | self.menuname = menu._w |
| 3180 | # 'command' is the only supported keyword |
| 3181 | callback = kwargs.get('command') |
Guido van Rossum | e014a13 | 2006-08-19 16:53:45 +0000 | [diff] [blame] | 3182 | if 'command' in kwargs: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3183 | del kwargs['command'] |
| 3184 | if kwargs: |
| 3185 | raise TclError, 'unknown option -'+kwargs.keys()[0] |
| 3186 | menu.add_command(label=value, |
| 3187 | command=_setit(variable, value, callback)) |
| 3188 | for v in values: |
| 3189 | menu.add_command(label=v, |
| 3190 | command=_setit(variable, v, callback)) |
| 3191 | self["menu"] = menu |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 3192 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3193 | def __getitem__(self, name): |
| 3194 | if name == 'menu': |
| 3195 | return self.__menu |
| 3196 | return Widget.__getitem__(self, name) |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 3197 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3198 | def destroy(self): |
| 3199 | """Destroy this widget and the associated menu.""" |
| 3200 | Menubutton.destroy(self) |
| 3201 | self.__menu = None |
Guido van Rossum | bf4d8f9 | 1995-09-01 20:35:37 +0000 | [diff] [blame] | 3202 | |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 3203 | class Image: |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3204 | """Base class for images.""" |
Martin v. Löwis | 0d8ce61 | 2000-09-08 16:28:30 +0000 | [diff] [blame] | 3205 | _last_id = 0 |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3206 | def __init__(self, imgtype, name=None, cnf={}, master=None, **kw): |
| 3207 | self.name = None |
| 3208 | if not master: |
| 3209 | master = _default_root |
| 3210 | if not master: |
| 3211 | raise RuntimeError, 'Too early to create image' |
| 3212 | self.tk = master.tk |
| 3213 | if not name: |
Martin v. Löwis | 0d8ce61 | 2000-09-08 16:28:30 +0000 | [diff] [blame] | 3214 | Image._last_id += 1 |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 3215 | name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x> |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3216 | # The following is needed for systems where id(x) |
| 3217 | # can return a negative number, such as Linux/m68k: |
| 3218 | if name[0] == '-': name = '_' + name[1:] |
| 3219 | if kw and cnf: cnf = _cnfmerge((cnf, kw)) |
| 3220 | elif kw: cnf = kw |
| 3221 | options = () |
| 3222 | for k, v in cnf.items(): |
| 3223 | if callable(v): |
| 3224 | v = self._register(v) |
| 3225 | options = options + ('-'+k, v) |
| 3226 | self.tk.call(('image', 'create', imgtype, name,) + options) |
| 3227 | self.name = name |
| 3228 | def __str__(self): return self.name |
| 3229 | def __del__(self): |
| 3230 | if self.name: |
| 3231 | try: |
| 3232 | self.tk.call('image', 'delete', self.name) |
| 3233 | except TclError: |
| 3234 | # May happen if the root was destroyed |
| 3235 | pass |
| 3236 | def __setitem__(self, key, value): |
| 3237 | self.tk.call(self.name, 'configure', '-'+key, value) |
| 3238 | def __getitem__(self, key): |
| 3239 | return self.tk.call(self.name, 'configure', '-'+key) |
| 3240 | def configure(self, **kw): |
| 3241 | """Configure the image.""" |
| 3242 | res = () |
| 3243 | for k, v in _cnfmerge(kw).items(): |
| 3244 | if v is not None: |
| 3245 | if k[-1] == '_': k = k[:-1] |
| 3246 | if callable(v): |
| 3247 | v = self._register(v) |
| 3248 | res = res + ('-'+k, v) |
| 3249 | self.tk.call((self.name, 'config') + res) |
| 3250 | config = configure |
| 3251 | def height(self): |
| 3252 | """Return the height of the image.""" |
| 3253 | return getint( |
| 3254 | self.tk.call('image', 'height', self.name)) |
| 3255 | def type(self): |
| 3256 | """Return the type of the imgage, e.g. "photo" or "bitmap".""" |
| 3257 | return self.tk.call('image', 'type', self.name) |
| 3258 | def width(self): |
| 3259 | """Return the width of the image.""" |
| 3260 | return getint( |
| 3261 | self.tk.call('image', 'width', self.name)) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 3262 | |
| 3263 | class PhotoImage(Image): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3264 | """Widget which can display colored images in GIF, PPM/PGM format.""" |
| 3265 | def __init__(self, name=None, cnf={}, master=None, **kw): |
| 3266 | """Create an image with NAME. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 3267 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3268 | Valid resource names: data, format, file, gamma, height, palette, |
| 3269 | width.""" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3270 | Image.__init__(self, 'photo', name, cnf, master, **kw) |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3271 | def blank(self): |
| 3272 | """Display a transparent image.""" |
| 3273 | self.tk.call(self.name, 'blank') |
| 3274 | def cget(self, option): |
| 3275 | """Return the value of OPTION.""" |
| 3276 | return self.tk.call(self.name, 'cget', '-' + option) |
| 3277 | # XXX config |
| 3278 | def __getitem__(self, key): |
| 3279 | return self.tk.call(self.name, 'cget', '-' + key) |
| 3280 | # XXX copy -from, -to, ...? |
| 3281 | def copy(self): |
| 3282 | """Return a new PhotoImage with the same image as this widget.""" |
| 3283 | destImage = PhotoImage() |
| 3284 | self.tk.call(destImage, 'copy', self.name) |
| 3285 | return destImage |
| 3286 | def zoom(self,x,y=''): |
| 3287 | """Return a new PhotoImage with the same image as this widget |
| 3288 | but zoom it with X and Y.""" |
| 3289 | destImage = PhotoImage() |
| 3290 | if y=='': y=x |
| 3291 | self.tk.call(destImage, 'copy', self.name, '-zoom',x,y) |
| 3292 | return destImage |
| 3293 | def subsample(self,x,y=''): |
| 3294 | """Return a new PhotoImage based on the same image as this widget |
| 3295 | but use only every Xth or Yth pixel.""" |
| 3296 | destImage = PhotoImage() |
| 3297 | if y=='': y=x |
| 3298 | self.tk.call(destImage, 'copy', self.name, '-subsample',x,y) |
| 3299 | return destImage |
| 3300 | def get(self, x, y): |
| 3301 | """Return the color (red, green, blue) of the pixel at X,Y.""" |
| 3302 | return self.tk.call(self.name, 'get', x, y) |
| 3303 | def put(self, data, to=None): |
| 3304 | """Put row formated colors to image starting from |
| 3305 | position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))""" |
| 3306 | args = (self.name, 'put', data) |
| 3307 | if to: |
| 3308 | if to[0] == '-to': |
| 3309 | to = to[1:] |
| 3310 | args = args + ('-to',) + tuple(to) |
| 3311 | self.tk.call(args) |
| 3312 | # XXX read |
| 3313 | def write(self, filename, format=None, from_coords=None): |
| 3314 | """Write image to file FILENAME in FORMAT starting from |
| 3315 | position FROM_COORDS.""" |
| 3316 | args = (self.name, 'write', filename) |
| 3317 | if format: |
| 3318 | args = args + ('-format', format) |
| 3319 | if from_coords: |
| 3320 | args = args + ('-from',) + tuple(from_coords) |
| 3321 | self.tk.call(args) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 3322 | |
| 3323 | class BitmapImage(Image): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3324 | """Widget which can display a bitmap.""" |
| 3325 | def __init__(self, name=None, cnf={}, master=None, **kw): |
| 3326 | """Create a bitmap with NAME. |
Guido van Rossum | 5917ecb | 2000-06-29 16:30:50 +0000 | [diff] [blame] | 3327 | |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3328 | Valid resource names: background, data, file, foreground, maskdata, maskfile.""" |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3329 | Image.__init__(self, 'bitmap', name, cnf, master, **kw) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 3330 | |
| 3331 | def image_names(): return _default_root.tk.call('image', 'names') |
| 3332 | def image_types(): return _default_root.tk.call('image', 'types') |
| 3333 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3334 | |
| 3335 | class Spinbox(Widget): |
| 3336 | """spinbox widget.""" |
| 3337 | def __init__(self, master=None, cnf={}, **kw): |
| 3338 | """Construct a spinbox widget with the parent MASTER. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3339 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3340 | STANDARD OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3341 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3342 | activebackground, background, borderwidth, |
| 3343 | cursor, exportselection, font, foreground, |
| 3344 | highlightbackground, highlightcolor, |
| 3345 | highlightthickness, insertbackground, |
| 3346 | insertborderwidth, insertofftime, |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3347 | insertontime, insertwidth, justify, relief, |
| 3348 | repeatdelay, repeatinterval, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3349 | selectbackground, selectborderwidth |
| 3350 | selectforeground, takefocus, textvariable |
| 3351 | xscrollcommand. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3352 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3353 | WIDGET-SPECIFIC OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3354 | |
| 3355 | buttonbackground, buttoncursor, |
| 3356 | buttondownrelief, buttonuprelief, |
| 3357 | command, disabledbackground, |
| 3358 | disabledforeground, format, from, |
| 3359 | invalidcommand, increment, |
| 3360 | readonlybackground, state, to, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3361 | validate, validatecommand values, |
| 3362 | width, wrap, |
| 3363 | """ |
| 3364 | Widget.__init__(self, master, 'spinbox', cnf, kw) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3365 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3366 | def bbox(self, index): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3367 | """Return a tuple of X1,Y1,X2,Y2 coordinates for a |
| 3368 | rectangle which encloses the character given by index. |
| 3369 | |
| 3370 | The first two elements of the list give the x and y |
| 3371 | coordinates of the upper-left corner of the screen |
| 3372 | area covered by the character (in pixels relative |
| 3373 | to the widget) and the last two elements give the |
| 3374 | width and height of the character, in pixels. The |
| 3375 | bounding box may refer to a region outside the |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3376 | visible area of the window. |
| 3377 | """ |
| 3378 | return self.tk.call(self._w, 'bbox', index) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3379 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3380 | def delete(self, first, last=None): |
| 3381 | """Delete one or more elements of the spinbox. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3382 | |
| 3383 | First is the index of the first character to delete, |
| 3384 | and last is the index of the character just after |
| 3385 | the last one to delete. If last isn't specified it |
| 3386 | defaults to first+1, i.e. a single character is |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3387 | deleted. This command returns an empty string. |
| 3388 | """ |
| 3389 | return self.tk.call(self._w, 'delete', first, last) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3390 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3391 | def get(self): |
| 3392 | """Returns the spinbox's string""" |
| 3393 | return self.tk.call(self._w, 'get') |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3394 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3395 | def icursor(self, index): |
| 3396 | """Alter the position of the insertion cursor. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3397 | |
| 3398 | The insertion cursor will be displayed just before |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3399 | the character given by index. Returns an empty string |
| 3400 | """ |
| 3401 | return self.tk.call(self._w, 'icursor', index) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3402 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3403 | def identify(self, x, y): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3404 | """Returns the name of the widget at position x, y |
| 3405 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3406 | Return value is one of: none, buttondown, buttonup, entry |
| 3407 | """ |
| 3408 | return self.tk.call(self._w, 'identify', x, y) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3409 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3410 | def index(self, index): |
| 3411 | """Returns the numerical index corresponding to index |
| 3412 | """ |
| 3413 | return self.tk.call(self._w, 'index', index) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3414 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3415 | def insert(self, index, s): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3416 | """Insert string s at index |
| 3417 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3418 | Returns an empty string. |
| 3419 | """ |
| 3420 | return self.tk.call(self._w, 'insert', index, s) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3421 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3422 | def invoke(self, element): |
| 3423 | """Causes the specified element to be invoked |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3424 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3425 | The element could be buttondown or buttonup |
| 3426 | triggering the action associated with it. |
| 3427 | """ |
| 3428 | return self.tk.call(self._w, 'invoke', element) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3429 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3430 | def scan(self, *args): |
| 3431 | """Internal function.""" |
| 3432 | return self._getints( |
| 3433 | self.tk.call((self._w, 'scan') + args)) or () |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3434 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3435 | def scan_mark(self, x): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3436 | """Records x and the current view in the spinbox window; |
| 3437 | |
| 3438 | used in conjunction with later scan dragto commands. |
| 3439 | Typically this command is associated with a mouse button |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3440 | press in the widget. It returns an empty string. |
| 3441 | """ |
| 3442 | return self.scan("mark", x) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3443 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3444 | def scan_dragto(self, x): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3445 | """Compute the difference between the given x argument |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3446 | and the x argument to the last scan mark command |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3447 | |
| 3448 | It then adjusts the view left or right by 10 times the |
| 3449 | difference in x-coordinates. This command is typically |
| 3450 | associated with mouse motion events in the widget, to |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3451 | produce the effect of dragging the spinbox at high speed |
| 3452 | through the window. The return value is an empty string. |
| 3453 | """ |
| 3454 | return self.scan("dragto", x) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3455 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3456 | def selection(self, *args): |
| 3457 | """Internal function.""" |
| 3458 | return self._getints( |
| 3459 | self.tk.call((self._w, 'selection') + args)) or () |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3460 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3461 | def selection_adjust(self, index): |
| 3462 | """Locate the end of the selection nearest to the character |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3463 | given by index, |
| 3464 | |
| 3465 | Then adjust that end of the selection to be at index |
| 3466 | (i.e including but not going beyond index). The other |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3467 | end of the selection is made the anchor point for future |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3468 | select to commands. If the selection isn't currently in |
| 3469 | the spinbox, then a new selection is created to include |
| 3470 | the characters between index and the most recent selection |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3471 | anchor point, inclusive. Returns an empty string. |
| 3472 | """ |
| 3473 | return self.selection("adjust", index) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3474 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3475 | def selection_clear(self): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3476 | """Clear the selection |
| 3477 | |
| 3478 | If the selection isn't in this widget then the |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3479 | command has no effect. Returns an empty string. |
| 3480 | """ |
| 3481 | return self.selection("clear") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3482 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3483 | def selection_element(self, element=None): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3484 | """Sets or gets the currently selected element. |
| 3485 | |
| 3486 | If a spinbutton element is specified, it will be |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3487 | displayed depressed |
| 3488 | """ |
| 3489 | return self.selection("element", element) |
| 3490 | |
| 3491 | ########################################################################### |
| 3492 | |
| 3493 | class LabelFrame(Widget): |
| 3494 | """labelframe widget.""" |
| 3495 | def __init__(self, master=None, cnf={}, **kw): |
| 3496 | """Construct a labelframe widget with the parent MASTER. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3497 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3498 | STANDARD OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3499 | |
| 3500 | borderwidth, cursor, font, foreground, |
| 3501 | highlightbackground, highlightcolor, |
| 3502 | highlightthickness, padx, pady, relief, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3503 | takefocus, text |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3504 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3505 | WIDGET-SPECIFIC OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3506 | |
| 3507 | background, class, colormap, container, |
| 3508 | height, labelanchor, labelwidget, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3509 | visual, width |
| 3510 | """ |
| 3511 | Widget.__init__(self, master, 'labelframe', cnf, kw) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3512 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3513 | ######################################################################## |
| 3514 | |
| 3515 | class PanedWindow(Widget): |
| 3516 | """panedwindow widget.""" |
| 3517 | def __init__(self, master=None, cnf={}, **kw): |
| 3518 | """Construct a panedwindow widget with the parent MASTER. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3519 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3520 | STANDARD OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3521 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3522 | background, borderwidth, cursor, height, |
| 3523 | orient, relief, width |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3524 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3525 | WIDGET-SPECIFIC OPTIONS |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3526 | |
| 3527 | handlepad, handlesize, opaqueresize, |
| 3528 | sashcursor, sashpad, sashrelief, |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3529 | sashwidth, showhandle, |
| 3530 | """ |
| 3531 | Widget.__init__(self, master, 'panedwindow', cnf, kw) |
| 3532 | |
| 3533 | def add(self, child, **kw): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3534 | """Add a child widget to the panedwindow in a new pane. |
| 3535 | |
| 3536 | The child argument is the name of the child widget |
| 3537 | followed by pairs of arguments that specify how to |
| 3538 | manage the windows. Options may have any of the values |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3539 | accepted by the configure subcommand. |
| 3540 | """ |
| 3541 | self.tk.call((self._w, 'add', child) + self._options(kw)) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3542 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3543 | def remove(self, child): |
| 3544 | """Remove the pane containing child from the panedwindow |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3545 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3546 | All geometry management options for child will be forgotten. |
| 3547 | """ |
| 3548 | self.tk.call(self._w, 'forget', child) |
| 3549 | forget=remove |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3550 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3551 | def identify(self, x, y): |
| 3552 | """Identify the panedwindow component at point x, y |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3553 | |
| 3554 | If the point is over a sash or a sash handle, the result |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3555 | is a two element list containing the index of the sash or |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3556 | handle, and a word indicating whether it is over a sash |
| 3557 | or a handle, such as {0 sash} or {2 handle}. If the point |
| 3558 | is over any other part of the panedwindow, the result is |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3559 | an empty list. |
| 3560 | """ |
| 3561 | return self.tk.call(self._w, 'identify', x, y) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3562 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3563 | def proxy(self, *args): |
| 3564 | """Internal function.""" |
| 3565 | return self._getints( |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3566 | self.tk.call((self._w, 'proxy') + args)) or () |
| 3567 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3568 | def proxy_coord(self): |
| 3569 | """Return the x and y pair of the most recent proxy location |
| 3570 | """ |
| 3571 | return self.proxy("coord") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3572 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3573 | def proxy_forget(self): |
| 3574 | """Remove the proxy from the display. |
| 3575 | """ |
| 3576 | return self.proxy("forget") |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3577 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3578 | def proxy_place(self, x, y): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3579 | """Place the proxy at the given x and y coordinates. |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3580 | """ |
| 3581 | return self.proxy("place", x, y) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3582 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3583 | def sash(self, *args): |
| 3584 | """Internal function.""" |
| 3585 | return self._getints( |
| 3586 | self.tk.call((self._w, 'sash') + args)) or () |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3587 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3588 | def sash_coord(self, index): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3589 | """Return the current x and y pair for the sash given by index. |
| 3590 | |
| 3591 | Index must be an integer between 0 and 1 less than the |
| 3592 | number of panes in the panedwindow. The coordinates given are |
| 3593 | those of the top left corner of the region containing the sash. |
| 3594 | pathName sash dragto index x y This command computes the |
| 3595 | difference between the given coordinates and the coordinates |
| 3596 | given to the last sash coord command for the given sash. It then |
| 3597 | moves that sash the computed difference. The return value is the |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3598 | empty string. |
| 3599 | """ |
| 3600 | return self.sash("coord", index) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3601 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3602 | def sash_mark(self, index): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3603 | """Records x and y for the sash given by index; |
| 3604 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3605 | Used in conjunction with later dragto commands to move the sash. |
| 3606 | """ |
| 3607 | return self.sash("mark", index) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3608 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3609 | def sash_place(self, index, x, y): |
| 3610 | """Place the sash given by index at the given coordinates |
| 3611 | """ |
| 3612 | return self.sash("place", index, x, y) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3613 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3614 | def panecget(self, child, option): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3615 | """Query a management option for window. |
| 3616 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3617 | Option may be any value allowed by the paneconfigure subcommand |
| 3618 | """ |
| 3619 | return self.tk.call( |
| 3620 | (self._w, 'panecget') + (child, '-'+option)) |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3621 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3622 | def paneconfigure(self, tagOrId, cnf=None, **kw): |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3623 | """Query or modify the management options for window. |
| 3624 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3625 | If no option is specified, returns a list describing all |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3626 | of the available options for pathName. If option is |
| 3627 | specified with no value, then the command returns a list |
| 3628 | describing the one named option (this list will be identical |
| 3629 | to the corresponding sublist of the value returned if no |
| 3630 | option is specified). If one or more option-value pairs are |
| 3631 | specified, then the command modifies the given widget |
| 3632 | option(s) to have the given value(s); in this case the |
| 3633 | command returns an empty string. The following options |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3634 | are supported: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3635 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3636 | after window |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3637 | Insert the window after the window specified. window |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3638 | should be the name of a window already managed by pathName. |
| 3639 | before window |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3640 | Insert the window before the window specified. window |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3641 | should be the name of a window already managed by pathName. |
| 3642 | height size |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3643 | Specify a height for the window. The height will be the |
| 3644 | outer dimension of the window including its border, if |
| 3645 | any. If size is an empty string, or if -height is not |
| 3646 | specified, then the height requested internally by the |
| 3647 | window will be used initially; the height may later be |
| 3648 | adjusted by the movement of sashes in the panedwindow. |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3649 | Size may be any value accepted by Tk_GetPixels. |
| 3650 | minsize n |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3651 | Specifies that the size of the window cannot be made |
| 3652 | less than n. This constraint only affects the size of |
| 3653 | the widget in the paned dimension -- the x dimension |
| 3654 | for horizontal panedwindows, the y dimension for |
| 3655 | vertical panedwindows. May be any value accepted by |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3656 | Tk_GetPixels. |
| 3657 | padx n |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3658 | Specifies a non-negative value indicating how much |
| 3659 | extra space to leave on each side of the window in |
| 3660 | the X-direction. The value may have any of the forms |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3661 | accepted by Tk_GetPixels. |
| 3662 | pady n |
| 3663 | Specifies a non-negative value indicating how much |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3664 | extra space to leave on each side of the window in |
| 3665 | the Y-direction. The value may have any of the forms |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3666 | accepted by Tk_GetPixels. |
| 3667 | sticky style |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3668 | If a window's pane is larger than the requested |
| 3669 | dimensions of the window, this option may be used |
| 3670 | to position (or stretch) the window within its pane. |
| 3671 | Style is a string that contains zero or more of the |
| 3672 | characters n, s, e or w. The string can optionally |
| 3673 | contains spaces or commas, but they are ignored. Each |
| 3674 | letter refers to a side (north, south, east, or west) |
| 3675 | that the window will "stick" to. If both n and s |
| 3676 | (or e and w) are specified, the window will be |
| 3677 | stretched to fill the entire height (or width) of |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3678 | its cavity. |
| 3679 | width size |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3680 | Specify a width for the window. The width will be |
| 3681 | the outer dimension of the window including its |
| 3682 | border, if any. If size is an empty string, or |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3683 | if -width is not specified, then the width requested |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3684 | internally by the window will be used initially; the |
| 3685 | width may later be adjusted by the movement of sashes |
| 3686 | in the panedwindow. Size may be any value accepted by |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3687 | Tk_GetPixels. |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 3688 | |
Martin v. Löwis | 2ec3627 | 2002-10-13 10:22:08 +0000 | [diff] [blame] | 3689 | """ |
| 3690 | if cnf is None and not kw: |
| 3691 | cnf = {} |
| 3692 | for x in self.tk.split( |
| 3693 | self.tk.call(self._w, |
| 3694 | 'paneconfigure', tagOrId)): |
| 3695 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 3696 | return cnf |
| 3697 | if type(cnf) == StringType and not kw: |
| 3698 | x = self.tk.split(self.tk.call( |
| 3699 | self._w, 'paneconfigure', tagOrId, '-'+cnf)) |
| 3700 | return (x[0][1:],) + x[1:] |
| 3701 | self.tk.call((self._w, 'paneconfigure', tagOrId) + |
| 3702 | self._options(cnf, kw)) |
| 3703 | paneconfig = paneconfigure |
| 3704 | |
| 3705 | def panes(self): |
| 3706 | """Returns an ordered list of the child panes.""" |
| 3707 | return self.tk.call(self._w, 'panes') |
| 3708 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 3709 | ###################################################################### |
| 3710 | # Extensions: |
| 3711 | |
| 3712 | class Studbutton(Button): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3713 | def __init__(self, master=None, cnf={}, **kw): |
| 3714 | Widget.__init__(self, master, 'studbutton', cnf, kw) |
| 3715 | self.bind('<Any-Enter>', self.tkButtonEnter) |
| 3716 | self.bind('<Any-Leave>', self.tkButtonLeave) |
| 3717 | self.bind('<1>', self.tkButtonDown) |
| 3718 | self.bind('<ButtonRelease-1>', self.tkButtonUp) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 3719 | |
| 3720 | class Tributton(Button): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3721 | def __init__(self, master=None, cnf={}, **kw): |
| 3722 | Widget.__init__(self, master, 'tributton', cnf, kw) |
| 3723 | self.bind('<Any-Enter>', self.tkButtonEnter) |
| 3724 | self.bind('<Any-Leave>', self.tkButtonLeave) |
| 3725 | self.bind('<1>', self.tkButtonDown) |
| 3726 | self.bind('<ButtonRelease-1>', self.tkButtonUp) |
| 3727 | self['fg'] = self['bg'] |
| 3728 | self['activebackground'] = self['bg'] |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 3729 | |
Guido van Rossum | c417ef8 | 1996-08-21 23:38:59 +0000 | [diff] [blame] | 3730 | ###################################################################### |
| 3731 | # Test: |
| 3732 | |
| 3733 | def _test(): |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3734 | root = Tk() |
| 3735 | text = "This is Tcl/Tk version %s" % TclVersion |
| 3736 | if TclVersion >= 8.1: |
Fredrik Lundh | 8fffa20 | 2000-08-09 18:51:01 +0000 | [diff] [blame] | 3737 | try: |
| 3738 | text = text + unicode("\nThis should be a cedilla: \347", |
| 3739 | "iso-8859-1") |
| 3740 | except NameError: |
| 3741 | pass # no unicode support |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3742 | label = Label(root, text=text) |
| 3743 | label.pack() |
| 3744 | test = Button(root, text="Click me!", |
| 3745 | command=lambda root=root: root.test.configure( |
| 3746 | text="[%s]" % root.test['text'])) |
| 3747 | test.pack() |
| 3748 | root.test = test |
| 3749 | quit = Button(root, text="QUIT", command=root.destroy) |
| 3750 | quit.pack() |
| 3751 | # The following three commands are needed so the window pops |
| 3752 | # up on top on Windows... |
| 3753 | root.iconify() |
| 3754 | root.update() |
| 3755 | root.deiconify() |
| 3756 | root.mainloop() |
Guido van Rossum | c417ef8 | 1996-08-21 23:38:59 +0000 | [diff] [blame] | 3757 | |
| 3758 | if __name__ == '__main__': |
Fredrik Lundh | 06d2815 | 2000-08-09 18:03:12 +0000 | [diff] [blame] | 3759 | _test() |