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