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