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