Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1 | # Tkinter.py -- Tk/Tcl widget wrappers |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 3 | __version__ = "$Revision$" |
| 4 | |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 5 | import _tkinter # If this fails your Python is not configured for Tk |
| 6 | tkinter = _tkinter # b/w compat for export |
| 7 | TclError = _tkinter.TclError |
Guido van Rossum | 7e9394a | 1995-03-17 16:21:33 +0000 | [diff] [blame] | 8 | from types import * |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 9 | from Tkconstants import * |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 10 | import string; _string = string; del string |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 12 | TkVersion = _string.atof(_tkinter.TK_VERSION) |
| 13 | TclVersion = _string.atof(_tkinter.TCL_VERSION) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | d6615ab | 1997-08-05 02:35:01 +0000 | [diff] [blame] | 15 | READABLE = _tkinter.READABLE |
| 16 | WRITABLE = _tkinter.WRITABLE |
| 17 | EXCEPTION = _tkinter.EXCEPTION |
Guido van Rossum | f53c86c | 1997-08-14 14:15:54 +0000 | [diff] [blame] | 18 | |
| 19 | # These are not always defined, e.g. not on Win32 with Tk 8.0 :-( |
| 20 | try: _tkinter.createfilehandler |
| 21 | except AttributeError: _tkinter.createfilehandler = None |
| 22 | try: _tkinter.deletefilehandler |
| 23 | except AttributeError: _tkinter.deletefilehandler = None |
Guido van Rossum | 3626999 | 1996-05-16 17:11:27 +0000 | [diff] [blame] | 24 | |
| 25 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 26 | def _flatten(tuple): |
| 27 | res = () |
| 28 | for item in tuple: |
| 29 | if type(item) in (TupleType, ListType): |
| 30 | res = res + _flatten(item) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 31 | elif item is not None: |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 32 | res = res + (item,) |
| 33 | return res |
| 34 | |
| 35 | def _cnfmerge(cnfs): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 36 | if type(cnfs) is DictionaryType: |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 37 | return cnfs |
| 38 | elif type(cnfs) in (NoneType, StringType): |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 39 | return cnfs |
| 40 | else: |
| 41 | cnf = {} |
| 42 | for c in _flatten(cnfs): |
Guido van Rossum | 65c78e1 | 1997-07-19 20:02:04 +0000 | [diff] [blame] | 43 | try: |
| 44 | cnf.update(c) |
| 45 | except (AttributeError, TypeError), msg: |
| 46 | print "_cnfmerge: fallback due to:", msg |
| 47 | for k, v in c.items(): |
| 48 | cnf[k] = v |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 49 | return cnf |
| 50 | |
| 51 | class Event: |
| 52 | pass |
| 53 | |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 54 | _support_default_root = 1 |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 55 | _default_root = None |
| 56 | |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 57 | def NoDefaultRoot(): |
| 58 | global _support_default_root |
| 59 | _support_default_root = 0 |
| 60 | del _default_root |
| 61 | |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 62 | def _tkerror(err): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 63 | pass |
| 64 | |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 65 | def _exit(code='0'): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 66 | raise SystemExit, code |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 68 | _varnum = 0 |
| 69 | class Variable: |
Guido van Rossum | e1a7a3b | 1996-09-05 16:45:49 +0000 | [diff] [blame] | 70 | _default = "" |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 71 | def __init__(self, master=None): |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 72 | global _varnum |
Guido van Rossum | e2c6e20 | 1998-01-14 16:44:34 +0000 | [diff] [blame] | 73 | if not master: |
| 74 | master = _default_root |
| 75 | self._master = master |
| 76 | self._tk = master.tk |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 77 | self._name = 'PY_VAR' + `_varnum` |
| 78 | _varnum = _varnum + 1 |
Guido van Rossum | e1a7a3b | 1996-09-05 16:45:49 +0000 | [diff] [blame] | 79 | self.set(self._default) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 80 | def __del__(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 81 | self._tk.globalunsetvar(self._name) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 82 | def __str__(self): |
| 83 | return self._name |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 84 | def set(self, value): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 85 | return self._tk.globalsetvar(self._name, value) |
Guido van Rossum | e2c6e20 | 1998-01-14 16:44:34 +0000 | [diff] [blame] | 86 | def trace_variable(self, mode, callback): |
| 87 | cbname = self._master._register(callback) |
| 88 | self._tk.call("trace", "variable", self._name, mode, cbname) |
| 89 | return cbname |
| 90 | trace = trace_variable |
| 91 | def trace_vdelete(self, mode, cbname): |
| 92 | self._tk.call("trace", "vdelete", self._name, mode, cbname) |
Guido van Rossum | 0001a11 | 1998-02-19 21:20:30 +0000 | [diff] [blame] | 93 | self._master.deletecommand(cbname) |
Guido van Rossum | e2c6e20 | 1998-01-14 16:44:34 +0000 | [diff] [blame] | 94 | def trace_vinfo(self): |
| 95 | return map(self._tk.split, self._tk.splitlist( |
| 96 | self._tk.call("trace", "vinfo", self._name))) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 97 | |
| 98 | class StringVar(Variable): |
Guido van Rossum | e1a7a3b | 1996-09-05 16:45:49 +0000 | [diff] [blame] | 99 | _default = "" |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 100 | def __init__(self, master=None): |
| 101 | Variable.__init__(self, master) |
| 102 | def get(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 103 | return self._tk.globalgetvar(self._name) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 104 | |
| 105 | class IntVar(Variable): |
Guido van Rossum | 0b96b94 | 1996-12-27 15:30:20 +0000 | [diff] [blame] | 106 | _default = 0 |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 107 | def __init__(self, master=None): |
| 108 | Variable.__init__(self, master) |
| 109 | def get(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 110 | return self._tk.getint(self._tk.globalgetvar(self._name)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 111 | |
| 112 | class DoubleVar(Variable): |
Guido van Rossum | 0b96b94 | 1996-12-27 15:30:20 +0000 | [diff] [blame] | 113 | _default = 0.0 |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 114 | def __init__(self, master=None): |
| 115 | Variable.__init__(self, master) |
| 116 | def get(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 117 | return self._tk.getdouble(self._tk.globalgetvar(self._name)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 118 | |
| 119 | class BooleanVar(Variable): |
Guido van Rossum | e1a7a3b | 1996-09-05 16:45:49 +0000 | [diff] [blame] | 120 | _default = "false" |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 121 | def __init__(self, master=None): |
| 122 | Variable.__init__(self, master) |
| 123 | def get(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 124 | return self._tk.getboolean(self._tk.globalgetvar(self._name)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 126 | def mainloop(n=0): |
| 127 | _default_root.tk.mainloop(n) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 128 | |
| 129 | def getint(s): |
| 130 | return _default_root.tk.getint(s) |
| 131 | |
| 132 | def getdouble(s): |
| 133 | return _default_root.tk.getdouble(s) |
| 134 | |
| 135 | def getboolean(s): |
| 136 | return _default_root.tk.getboolean(s) |
| 137 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 138 | # Methods defined on both toplevel and interior widgets |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 139 | class Misc: |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 140 | # XXX font command? |
Fred Drake | 526749b | 1997-05-03 04:16:23 +0000 | [diff] [blame] | 141 | _tclCommands = None |
Guido van Rossum | 103cc6d | 1997-04-14 13:30:24 +0000 | [diff] [blame] | 142 | def destroy(self): |
| 143 | if self._tclCommands is not None: |
| 144 | for name in self._tclCommands: |
| 145 | #print '- Tkinter: deleted command', name |
| 146 | self.tk.deletecommand(name) |
| 147 | self._tclCommands = None |
| 148 | def deletecommand(self, name): |
| 149 | #print '- Tkinter: deleted command', name |
| 150 | self.tk.deletecommand(name) |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 151 | try: |
| 152 | self._tclCommands.remove(name) |
| 153 | except ValueError: |
| 154 | pass |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 155 | def tk_strictMotif(self, boolean=None): |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 156 | return self.tk.getboolean(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 157 | 'set', 'tk_strictMotif', boolean)) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 158 | def tk_bisque(self): |
| 159 | self.tk.call('tk_bisque') |
| 160 | def tk_setPalette(self, *args, **kw): |
Fred Drake | 3faf9b4 | 1996-10-04 19:23:04 +0000 | [diff] [blame] | 161 | apply(self.tk.call, ('tk_setPalette',) |
| 162 | + _flatten(args) + _flatten(kw.items())) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 163 | def tk_menuBar(self, *args): |
Guido van Rossum | 688bbfc | 1996-09-10 12:39:26 +0000 | [diff] [blame] | 164 | pass # obsolete since Tk 4.0 |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 165 | def wait_variable(self, name='PY_VAR'): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 166 | self.tk.call('tkwait', 'variable', name) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 167 | waitvar = wait_variable # XXX b/w compat |
Guido van Rossum | 9beb932 | 1994-06-27 23:15:31 +0000 | [diff] [blame] | 168 | def wait_window(self, window=None): |
| 169 | if window == None: |
| 170 | window = self |
| 171 | self.tk.call('tkwait', 'window', window._w) |
| 172 | def wait_visibility(self, window=None): |
| 173 | if window == None: |
| 174 | window = self |
| 175 | self.tk.call('tkwait', 'visibility', window._w) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 176 | def setvar(self, name='PY_VAR', value='1'): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 177 | self.tk.setvar(name, value) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 178 | def getvar(self, name='PY_VAR'): |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 179 | return self.tk.getvar(name) |
| 180 | def getint(self, s): |
| 181 | return self.tk.getint(s) |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame] | 182 | def getdouble(self, s): |
| 183 | return self.tk.getdouble(s) |
| 184 | def getboolean(self, s): |
| 185 | return self.tk.getboolean(s) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 186 | def focus_set(self): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 187 | self.tk.call('focus', self._w) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 188 | focus = focus_set # XXX b/w compat? |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 189 | def focus_force(self): |
| 190 | self.tk.call('focus', '-force', self._w) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 191 | def focus_get(self): |
| 192 | name = self.tk.call('focus') |
Guido van Rossum | 5468a7b | 1996-08-08 18:31:42 +0000 | [diff] [blame] | 193 | if name == 'none' or not name: return None |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 194 | return self._nametowidget(name) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 195 | def focus_displayof(self): |
| 196 | name = self.tk.call('focus', '-displayof', self._w) |
| 197 | if name == 'none' or not name: return None |
| 198 | return self._nametowidget(name) |
| 199 | def focus_lastfor(self): |
| 200 | name = self.tk.call('focus', '-lastfor', self._w) |
| 201 | if name == 'none' or not name: return None |
| 202 | return self._nametowidget(name) |
| 203 | def tk_focusFollowsMouse(self): |
| 204 | self.tk.call('tk_focusFollowsMouse') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 205 | def tk_focusNext(self): |
| 206 | name = self.tk.call('tk_focusNext', self._w) |
| 207 | if not name: return None |
| 208 | return self._nametowidget(name) |
| 209 | def tk_focusPrev(self): |
| 210 | name = self.tk.call('tk_focusPrev', self._w) |
| 211 | if not name: return None |
| 212 | return self._nametowidget(name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 213 | def after(self, ms, func=None, *args): |
| 214 | if not func: |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 215 | # I'd rather use time.sleep(ms*0.001) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 216 | self.tk.call('after', ms) |
| 217 | else: |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 218 | # XXX Disgusting hack to clean up after calling func |
| 219 | tmp = [] |
Guido van Rossum | 103cc6d | 1997-04-14 13:30:24 +0000 | [diff] [blame] | 220 | def callit(func=func, args=args, self=self, tmp=tmp): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 221 | try: |
| 222 | apply(func, args) |
| 223 | finally: |
Guido van Rossum | 103cc6d | 1997-04-14 13:30:24 +0000 | [diff] [blame] | 224 | self.deletecommand(tmp[0]) |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 225 | name = self._register(callit) |
| 226 | tmp.append(name) |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 227 | return self.tk.call('after', ms, name) |
| 228 | def after_idle(self, func, *args): |
| 229 | return apply(self.after, ('idle', func) + args) |
| 230 | def after_cancel(self, id): |
| 231 | self.tk.call('after', 'cancel', id) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 232 | def bell(self, displayof=0): |
| 233 | apply(self.tk.call, ('bell',) + self._displayof(displayof)) |
| 234 | # Clipboard handling: |
| 235 | def clipboard_clear(self, **kw): |
| 236 | if not kw.has_key('displayof'): kw['displayof'] = self._w |
| 237 | apply(self.tk.call, |
| 238 | ('clipboard', 'clear') + self._options(kw)) |
| 239 | def clipboard_append(self, string, **kw): |
| 240 | if not kw.has_key('displayof'): kw['displayof'] = self._w |
| 241 | apply(self.tk.call, |
| 242 | ('clipboard', 'append') + self._options(kw) |
| 243 | + ('--', string)) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 244 | # XXX grab current w/o window argument |
| 245 | def grab_current(self): |
| 246 | name = self.tk.call('grab', 'current', self._w) |
| 247 | if not name: return None |
| 248 | return self._nametowidget(name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 249 | def grab_release(self): |
| 250 | self.tk.call('grab', 'release', self._w) |
| 251 | def grab_set(self): |
| 252 | self.tk.call('grab', 'set', self._w) |
| 253 | def grab_set_global(self): |
| 254 | self.tk.call('grab', 'set', '-global', self._w) |
| 255 | def grab_status(self): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 256 | status = self.tk.call('grab', 'status', self._w) |
| 257 | if status == 'none': status = None |
| 258 | return status |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 259 | def lower(self, belowThis=None): |
| 260 | self.tk.call('lower', self._w, belowThis) |
Guido van Rossum | 780044f | 1994-10-20 22:02:27 +0000 | [diff] [blame] | 261 | def option_add(self, pattern, value, priority = None): |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 262 | self.tk.call('option', 'add', pattern, value, priority) |
Guido van Rossum | 780044f | 1994-10-20 22:02:27 +0000 | [diff] [blame] | 263 | def option_clear(self): |
| 264 | self.tk.call('option', 'clear') |
| 265 | def option_get(self, name, className): |
| 266 | return self.tk.call('option', 'get', self._w, name, className) |
| 267 | def option_readfile(self, fileName, priority = None): |
| 268 | self.tk.call('option', 'readfile', fileName, priority) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 269 | def selection_clear(self, **kw): |
| 270 | if not kw.has_key('displayof'): kw['displayof'] = self._w |
| 271 | apply(self.tk.call, ('selection', 'clear') + self._options(kw)) |
| 272 | def selection_get(self, **kw): |
| 273 | if not kw.has_key('displayof'): kw['displayof'] = self._w |
| 274 | return apply(self.tk.call, |
| 275 | ('selection', 'get') + self._options(kw)) |
| 276 | def selection_handle(self, command, **kw): |
| 277 | name = self._register(command) |
| 278 | apply(self.tk.call, |
| 279 | ('selection', 'handle') + self._options(kw) |
| 280 | + (self._w, name)) |
| 281 | def selection_own(self, **kw): |
| 282 | "Become owner of X selection." |
| 283 | apply(self.tk.call, |
| 284 | ('selection', 'own') + self._options(kw) + (self._w,)) |
| 285 | def selection_own_get(self, **kw): |
| 286 | "Find owner of X selection." |
| 287 | if not kw.has_key('displayof'): kw['displayof'] = self._w |
Guido van Rossum | 76f587b | 1997-01-21 23:22:03 +0000 | [diff] [blame] | 288 | name = apply(self.tk.call, |
| 289 | ('selection', 'own') + self._options(kw)) |
| 290 | if not name: return None |
| 291 | return self._nametowidget(name) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 292 | def send(self, interp, cmd, *args): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 293 | return apply(self.tk.call, ('send', interp, cmd) + args) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 294 | def lower(self, belowThis=None): |
Guido van Rossum | 6e8ec59 | 1996-09-11 14:25:41 +0000 | [diff] [blame] | 295 | self.tk.call('lower', self._w, belowThis) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 296 | def tkraise(self, aboveThis=None): |
| 297 | self.tk.call('raise', self._w, aboveThis) |
| 298 | lift = tkraise |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 299 | def colormodel(self, value=None): |
| 300 | return self.tk.call('tk', 'colormodel', self._w, value) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 301 | def winfo_atom(self, name, displayof=0): |
| 302 | args = ('winfo', 'atom') + self._displayof(displayof) + (name,) |
| 303 | return self.tk.getint(apply(self.tk.call, args)) |
| 304 | def winfo_atomname(self, id, displayof=0): |
| 305 | args = ('winfo', 'atomname') \ |
| 306 | + self._displayof(displayof) + (id,) |
| 307 | return apply(self.tk.call, args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 308 | def winfo_cells(self): |
| 309 | return self.tk.getint( |
| 310 | self.tk.call('winfo', 'cells', self._w)) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 311 | def winfo_children(self): |
| 312 | return map(self._nametowidget, |
| 313 | self.tk.splitlist(self.tk.call( |
| 314 | 'winfo', 'children', self._w))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 315 | def winfo_class(self): |
| 316 | return self.tk.call('winfo', 'class', self._w) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 317 | def winfo_colormapfull(self): |
| 318 | return self.tk.getboolean( |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 319 | self.tk.call('winfo', 'colormapfull', self._w)) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 320 | def winfo_containing(self, rootX, rootY, displayof=0): |
| 321 | args = ('winfo', 'containing') \ |
| 322 | + self._displayof(displayof) + (rootX, rootY) |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 323 | name = apply(self.tk.call, args) |
| 324 | if not name: return None |
| 325 | return self._nametowidget(name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 326 | def winfo_depth(self): |
| 327 | return self.tk.getint(self.tk.call('winfo', 'depth', self._w)) |
| 328 | def winfo_exists(self): |
| 329 | return self.tk.getint( |
| 330 | self.tk.call('winfo', 'exists', self._w)) |
| 331 | def winfo_fpixels(self, number): |
| 332 | return self.tk.getdouble(self.tk.call( |
| 333 | 'winfo', 'fpixels', self._w, number)) |
| 334 | def winfo_geometry(self): |
| 335 | return self.tk.call('winfo', 'geometry', self._w) |
| 336 | def winfo_height(self): |
| 337 | return self.tk.getint( |
| 338 | self.tk.call('winfo', 'height', self._w)) |
| 339 | def winfo_id(self): |
| 340 | return self.tk.getint( |
| 341 | self.tk.call('winfo', 'id', self._w)) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 342 | def winfo_interps(self, displayof=0): |
| 343 | args = ('winfo', 'interps') + self._displayof(displayof) |
| 344 | return self.tk.splitlist(apply(self.tk.call, args)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 345 | def winfo_ismapped(self): |
| 346 | return self.tk.getint( |
| 347 | self.tk.call('winfo', 'ismapped', self._w)) |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 348 | def winfo_manager(self): |
| 349 | return self.tk.call('winfo', 'manager', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 350 | def winfo_name(self): |
| 351 | return self.tk.call('winfo', 'name', self._w) |
| 352 | def winfo_parent(self): |
| 353 | return self.tk.call('winfo', 'parent', self._w) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 354 | def winfo_pathname(self, id, displayof=0): |
| 355 | args = ('winfo', 'pathname') \ |
| 356 | + self._displayof(displayof) + (id,) |
| 357 | return apply(self.tk.call, args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 358 | def winfo_pixels(self, number): |
| 359 | return self.tk.getint( |
| 360 | self.tk.call('winfo', 'pixels', self._w, number)) |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 361 | def winfo_pointerx(self): |
| 362 | return self.tk.getint( |
| 363 | self.tk.call('winfo', 'pointerx', self._w)) |
| 364 | def winfo_pointerxy(self): |
| 365 | return self._getints( |
| 366 | self.tk.call('winfo', 'pointerxy', self._w)) |
| 367 | def winfo_pointery(self): |
| 368 | return self.tk.getint( |
| 369 | self.tk.call('winfo', 'pointery', self._w)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 370 | def winfo_reqheight(self): |
| 371 | return self.tk.getint( |
| 372 | self.tk.call('winfo', 'reqheight', self._w)) |
| 373 | def winfo_reqwidth(self): |
| 374 | return self.tk.getint( |
| 375 | self.tk.call('winfo', 'reqwidth', self._w)) |
| 376 | def winfo_rgb(self, color): |
| 377 | return self._getints( |
| 378 | self.tk.call('winfo', 'rgb', self._w, color)) |
| 379 | def winfo_rootx(self): |
| 380 | return self.tk.getint( |
| 381 | self.tk.call('winfo', 'rootx', self._w)) |
| 382 | def winfo_rooty(self): |
| 383 | return self.tk.getint( |
| 384 | self.tk.call('winfo', 'rooty', self._w)) |
| 385 | def winfo_screen(self): |
| 386 | return self.tk.call('winfo', 'screen', self._w) |
| 387 | def winfo_screencells(self): |
| 388 | return self.tk.getint( |
| 389 | self.tk.call('winfo', 'screencells', self._w)) |
| 390 | def winfo_screendepth(self): |
| 391 | return self.tk.getint( |
| 392 | self.tk.call('winfo', 'screendepth', self._w)) |
| 393 | def winfo_screenheight(self): |
| 394 | return self.tk.getint( |
| 395 | self.tk.call('winfo', 'screenheight', self._w)) |
| 396 | def winfo_screenmmheight(self): |
| 397 | return self.tk.getint( |
| 398 | self.tk.call('winfo', 'screenmmheight', self._w)) |
| 399 | def winfo_screenmmwidth(self): |
| 400 | return self.tk.getint( |
| 401 | self.tk.call('winfo', 'screenmmwidth', self._w)) |
| 402 | def winfo_screenvisual(self): |
| 403 | return self.tk.call('winfo', 'screenvisual', self._w) |
| 404 | def winfo_screenwidth(self): |
| 405 | return self.tk.getint( |
| 406 | self.tk.call('winfo', 'screenwidth', self._w)) |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 407 | def winfo_server(self): |
| 408 | return self.tk.call('winfo', 'server', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 409 | def winfo_toplevel(self): |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 410 | return self._nametowidget(self.tk.call( |
| 411 | 'winfo', 'toplevel', self._w)) |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 412 | def winfo_viewable(self): |
| 413 | return self.tk.getint( |
| 414 | self.tk.call('winfo', 'viewable', self._w)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 415 | def winfo_visual(self): |
| 416 | return self.tk.call('winfo', 'visual', self._w) |
Guido van Rossum | c0967cd | 1996-12-12 16:43:05 +0000 | [diff] [blame] | 417 | def winfo_visualid(self): |
| 418 | return self.tk.call('winfo', 'visualid', self._w) |
| 419 | def winfo_visualsavailable(self, includeids=0): |
| 420 | data = self.tk.split( |
| 421 | self.tk.call('winfo', 'visualsavailable', self._w, |
| 422 | includeids and 'includeids' or None)) |
| 423 | def parseitem(x, self=self): |
| 424 | return x[:1] + tuple(map(self.tk.getint, x[1:])) |
| 425 | return map(parseitem, data) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 426 | def winfo_vrootheight(self): |
| 427 | return self.tk.getint( |
| 428 | self.tk.call('winfo', 'vrootheight', self._w)) |
| 429 | def winfo_vrootwidth(self): |
| 430 | return self.tk.getint( |
| 431 | self.tk.call('winfo', 'vrootwidth', self._w)) |
| 432 | def winfo_vrootx(self): |
| 433 | return self.tk.getint( |
| 434 | self.tk.call('winfo', 'vrootx', self._w)) |
| 435 | def winfo_vrooty(self): |
| 436 | return self.tk.getint( |
| 437 | self.tk.call('winfo', 'vrooty', self._w)) |
| 438 | def winfo_width(self): |
| 439 | return self.tk.getint( |
| 440 | self.tk.call('winfo', 'width', self._w)) |
| 441 | def winfo_x(self): |
| 442 | return self.tk.getint( |
| 443 | self.tk.call('winfo', 'x', self._w)) |
| 444 | def winfo_y(self): |
| 445 | return self.tk.getint( |
| 446 | self.tk.call('winfo', 'y', self._w)) |
| 447 | def update(self): |
| 448 | self.tk.call('update') |
| 449 | def update_idletasks(self): |
| 450 | self.tk.call('update', 'idletasks') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 451 | def bindtags(self, tagList=None): |
| 452 | if tagList is None: |
| 453 | return self.tk.splitlist( |
| 454 | self.tk.call('bindtags', self._w)) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 455 | else: |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 456 | self.tk.call('bindtags', self._w, tagList) |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 457 | def _bind(self, what, sequence, func, add, needcleanup=1): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 458 | if func: |
| 459 | cmd = ("%sset _tkinter_break [%s %s]\n" |
| 460 | 'if {"$_tkinter_break" == "break"} break\n') \ |
| 461 | % (add and '+' or '', |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 462 | self._register(func, self._substitute, |
| 463 | needcleanup), |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 464 | _string.join(self._subst_format)) |
| 465 | apply(self.tk.call, what + (sequence, cmd)) |
| 466 | elif func == '': |
| 467 | apply(self.tk.call, what + (sequence, func)) |
| 468 | else: |
| 469 | return apply(self.tk.call, what + (sequence,)) |
| 470 | def bind(self, sequence=None, func=None, add=None): |
| 471 | return self._bind(('bind', self._w), sequence, func, add) |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 472 | def unbind(self, sequence): |
| 473 | self.tk.call('bind', self._w, sequence, '') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 474 | def bind_all(self, sequence=None, func=None, add=None): |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 475 | return self._bind(('bind', 'all'), sequence, func, add, 0) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 476 | def unbind_all(self, sequence): |
| 477 | self.tk.call('bind', 'all' , sequence, '') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 478 | def bind_class(self, className, sequence=None, func=None, add=None): |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 479 | return self._bind(('bind', className), sequence, func, add, 0) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 480 | def unbind_class(self, className, sequence): |
| 481 | self.tk.call('bind', className , sequence, '') |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 482 | def mainloop(self, n=0): |
| 483 | self.tk.mainloop(n) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 484 | def quit(self): |
| 485 | self.tk.quit() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 486 | def _getints(self, string): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 487 | if not string: return None |
Guido van Rossum | 7e9394a | 1995-03-17 16:21:33 +0000 | [diff] [blame] | 488 | return tuple(map(self.tk.getint, self.tk.splitlist(string))) |
| 489 | def _getdoubles(self, string): |
| 490 | if not string: return None |
| 491 | return tuple(map(self.tk.getdouble, self.tk.splitlist(string))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 492 | def _getboolean(self, string): |
| 493 | if string: |
| 494 | return self.tk.getboolean(string) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 495 | def _displayof(self, displayof): |
| 496 | if displayof: |
| 497 | return ('-displayof', displayof) |
| 498 | if displayof is None: |
| 499 | return ('-displayof', self._w) |
| 500 | return () |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 501 | def _options(self, cnf, kw = None): |
| 502 | if kw: |
| 503 | cnf = _cnfmerge((cnf, kw)) |
| 504 | else: |
| 505 | cnf = _cnfmerge(cnf) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 506 | res = () |
| 507 | for k, v in cnf.items(): |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 508 | if v is not None: |
| 509 | if k[-1] == '_': k = k[:-1] |
| 510 | if callable(v): |
| 511 | v = self._register(v) |
| 512 | res = res + ('-'+k, v) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 513 | return res |
Guido van Rossum | 98b9d77 | 1997-12-12 00:09:34 +0000 | [diff] [blame] | 514 | def nametowidget(self, name): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 515 | w = self |
| 516 | if name[0] == '.': |
| 517 | w = w._root() |
| 518 | name = name[1:] |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 519 | find = _string.find |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 520 | while name: |
| 521 | i = find(name, '.') |
| 522 | if i >= 0: |
| 523 | name, tail = name[:i], name[i+1:] |
| 524 | else: |
| 525 | tail = '' |
| 526 | w = w.children[name] |
| 527 | name = tail |
| 528 | return w |
Guido van Rossum | 98b9d77 | 1997-12-12 00:09:34 +0000 | [diff] [blame] | 529 | _nametowidget = nametowidget |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 530 | def _register(self, func, subst=None, needcleanup=1): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 531 | f = CallWrapper(func, subst, self).__call__ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 532 | name = `id(f)` |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 533 | try: |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 534 | func = func.im_func |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 535 | except AttributeError: |
| 536 | pass |
| 537 | try: |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 538 | name = name + func.__name__ |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 539 | except AttributeError: |
| 540 | pass |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 541 | self.tk.createcommand(name, f) |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 542 | if needcleanup: |
| 543 | if self._tclCommands is None: |
| 544 | self._tclCommands = [] |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 545 | self._tclCommands.append(name) |
Guido van Rossum | 103cc6d | 1997-04-14 13:30:24 +0000 | [diff] [blame] | 546 | #print '+ Tkinter created command', name |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 547 | return name |
Guido van Rossum | 9beb932 | 1994-06-27 23:15:31 +0000 | [diff] [blame] | 548 | register = _register |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 549 | def _root(self): |
| 550 | w = self |
| 551 | while w.master: w = w.master |
| 552 | return w |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 553 | _subst_format = ('%#', '%b', '%f', '%h', '%k', |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 554 | '%s', '%t', '%w', '%x', '%y', |
| 555 | '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y') |
| 556 | def _substitute(self, *args): |
| 557 | tk = self.tk |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 558 | if len(args) != len(self._subst_format): return args |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 559 | nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args |
| 560 | # Missing: (a, c, d, m, o, v, B, R) |
| 561 | e = Event() |
| 562 | e.serial = tk.getint(nsign) |
| 563 | e.num = tk.getint(b) |
| 564 | try: e.focus = tk.getboolean(f) |
| 565 | except TclError: pass |
| 566 | e.height = tk.getint(h) |
| 567 | e.keycode = tk.getint(k) |
Guido van Rossum | 3626999 | 1996-05-16 17:11:27 +0000 | [diff] [blame] | 568 | # For Visibility events, event state is a string and |
| 569 | # not an integer: |
| 570 | try: |
| 571 | e.state = tk.getint(s) |
| 572 | except TclError: |
| 573 | e.state = s |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 574 | e.time = tk.getint(t) |
| 575 | e.width = tk.getint(w) |
| 576 | e.x = tk.getint(x) |
| 577 | e.y = tk.getint(y) |
| 578 | e.char = A |
| 579 | try: e.send_event = tk.getboolean(E) |
| 580 | except TclError: pass |
| 581 | e.keysym = K |
| 582 | e.keysym_num = tk.getint(N) |
| 583 | e.type = T |
| 584 | e.widget = self._nametowidget(W) |
| 585 | e.x_root = tk.getint(X) |
| 586 | e.y_root = tk.getint(Y) |
| 587 | return (e,) |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 588 | def _report_exception(self): |
| 589 | import sys |
| 590 | exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback |
| 591 | root = self._root() |
| 592 | root.report_callback_exception(exc, val, tb) |
Guido van Rossum | 83bd9a9 | 1997-09-29 23:24:52 +0000 | [diff] [blame] | 593 | # These used to be defined in Widget: |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 594 | def configure(self, cnf=None, **kw): |
Guido van Rossum | 83bd9a9 | 1997-09-29 23:24:52 +0000 | [diff] [blame] | 595 | # XXX ought to generalize this so tag_config etc. can use it |
| 596 | if kw: |
| 597 | cnf = _cnfmerge((cnf, kw)) |
| 598 | elif cnf: |
| 599 | cnf = _cnfmerge(cnf) |
| 600 | if cnf is None: |
| 601 | cnf = {} |
| 602 | for x in self.tk.split( |
| 603 | self.tk.call(self._w, 'configure')): |
| 604 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 605 | return cnf |
| 606 | if type(cnf) is StringType: |
| 607 | x = self.tk.split(self.tk.call( |
| 608 | self._w, 'configure', '-'+cnf)) |
| 609 | return (x[0][1:],) + x[1:] |
| 610 | apply(self.tk.call, (self._w, 'configure') |
| 611 | + self._options(cnf)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 612 | config = configure |
Guido van Rossum | 83bd9a9 | 1997-09-29 23:24:52 +0000 | [diff] [blame] | 613 | def cget(self, key): |
| 614 | return self.tk.call(self._w, 'cget', '-' + key) |
| 615 | __getitem__ = cget |
| 616 | def __setitem__(self, key, value): |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 617 | self.configure({key: value}) |
Guido van Rossum | 83bd9a9 | 1997-09-29 23:24:52 +0000 | [diff] [blame] | 618 | def keys(self): |
| 619 | return map(lambda x: x[0][1:], |
| 620 | self.tk.split(self.tk.call(self._w, 'configure'))) |
| 621 | def __str__(self): |
| 622 | return self._w |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 623 | # Pack methods that apply to the master |
| 624 | _noarg_ = ['_noarg_'] |
| 625 | def pack_propagate(self, flag=_noarg_): |
| 626 | if flag is Misc._noarg_: |
| 627 | return self._getboolean(self.tk.call( |
| 628 | 'pack', 'propagate', self._w)) |
| 629 | else: |
| 630 | self.tk.call('pack', 'propagate', self._w, flag) |
| 631 | propagate = pack_propagate |
| 632 | def pack_slaves(self): |
| 633 | return map(self._nametowidget, |
| 634 | self.tk.splitlist( |
| 635 | self.tk.call('pack', 'slaves', self._w))) |
| 636 | slaves = pack_slaves |
| 637 | # Place method that applies to the master |
| 638 | def place_slaves(self): |
| 639 | return map(self._nametowidget, |
| 640 | self.tk.splitlist( |
| 641 | self.tk.call( |
| 642 | 'place', 'slaves', self._w))) |
| 643 | # Grid methods that apply to the master |
| 644 | def grid_bbox(self, column, row): |
| 645 | return self._getints( |
| 646 | self.tk.call( |
| 647 | 'grid', 'bbox', self._w, column, row)) or None |
| 648 | bbox = grid_bbox |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 649 | def _grid_configure(self, command, index, cnf, kw): |
| 650 | if type(cnf) is StringType and not kw: |
| 651 | if cnf[-1:] == '_': |
| 652 | cnf = cnf[:-1] |
| 653 | if cnf[:1] != '-': |
| 654 | cnf = '-'+cnf |
| 655 | options = (cnf,) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 656 | else: |
| 657 | options = self._options(cnf, kw) |
| 658 | if not options: |
| 659 | res = self.tk.call('grid', |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 660 | command, self._w, index) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 661 | words = self.tk.splitlist(res) |
| 662 | dict = {} |
| 663 | for i in range(0, len(words), 2): |
| 664 | key = words[i][1:] |
| 665 | value = words[i+1] |
| 666 | if not value: |
| 667 | value = None |
| 668 | elif '.' in value: |
| 669 | value = self.tk.getdouble(value) |
| 670 | else: |
| 671 | value = self.tk.getint(value) |
| 672 | dict[key] = value |
| 673 | return dict |
| 674 | res = apply(self.tk.call, |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 675 | ('grid', command, self._w, index) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 676 | + options) |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 677 | if len(options) == 1: |
| 678 | if not res: return None |
| 679 | # In Tk 7.5, -width can be a float |
| 680 | if '.' in res: return self.tk.getdouble(res) |
| 681 | return self.tk.getint(res) |
| 682 | def grid_columnconfigure(self, index, cnf={}, **kw): |
| 683 | return self._grid_configure('columnconfigure', index, cnf, kw) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 684 | columnconfigure = grid_columnconfigure |
| 685 | def grid_propagate(self, flag=_noarg_): |
| 686 | if flag is Misc._noarg_: |
| 687 | return self._getboolean(self.tk.call( |
| 688 | 'grid', 'propagate', self._w)) |
| 689 | else: |
| 690 | self.tk.call('grid', 'propagate', self._w, flag) |
| 691 | def grid_rowconfigure(self, index, cnf={}, **kw): |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 692 | return self._grid_configure('rowconfigure', index, cnf, kw) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 693 | rowconfigure = grid_rowconfigure |
| 694 | def grid_size(self): |
| 695 | return self._getints( |
| 696 | self.tk.call('grid', 'size', self._w)) or None |
| 697 | size = grid_size |
Guido van Rossum | 1cd6a45 | 1997-12-30 04:07:19 +0000 | [diff] [blame] | 698 | def grid_slaves(self, row=None, column=None): |
| 699 | args = () |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 700 | if row: |
| 701 | args = args + ('-row', row) |
| 702 | if column: |
| 703 | args = args + ('-column', column) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 704 | return map(self._nametowidget, |
| 705 | self.tk.splitlist( |
| 706 | apply(self.tk.call, |
| 707 | ('grid', 'slaves', self._w) + args))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 708 | |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 709 | # Support for the "event" command, new in Tk 4.2. |
| 710 | # By Case Roole. |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 711 | # XXX Why is this using the default root? |
Guido van Rossum | 80f8be8 | 1997-12-02 19:51:39 +0000 | [diff] [blame] | 712 | |
| 713 | def event_add(self,virtual, *sequences): |
| 714 | args = ('event', 'add', virtual) + sequences |
| 715 | apply( _default_root.tk.call, args ) |
| 716 | |
| 717 | def event_delete(self,virtual,*sequences): |
| 718 | args = ('event', 'delete', virtual) + sequences |
| 719 | apply( _default_root.tk.call, args ) |
| 720 | |
| 721 | def event_generate(self, sequence, **kw): |
| 722 | args = ('event', 'generate', self._w, sequence) |
| 723 | for k,v in kw.items(): |
| 724 | args = args + ('-%s' % k,str(v)) |
| 725 | apply( _default_root.tk.call, args ) |
| 726 | |
| 727 | def event_info(self,virtual=None): |
| 728 | args = ('event', 'info') |
| 729 | if virtual is not None: args = args + (virtual,) |
| 730 | s = apply( _default_root.tk.call, args ) |
| 731 | return _string.split(s) |
| 732 | |
| 733 | |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 734 | class CallWrapper: |
| 735 | def __init__(self, func, subst, widget): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 736 | self.func = func |
| 737 | self.subst = subst |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 738 | self.widget = widget |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 739 | def __call__(self, *args): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 740 | try: |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 741 | if self.subst: |
| 742 | args = apply(self.subst, args) |
| 743 | return apply(self.func, args) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 744 | except SystemExit, msg: |
| 745 | raise SystemExit, msg |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 746 | except: |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 747 | self.widget._report_exception() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 748 | |
| 749 | class Wm: |
| 750 | def aspect(self, |
| 751 | minNumer=None, minDenom=None, |
| 752 | maxNumer=None, maxDenom=None): |
| 753 | return self._getints( |
| 754 | self.tk.call('wm', 'aspect', self._w, |
| 755 | minNumer, minDenom, |
| 756 | maxNumer, maxDenom)) |
| 757 | def client(self, name=None): |
| 758 | return self.tk.call('wm', 'client', self._w, name) |
Fred Drake | 3c602d7 | 1996-09-27 14:06:54 +0000 | [diff] [blame] | 759 | def colormapwindows(self, *wlist): |
| 760 | args = ('wm', 'colormapwindows', self._w) + _flatten(wlist) |
| 761 | return map(self._nametowidget, apply(self.tk.call, args)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 762 | def command(self, value=None): |
| 763 | return self.tk.call('wm', 'command', self._w, value) |
| 764 | def deiconify(self): |
| 765 | return self.tk.call('wm', 'deiconify', self._w) |
| 766 | def focusmodel(self, model=None): |
| 767 | return self.tk.call('wm', 'focusmodel', self._w, model) |
| 768 | def frame(self): |
| 769 | return self.tk.call('wm', 'frame', self._w) |
| 770 | def geometry(self, newGeometry=None): |
| 771 | return self.tk.call('wm', 'geometry', self._w, newGeometry) |
| 772 | def grid(self, |
Guido van Rossum | 21df8f5 | 1998-02-24 23:26:18 +0000 | [diff] [blame] | 773 | baseWidth=None, baseHeight=None, |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 774 | widthInc=None, heightInc=None): |
| 775 | return self._getints(self.tk.call( |
| 776 | 'wm', 'grid', self._w, |
Guido van Rossum | 4d9d3f1 | 1997-12-27 15:14:43 +0000 | [diff] [blame] | 777 | baseWidth, baseHeight, widthInc, heightInc)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 778 | def group(self, pathName=None): |
| 779 | return self.tk.call('wm', 'group', self._w, pathName) |
| 780 | def iconbitmap(self, bitmap=None): |
| 781 | return self.tk.call('wm', 'iconbitmap', self._w, bitmap) |
| 782 | def iconify(self): |
| 783 | return self.tk.call('wm', 'iconify', self._w) |
| 784 | def iconmask(self, bitmap=None): |
| 785 | return self.tk.call('wm', 'iconmask', self._w, bitmap) |
| 786 | def iconname(self, newName=None): |
| 787 | return self.tk.call('wm', 'iconname', self._w, newName) |
| 788 | def iconposition(self, x=None, y=None): |
| 789 | return self._getints(self.tk.call( |
| 790 | 'wm', 'iconposition', self._w, x, y)) |
| 791 | def iconwindow(self, pathName=None): |
| 792 | return self.tk.call('wm', 'iconwindow', self._w, pathName) |
| 793 | def maxsize(self, width=None, height=None): |
| 794 | return self._getints(self.tk.call( |
| 795 | 'wm', 'maxsize', self._w, width, height)) |
| 796 | def minsize(self, width=None, height=None): |
| 797 | return self._getints(self.tk.call( |
| 798 | 'wm', 'minsize', self._w, width, height)) |
| 799 | def overrideredirect(self, boolean=None): |
| 800 | return self._getboolean(self.tk.call( |
| 801 | 'wm', 'overrideredirect', self._w, boolean)) |
| 802 | def positionfrom(self, who=None): |
| 803 | return self.tk.call('wm', 'positionfrom', self._w, who) |
| 804 | def protocol(self, name=None, func=None): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 805 | if callable(func): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 806 | command = self._register(func) |
| 807 | else: |
| 808 | command = func |
| 809 | return self.tk.call( |
| 810 | 'wm', 'protocol', self._w, name, command) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 811 | def resizable(self, width=None, height=None): |
| 812 | return self.tk.call('wm', 'resizable', self._w, width, height) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 813 | def sizefrom(self, who=None): |
| 814 | return self.tk.call('wm', 'sizefrom', self._w, who) |
| 815 | def state(self): |
| 816 | return self.tk.call('wm', 'state', self._w) |
| 817 | def title(self, string=None): |
| 818 | return self.tk.call('wm', 'title', self._w, string) |
| 819 | def transient(self, master=None): |
| 820 | return self.tk.call('wm', 'transient', self._w, master) |
| 821 | def withdraw(self): |
| 822 | return self.tk.call('wm', 'withdraw', self._w) |
| 823 | |
| 824 | class Tk(Misc, Wm): |
| 825 | _w = '.' |
| 826 | def __init__(self, screenName=None, baseName=None, className='Tk'): |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 827 | global _default_root |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 828 | self.master = None |
| 829 | self.children = {} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 830 | if baseName is None: |
| 831 | import sys, os |
| 832 | baseName = os.path.basename(sys.argv[0]) |
Fred Drake | cab3c3b | 1996-10-06 17:55:20 +0000 | [diff] [blame] | 833 | baseName, ext = os.path.splitext(baseName) |
| 834 | if ext not in ('.py', 'pyc'): baseName = baseName + ext |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 835 | self.tk = _tkinter.create(screenName, baseName, className) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 836 | try: |
| 837 | # Disable event scanning except for Command-Period |
| 838 | import MacOS |
Guido van Rossum | 9d9af2c | 1997-08-12 18:21:08 +0000 | [diff] [blame] | 839 | try: |
| 840 | MacOS.SchedParams(1, 0) |
| 841 | except AttributeError: |
| 842 | # pre-1.5, use old routine |
| 843 | MacOS.EnableAppswitch(0) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 844 | except ImportError: |
| 845 | pass |
| 846 | else: |
| 847 | # Work around nasty MacTk bug |
| 848 | self.update() |
Guido van Rossum | f7f79ac | 1995-10-07 19:08:37 +0000 | [diff] [blame] | 849 | # Version sanity checks |
| 850 | tk_version = self.tk.getvar('tk_version') |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 851 | if tk_version != _tkinter.TK_VERSION: |
Guido van Rossum | f7f79ac | 1995-10-07 19:08:37 +0000 | [diff] [blame] | 852 | raise RuntimeError, \ |
| 853 | "tk.h version (%s) doesn't match libtk.a version (%s)" \ |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 854 | % (_tkinter.TK_VERSION, tk_version) |
Guido van Rossum | f7f79ac | 1995-10-07 19:08:37 +0000 | [diff] [blame] | 855 | tcl_version = self.tk.getvar('tcl_version') |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 856 | if tcl_version != _tkinter.TCL_VERSION: |
Guido van Rossum | f7f79ac | 1995-10-07 19:08:37 +0000 | [diff] [blame] | 857 | raise RuntimeError, \ |
| 858 | "tcl.h version (%s) doesn't match libtcl.a version (%s)" \ |
Guido van Rossum | 9580609 | 1997-02-15 18:33:24 +0000 | [diff] [blame] | 859 | % (_tkinter.TCL_VERSION, tcl_version) |
Guido van Rossum | f7f79ac | 1995-10-07 19:08:37 +0000 | [diff] [blame] | 860 | if TkVersion < 4.0: |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 861 | raise RuntimeError, \ |
| 862 | "Tk 4.0 or higher is required; found Tk %s" \ |
| 863 | % str(TkVersion) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 864 | self.tk.createcommand('tkerror', _tkerror) |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 865 | self.tk.createcommand('exit', _exit) |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 866 | self.readprofile(baseName, className) |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 867 | if _support_default_root and not _default_root: |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 868 | _default_root = self |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 869 | def destroy(self): |
| 870 | for c in self.children.values(): c.destroy() |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 871 | self.tk.call('destroy', self._w) |
Guido van Rossum | 103cc6d | 1997-04-14 13:30:24 +0000 | [diff] [blame] | 872 | Misc.destroy(self) |
Guido van Rossum | d6615ab | 1997-08-05 02:35:01 +0000 | [diff] [blame] | 873 | global _default_root |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 874 | if _support_default_root and _default_root is self: |
Guido van Rossum | d6615ab | 1997-08-05 02:35:01 +0000 | [diff] [blame] | 875 | _default_root = None |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 876 | def readprofile(self, baseName, className): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 877 | import os |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 878 | if os.environ.has_key('HOME'): home = os.environ['HOME'] |
| 879 | else: home = os.curdir |
| 880 | class_tcl = os.path.join(home, '.%s.tcl' % className) |
| 881 | class_py = os.path.join(home, '.%s.py' % className) |
| 882 | base_tcl = os.path.join(home, '.%s.tcl' % baseName) |
| 883 | base_py = os.path.join(home, '.%s.py' % baseName) |
| 884 | dir = {'self': self} |
| 885 | exec 'from Tkinter import *' in dir |
| 886 | if os.path.isfile(class_tcl): |
| 887 | print 'source', `class_tcl` |
| 888 | self.tk.call('source', class_tcl) |
| 889 | if os.path.isfile(class_py): |
| 890 | print 'execfile', `class_py` |
| 891 | execfile(class_py, dir) |
| 892 | if os.path.isfile(base_tcl): |
| 893 | print 'source', `base_tcl` |
| 894 | self.tk.call('source', base_tcl) |
| 895 | if os.path.isfile(base_py): |
| 896 | print 'execfile', `base_py` |
| 897 | execfile(base_py, dir) |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame] | 898 | def report_callback_exception(self, exc, val, tb): |
| 899 | import traceback |
| 900 | print "Exception in Tkinter callback" |
| 901 | traceback.print_exception(exc, val, tb) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 903 | # Ideally, the classes Pack, Place and Grid disappear, the |
| 904 | # pack/place/grid methods are defined on the Widget class, and |
| 905 | # everybody uses w.pack_whatever(...) instead of Pack.whatever(w, |
| 906 | # ...), with pack(), place() and grid() being short for |
| 907 | # pack_configure(), place_configure() and grid_columnconfigure(), and |
| 908 | # forget() being short for pack_forget(). As a practical matter, I'm |
| 909 | # afraid that there is too much code out there that may be using the |
| 910 | # Pack, Place or Grid class, so I leave them intact -- but only as |
| 911 | # backwards compatibility features. Also note that those methods that |
| 912 | # take a master as argument (e.g. pack_propagate) have been moved to |
| 913 | # the Misc class (which now incorporates all methods common between |
| 914 | # toplevel and interior widgets). Again, for compatibility, these are |
| 915 | # copied into the Pack, Place or Grid class. |
| 916 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 917 | class Pack: |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 918 | def pack_configure(self, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 919 | apply(self.tk.call, |
| 920 | ('pack', 'configure', self._w) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 921 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 922 | pack = configure = config = pack_configure |
| 923 | def pack_forget(self): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 924 | self.tk.call('pack', 'forget', self._w) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 925 | forget = pack_forget |
| 926 | def pack_info(self): |
Guido van Rossum | 69170c5 | 1994-07-11 15:21:31 +0000 | [diff] [blame] | 927 | words = self.tk.splitlist( |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 928 | self.tk.call('pack', 'info', self._w)) |
Guido van Rossum | 69170c5 | 1994-07-11 15:21:31 +0000 | [diff] [blame] | 929 | dict = {} |
| 930 | for i in range(0, len(words), 2): |
| 931 | key = words[i][1:] |
| 932 | value = words[i+1] |
Guido van Rossum | a5f875f | 1996-05-16 17:50:07 +0000 | [diff] [blame] | 933 | if value[:1] == '.': |
Guido van Rossum | 69170c5 | 1994-07-11 15:21:31 +0000 | [diff] [blame] | 934 | value = self._nametowidget(value) |
| 935 | dict[key] = value |
| 936 | return dict |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 937 | info = pack_info |
| 938 | propagate = pack_propagate = Misc.pack_propagate |
| 939 | slaves = pack_slaves = Misc.pack_slaves |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 940 | |
| 941 | class Place: |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 942 | def place_configure(self, cnf={}, **kw): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 943 | for k in ['in_']: |
| 944 | if kw.has_key(k): |
| 945 | kw[k[:-1]] = kw[k] |
| 946 | del kw[k] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 947 | apply(self.tk.call, |
| 948 | ('place', 'configure', self._w) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 949 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 950 | place = configure = config = place_configure |
| 951 | def place_forget(self): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 952 | self.tk.call('place', 'forget', self._w) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 953 | forget = place_forget |
| 954 | def place_info(self): |
Guido van Rossum | 63e39ae | 1996-05-16 17:53:48 +0000 | [diff] [blame] | 955 | words = self.tk.splitlist( |
| 956 | self.tk.call('place', 'info', self._w)) |
| 957 | dict = {} |
| 958 | for i in range(0, len(words), 2): |
| 959 | key = words[i][1:] |
| 960 | value = words[i+1] |
| 961 | if value[:1] == '.': |
| 962 | value = self._nametowidget(value) |
| 963 | dict[key] = value |
| 964 | return dict |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 965 | info = place_info |
| 966 | slaves = place_slaves = Misc.place_slaves |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 967 | |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 968 | class Grid: |
Guido van Rossum | 422cc7f | 1996-05-21 20:30:07 +0000 | [diff] [blame] | 969 | # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 970 | def grid_configure(self, cnf={}, **kw): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 971 | apply(self.tk.call, |
| 972 | ('grid', 'configure', self._w) |
| 973 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 974 | grid = configure = config = grid_configure |
| 975 | bbox = grid_bbox = Misc.grid_bbox |
| 976 | columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure |
| 977 | def grid_forget(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 978 | self.tk.call('grid', 'forget', self._w) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 979 | forget = grid_forget |
| 980 | def grid_info(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 981 | words = self.tk.splitlist( |
| 982 | self.tk.call('grid', 'info', self._w)) |
| 983 | dict = {} |
| 984 | for i in range(0, len(words), 2): |
| 985 | key = words[i][1:] |
| 986 | value = words[i+1] |
Guido van Rossum | a5f875f | 1996-05-16 17:50:07 +0000 | [diff] [blame] | 987 | if value[:1] == '.': |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 988 | value = self._nametowidget(value) |
| 989 | dict[key] = value |
| 990 | return dict |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 991 | info = grid_info |
| 992 | def grid_location(self, x, y): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 993 | return self._getints( |
| 994 | self.tk.call( |
| 995 | 'grid', 'location', self._w, x, y)) or None |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 996 | location = grid_location |
| 997 | propagate = grid_propagate = Misc.grid_propagate |
| 998 | rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure |
| 999 | size = grid_size = Misc.grid_size |
| 1000 | slaves = grid_slaves = Misc.grid_slaves |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1001 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1002 | class BaseWidget(Misc): |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1003 | def _setup(self, master, cnf): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 1004 | if _support_default_root: |
| 1005 | global _default_root |
| 1006 | if not master: |
| 1007 | if not _default_root: |
| 1008 | _default_root = Tk() |
| 1009 | master = _default_root |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 1010 | if not _default_root: |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 1011 | _default_root = master |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1012 | self.master = master |
| 1013 | self.tk = master.tk |
Fred Drake | c8296db | 1997-05-27 22:45:10 +0000 | [diff] [blame] | 1014 | name = None |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1015 | if cnf.has_key('name'): |
| 1016 | name = cnf['name'] |
| 1017 | del cnf['name'] |
Fred Drake | c8296db | 1997-05-27 22:45:10 +0000 | [diff] [blame] | 1018 | if not name: |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1019 | name = `id(self)` |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 1020 | self._name = name |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1021 | if master._w=='.': |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1022 | self._w = '.' + name |
| 1023 | else: |
| 1024 | self._w = master._w + '.' + name |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1025 | self.children = {} |
| 1026 | if self.master.children.has_key(self._name): |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 1027 | self.master.children[self._name].destroy() |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1028 | self.master.children[self._name] = self |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1029 | def __init__(self, master, widgetName, cnf={}, kw={}, extra=()): |
| 1030 | if kw: |
| 1031 | cnf = _cnfmerge((cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1032 | self.widgetName = widgetName |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1033 | BaseWidget._setup(self, master, cnf) |
Guido van Rossum | ad8b3ba | 1996-07-21 03:05:05 +0000 | [diff] [blame] | 1034 | classes = [] |
| 1035 | for k in cnf.keys(): |
| 1036 | if type(k) is ClassType: |
| 1037 | classes.append((k, cnf[k])) |
| 1038 | del cnf[k] |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1039 | apply(self.tk.call, |
| 1040 | (widgetName, self._w) + extra + self._options(cnf)) |
Guido van Rossum | ad8b3ba | 1996-07-21 03:05:05 +0000 | [diff] [blame] | 1041 | for k, v in classes: |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1042 | k.configure(self, v) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 1043 | def destroy(self): |
| 1044 | for c in self.children.values(): c.destroy() |
Guido van Rossum | f023ab0 | 1994-08-30 12:13:44 +0000 | [diff] [blame] | 1045 | if self.master.children.has_key(self._name): |
| 1046 | del self.master.children[self._name] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1047 | self.tk.call('destroy', self._w) |
Guido van Rossum | 103cc6d | 1997-04-14 13:30:24 +0000 | [diff] [blame] | 1048 | Misc.destroy(self) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1049 | def _do(self, name, args=()): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1050 | return apply(self.tk.call, (self._w, name) + args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1051 | |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1052 | class Widget(BaseWidget, Pack, Place, Grid): |
| 1053 | pass |
| 1054 | |
| 1055 | class Toplevel(BaseWidget, Wm): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1056 | def __init__(self, master=None, cnf={}, **kw): |
| 1057 | if kw: |
| 1058 | cnf = _cnfmerge((cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1059 | extra = () |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1060 | for wmkey in ['screen', 'class_', 'class', 'visual', |
| 1061 | 'colormap']: |
| 1062 | if cnf.has_key(wmkey): |
| 1063 | val = cnf[wmkey] |
| 1064 | # TBD: a hack needed because some keys |
| 1065 | # are not valid as keyword arguments |
| 1066 | if wmkey[-1] == '_': opt = '-'+wmkey[:-1] |
| 1067 | else: opt = '-'+wmkey |
| 1068 | extra = extra + (opt, val) |
| 1069 | del cnf[wmkey] |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1070 | BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 1071 | root = self._root() |
| 1072 | self.iconname(root.iconname()) |
| 1073 | self.title(root.title()) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1074 | |
| 1075 | class Button(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1076 | def __init__(self, master=None, cnf={}, **kw): |
| 1077 | Widget.__init__(self, master, 'button', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1078 | def tkButtonEnter(self, *dummy): |
| 1079 | self.tk.call('tkButtonEnter', self._w) |
| 1080 | def tkButtonLeave(self, *dummy): |
| 1081 | self.tk.call('tkButtonLeave', self._w) |
| 1082 | def tkButtonDown(self, *dummy): |
| 1083 | self.tk.call('tkButtonDown', self._w) |
| 1084 | def tkButtonUp(self, *dummy): |
| 1085 | self.tk.call('tkButtonUp', self._w) |
Guido van Rossum | 3626999 | 1996-05-16 17:11:27 +0000 | [diff] [blame] | 1086 | def tkButtonInvoke(self, *dummy): |
| 1087 | self.tk.call('tkButtonInvoke', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1088 | def flash(self): |
| 1089 | self.tk.call(self._w, 'flash') |
| 1090 | def invoke(self): |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 1091 | return self.tk.call(self._w, 'invoke') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1092 | |
| 1093 | # Indices: |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1094 | # XXX I don't like these -- take them away |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1095 | def AtEnd(): |
| 1096 | return 'end' |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame] | 1097 | def AtInsert(*args): |
| 1098 | s = 'insert' |
| 1099 | for a in args: |
| 1100 | if a: s = s + (' ' + a) |
| 1101 | return s |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1102 | def AtSelFirst(): |
| 1103 | return 'sel.first' |
| 1104 | def AtSelLast(): |
| 1105 | return 'sel.last' |
| 1106 | def At(x, y=None): |
Guido van Rossum | 5e0c25b | 1994-07-12 09:04:41 +0000 | [diff] [blame] | 1107 | if y is None: |
| 1108 | return '@' + `x` |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1109 | else: |
Guido van Rossum | 5e0c25b | 1994-07-12 09:04:41 +0000 | [diff] [blame] | 1110 | return '@' + `x` + ',' + `y` |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1111 | |
| 1112 | class Canvas(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1113 | def __init__(self, master=None, cnf={}, **kw): |
| 1114 | Widget.__init__(self, master, 'canvas', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1115 | def addtag(self, *args): |
| 1116 | self._do('addtag', args) |
Guido van Rossum | 5c8c91b | 1996-08-22 23:18:09 +0000 | [diff] [blame] | 1117 | def addtag_above(self, newtag, tagOrId): |
| 1118 | self.addtag(newtag, 'above', tagOrId) |
| 1119 | def addtag_all(self, newtag): |
| 1120 | self.addtag(newtag, 'all') |
| 1121 | def addtag_below(self, newtag, tagOrId): |
| 1122 | self.addtag(newtag, 'below', tagOrId) |
| 1123 | def addtag_closest(self, newtag, x, y, halo=None, start=None): |
| 1124 | self.addtag(newtag, 'closest', x, y, halo, start) |
| 1125 | def addtag_enclosed(self, newtag, x1, y1, x2, y2): |
| 1126 | self.addtag(newtag, 'enclosed', x1, y1, x2, y2) |
| 1127 | def addtag_overlapping(self, newtag, x1, y1, x2, y2): |
| 1128 | self.addtag(newtag, 'overlapping', x1, y1, x2, y2) |
| 1129 | def addtag_withtag(self, newtag, tagOrId): |
| 1130 | self.addtag(newtag, 'withtag', tagOrId) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1131 | def bbox(self, *args): |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 1132 | return self._getints(self._do('bbox', args)) or None |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 1133 | def tag_unbind(self, tagOrId, sequence): |
| 1134 | self.tk.call(self._w, 'bind', tagOrId, sequence, '') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1135 | def tag_bind(self, tagOrId, sequence=None, func=None, add=None): |
Guido van Rossum | 421bb0e | 1996-07-21 02:19:32 +0000 | [diff] [blame] | 1136 | return self._bind((self._w, 'bind', tagOrId), |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1137 | sequence, func, add) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1138 | def canvasx(self, screenx, gridspacing=None): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1139 | return self.tk.getdouble(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1140 | self._w, 'canvasx', screenx, gridspacing)) |
| 1141 | def canvasy(self, screeny, gridspacing=None): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1142 | return self.tk.getdouble(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1143 | self._w, 'canvasy', screeny, gridspacing)) |
| 1144 | def coords(self, *args): |
Guido van Rossum | 0001a11 | 1998-02-19 21:20:30 +0000 | [diff] [blame] | 1145 | # XXX Should use _flatten on args |
Guido van Rossum | c8b4791 | 1996-07-30 16:31:32 +0000 | [diff] [blame] | 1146 | return map(self.tk.getdouble, |
Guido van Rossum | 9afdabf | 1996-07-30 20:16:21 +0000 | [diff] [blame] | 1147 | self.tk.splitlist(self._do('coords', args))) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1148 | def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={}) |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 1149 | args = _flatten(args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1150 | cnf = args[-1] |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 1151 | if type(cnf) in (DictionaryType, TupleType): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1152 | args = args[:-1] |
| 1153 | else: |
| 1154 | cnf = {} |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 1155 | return self.tk.getint(apply( |
| 1156 | self.tk.call, |
| 1157 | (self._w, 'create', itemType) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1158 | + args + self._options(cnf, kw))) |
| 1159 | def create_arc(self, *args, **kw): |
| 1160 | return self._create('arc', args, kw) |
| 1161 | def create_bitmap(self, *args, **kw): |
| 1162 | return self._create('bitmap', args, kw) |
| 1163 | def create_image(self, *args, **kw): |
| 1164 | return self._create('image', args, kw) |
| 1165 | def create_line(self, *args, **kw): |
| 1166 | return self._create('line', args, kw) |
| 1167 | def create_oval(self, *args, **kw): |
| 1168 | return self._create('oval', args, kw) |
| 1169 | def create_polygon(self, *args, **kw): |
| 1170 | return self._create('polygon', args, kw) |
| 1171 | def create_rectangle(self, *args, **kw): |
| 1172 | return self._create('rectangle', args, kw) |
| 1173 | def create_text(self, *args, **kw): |
| 1174 | return self._create('text', args, kw) |
| 1175 | def create_window(self, *args, **kw): |
| 1176 | return self._create('window', args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1177 | def dchars(self, *args): |
| 1178 | self._do('dchars', args) |
| 1179 | def delete(self, *args): |
| 1180 | self._do('delete', args) |
| 1181 | def dtag(self, *args): |
| 1182 | self._do('dtag', args) |
| 1183 | def find(self, *args): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1184 | return self._getints(self._do('find', args)) or () |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 1185 | def find_above(self, tagOrId): |
| 1186 | return self.find('above', tagOrId) |
| 1187 | def find_all(self): |
| 1188 | return self.find('all') |
| 1189 | def find_below(self, tagOrId): |
| 1190 | return self.find('below', tagOrId) |
| 1191 | def find_closest(self, x, y, halo=None, start=None): |
| 1192 | return self.find('closest', x, y, halo, start) |
| 1193 | def find_enclosed(self, x1, y1, x2, y2): |
| 1194 | return self.find('enclosed', x1, y1, x2, y2) |
| 1195 | def find_overlapping(self, x1, y1, x2, y2): |
| 1196 | return self.find('overlapping', x1, y1, x2, y2) |
| 1197 | def find_withtag(self, tagOrId): |
| 1198 | return self.find('withtag', tagOrId) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1199 | def focus(self, *args): |
| 1200 | return self._do('focus', args) |
| 1201 | def gettags(self, *args): |
| 1202 | return self.tk.splitlist(self._do('gettags', args)) |
| 1203 | def icursor(self, *args): |
| 1204 | self._do('icursor', args) |
| 1205 | def index(self, *args): |
| 1206 | return self.tk.getint(self._do('index', args)) |
| 1207 | def insert(self, *args): |
| 1208 | self._do('insert', args) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1209 | def itemcget(self, tagOrId, option): |
| 1210 | return self._do('itemcget', (tagOrId, '-'+option)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1211 | def itemconfigure(self, tagOrId, cnf=None, **kw): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1212 | if cnf is None and not kw: |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 1213 | cnf = {} |
| 1214 | for x in self.tk.split( |
Guido van Rossum | 9918e0c | 1997-08-18 14:44:04 +0000 | [diff] [blame] | 1215 | self._do('itemconfigure', (tagOrId,))): |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 1216 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 1217 | return cnf |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1218 | if type(cnf) == StringType and not kw: |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 1219 | x = self.tk.split(self._do('itemconfigure', |
| 1220 | (tagOrId, '-'+cnf,))) |
| 1221 | return (x[0][1:],) + x[1:] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1222 | self._do('itemconfigure', (tagOrId,) |
| 1223 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1224 | itemconfig = itemconfigure |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1225 | def lower(self, *args): |
| 1226 | self._do('lower', args) |
| 1227 | def move(self, *args): |
| 1228 | self._do('move', args) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1229 | def postscript(self, cnf={}, **kw): |
| 1230 | return self._do('postscript', self._options(cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1231 | def tkraise(self, *args): |
| 1232 | self._do('raise', args) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 1233 | lift = tkraise |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1234 | def scale(self, *args): |
| 1235 | self._do('scale', args) |
| 1236 | def scan_mark(self, x, y): |
| 1237 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 1238 | def scan_dragto(self, x, y): |
| 1239 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 1240 | def select_adjust(self, tagOrId, index): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 1241 | self.tk.call(self._w, 'select', 'adjust', tagOrId, index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1242 | def select_clear(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1243 | self.tk.call(self._w, 'select', 'clear') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1244 | def select_from(self, tagOrId, index): |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 1245 | self.tk.call(self._w, 'select', 'from', tagOrId, index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1246 | def select_item(self): |
| 1247 | self.tk.call(self._w, 'select', 'item') |
| 1248 | def select_to(self, tagOrId, index): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 1249 | self.tk.call(self._w, 'select', 'to', tagOrId, index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1250 | def type(self, tagOrId): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 1251 | return self.tk.call(self._w, 'type', tagOrId) or None |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1252 | def xview(self, *args): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1253 | if not args: |
| 1254 | return self._getdoubles(self.tk.call(self._w, 'xview')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1255 | apply(self.tk.call, (self._w, 'xview')+args) |
| 1256 | def yview(self, *args): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1257 | if not args: |
| 1258 | return self._getdoubles(self.tk.call(self._w, 'yview')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1259 | apply(self.tk.call, (self._w, 'yview')+args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1260 | |
| 1261 | class Checkbutton(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1262 | def __init__(self, master=None, cnf={}, **kw): |
| 1263 | Widget.__init__(self, master, 'checkbutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1264 | def deselect(self): |
| 1265 | self.tk.call(self._w, 'deselect') |
| 1266 | def flash(self): |
| 1267 | self.tk.call(self._w, 'flash') |
| 1268 | def invoke(self): |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 1269 | return self.tk.call(self._w, 'invoke') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1270 | def select(self): |
| 1271 | self.tk.call(self._w, 'select') |
| 1272 | def toggle(self): |
| 1273 | self.tk.call(self._w, 'toggle') |
| 1274 | |
| 1275 | class Entry(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1276 | def __init__(self, master=None, cnf={}, **kw): |
| 1277 | Widget.__init__(self, master, 'entry', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1278 | def delete(self, first, last=None): |
| 1279 | self.tk.call(self._w, 'delete', first, last) |
| 1280 | def get(self): |
| 1281 | return self.tk.call(self._w, 'get') |
| 1282 | def icursor(self, index): |
| 1283 | self.tk.call(self._w, 'icursor', index) |
| 1284 | def index(self, index): |
| 1285 | return self.tk.getint(self.tk.call( |
| 1286 | self._w, 'index', index)) |
| 1287 | def insert(self, index, string): |
| 1288 | self.tk.call(self._w, 'insert', index, string) |
| 1289 | def scan_mark(self, x): |
| 1290 | self.tk.call(self._w, 'scan', 'mark', x) |
| 1291 | def scan_dragto(self, x): |
| 1292 | self.tk.call(self._w, 'scan', 'dragto', x) |
Guido van Rossum | 422cc7f | 1996-05-21 20:30:07 +0000 | [diff] [blame] | 1293 | def selection_adjust(self, index): |
| 1294 | self.tk.call(self._w, 'selection', 'adjust', index) |
| 1295 | select_adjust = selection_adjust |
| 1296 | def selection_clear(self): |
| 1297 | self.tk.call(self._w, 'selection', 'clear') |
| 1298 | select_clear = selection_clear |
| 1299 | def selection_from(self, index): |
Guido van Rossum | 42b78e6 | 1996-09-06 14:20:23 +0000 | [diff] [blame] | 1300 | self.tk.call(self._w, 'selection', 'from', index) |
Guido van Rossum | 422cc7f | 1996-05-21 20:30:07 +0000 | [diff] [blame] | 1301 | select_from = selection_from |
| 1302 | def selection_present(self): |
Guido van Rossum | 1d59df2 | 1995-08-11 14:21:06 +0000 | [diff] [blame] | 1303 | return self.tk.getboolean( |
Guido van Rossum | 422cc7f | 1996-05-21 20:30:07 +0000 | [diff] [blame] | 1304 | self.tk.call(self._w, 'selection', 'present')) |
| 1305 | select_present = selection_present |
| 1306 | def selection_range(self, start, end): |
| 1307 | self.tk.call(self._w, 'selection', 'range', start, end) |
| 1308 | select_range = selection_range |
| 1309 | def selection_to(self, index): |
| 1310 | self.tk.call(self._w, 'selection', 'to', index) |
| 1311 | select_to = selection_to |
| 1312 | def xview(self, index): |
| 1313 | self.tk.call(self._w, 'xview', index) |
| 1314 | def xview_moveto(self, fraction): |
| 1315 | self.tk.call(self._w, 'xview', 'moveto', fraction) |
| 1316 | def xview_scroll(self, number, what): |
| 1317 | self.tk.call(self._w, 'xview', 'scroll', number, what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1318 | |
| 1319 | class Frame(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1320 | def __init__(self, master=None, cnf={}, **kw): |
| 1321 | cnf = _cnfmerge((cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1322 | extra = () |
Fred Drake | 41dc09d | 1997-01-10 15:13:12 +0000 | [diff] [blame] | 1323 | if cnf.has_key('class_'): |
| 1324 | extra = ('-class', cnf['class_']) |
| 1325 | del cnf['class_'] |
| 1326 | elif cnf.has_key('class'): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1327 | extra = ('-class', cnf['class']) |
| 1328 | del cnf['class'] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1329 | Widget.__init__(self, master, 'frame', cnf, {}, extra) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1330 | |
| 1331 | class Label(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1332 | def __init__(self, master=None, cnf={}, **kw): |
| 1333 | Widget.__init__(self, master, 'label', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1334 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1335 | class Listbox(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1336 | def __init__(self, master=None, cnf={}, **kw): |
| 1337 | Widget.__init__(self, master, 'listbox', cnf, kw) |
Guido van Rossum | 46f92d2 | 1995-10-11 17:41:00 +0000 | [diff] [blame] | 1338 | def activate(self, index): |
| 1339 | self.tk.call(self._w, 'activate', index) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1340 | def bbox(self, *args): |
| 1341 | return self._getints(self._do('bbox', args)) or None |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1342 | def curselection(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1343 | # XXX Ought to apply self._getints()... |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1344 | return self.tk.splitlist(self.tk.call( |
| 1345 | self._w, 'curselection')) |
| 1346 | def delete(self, first, last=None): |
| 1347 | self.tk.call(self._w, 'delete', first, last) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1348 | def get(self, first, last=None): |
| 1349 | if last: |
| 1350 | return self.tk.splitlist(self.tk.call( |
| 1351 | self._w, 'get', first, last)) |
| 1352 | else: |
| 1353 | return self.tk.call(self._w, 'get', first) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1354 | def insert(self, index, *elements): |
| 1355 | apply(self.tk.call, |
| 1356 | (self._w, 'insert', index) + elements) |
| 1357 | def nearest(self, y): |
| 1358 | return self.tk.getint(self.tk.call( |
| 1359 | self._w, 'nearest', y)) |
| 1360 | def scan_mark(self, x, y): |
| 1361 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 1362 | def scan_dragto(self, x, y): |
| 1363 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1364 | def see(self, index): |
| 1365 | self.tk.call(self._w, 'see', index) |
| 1366 | def index(self, index): |
| 1367 | i = self.tk.call(self._w, 'index', index) |
| 1368 | if i == 'none': return None |
| 1369 | return self.tk.getint(i) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1370 | def select_anchor(self, index): |
| 1371 | self.tk.call(self._w, 'selection', 'anchor', index) |
Guido van Rossum | 764d6c7 | 1997-02-14 16:21:16 +0000 | [diff] [blame] | 1372 | selection_anchor = select_anchor |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1373 | def select_clear(self, first, last=None): |
| 1374 | self.tk.call(self._w, |
| 1375 | 'selection', 'clear', first, last) |
Guido van Rossum | 764d6c7 | 1997-02-14 16:21:16 +0000 | [diff] [blame] | 1376 | selection_clear = select_clear |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1377 | def select_includes(self, index): |
| 1378 | return self.tk.getboolean(self.tk.call( |
| 1379 | self._w, 'selection', 'includes', index)) |
Guido van Rossum | 764d6c7 | 1997-02-14 16:21:16 +0000 | [diff] [blame] | 1380 | selection_includes = select_includes |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1381 | def select_set(self, first, last=None): |
| 1382 | self.tk.call(self._w, 'selection', 'set', first, last) |
Guido van Rossum | 764d6c7 | 1997-02-14 16:21:16 +0000 | [diff] [blame] | 1383 | selection_set = select_set |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1384 | def size(self): |
| 1385 | return self.tk.getint(self.tk.call(self._w, 'size')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1386 | def xview(self, *what): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1387 | if not what: |
| 1388 | return self._getdoubles(self.tk.call(self._w, 'xview')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1389 | apply(self.tk.call, (self._w, 'xview')+what) |
| 1390 | def yview(self, *what): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1391 | if not what: |
| 1392 | return self._getdoubles(self.tk.call(self._w, 'yview')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1393 | apply(self.tk.call, (self._w, 'yview')+what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1394 | |
| 1395 | class Menu(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1396 | def __init__(self, master=None, cnf={}, **kw): |
| 1397 | Widget.__init__(self, master, 'menu', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1398 | def tk_bindForTraversal(self): |
Guido van Rossum | 688bbfc | 1996-09-10 12:39:26 +0000 | [diff] [blame] | 1399 | pass # obsolete since Tk 4.0 |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1400 | def tk_mbPost(self): |
| 1401 | self.tk.call('tk_mbPost', self._w) |
| 1402 | def tk_mbUnpost(self): |
| 1403 | self.tk.call('tk_mbUnpost') |
| 1404 | def tk_traverseToMenu(self, char): |
| 1405 | self.tk.call('tk_traverseToMenu', self._w, char) |
| 1406 | def tk_traverseWithinMenu(self, char): |
| 1407 | self.tk.call('tk_traverseWithinMenu', self._w, char) |
| 1408 | def tk_getMenuButtons(self): |
| 1409 | return self.tk.call('tk_getMenuButtons', self._w) |
| 1410 | def tk_nextMenu(self, count): |
| 1411 | self.tk.call('tk_nextMenu', count) |
| 1412 | def tk_nextMenuEntry(self, count): |
| 1413 | self.tk.call('tk_nextMenuEntry', count) |
| 1414 | def tk_invokeMenu(self): |
| 1415 | self.tk.call('tk_invokeMenu', self._w) |
| 1416 | def tk_firstMenu(self): |
| 1417 | self.tk.call('tk_firstMenu', self._w) |
| 1418 | def tk_mbButtonDown(self): |
| 1419 | self.tk.call('tk_mbButtonDown', self._w) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1420 | def tk_popup(self, x, y, entry=""): |
| 1421 | self.tk.call('tk_popup', self._w, x, y, entry) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1422 | def activate(self, index): |
| 1423 | self.tk.call(self._w, 'activate', index) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1424 | def add(self, itemType, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1425 | apply(self.tk.call, (self._w, 'add', itemType) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1426 | + self._options(cnf, kw)) |
Guido van Rossum | a1db48b | 1995-10-09 22:37:28 +0000 | [diff] [blame] | 1427 | def add_cascade(self, cnf={}, **kw): |
| 1428 | self.add('cascade', cnf or kw) |
| 1429 | def add_checkbutton(self, cnf={}, **kw): |
| 1430 | self.add('checkbutton', cnf or kw) |
| 1431 | def add_command(self, cnf={}, **kw): |
| 1432 | self.add('command', cnf or kw) |
| 1433 | def add_radiobutton(self, cnf={}, **kw): |
| 1434 | self.add('radiobutton', cnf or kw) |
| 1435 | def add_separator(self, cnf={}, **kw): |
| 1436 | self.add('separator', cnf or kw) |
Guido van Rossum | 2caac73 | 1996-09-05 16:46:31 +0000 | [diff] [blame] | 1437 | def insert(self, index, itemType, cnf={}, **kw): |
| 1438 | apply(self.tk.call, (self._w, 'insert', index, itemType) |
| 1439 | + self._options(cnf, kw)) |
| 1440 | def insert_cascade(self, index, cnf={}, **kw): |
| 1441 | self.insert(index, 'cascade', cnf or kw) |
| 1442 | def insert_checkbutton(self, index, cnf={}, **kw): |
| 1443 | self.insert(index, 'checkbutton', cnf or kw) |
| 1444 | def insert_command(self, index, cnf={}, **kw): |
| 1445 | self.insert(index, 'command', cnf or kw) |
| 1446 | def insert_radiobutton(self, index, cnf={}, **kw): |
| 1447 | self.insert(index, 'radiobutton', cnf or kw) |
| 1448 | def insert_separator(self, index, cnf={}, **kw): |
| 1449 | self.insert(index, 'separator', cnf or kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1450 | def delete(self, index1, index2=None): |
| 1451 | self.tk.call(self._w, 'delete', index1, index2) |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 1452 | def entrycget(self, index, option): |
Guido van Rossum | 1cd6a45 | 1997-12-30 04:07:19 +0000 | [diff] [blame] | 1453 | return self.tk.call(self._w, 'entrycget', index, '-' + option) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1454 | def entryconfigure(self, index, cnf=None, **kw): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1455 | if cnf is None and not kw: |
| 1456 | cnf = {} |
| 1457 | for x in self.tk.split(apply(self.tk.call, |
| 1458 | (self._w, 'entryconfigure', index))): |
| 1459 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 1460 | return cnf |
| 1461 | if type(cnf) == StringType and not kw: |
| 1462 | x = self.tk.split(apply(self.tk.call, |
| 1463 | (self._w, 'entryconfigure', index, '-'+cnf))) |
| 1464 | return (x[0][1:],) + x[1:] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1465 | apply(self.tk.call, (self._w, 'entryconfigure', index) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1466 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1467 | entryconfig = entryconfigure |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1468 | def index(self, index): |
Guido van Rossum | 535cf0c | 1994-06-27 07:55:59 +0000 | [diff] [blame] | 1469 | i = self.tk.call(self._w, 'index', index) |
| 1470 | if i == 'none': return None |
| 1471 | return self.tk.getint(i) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1472 | def invoke(self, index): |
| 1473 | return self.tk.call(self._w, 'invoke', index) |
| 1474 | def post(self, x, y): |
| 1475 | self.tk.call(self._w, 'post', x, y) |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 1476 | def type(self, index): |
| 1477 | return self.tk.call(self._w, 'type', index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1478 | def unpost(self): |
| 1479 | self.tk.call(self._w, 'unpost') |
| 1480 | def yposition(self, index): |
| 1481 | return self.tk.getint(self.tk.call( |
| 1482 | self._w, 'yposition', index)) |
| 1483 | |
| 1484 | class Menubutton(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1485 | def __init__(self, master=None, cnf={}, **kw): |
| 1486 | Widget.__init__(self, master, 'menubutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1487 | |
| 1488 | class Message(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1489 | def __init__(self, master=None, cnf={}, **kw): |
| 1490 | Widget.__init__(self, master, 'message', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1491 | |
| 1492 | class Radiobutton(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1493 | def __init__(self, master=None, cnf={}, **kw): |
| 1494 | Widget.__init__(self, master, 'radiobutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1495 | def deselect(self): |
| 1496 | self.tk.call(self._w, 'deselect') |
| 1497 | def flash(self): |
| 1498 | self.tk.call(self._w, 'flash') |
| 1499 | def invoke(self): |
Guido van Rossum | 9fd41e3 | 1997-12-29 19:59:33 +0000 | [diff] [blame] | 1500 | return self.tk.call(self._w, 'invoke') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1501 | def select(self): |
| 1502 | self.tk.call(self._w, 'select') |
| 1503 | |
| 1504 | class Scale(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1505 | def __init__(self, master=None, cnf={}, **kw): |
| 1506 | Widget.__init__(self, master, 'scale', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1507 | def get(self): |
Guido van Rossum | 1495747 | 1996-10-23 14:16:28 +0000 | [diff] [blame] | 1508 | value = self.tk.call(self._w, 'get') |
| 1509 | try: |
| 1510 | return self.tk.getint(value) |
| 1511 | except TclError: |
| 1512 | return self.tk.getdouble(value) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1513 | def set(self, value): |
| 1514 | self.tk.call(self._w, 'set', value) |
| 1515 | |
| 1516 | class Scrollbar(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1517 | def __init__(self, master=None, cnf={}, **kw): |
| 1518 | Widget.__init__(self, master, 'scrollbar', cnf, kw) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1519 | def activate(self, index): |
| 1520 | self.tk.call(self._w, 'activate', index) |
| 1521 | def delta(self, deltax, deltay): |
| 1522 | return self.getdouble(self.tk.call( |
| 1523 | self._w, 'delta', deltax, deltay)) |
| 1524 | def fraction(self, x, y): |
| 1525 | return self.getdouble(self.tk.call( |
| 1526 | self._w, 'fraction', x, y)) |
| 1527 | def identify(self, x, y): |
| 1528 | return self.tk.call(self._w, 'identify', x, y) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1529 | def get(self): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1530 | return self._getdoubles(self.tk.call(self._w, 'get')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1531 | def set(self, *args): |
| 1532 | apply(self.tk.call, (self._w, 'set')+args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1533 | |
| 1534 | class Text(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1535 | def __init__(self, master=None, cnf={}, **kw): |
| 1536 | Widget.__init__(self, master, 'text', cnf, kw) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1537 | def bbox(self, *args): |
| 1538 | return self._getints(self._do('bbox', args)) or None |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1539 | def tk_textSelectTo(self, index): |
| 1540 | self.tk.call('tk_textSelectTo', self._w, index) |
| 1541 | def tk_textBackspace(self): |
| 1542 | self.tk.call('tk_textBackspace', self._w) |
| 1543 | def tk_textIndexCloser(self, a, b, c): |
| 1544 | self.tk.call('tk_textIndexCloser', self._w, a, b, c) |
| 1545 | def tk_textResetAnchor(self, index): |
| 1546 | self.tk.call('tk_textResetAnchor', self._w, index) |
| 1547 | def compare(self, index1, op, index2): |
| 1548 | return self.tk.getboolean(self.tk.call( |
| 1549 | self._w, 'compare', index1, op, index2)) |
| 1550 | def debug(self, boolean=None): |
| 1551 | return self.tk.getboolean(self.tk.call( |
| 1552 | self._w, 'debug', boolean)) |
| 1553 | def delete(self, index1, index2=None): |
| 1554 | self.tk.call(self._w, 'delete', index1, index2) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1555 | def dlineinfo(self, index): |
| 1556 | return self._getints(self.tk.call(self._w, 'dlineinfo', index)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1557 | def get(self, index1, index2=None): |
| 1558 | return self.tk.call(self._w, 'get', index1, index2) |
| 1559 | def index(self, index): |
| 1560 | return self.tk.call(self._w, 'index', index) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1561 | def insert(self, index, chars, *args): |
| 1562 | apply(self.tk.call, (self._w, 'insert', index, chars)+args) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1563 | def mark_gravity(self, markName, direction=None): |
| 1564 | return apply(self.tk.call, |
| 1565 | (self._w, 'mark', 'gravity', markName, direction)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1566 | def mark_names(self): |
| 1567 | return self.tk.splitlist(self.tk.call( |
| 1568 | self._w, 'mark', 'names')) |
| 1569 | def mark_set(self, markName, index): |
| 1570 | self.tk.call(self._w, 'mark', 'set', markName, index) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1571 | def mark_unset(self, *markNames): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1572 | apply(self.tk.call, (self._w, 'mark', 'unset') + markNames) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1573 | def scan_mark(self, x, y): |
| 1574 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 1575 | def scan_dragto(self, x, y): |
| 1576 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 1577 | def search(self, pattern, index, stopindex=None, |
| 1578 | forwards=None, backwards=None, exact=None, |
| 1579 | regexp=None, nocase=None, count=None): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1580 | args = [self._w, 'search'] |
| 1581 | if forwards: args.append('-forwards') |
| 1582 | if backwards: args.append('-backwards') |
| 1583 | if exact: args.append('-exact') |
| 1584 | if regexp: args.append('-regexp') |
| 1585 | if nocase: args.append('-nocase') |
| 1586 | if count: args.append('-count'); args.append(count) |
| 1587 | if pattern[0] == '-': args.append('--') |
| 1588 | args.append(pattern) |
| 1589 | args.append(index) |
| 1590 | if stopindex: args.append(stopindex) |
| 1591 | return apply(self.tk.call, tuple(args)) |
| 1592 | def see(self, index): |
| 1593 | self.tk.call(self._w, 'see', index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1594 | def tag_add(self, tagName, index1, index2=None): |
| 1595 | self.tk.call( |
| 1596 | self._w, 'tag', 'add', tagName, index1, index2) |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 1597 | def tag_unbind(self, tagName, sequence): |
| 1598 | self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1599 | def tag_bind(self, tagName, sequence, func, add=None): |
| 1600 | return self._bind((self._w, 'tag', 'bind', tagName), |
| 1601 | sequence, func, add) |
| 1602 | def tag_cget(self, tagName, option): |
Guido van Rossum | 73eba25 | 1996-11-11 19:10:58 +0000 | [diff] [blame] | 1603 | if option[:1] != '-': |
| 1604 | option = '-' + option |
| 1605 | if option[-1:] == '_': |
| 1606 | option = option[:-1] |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1607 | return self.tk.call(self._w, 'tag', 'cget', tagName, option) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1608 | def tag_configure(self, tagName, cnf={}, **kw): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1609 | if type(cnf) == StringType: |
| 1610 | x = self.tk.split(self.tk.call( |
| 1611 | self._w, 'tag', 'configure', tagName, '-'+cnf)) |
| 1612 | return (x[0][1:],) + x[1:] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1613 | apply(self.tk.call, |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1614 | (self._w, 'tag', 'configure', tagName) |
| 1615 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1616 | tag_config = tag_configure |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 1617 | def tag_delete(self, *tagNames): |
Guido van Rossum | 2a39031 | 1994-07-06 10:20:11 +0000 | [diff] [blame] | 1618 | apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1619 | def tag_lower(self, tagName, belowThis=None): |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 1620 | self.tk.call(self._w, 'tag', 'lower', tagName, belowThis) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1621 | def tag_names(self, index=None): |
| 1622 | return self.tk.splitlist( |
| 1623 | self.tk.call(self._w, 'tag', 'names', index)) |
| 1624 | def tag_nextrange(self, tagName, index1, index2=None): |
| 1625 | return self.tk.splitlist(self.tk.call( |
Guido van Rossum | 903abee | 1995-03-20 15:09:13 +0000 | [diff] [blame] | 1626 | self._w, 'tag', 'nextrange', tagName, index1, index2)) |
Guido van Rossum | f0413d4 | 1997-12-15 17:31:52 +0000 | [diff] [blame] | 1627 | def tag_prevrange(self, tagName, index1, index2=None): |
| 1628 | return self.tk.splitlist(self.tk.call( |
| 1629 | self._w, 'tag', 'prevrange', tagName, index1, index2)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1630 | def tag_raise(self, tagName, aboveThis=None): |
| 1631 | self.tk.call( |
| 1632 | self._w, 'tag', 'raise', tagName, aboveThis) |
| 1633 | def tag_ranges(self, tagName): |
| 1634 | return self.tk.splitlist(self.tk.call( |
| 1635 | self._w, 'tag', 'ranges', tagName)) |
| 1636 | def tag_remove(self, tagName, index1, index2=None): |
| 1637 | self.tk.call( |
Guido van Rossum | 5113569 | 1994-07-06 21:16:58 +0000 | [diff] [blame] | 1638 | self._w, 'tag', 'remove', tagName, index1, index2) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1639 | def window_cget(self, index, option): |
Guido van Rossum | 7814ea6 | 1997-12-11 17:08:52 +0000 | [diff] [blame] | 1640 | if option[:1] != '-': |
| 1641 | option = '-' + option |
| 1642 | if option[-1:] == '_': |
| 1643 | option = option[:-1] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1644 | return self.tk.call(self._w, 'window', 'cget', index, option) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1645 | def window_configure(self, index, cnf={}, **kw): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1646 | if type(cnf) == StringType: |
| 1647 | x = self.tk.split(self.tk.call( |
| 1648 | self._w, 'window', 'configure', |
| 1649 | index, '-'+cnf)) |
| 1650 | return (x[0][1:],) + x[1:] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1651 | apply(self.tk.call, |
| 1652 | (self._w, 'window', 'configure', index) |
| 1653 | + self._options(cnf, kw)) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1654 | window_config = window_configure |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1655 | def window_create(self, index, cnf={}, **kw): |
| 1656 | apply(self.tk.call, |
| 1657 | (self._w, 'window', 'create', index) |
| 1658 | + self._options(cnf, kw)) |
| 1659 | def window_names(self): |
| 1660 | return self.tk.splitlist( |
| 1661 | self.tk.call(self._w, 'window', 'names')) |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1662 | def xview(self, *what): |
| 1663 | if not what: |
| 1664 | return self._getdoubles(self.tk.call(self._w, 'xview')) |
| 1665 | apply(self.tk.call, (self._w, 'xview')+what) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1666 | def yview(self, *what): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1667 | if not what: |
| 1668 | return self._getdoubles(self.tk.call(self._w, 'yview')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1669 | apply(self.tk.call, (self._w, 'yview')+what) |
| 1670 | def yview_pickplace(self, *what): |
| 1671 | apply(self.tk.call, (self._w, 'yview', '-pickplace')+what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1672 | |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 1673 | class _setit: |
| 1674 | def __init__(self, var, value): |
| 1675 | self.__value = value |
| 1676 | self.__var = var |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 1677 | def __call__(self, *args): |
Fred Drake | 0c37369 | 1996-10-21 17:09:31 +0000 | [diff] [blame] | 1678 | self.__var.set(self.__value) |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 1679 | |
| 1680 | class OptionMenu(Menubutton): |
Guido van Rossum | bf4d8f9 | 1995-09-01 20:35:37 +0000 | [diff] [blame] | 1681 | def __init__(self, master, variable, value, *values): |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 1682 | kw = {"borderwidth": 2, "textvariable": variable, |
| 1683 | "indicatoron": 1, "relief": RAISED, "anchor": "c", |
| 1684 | "highlightthickness": 2} |
| 1685 | Widget.__init__(self, master, "menubutton", kw) |
Guido van Rossum | bf4d8f9 | 1995-09-01 20:35:37 +0000 | [diff] [blame] | 1686 | self.widgetName = 'tk_optionMenu' |
Guido van Rossum | 28574b5 | 1996-10-21 15:16:51 +0000 | [diff] [blame] | 1687 | menu = self.__menu = Menu(self, name="menu", tearoff=0) |
| 1688 | self.menuname = menu._w |
| 1689 | menu.add_command(label=value, command=_setit(variable, value)) |
| 1690 | for v in values: |
| 1691 | menu.add_command(label=v, command=_setit(variable, v)) |
| 1692 | self["menu"] = menu |
| 1693 | |
| 1694 | def __getitem__(self, name): |
| 1695 | if name == 'menu': |
| 1696 | return self.__menu |
| 1697 | return Widget.__getitem__(self, name) |
| 1698 | |
| 1699 | def destroy(self): |
| 1700 | Menubutton.destroy(self) |
| 1701 | self.__menu = None |
Guido van Rossum | bf4d8f9 | 1995-09-01 20:35:37 +0000 | [diff] [blame] | 1702 | |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1703 | class Image: |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 1704 | def __init__(self, imgtype, name=None, cnf={}, master=None, **kw): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1705 | self.name = None |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 1706 | if not master: |
| 1707 | master = _default_root |
| 1708 | if not master: |
| 1709 | raise RuntimeError, 'Too early to create image' |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1710 | self.tk = master.tk |
Guido van Rossum | 58103d3 | 1996-11-20 22:17:38 +0000 | [diff] [blame] | 1711 | if not name: |
| 1712 | name = `id(self)` |
| 1713 | # The following is needed for systems where id(x) |
| 1714 | # can return a negative number, such as Linux/m68k: |
| 1715 | if name[0] == '-': name = '_' + name[1:] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1716 | if kw and cnf: cnf = _cnfmerge((cnf, kw)) |
| 1717 | elif kw: cnf = kw |
| 1718 | options = () |
| 1719 | for k, v in cnf.items(): |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1720 | if callable(v): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1721 | v = self._register(v) |
| 1722 | options = options + ('-'+k, v) |
| 1723 | apply(self.tk.call, |
| 1724 | ('image', 'create', imgtype, name,) + options) |
| 1725 | self.name = name |
| 1726 | def __str__(self): return self.name |
| 1727 | def __del__(self): |
| 1728 | if self.name: |
| 1729 | self.tk.call('image', 'delete', self.name) |
Guido van Rossum | 71b1a90 | 1995-09-18 21:54:35 +0000 | [diff] [blame] | 1730 | def __setitem__(self, key, value): |
| 1731 | self.tk.call(self.name, 'configure', '-'+key, value) |
| 1732 | def __getitem__(self, key): |
| 1733 | return self.tk.call(self.name, 'configure', '-'+key) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1734 | def configure(self, **kw): |
Guido van Rossum | 8371013 | 1996-12-27 15:33:17 +0000 | [diff] [blame] | 1735 | res = () |
| 1736 | for k, v in _cnfmerge(kw).items(): |
| 1737 | if v is not None: |
| 1738 | if k[-1] == '_': k = k[:-1] |
| 1739 | if callable(v): |
| 1740 | v = self._register(v) |
| 1741 | res = res + ('-'+k, v) |
| 1742 | apply(self.tk.call, (self.name, 'config') + res) |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1743 | config = configure |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1744 | def height(self): |
| 1745 | return self.tk.getint( |
| 1746 | self.tk.call('image', 'height', self.name)) |
| 1747 | def type(self): |
| 1748 | return self.tk.call('image', 'type', self.name) |
| 1749 | def width(self): |
| 1750 | return self.tk.getint( |
| 1751 | self.tk.call('image', 'width', self.name)) |
| 1752 | |
| 1753 | class PhotoImage(Image): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 1754 | def __init__(self, name=None, cnf={}, master=None, **kw): |
| 1755 | apply(Image.__init__, (self, 'photo', name, cnf, master), kw) |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 1756 | def blank(self): |
| 1757 | self.tk.call(self.name, 'blank') |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1758 | def cget(self, option): |
| 1759 | return self.tk.call(self.name, 'cget', '-' + option) |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 1760 | # XXX config |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1761 | def __getitem__(self, key): |
| 1762 | return self.tk.call(self.name, 'cget', '-' + key) |
Guido van Rossum | 5ac00ac | 1997-12-11 02:03:55 +0000 | [diff] [blame] | 1763 | # XXX copy -from, -to, ...? |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1764 | def copy(self): |
| 1765 | destImage = PhotoImage() |
| 1766 | self.tk.call(destImage, 'copy', self.name) |
| 1767 | return destImage |
| 1768 | def zoom(self,x,y=''): |
| 1769 | destImage = PhotoImage() |
| 1770 | if y=='': y=x |
| 1771 | self.tk.call(destImage, 'copy', self.name, '-zoom',x,y) |
| 1772 | return destImage |
| 1773 | def subsample(self,x,y=''): |
| 1774 | destImage = PhotoImage() |
| 1775 | if y=='': y=x |
| 1776 | self.tk.call(destImage, 'copy', self.name, '-subsample',x,y) |
| 1777 | return destImage |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 1778 | def get(self, x, y): |
| 1779 | return self.tk.call(self.name, 'get', x, y) |
| 1780 | def put(self, data, to=None): |
| 1781 | args = (self.name, 'put', data) |
| 1782 | if to: |
Fred Drake | b532399 | 1997-12-16 15:03:43 +0000 | [diff] [blame] | 1783 | if to[0] == '-to': |
| 1784 | to = to[1:] |
| 1785 | args = args + ('-to',) + tuple(to) |
Guido van Rossum | 96ebbd3 | 1995-09-30 17:05:26 +0000 | [diff] [blame] | 1786 | apply(self.tk.call, args) |
| 1787 | # XXX read |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1788 | def write(self, filename, format=None, from_coords=None): |
| 1789 | args = (self.name, 'write', filename) |
| 1790 | if format: |
| 1791 | args = args + ('-format', format) |
| 1792 | if from_coords: |
| 1793 | args = args + ('-from',) + tuple(from_coords) |
| 1794 | apply(self.tk.call, args) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1795 | |
| 1796 | class BitmapImage(Image): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame^] | 1797 | def __init__(self, name=None, cnf={}, master=None, **kw): |
| 1798 | apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1799 | |
| 1800 | def image_names(): return _default_root.tk.call('image', 'names') |
| 1801 | def image_types(): return _default_root.tk.call('image', 'types') |
| 1802 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1803 | ###################################################################### |
| 1804 | # Extensions: |
| 1805 | |
| 1806 | class Studbutton(Button): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1807 | def __init__(self, master=None, cnf={}, **kw): |
| 1808 | Widget.__init__(self, master, 'studbutton', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1809 | self.bind('<Any-Enter>', self.tkButtonEnter) |
| 1810 | self.bind('<Any-Leave>', self.tkButtonLeave) |
| 1811 | self.bind('<1>', self.tkButtonDown) |
| 1812 | self.bind('<ButtonRelease-1>', self.tkButtonUp) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1813 | |
| 1814 | class Tributton(Button): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1815 | def __init__(self, master=None, cnf={}, **kw): |
| 1816 | Widget.__init__(self, master, 'tributton', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1817 | self.bind('<Any-Enter>', self.tkButtonEnter) |
| 1818 | self.bind('<Any-Leave>', self.tkButtonLeave) |
| 1819 | self.bind('<1>', self.tkButtonDown) |
| 1820 | self.bind('<ButtonRelease-1>', self.tkButtonUp) |
| 1821 | self['fg'] = self['bg'] |
| 1822 | self['activebackground'] = self['bg'] |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1823 | |
Guido van Rossum | c417ef8 | 1996-08-21 23:38:59 +0000 | [diff] [blame] | 1824 | ###################################################################### |
| 1825 | # Test: |
| 1826 | |
| 1827 | def _test(): |
| 1828 | root = Tk() |
| 1829 | label = Label(root, text="Proof-of-existence test for Tk") |
| 1830 | label.pack() |
| 1831 | test = Button(root, text="Click me!", |
Guido van Rossum | 368e06b | 1997-11-07 20:38:49 +0000 | [diff] [blame] | 1832 | command=lambda root=root: root.test.configure( |
Guido van Rossum | c417ef8 | 1996-08-21 23:38:59 +0000 | [diff] [blame] | 1833 | text="[%s]" % root.test['text'])) |
| 1834 | test.pack() |
| 1835 | root.test = test |
| 1836 | quit = Button(root, text="QUIT", command=root.destroy) |
| 1837 | quit.pack() |
Guido van Rossum | 16cd332 | 1997-05-09 00:59:43 +0000 | [diff] [blame] | 1838 | root.tkraise() |
Guido van Rossum | c417ef8 | 1996-08-21 23:38:59 +0000 | [diff] [blame] | 1839 | root.mainloop() |
| 1840 | |
| 1841 | if __name__ == '__main__': |
| 1842 | _test() |
| 1843 | |
Guido van Rossum | 37dcab1 | 1996-05-16 16:00:19 +0000 | [diff] [blame] | 1844 | |
| 1845 | # Emacs cruft |
| 1846 | # Local Variables: |
| 1847 | # py-indent-offset: 8 |
| 1848 | # End: |