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