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 | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3 | import tkinter |
| 4 | from tkinter import TclError |
Guido van Rossum | 7e9394a | 1995-03-17 16:21:33 +0000 | [diff] [blame] | 5 | from types import * |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 6 | from Tkconstants import * |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 8 | CallableTypes = (FunctionType, MethodType, |
| 9 | BuiltinFunctionType, BuiltinMethodType) |
| 10 | |
| 11 | TkVersion = eval(tkinter.TK_VERSION) |
| 12 | TclVersion = eval(tkinter.TCL_VERSION) |
| 13 | if TkVersion < 4.0: |
| 14 | raise ImportError, "This version of Tkinter.py requires Tk 4.0 or higher" |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | a22a70a | 1995-08-04 03:51:48 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 17 | def _flatten(tuple): |
| 18 | res = () |
| 19 | for item in tuple: |
| 20 | if type(item) in (TupleType, ListType): |
| 21 | res = res + _flatten(item) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 22 | elif item is not None: |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 23 | res = res + (item,) |
| 24 | return res |
| 25 | |
| 26 | def _cnfmerge(cnfs): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 27 | if type(cnfs) is DictionaryType: |
| 28 | _fixgeometry(cnfs) |
| 29 | return cnfs |
| 30 | elif type(cnfs) in (NoneType, StringType): |
| 31 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 32 | return cnfs |
| 33 | else: |
| 34 | cnf = {} |
| 35 | for c in _flatten(cnfs): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 36 | _fixgeometry(c) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 37 | for k, v in c.items(): |
| 38 | cnf[k] = v |
| 39 | return cnf |
| 40 | |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 41 | if TkVersion >= 4.0: |
| 42 | _fixg_warning = "Warning: patched up pre-Tk-4.0 geometry option\n" |
| 43 | def _fixgeometry(c): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 44 | if c and c.has_key('geometry'): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 45 | wh = _parsegeometry(c['geometry']) |
| 46 | if wh: |
| 47 | # Print warning message -- once |
| 48 | global _fixg_warning |
| 49 | if _fixg_warning: |
| 50 | import sys |
| 51 | sys.stderr.write(_fixg_warning) |
| 52 | _fixg_warning = None |
| 53 | w, h = wh |
| 54 | c['width'] = w |
| 55 | c['height'] = h |
| 56 | del c['geometry'] |
| 57 | def _parsegeometry(s): |
| 58 | from string import splitfields |
| 59 | fields = splitfields(s, 'x') |
| 60 | if len(fields) == 2: |
| 61 | return tuple(fields) |
| 62 | # else: return None |
| 63 | else: |
| 64 | def _fixgeometry(c): pass |
| 65 | |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 66 | class Event: |
| 67 | pass |
| 68 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 69 | _default_root = None |
| 70 | |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 71 | def _tkerror(err): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 72 | pass |
| 73 | |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 74 | def _exit(code='0'): |
| 75 | import sys |
| 76 | sys.exit(getint(code)) |
| 77 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 78 | _varnum = 0 |
| 79 | class Variable: |
| 80 | def __init__(self, master=None): |
| 81 | global _default_root |
| 82 | global _varnum |
| 83 | if master: |
| 84 | self._tk = master.tk |
| 85 | else: |
| 86 | self._tk = _default_root.tk |
| 87 | self._name = 'PY_VAR' + `_varnum` |
| 88 | _varnum = _varnum + 1 |
| 89 | def __del__(self): |
| 90 | self._tk.unsetvar(self._name) |
| 91 | def __str__(self): |
| 92 | return self._name |
| 93 | def __call__(self, value=None): |
| 94 | if value == None: |
| 95 | return self.get() |
| 96 | else: |
| 97 | self.set(value) |
| 98 | def set(self, value): |
| 99 | return self._tk.setvar(self._name, value) |
| 100 | |
| 101 | class StringVar(Variable): |
| 102 | def __init__(self, master=None): |
| 103 | Variable.__init__(self, master) |
| 104 | def get(self): |
| 105 | return self._tk.getvar(self._name) |
| 106 | |
| 107 | class IntVar(Variable): |
| 108 | def __init__(self, master=None): |
| 109 | Variable.__init__(self, master) |
| 110 | def get(self): |
| 111 | return self._tk.getint(self._tk.getvar(self._name)) |
| 112 | |
| 113 | class DoubleVar(Variable): |
| 114 | def __init__(self, master=None): |
| 115 | Variable.__init__(self, master) |
| 116 | def get(self): |
| 117 | return self._tk.getdouble(self._tk.getvar(self._name)) |
| 118 | |
| 119 | class BooleanVar(Variable): |
| 120 | def __init__(self, master=None): |
| 121 | Variable.__init__(self, master) |
| 122 | def get(self): |
| 123 | return self._tk.getboolean(self._tk.getvar(self._name)) |
| 124 | |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 125 | def mainloop(n=0): |
| 126 | _default_root.tk.mainloop(n) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 127 | |
| 128 | def getint(s): |
| 129 | return _default_root.tk.getint(s) |
| 130 | |
| 131 | def getdouble(s): |
| 132 | return _default_root.tk.getdouble(s) |
| 133 | |
| 134 | def getboolean(s): |
| 135 | return _default_root.tk.getboolean(s) |
| 136 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 137 | class Misc: |
| 138 | def tk_strictMotif(self, boolean=None): |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 139 | return self.tk.getboolean(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 140 | 'set', 'tk_strictMotif', boolean)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 141 | def tk_menuBar(self, *args): |
| 142 | apply(self.tk.call, ('tk_menuBar', self._w) + args) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 143 | def wait_variable(self, name='PY_VAR'): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 144 | self.tk.call('tkwait', 'variable', name) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 145 | waitvar = wait_variable # XXX b/w compat |
Guido van Rossum | 9beb932 | 1994-06-27 23:15:31 +0000 | [diff] [blame] | 146 | def wait_window(self, window=None): |
| 147 | if window == None: |
| 148 | window = self |
| 149 | self.tk.call('tkwait', 'window', window._w) |
| 150 | def wait_visibility(self, window=None): |
| 151 | if window == None: |
| 152 | window = self |
| 153 | self.tk.call('tkwait', 'visibility', window._w) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 154 | def setvar(self, name='PY_VAR', value='1'): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 155 | self.tk.setvar(name, value) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 156 | def getvar(self, name='PY_VAR'): |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 157 | return self.tk.getvar(name) |
| 158 | def getint(self, s): |
| 159 | return self.tk.getint(s) |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame] | 160 | def getdouble(self, s): |
| 161 | return self.tk.getdouble(s) |
| 162 | def getboolean(self, s): |
| 163 | return self.tk.getboolean(s) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 164 | def focus_set(self): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 165 | self.tk.call('focus', self._w) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 166 | focus = focus_set # XXX b/w compat? |
| 167 | def focus_default_set(self): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 168 | self.tk.call('focus', 'default', self._w) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 169 | def focus_default_none(self): |
| 170 | self.tk.call('focus', 'default', 'none') |
| 171 | focus_default = focus_default_set |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 172 | def focus_none(self): |
| 173 | self.tk.call('focus', 'none') |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 174 | def focus_get(self): |
| 175 | name = self.tk.call('focus') |
| 176 | if name == 'none': return None |
| 177 | return self._nametowidget(name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 178 | def after(self, ms, func=None, *args): |
| 179 | if not func: |
| 180 | self.tk.call('after', ms) |
| 181 | else: |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 182 | # XXX Disgusting hack to clean up after calling func |
| 183 | tmp = [] |
| 184 | def callit(func=func, args=args, tk=self.tk, tmp=tmp): |
| 185 | try: |
| 186 | apply(func, args) |
| 187 | finally: |
| 188 | tk.deletecommand(tmp[0]) |
| 189 | name = self._register(callit) |
| 190 | tmp.append(name) |
| 191 | self.tk.call('after', ms, name) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 192 | def bell(self, displayof=None): |
| 193 | if displayof: |
| 194 | self.tk.call('bell', '-displayof', displayof) |
| 195 | else: |
| 196 | self.tk.call('bell', '-displayof', self._w) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 197 | # XXX grab current w/o window argument |
| 198 | def grab_current(self): |
| 199 | name = self.tk.call('grab', 'current', self._w) |
| 200 | if not name: return None |
| 201 | return self._nametowidget(name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 202 | def grab_release(self): |
| 203 | self.tk.call('grab', 'release', self._w) |
| 204 | def grab_set(self): |
| 205 | self.tk.call('grab', 'set', self._w) |
| 206 | def grab_set_global(self): |
| 207 | self.tk.call('grab', 'set', '-global', self._w) |
| 208 | def grab_status(self): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 209 | status = self.tk.call('grab', 'status', self._w) |
| 210 | if status == 'none': status = None |
| 211 | return status |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 212 | def lower(self, belowThis=None): |
| 213 | self.tk.call('lower', self._w, belowThis) |
Guido van Rossum | 780044f | 1994-10-20 22:02:27 +0000 | [diff] [blame] | 214 | def option_add(self, pattern, value, priority = None): |
| 215 | self.tk.call('option', 'add', pattern, priority) |
| 216 | def option_clear(self): |
| 217 | self.tk.call('option', 'clear') |
| 218 | def option_get(self, name, className): |
| 219 | return self.tk.call('option', 'get', self._w, name, className) |
| 220 | def option_readfile(self, fileName, priority = None): |
| 221 | self.tk.call('option', 'readfile', fileName, priority) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 222 | def selection_clear(self): |
| 223 | self.tk.call('selection', 'clear', self._w) |
| 224 | def selection_get(self, type=None): |
Guido van Rossum | bd84b04 | 1994-07-04 10:48:25 +0000 | [diff] [blame] | 225 | return self.tk.call('selection', 'get', type) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 226 | def selection_handle(self, func, type=None, format=None): |
| 227 | name = self._register(func) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 228 | self.tk.call('selection', 'handle', self._w, |
| 229 | name, type, format) |
| 230 | def selection_own(self, func=None): |
| 231 | name = self._register(func) |
| 232 | self.tk.call('selection', 'own', self._w, name) |
| 233 | def selection_own_get(self): |
| 234 | return self._nametowidget(self.tk.call('selection', 'own')) |
| 235 | def send(self, interp, cmd, *args): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 236 | return apply(self.tk.call, ('send', interp, cmd) + args) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 237 | def lower(self, belowThis=None): |
| 238 | self.tk.call('lift', self._w, belowThis) |
| 239 | def tkraise(self, aboveThis=None): |
| 240 | self.tk.call('raise', self._w, aboveThis) |
| 241 | lift = tkraise |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 242 | def colormodel(self, value=None): |
| 243 | return self.tk.call('tk', 'colormodel', self._w, value) |
| 244 | def winfo_atom(self, name): |
| 245 | return self.tk.getint(self.tk.call('winfo', 'atom', name)) |
| 246 | def winfo_atomname(self, id): |
| 247 | return self.tk.call('winfo', 'atomname', id) |
| 248 | def winfo_cells(self): |
| 249 | return self.tk.getint( |
| 250 | self.tk.call('winfo', 'cells', self._w)) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 251 | def winfo_children(self): |
| 252 | return map(self._nametowidget, |
| 253 | self.tk.splitlist(self.tk.call( |
| 254 | 'winfo', 'children', self._w))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 255 | def winfo_class(self): |
| 256 | return self.tk.call('winfo', 'class', self._w) |
| 257 | def winfo_containing(self, rootX, rootY): |
| 258 | return self.tk.call('winfo', 'containing', rootx, rootY) |
| 259 | def winfo_depth(self): |
| 260 | return self.tk.getint(self.tk.call('winfo', 'depth', self._w)) |
| 261 | def winfo_exists(self): |
| 262 | return self.tk.getint( |
| 263 | self.tk.call('winfo', 'exists', self._w)) |
| 264 | def winfo_fpixels(self, number): |
| 265 | return self.tk.getdouble(self.tk.call( |
| 266 | 'winfo', 'fpixels', self._w, number)) |
| 267 | def winfo_geometry(self): |
| 268 | return self.tk.call('winfo', 'geometry', self._w) |
| 269 | def winfo_height(self): |
| 270 | return self.tk.getint( |
| 271 | self.tk.call('winfo', 'height', self._w)) |
| 272 | def winfo_id(self): |
| 273 | return self.tk.getint( |
| 274 | self.tk.call('winfo', 'id', self._w)) |
| 275 | def winfo_interps(self): |
| 276 | return self.tk.splitlist( |
| 277 | self.tk.call('winfo', 'interps')) |
| 278 | def winfo_ismapped(self): |
| 279 | return self.tk.getint( |
| 280 | self.tk.call('winfo', 'ismapped', self._w)) |
| 281 | def winfo_name(self): |
| 282 | return self.tk.call('winfo', 'name', self._w) |
| 283 | def winfo_parent(self): |
| 284 | return self.tk.call('winfo', 'parent', self._w) |
| 285 | def winfo_pathname(self, id): |
| 286 | return self.tk.call('winfo', 'pathname', id) |
| 287 | def winfo_pixels(self, number): |
| 288 | return self.tk.getint( |
| 289 | self.tk.call('winfo', 'pixels', self._w, number)) |
| 290 | def winfo_reqheight(self): |
| 291 | return self.tk.getint( |
| 292 | self.tk.call('winfo', 'reqheight', self._w)) |
| 293 | def winfo_reqwidth(self): |
| 294 | return self.tk.getint( |
| 295 | self.tk.call('winfo', 'reqwidth', self._w)) |
| 296 | def winfo_rgb(self, color): |
| 297 | return self._getints( |
| 298 | self.tk.call('winfo', 'rgb', self._w, color)) |
| 299 | def winfo_rootx(self): |
| 300 | return self.tk.getint( |
| 301 | self.tk.call('winfo', 'rootx', self._w)) |
| 302 | def winfo_rooty(self): |
| 303 | return self.tk.getint( |
| 304 | self.tk.call('winfo', 'rooty', self._w)) |
| 305 | def winfo_screen(self): |
| 306 | return self.tk.call('winfo', 'screen', self._w) |
| 307 | def winfo_screencells(self): |
| 308 | return self.tk.getint( |
| 309 | self.tk.call('winfo', 'screencells', self._w)) |
| 310 | def winfo_screendepth(self): |
| 311 | return self.tk.getint( |
| 312 | self.tk.call('winfo', 'screendepth', self._w)) |
| 313 | def winfo_screenheight(self): |
| 314 | return self.tk.getint( |
| 315 | self.tk.call('winfo', 'screenheight', self._w)) |
| 316 | def winfo_screenmmheight(self): |
| 317 | return self.tk.getint( |
| 318 | self.tk.call('winfo', 'screenmmheight', self._w)) |
| 319 | def winfo_screenmmwidth(self): |
| 320 | return self.tk.getint( |
| 321 | self.tk.call('winfo', 'screenmmwidth', self._w)) |
| 322 | def winfo_screenvisual(self): |
| 323 | return self.tk.call('winfo', 'screenvisual', self._w) |
| 324 | def winfo_screenwidth(self): |
| 325 | return self.tk.getint( |
| 326 | self.tk.call('winfo', 'screenwidth', self._w)) |
| 327 | def winfo_toplevel(self): |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 328 | return self._nametowidget(self.tk.call( |
| 329 | 'winfo', 'toplevel', self._w)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 330 | def winfo_visual(self): |
| 331 | return self.tk.call('winfo', 'visual', self._w) |
| 332 | def winfo_vrootheight(self): |
| 333 | return self.tk.getint( |
| 334 | self.tk.call('winfo', 'vrootheight', self._w)) |
| 335 | def winfo_vrootwidth(self): |
| 336 | return self.tk.getint( |
| 337 | self.tk.call('winfo', 'vrootwidth', self._w)) |
| 338 | def winfo_vrootx(self): |
| 339 | return self.tk.getint( |
| 340 | self.tk.call('winfo', 'vrootx', self._w)) |
| 341 | def winfo_vrooty(self): |
| 342 | return self.tk.getint( |
| 343 | self.tk.call('winfo', 'vrooty', self._w)) |
| 344 | def winfo_width(self): |
| 345 | return self.tk.getint( |
| 346 | self.tk.call('winfo', 'width', self._w)) |
| 347 | def winfo_x(self): |
| 348 | return self.tk.getint( |
| 349 | self.tk.call('winfo', 'x', self._w)) |
| 350 | def winfo_y(self): |
| 351 | return self.tk.getint( |
| 352 | self.tk.call('winfo', 'y', self._w)) |
| 353 | def update(self): |
| 354 | self.tk.call('update') |
| 355 | def update_idletasks(self): |
| 356 | self.tk.call('update', 'idletasks') |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 357 | def bind(self, sequence, func=None, add=''): |
| 358 | if add: add = '+' |
| 359 | if func: |
| 360 | name = self._register(func, self._substitute) |
| 361 | self.tk.call('bind', self._w, sequence, |
| 362 | (add + name,) + self._subst_format) |
| 363 | else: |
| 364 | return self.tk.call('bind', self._w, sequence) |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 365 | def unbind(self, sequence): |
| 366 | self.tk.call('bind', self._w, sequence, '') |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 367 | def bind_all(self, sequence, func=None, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 368 | if add: add = '+' |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 369 | if func: |
| 370 | name = self._register(func, self._substitute) |
| 371 | self.tk.call('bind', 'all' , sequence, |
| 372 | (add + name,) + self._subst_format) |
| 373 | else: |
| 374 | return self.tk.call('bind', 'all', sequence) |
| 375 | def unbind_all(self, sequence): |
| 376 | self.tk.call('bind', 'all' , sequence, '') |
| 377 | def bind_class(self, className, sequence, func=None, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 378 | if add: add = '+' |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 379 | if func: |
| 380 | name = self._register(func, self._substitute) |
| 381 | self.tk.call('bind', className , sequence, |
| 382 | (add + name,) + self._subst_format) |
| 383 | else: |
| 384 | return self.tk.call('bind', className, sequence) |
| 385 | def unbind_class(self, className, sequence): |
| 386 | self.tk.call('bind', className , sequence, '') |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 387 | def mainloop(self, n=0): |
| 388 | self.tk.mainloop(n) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 389 | def quit(self): |
| 390 | self.tk.quit() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 391 | def _getints(self, string): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 392 | if not string: return None |
Guido van Rossum | 7e9394a | 1995-03-17 16:21:33 +0000 | [diff] [blame] | 393 | return tuple(map(self.tk.getint, self.tk.splitlist(string))) |
| 394 | def _getdoubles(self, string): |
| 395 | if not string: return None |
| 396 | return tuple(map(self.tk.getdouble, self.tk.splitlist(string))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 397 | def _getboolean(self, string): |
| 398 | if string: |
| 399 | return self.tk.getboolean(string) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 400 | def _options(self, cnf, kw = None): |
| 401 | if kw: |
| 402 | cnf = _cnfmerge((cnf, kw)) |
| 403 | else: |
| 404 | cnf = _cnfmerge(cnf) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 405 | res = () |
| 406 | for k, v in cnf.items(): |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 407 | if k[-1] == '_': k = k[:-1] |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 408 | if type(v) in CallableTypes: |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 409 | v = self._register(v) |
| 410 | res = res + ('-'+k, v) |
| 411 | return res |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 412 | def _nametowidget(self, name): |
| 413 | w = self |
| 414 | if name[0] == '.': |
| 415 | w = w._root() |
| 416 | name = name[1:] |
| 417 | from string import find |
| 418 | while name: |
| 419 | i = find(name, '.') |
| 420 | if i >= 0: |
| 421 | name, tail = name[:i], name[i+1:] |
| 422 | else: |
| 423 | tail = '' |
| 424 | w = w.children[name] |
| 425 | name = tail |
| 426 | return w |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 427 | def _register(self, func, subst=None): |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 428 | f = CallWrapper(func, subst, self).__call__ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 429 | name = `id(f)` |
| 430 | if hasattr(func, 'im_func'): |
| 431 | func = func.im_func |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 432 | if hasattr(func, '__name__') and \ |
| 433 | type(func.__name__) == type(''): |
| 434 | name = name + func.__name__ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 435 | self.tk.createcommand(name, f) |
| 436 | return name |
Guido van Rossum | 9beb932 | 1994-06-27 23:15:31 +0000 | [diff] [blame] | 437 | register = _register |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 438 | def _root(self): |
| 439 | w = self |
| 440 | while w.master: w = w.master |
| 441 | return w |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 442 | _subst_format = ('%#', '%b', '%f', '%h', '%k', |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 443 | '%s', '%t', '%w', '%x', '%y', |
| 444 | '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y') |
| 445 | def _substitute(self, *args): |
| 446 | tk = self.tk |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 447 | if len(args) != len(self._subst_format): return args |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 448 | nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args |
| 449 | # Missing: (a, c, d, m, o, v, B, R) |
| 450 | e = Event() |
| 451 | e.serial = tk.getint(nsign) |
| 452 | e.num = tk.getint(b) |
| 453 | try: e.focus = tk.getboolean(f) |
| 454 | except TclError: pass |
| 455 | e.height = tk.getint(h) |
| 456 | e.keycode = tk.getint(k) |
| 457 | e.state = tk.getint(s) |
| 458 | e.time = tk.getint(t) |
| 459 | e.width = tk.getint(w) |
| 460 | e.x = tk.getint(x) |
| 461 | e.y = tk.getint(y) |
| 462 | e.char = A |
| 463 | try: e.send_event = tk.getboolean(E) |
| 464 | except TclError: pass |
| 465 | e.keysym = K |
| 466 | e.keysym_num = tk.getint(N) |
| 467 | e.type = T |
| 468 | e.widget = self._nametowidget(W) |
| 469 | e.x_root = tk.getint(X) |
| 470 | e.y_root = tk.getint(Y) |
| 471 | return (e,) |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 472 | def _report_exception(self): |
| 473 | import sys |
| 474 | exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback |
| 475 | root = self._root() |
| 476 | root.report_callback_exception(exc, val, tb) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 478 | class CallWrapper: |
| 479 | def __init__(self, func, subst, widget): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 480 | self.func = func |
| 481 | self.subst = subst |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 482 | self.widget = widget |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 483 | def __call__(self, *args): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 484 | try: |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 485 | if self.subst: |
| 486 | args = apply(self.subst, args) |
| 487 | return apply(self.func, args) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 488 | except SystemExit, msg: |
| 489 | raise SystemExit, msg |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 490 | except: |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 491 | self.widget._report_exception() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 492 | |
| 493 | class Wm: |
| 494 | def aspect(self, |
| 495 | minNumer=None, minDenom=None, |
| 496 | maxNumer=None, maxDenom=None): |
| 497 | return self._getints( |
| 498 | self.tk.call('wm', 'aspect', self._w, |
| 499 | minNumer, minDenom, |
| 500 | maxNumer, maxDenom)) |
| 501 | def client(self, name=None): |
| 502 | return self.tk.call('wm', 'client', self._w, name) |
| 503 | def command(self, value=None): |
| 504 | return self.tk.call('wm', 'command', self._w, value) |
| 505 | def deiconify(self): |
| 506 | return self.tk.call('wm', 'deiconify', self._w) |
| 507 | def focusmodel(self, model=None): |
| 508 | return self.tk.call('wm', 'focusmodel', self._w, model) |
| 509 | def frame(self): |
| 510 | return self.tk.call('wm', 'frame', self._w) |
| 511 | def geometry(self, newGeometry=None): |
| 512 | return self.tk.call('wm', 'geometry', self._w, newGeometry) |
| 513 | def grid(self, |
| 514 | baseWidht=None, baseHeight=None, |
| 515 | widthInc=None, heightInc=None): |
| 516 | return self._getints(self.tk.call( |
| 517 | 'wm', 'grid', self._w, |
| 518 | baseWidht, baseHeight, widthInc, heightInc)) |
| 519 | def group(self, pathName=None): |
| 520 | return self.tk.call('wm', 'group', self._w, pathName) |
| 521 | def iconbitmap(self, bitmap=None): |
| 522 | return self.tk.call('wm', 'iconbitmap', self._w, bitmap) |
| 523 | def iconify(self): |
| 524 | return self.tk.call('wm', 'iconify', self._w) |
| 525 | def iconmask(self, bitmap=None): |
| 526 | return self.tk.call('wm', 'iconmask', self._w, bitmap) |
| 527 | def iconname(self, newName=None): |
| 528 | return self.tk.call('wm', 'iconname', self._w, newName) |
| 529 | def iconposition(self, x=None, y=None): |
| 530 | return self._getints(self.tk.call( |
| 531 | 'wm', 'iconposition', self._w, x, y)) |
| 532 | def iconwindow(self, pathName=None): |
| 533 | return self.tk.call('wm', 'iconwindow', self._w, pathName) |
| 534 | def maxsize(self, width=None, height=None): |
| 535 | return self._getints(self.tk.call( |
| 536 | 'wm', 'maxsize', self._w, width, height)) |
| 537 | def minsize(self, width=None, height=None): |
| 538 | return self._getints(self.tk.call( |
| 539 | 'wm', 'minsize', self._w, width, height)) |
| 540 | def overrideredirect(self, boolean=None): |
| 541 | return self._getboolean(self.tk.call( |
| 542 | 'wm', 'overrideredirect', self._w, boolean)) |
| 543 | def positionfrom(self, who=None): |
| 544 | return self.tk.call('wm', 'positionfrom', self._w, who) |
| 545 | def protocol(self, name=None, func=None): |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 546 | if type(func) in CallableTypes: |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 547 | command = self._register(func) |
| 548 | else: |
| 549 | command = func |
| 550 | return self.tk.call( |
| 551 | 'wm', 'protocol', self._w, name, command) |
| 552 | def sizefrom(self, who=None): |
| 553 | return self.tk.call('wm', 'sizefrom', self._w, who) |
| 554 | def state(self): |
| 555 | return self.tk.call('wm', 'state', self._w) |
| 556 | def title(self, string=None): |
| 557 | return self.tk.call('wm', 'title', self._w, string) |
| 558 | def transient(self, master=None): |
| 559 | return self.tk.call('wm', 'transient', self._w, master) |
| 560 | def withdraw(self): |
| 561 | return self.tk.call('wm', 'withdraw', self._w) |
| 562 | |
| 563 | class Tk(Misc, Wm): |
| 564 | _w = '.' |
| 565 | def __init__(self, screenName=None, baseName=None, className='Tk'): |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 566 | global _default_root |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 567 | self.master = None |
| 568 | self.children = {} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 569 | if baseName is None: |
| 570 | import sys, os |
| 571 | baseName = os.path.basename(sys.argv[0]) |
| 572 | if baseName[-3:] == '.py': baseName = baseName[:-3] |
| 573 | self.tk = tkinter.create(screenName, baseName, className) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 574 | self.tk.createcommand('tkerror', _tkerror) |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 575 | self.tk.createcommand('exit', _exit) |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 576 | self.readprofile(baseName, className) |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 577 | if not _default_root: |
| 578 | _default_root = self |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 579 | def destroy(self): |
| 580 | for c in self.children.values(): c.destroy() |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 581 | self.tk.call('destroy', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 582 | def __str__(self): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 583 | return self._w |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 584 | def readprofile(self, baseName, className): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 585 | ##print __import__ |
| 586 | import os, pdb |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 587 | if os.environ.has_key('HOME'): home = os.environ['HOME'] |
| 588 | else: home = os.curdir |
| 589 | class_tcl = os.path.join(home, '.%s.tcl' % className) |
| 590 | class_py = os.path.join(home, '.%s.py' % className) |
| 591 | base_tcl = os.path.join(home, '.%s.tcl' % baseName) |
| 592 | base_py = os.path.join(home, '.%s.py' % baseName) |
| 593 | dir = {'self': self} |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 594 | ##pdb.run('from Tkinter import *', dir) |
Guido van Rossum | 27b77a4 | 1994-07-12 15:52:32 +0000 | [diff] [blame] | 595 | exec 'from Tkinter import *' in dir |
| 596 | if os.path.isfile(class_tcl): |
| 597 | print 'source', `class_tcl` |
| 598 | self.tk.call('source', class_tcl) |
| 599 | if os.path.isfile(class_py): |
| 600 | print 'execfile', `class_py` |
| 601 | execfile(class_py, dir) |
| 602 | if os.path.isfile(base_tcl): |
| 603 | print 'source', `base_tcl` |
| 604 | self.tk.call('source', base_tcl) |
| 605 | if os.path.isfile(base_py): |
| 606 | print 'execfile', `base_py` |
| 607 | execfile(base_py, dir) |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 608 | def report_callback_exception(self, exc, val, tb): |
| 609 | import traceback |
| 610 | print "Exception in Tkinter callback" |
| 611 | traceback.print_exception(exc, val, tb) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 612 | |
| 613 | class Pack: |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 614 | def config(self, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 615 | apply(self.tk.call, |
| 616 | ('pack', 'configure', self._w) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 617 | + self._options(cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 618 | pack = config |
| 619 | def __setitem__(self, key, value): |
| 620 | Pack.config({key: value}) |
| 621 | def forget(self): |
| 622 | self.tk.call('pack', 'forget', self._w) |
| 623 | def newinfo(self): |
Guido van Rossum | 69170c5 | 1994-07-11 15:21:31 +0000 | [diff] [blame] | 624 | words = self.tk.splitlist( |
| 625 | self.tk.call('pack', 'newinfo', self._w)) |
| 626 | dict = {} |
| 627 | for i in range(0, len(words), 2): |
| 628 | key = words[i][1:] |
| 629 | value = words[i+1] |
| 630 | if value[0] == '.': |
| 631 | value = self._nametowidget(value) |
| 632 | dict[key] = value |
| 633 | return dict |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 634 | info = newinfo |
Guido van Rossum | 5505d56 | 1994-12-30 17:16:35 +0000 | [diff] [blame] | 635 | _noarg_ = ['_noarg_'] |
| 636 | def propagate(self, flag=_noarg_): |
Guido van Rossum | a5773dd | 1995-09-07 19:22:00 +0000 | [diff] [blame^] | 637 | if flag is Pack._noarg_: |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 638 | return self._getboolean(self.tk.call( |
| 639 | 'pack', 'propagate', self._w)) |
Guido van Rossum | 5505d56 | 1994-12-30 17:16:35 +0000 | [diff] [blame] | 640 | else: |
| 641 | self.tk.call('pack', 'propagate', self._w, flag) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 642 | def slaves(self): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 643 | return map(self._nametowidget, |
| 644 | self.tk.splitlist( |
| 645 | self.tk.call('pack', 'slaves', self._w))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 646 | |
| 647 | class Place: |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 648 | def config(self, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 649 | apply(self.tk.call, |
| 650 | ('place', 'configure', self._w) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 651 | + self._options(cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 652 | place = config |
| 653 | def __setitem__(self, key, value): |
| 654 | Place.config({key: value}) |
| 655 | def forget(self): |
| 656 | self.tk.call('place', 'forget', self._w) |
| 657 | def info(self): |
| 658 | return self.tk.call('place', 'info', self._w) |
| 659 | def slaves(self): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 660 | return map(self._nametowidget, |
| 661 | self.tk.splitlist( |
| 662 | self.tk.call( |
| 663 | 'place', 'slaves', self._w))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 664 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 665 | class Widget(Misc, Pack, Place): |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 666 | def _setup(self, master, cnf): |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 667 | global _default_root |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 668 | if not master: |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 669 | if not _default_root: |
| 670 | _default_root = Tk() |
| 671 | master = _default_root |
| 672 | if not _default_root: |
| 673 | _default_root = master |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 674 | self.master = master |
| 675 | self.tk = master.tk |
| 676 | if cnf.has_key('name'): |
| 677 | name = cnf['name'] |
| 678 | del cnf['name'] |
| 679 | else: |
| 680 | name = `id(self)` |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 681 | self._name = name |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 682 | if master._w=='.': |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 683 | self._w = '.' + name |
| 684 | else: |
| 685 | self._w = master._w + '.' + name |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 686 | self.children = {} |
| 687 | if self.master.children.has_key(self._name): |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 688 | self.master.children[self._name].destroy() |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 689 | self.master.children[self._name] = self |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 690 | def __init__(self, master, widgetName, cnf={}, kw={}, extra=()): |
| 691 | if kw: |
| 692 | cnf = _cnfmerge((cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 693 | self.widgetName = widgetName |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 694 | Widget._setup(self, master, cnf) |
Guido van Rossum | bf4d8f9 | 1995-09-01 20:35:37 +0000 | [diff] [blame] | 695 | apply(self.tk.call, (widgetName, self._w)+extra) |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 696 | if cnf: |
| 697 | Widget.config(self, cnf) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 698 | def config(self, cnf=None, **kw): |
| 699 | # XXX ought to generalize this so tag_config etc. can use it |
| 700 | if kw: |
| 701 | cnf = _cnfmerge((cnf, kw)) |
| 702 | else: |
| 703 | cnf = _cnfmerge(cnf) |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 704 | if cnf is None: |
| 705 | cnf = {} |
| 706 | for x in self.tk.split( |
| 707 | self.tk.call(self._w, 'configure')): |
| 708 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 709 | return cnf |
| 710 | if type(cnf) == StringType: |
| 711 | x = self.tk.split(self.tk.call( |
| 712 | self._w, 'configure', '-'+cnf)) |
| 713 | return (x[0][1:],) + x[1:] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 714 | for k in cnf.keys(): |
| 715 | if type(k) == ClassType: |
| 716 | k.config(self, cnf[k]) |
| 717 | del cnf[k] |
| 718 | apply(self.tk.call, (self._w, 'configure') |
| 719 | + self._options(cnf)) |
| 720 | def __getitem__(self, key): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 721 | if TkVersion >= 4.0: |
| 722 | return self.tk.call(self._w, 'cget', '-' + key) |
Guido van Rossum | 535cf0c | 1994-06-27 07:55:59 +0000 | [diff] [blame] | 723 | v = self.tk.splitlist(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 724 | self._w, 'configure', '-' + key)) |
| 725 | return v[4] |
| 726 | def __setitem__(self, key, value): |
| 727 | Widget.config(self, {key: value}) |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 728 | def keys(self): |
| 729 | return map(lambda x: x[0][1:], |
| 730 | self.tk.split(self.tk.call(self._w, 'configure'))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 731 | def __str__(self): |
| 732 | return self._w |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 733 | def destroy(self): |
| 734 | for c in self.children.values(): c.destroy() |
Guido van Rossum | f023ab0 | 1994-08-30 12:13:44 +0000 | [diff] [blame] | 735 | if self.master.children.has_key(self._name): |
| 736 | del self.master.children[self._name] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 737 | self.tk.call('destroy', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 738 | def _do(self, name, args=()): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 739 | return apply(self.tk.call, (self._w, name) + args) |
| 740 | def unbind_class(self, seq): |
| 741 | Misc.unbind_class(self, self.widgetName, seq) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 742 | |
| 743 | class Toplevel(Widget, Wm): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 744 | def __init__(self, master=None, cnf={}, **kw): |
| 745 | if kw: |
| 746 | cnf = _cnfmerge((cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 747 | extra = () |
| 748 | if cnf.has_key('screen'): |
| 749 | extra = ('-screen', cnf['screen']) |
| 750 | del cnf['screen'] |
| 751 | if cnf.has_key('class'): |
| 752 | extra = extra + ('-class', cnf['class']) |
| 753 | del cnf['class'] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 754 | Widget.__init__(self, master, 'toplevel', cnf, {}, extra) |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 755 | root = self._root() |
| 756 | self.iconname(root.iconname()) |
| 757 | self.title(root.title()) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 758 | |
| 759 | class Button(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 760 | def __init__(self, master=None, cnf={}, **kw): |
| 761 | Widget.__init__(self, master, 'button', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 762 | def tkButtonEnter(self, *dummy): |
| 763 | self.tk.call('tkButtonEnter', self._w) |
| 764 | def tkButtonLeave(self, *dummy): |
| 765 | self.tk.call('tkButtonLeave', self._w) |
| 766 | def tkButtonDown(self, *dummy): |
| 767 | self.tk.call('tkButtonDown', self._w) |
| 768 | def tkButtonUp(self, *dummy): |
| 769 | self.tk.call('tkButtonUp', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 770 | def flash(self): |
| 771 | self.tk.call(self._w, 'flash') |
| 772 | def invoke(self): |
| 773 | self.tk.call(self._w, 'invoke') |
| 774 | |
| 775 | # Indices: |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 776 | # XXX I don't like these -- take them away |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 777 | def AtEnd(): |
| 778 | return 'end' |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame] | 779 | def AtInsert(*args): |
| 780 | s = 'insert' |
| 781 | for a in args: |
| 782 | if a: s = s + (' ' + a) |
| 783 | return s |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 784 | def AtSelFirst(): |
| 785 | return 'sel.first' |
| 786 | def AtSelLast(): |
| 787 | return 'sel.last' |
| 788 | def At(x, y=None): |
Guido van Rossum | 5e0c25b | 1994-07-12 09:04:41 +0000 | [diff] [blame] | 789 | if y is None: |
| 790 | return '@' + `x` |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 791 | else: |
Guido van Rossum | 5e0c25b | 1994-07-12 09:04:41 +0000 | [diff] [blame] | 792 | return '@' + `x` + ',' + `y` |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 793 | |
| 794 | class Canvas(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 795 | def __init__(self, master=None, cnf={}, **kw): |
| 796 | Widget.__init__(self, master, 'canvas', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 797 | def addtag(self, *args): |
| 798 | self._do('addtag', args) |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 799 | def addtag_above(self, tagOrId): |
| 800 | self.addtag('above', tagOrId) |
| 801 | def addtag_all(self): |
| 802 | self.addtag('all') |
| 803 | def addtag_below(self, tagOrId): |
| 804 | self.addtag('below', tagOrId) |
| 805 | def addtag_closest(self, x, y, halo=None, start=None): |
| 806 | self.addtag('closest', x, y, halo, start) |
| 807 | def addtag_enclosed(self, x1, y1, x2, y2): |
| 808 | self.addtag('enclosed', x1, y1, x2, y2) |
| 809 | def addtag_overlapping(self, x1, y1, x2, y2): |
| 810 | self.addtag('overlapping', x1, y1, x2, y2) |
| 811 | def addtag_withtag(self, tagOrId): |
| 812 | self.addtag('withtag', tagOrId) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 813 | def bbox(self, *args): |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 814 | return self._getints(self._do('bbox', args)) or None |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 815 | def tag_unbind(self, tagOrId, sequence): |
| 816 | self.tk.call(self._w, 'bind', tagOrId, sequence, '') |
| 817 | def tag_bind(self, tagOrId, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 818 | if add: add='+' |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 819 | name = self._register(func, self._substitute) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 820 | self.tk.call(self._w, 'bind', tagOrId, sequence, |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 821 | (add + name,) + self._subst_format) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 822 | def canvasx(self, screenx, gridspacing=None): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 823 | return self.tk.getdouble(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 824 | self._w, 'canvasx', screenx, gridspacing)) |
| 825 | def canvasy(self, screeny, gridspacing=None): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 826 | return self.tk.getdouble(self.tk.call( |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 827 | self._w, 'canvasy', screeny, gridspacing)) |
| 828 | def coords(self, *args): |
| 829 | return self._do('coords', args) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 830 | def _create(self, itemType, args, kw): # Args: (value, value, ..., cnf={}) |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 831 | args = _flatten(args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 832 | cnf = args[-1] |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 833 | if type(cnf) in (DictionaryType, TupleType): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 834 | args = args[:-1] |
| 835 | else: |
| 836 | cnf = {} |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 837 | return self.tk.getint(apply( |
| 838 | self.tk.call, |
| 839 | (self._w, 'create', itemType) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 840 | + args + self._options(cnf, kw))) |
| 841 | def create_arc(self, *args, **kw): |
| 842 | return self._create('arc', args, kw) |
| 843 | def create_bitmap(self, *args, **kw): |
| 844 | return self._create('bitmap', args, kw) |
| 845 | def create_image(self, *args, **kw): |
| 846 | return self._create('image', args, kw) |
| 847 | def create_line(self, *args, **kw): |
| 848 | return self._create('line', args, kw) |
| 849 | def create_oval(self, *args, **kw): |
| 850 | return self._create('oval', args, kw) |
| 851 | def create_polygon(self, *args, **kw): |
| 852 | return self._create('polygon', args, kw) |
| 853 | def create_rectangle(self, *args, **kw): |
| 854 | return self._create('rectangle', args, kw) |
| 855 | def create_text(self, *args, **kw): |
| 856 | return self._create('text', args, kw) |
| 857 | def create_window(self, *args, **kw): |
| 858 | return self._create('window', args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 859 | def dchars(self, *args): |
| 860 | self._do('dchars', args) |
| 861 | def delete(self, *args): |
| 862 | self._do('delete', args) |
| 863 | def dtag(self, *args): |
| 864 | self._do('dtag', args) |
| 865 | def find(self, *args): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 866 | return self._getints(self._do('find', args)) |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 867 | def find_above(self, tagOrId): |
| 868 | return self.find('above', tagOrId) |
| 869 | def find_all(self): |
| 870 | return self.find('all') |
| 871 | def find_below(self, tagOrId): |
| 872 | return self.find('below', tagOrId) |
| 873 | def find_closest(self, x, y, halo=None, start=None): |
| 874 | return self.find('closest', x, y, halo, start) |
| 875 | def find_enclosed(self, x1, y1, x2, y2): |
| 876 | return self.find('enclosed', x1, y1, x2, y2) |
| 877 | def find_overlapping(self, x1, y1, x2, y2): |
| 878 | return self.find('overlapping', x1, y1, x2, y2) |
| 879 | def find_withtag(self, tagOrId): |
| 880 | return self.find('withtag', tagOrId) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 881 | def focus(self, *args): |
| 882 | return self._do('focus', args) |
| 883 | def gettags(self, *args): |
| 884 | return self.tk.splitlist(self._do('gettags', args)) |
| 885 | def icursor(self, *args): |
| 886 | self._do('icursor', args) |
| 887 | def index(self, *args): |
| 888 | return self.tk.getint(self._do('index', args)) |
| 889 | def insert(self, *args): |
| 890 | self._do('insert', args) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 891 | def itemconfig(self, tagOrId, cnf=None, **kw): |
| 892 | if cnf is None and not kw: |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 893 | cnf = {} |
| 894 | for x in self.tk.split( |
| 895 | self._do('itemconfigure', (tagOrId))): |
| 896 | cnf[x[0][1:]] = (x[0][1:],) + x[1:] |
| 897 | return cnf |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 898 | if type(cnf) == StringType and not kw: |
Guido van Rossum | 9b68fd9 | 1994-06-23 07:40:14 +0000 | [diff] [blame] | 899 | x = self.tk.split(self._do('itemconfigure', |
| 900 | (tagOrId, '-'+cnf,))) |
| 901 | return (x[0][1:],) + x[1:] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 902 | self._do('itemconfigure', (tagOrId,) |
| 903 | + self._options(cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 904 | def lower(self, *args): |
| 905 | self._do('lower', args) |
| 906 | def move(self, *args): |
| 907 | self._do('move', args) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 908 | def postscript(self, cnf={}, **kw): |
| 909 | return self._do('postscript', self._options(cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 910 | def tkraise(self, *args): |
| 911 | self._do('raise', args) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 912 | lift = tkraise |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 913 | def scale(self, *args): |
| 914 | self._do('scale', args) |
| 915 | def scan_mark(self, x, y): |
| 916 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 917 | def scan_dragto(self, x, y): |
| 918 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 919 | def select_adjust(self, tagOrId, index): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 920 | self.tk.call(self._w, 'select', 'adjust', tagOrId, index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 921 | def select_clear(self): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 922 | self.tk.call(self._w, 'select', 'clear', 'end') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 923 | def select_from(self, tagOrId, index): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 924 | self.tk.call(self._w, 'select', 'set', tagOrId, index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 925 | def select_item(self): |
| 926 | self.tk.call(self._w, 'select', 'item') |
| 927 | def select_to(self, tagOrId, index): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 928 | self.tk.call(self._w, 'select', 'to', tagOrId, index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 929 | def type(self, tagOrId): |
Guido van Rossum | 08a4038 | 1994-06-21 11:44:21 +0000 | [diff] [blame] | 930 | return self.tk.call(self._w, 'type', tagOrId) or None |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 931 | def xview(self, *args): |
| 932 | apply(self.tk.call, (self._w, 'xview')+args) |
| 933 | def yview(self, *args): |
| 934 | apply(self.tk.call, (self._w, 'yview')+args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 935 | |
| 936 | class Checkbutton(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 937 | def __init__(self, master=None, cnf={}, **kw): |
| 938 | Widget.__init__(self, master, 'checkbutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 939 | def deselect(self): |
| 940 | self.tk.call(self._w, 'deselect') |
| 941 | def flash(self): |
| 942 | self.tk.call(self._w, 'flash') |
| 943 | def invoke(self): |
| 944 | self.tk.call(self._w, 'invoke') |
| 945 | def select(self): |
| 946 | self.tk.call(self._w, 'select') |
| 947 | def toggle(self): |
| 948 | self.tk.call(self._w, 'toggle') |
| 949 | |
| 950 | class Entry(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 951 | def __init__(self, master=None, cnf={}, **kw): |
| 952 | Widget.__init__(self, master, 'entry', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 953 | def tk_entryBackspace(self): |
| 954 | self.tk.call('tk_entryBackspace', self._w) |
| 955 | def tk_entryBackword(self): |
| 956 | self.tk.call('tk_entryBackword', self._w) |
| 957 | def tk_entrySeeCaret(self): |
| 958 | self.tk.call('tk_entrySeeCaret', self._w) |
| 959 | def delete(self, first, last=None): |
| 960 | self.tk.call(self._w, 'delete', first, last) |
| 961 | def get(self): |
| 962 | return self.tk.call(self._w, 'get') |
| 963 | def icursor(self, index): |
| 964 | self.tk.call(self._w, 'icursor', index) |
| 965 | def index(self, index): |
| 966 | return self.tk.getint(self.tk.call( |
| 967 | self._w, 'index', index)) |
| 968 | def insert(self, index, string): |
| 969 | self.tk.call(self._w, 'insert', index, string) |
| 970 | def scan_mark(self, x): |
| 971 | self.tk.call(self._w, 'scan', 'mark', x) |
| 972 | def scan_dragto(self, x): |
| 973 | self.tk.call(self._w, 'scan', 'dragto', x) |
| 974 | def select_adjust(self, index): |
| 975 | self.tk.call(self._w, 'select', 'adjust', index) |
| 976 | def select_clear(self): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 977 | self.tk.call(self._w, 'select', 'clear', 'end') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 978 | def select_from(self, index): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 979 | self.tk.call(self._w, 'select', 'set', index) |
Guido van Rossum | 1d59df2 | 1995-08-11 14:21:06 +0000 | [diff] [blame] | 980 | def select_present(self): |
| 981 | return self.tk.getboolean( |
| 982 | self.tk.call(self._w, 'select', 'present')) |
| 983 | def select_range(self, start, end): |
| 984 | self.tk.call(self._w, 'select', 'range', start, end) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 985 | def select_to(self, index): |
| 986 | self.tk.call(self._w, 'select', 'to', index) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 987 | def view(self, index): |
| 988 | self.tk.call(self._w, 'view', index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 989 | |
| 990 | class Frame(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 991 | def __init__(self, master=None, cnf={}, **kw): |
| 992 | cnf = _cnfmerge((cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 993 | extra = () |
| 994 | if cnf.has_key('class'): |
| 995 | extra = ('-class', cnf['class']) |
| 996 | del cnf['class'] |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 997 | Widget.__init__(self, master, 'frame', cnf, {}, extra) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 998 | def tk_menuBar(self, *args): |
| 999 | apply(self.tk.call, ('tk_menuBar', self._w) + args) |
| 1000 | |
| 1001 | class Label(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1002 | def __init__(self, master=None, cnf={}, **kw): |
| 1003 | Widget.__init__(self, master, 'label', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1004 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1005 | class Listbox(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1006 | def __init__(self, master=None, cnf={}, **kw): |
| 1007 | Widget.__init__(self, master, 'listbox', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1008 | def tk_listboxSingleSelect(self): |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1009 | if TkVersion >= 4.0: |
| 1010 | self['selectmode'] = 'single' |
| 1011 | else: |
| 1012 | self.tk.call('tk_listboxSingleSelect', self._w) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1013 | def curselection(self): |
| 1014 | return self.tk.splitlist(self.tk.call( |
| 1015 | self._w, 'curselection')) |
| 1016 | def delete(self, first, last=None): |
| 1017 | self.tk.call(self._w, 'delete', first, last) |
| 1018 | def get(self, index): |
| 1019 | return self.tk.call(self._w, 'get', index) |
| 1020 | def insert(self, index, *elements): |
| 1021 | apply(self.tk.call, |
| 1022 | (self._w, 'insert', index) + elements) |
| 1023 | def nearest(self, y): |
| 1024 | return self.tk.getint(self.tk.call( |
| 1025 | self._w, 'nearest', y)) |
| 1026 | def scan_mark(self, x, y): |
| 1027 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 1028 | def scan_dragto(self, x, y): |
| 1029 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 1030 | def select_adjust(self, index): |
| 1031 | self.tk.call(self._w, 'select', 'adjust', index) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1032 | if TkVersion >= 4.0: |
| 1033 | def select_anchor(self, index): |
| 1034 | self.tk.call(self._w, 'selection', 'anchor', index) |
| 1035 | def select_clear(self, first, last=None): |
| 1036 | self.tk.call(self._w, |
| 1037 | 'selection', 'clear', first, last) |
| 1038 | def select_includes(self, index): |
| 1039 | return self.tk.getboolean(self.tk.call( |
| 1040 | self._w, 'selection', 'includes', index)) |
| 1041 | def select_set(self, first, last=None): |
| 1042 | self.tk.call(self._w, 'selection', 'set', first, last) |
| 1043 | else: |
| 1044 | def select_clear(self): |
| 1045 | self.tk.call(self._w, 'select', 'clear') |
| 1046 | def select_from(self, index): |
| 1047 | self.tk.call(self._w, 'select', 'from', index) |
| 1048 | def select_to(self, index): |
| 1049 | self.tk.call(self._w, 'select', 'to', index) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1050 | def size(self): |
| 1051 | return self.tk.getint(self.tk.call(self._w, 'size')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1052 | def xview(self, *what): |
| 1053 | apply(self.tk.call, (self._w, 'xview')+what) |
| 1054 | def yview(self, *what): |
| 1055 | apply(self.tk.call, (self._w, 'yview')+what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1056 | |
| 1057 | class Menu(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1058 | def __init__(self, master=None, cnf={}, **kw): |
| 1059 | Widget.__init__(self, master, 'menu', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1060 | def tk_bindForTraversal(self): |
| 1061 | self.tk.call('tk_bindForTraversal', self._w) |
| 1062 | def tk_mbPost(self): |
| 1063 | self.tk.call('tk_mbPost', self._w) |
| 1064 | def tk_mbUnpost(self): |
| 1065 | self.tk.call('tk_mbUnpost') |
| 1066 | def tk_traverseToMenu(self, char): |
| 1067 | self.tk.call('tk_traverseToMenu', self._w, char) |
| 1068 | def tk_traverseWithinMenu(self, char): |
| 1069 | self.tk.call('tk_traverseWithinMenu', self._w, char) |
| 1070 | def tk_getMenuButtons(self): |
| 1071 | return self.tk.call('tk_getMenuButtons', self._w) |
| 1072 | def tk_nextMenu(self, count): |
| 1073 | self.tk.call('tk_nextMenu', count) |
| 1074 | def tk_nextMenuEntry(self, count): |
| 1075 | self.tk.call('tk_nextMenuEntry', count) |
| 1076 | def tk_invokeMenu(self): |
| 1077 | self.tk.call('tk_invokeMenu', self._w) |
| 1078 | def tk_firstMenu(self): |
| 1079 | self.tk.call('tk_firstMenu', self._w) |
| 1080 | def tk_mbButtonDown(self): |
| 1081 | self.tk.call('tk_mbButtonDown', self._w) |
| 1082 | def activate(self, index): |
| 1083 | self.tk.call(self._w, 'activate', index) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1084 | def add(self, itemType, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1085 | apply(self.tk.call, (self._w, 'add', itemType) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1086 | + self._options(cnf, kw)) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1087 | def add_cascade(self, cnf={}): |
| 1088 | self.add('cascade', cnf) |
| 1089 | def add_checkbutton(self, cnf={}): |
| 1090 | self.add('checkbutton', cnf) |
| 1091 | def add_command(self, cnf={}): |
| 1092 | self.add('command', cnf) |
| 1093 | def add_radiobutton(self, cnf={}): |
| 1094 | self.add('radiobutton', cnf) |
| 1095 | def add_separator(self, cnf={}): |
| 1096 | self.add('separator', cnf) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1097 | def delete(self, index1, index2=None): |
| 1098 | self.tk.call(self._w, 'delete', index1, index2) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1099 | def entryconfig(self, index, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1100 | apply(self.tk.call, (self._w, 'entryconfigure', index) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1101 | + self._options(cnf, kw)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1102 | def index(self, index): |
Guido van Rossum | 535cf0c | 1994-06-27 07:55:59 +0000 | [diff] [blame] | 1103 | i = self.tk.call(self._w, 'index', index) |
| 1104 | if i == 'none': return None |
| 1105 | return self.tk.getint(i) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1106 | def invoke(self, index): |
| 1107 | return self.tk.call(self._w, 'invoke', index) |
| 1108 | def post(self, x, y): |
| 1109 | self.tk.call(self._w, 'post', x, y) |
| 1110 | def unpost(self): |
| 1111 | self.tk.call(self._w, 'unpost') |
| 1112 | def yposition(self, index): |
| 1113 | return self.tk.getint(self.tk.call( |
| 1114 | self._w, 'yposition', index)) |
| 1115 | |
| 1116 | class Menubutton(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1117 | def __init__(self, master=None, cnf={}, **kw): |
| 1118 | Widget.__init__(self, master, 'menubutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1119 | |
| 1120 | class Message(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1121 | def __init__(self, master=None, cnf={}, **kw): |
| 1122 | Widget.__init__(self, master, 'message', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1123 | |
| 1124 | class Radiobutton(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1125 | def __init__(self, master=None, cnf={}, **kw): |
| 1126 | Widget.__init__(self, master, 'radiobutton', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1127 | def deselect(self): |
| 1128 | self.tk.call(self._w, 'deselect') |
| 1129 | def flash(self): |
| 1130 | self.tk.call(self._w, 'flash') |
| 1131 | def invoke(self): |
| 1132 | self.tk.call(self._w, 'invoke') |
| 1133 | def select(self): |
| 1134 | self.tk.call(self._w, 'select') |
| 1135 | |
| 1136 | class Scale(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1137 | def __init__(self, master=None, cnf={}, **kw): |
| 1138 | Widget.__init__(self, master, 'scale', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1139 | def get(self): |
| 1140 | return self.tk.getint(self.tk.call(self._w, 'get')) |
| 1141 | def set(self, value): |
| 1142 | self.tk.call(self._w, 'set', value) |
| 1143 | |
| 1144 | class Scrollbar(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1145 | def __init__(self, master=None, cnf={}, **kw): |
| 1146 | Widget.__init__(self, master, 'scrollbar', cnf, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1147 | def get(self): |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1148 | return self._getints(self.tk.call(self._w, 'get')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1149 | def set(self, *args): |
| 1150 | apply(self.tk.call, (self._w, 'set')+args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1151 | |
| 1152 | class Text(Widget): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1153 | def __init__(self, master=None, cnf={}, **kw): |
| 1154 | Widget.__init__(self, master, 'text', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1155 | self.bind('<Delete>', self.bspace) |
| 1156 | def bspace(self, *args): |
| 1157 | self.delete('insert') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1158 | def tk_textSelectTo(self, index): |
| 1159 | self.tk.call('tk_textSelectTo', self._w, index) |
| 1160 | def tk_textBackspace(self): |
| 1161 | self.tk.call('tk_textBackspace', self._w) |
| 1162 | def tk_textIndexCloser(self, a, b, c): |
| 1163 | self.tk.call('tk_textIndexCloser', self._w, a, b, c) |
| 1164 | def tk_textResetAnchor(self, index): |
| 1165 | self.tk.call('tk_textResetAnchor', self._w, index) |
| 1166 | def compare(self, index1, op, index2): |
| 1167 | return self.tk.getboolean(self.tk.call( |
| 1168 | self._w, 'compare', index1, op, index2)) |
| 1169 | def debug(self, boolean=None): |
| 1170 | return self.tk.getboolean(self.tk.call( |
| 1171 | self._w, 'debug', boolean)) |
| 1172 | def delete(self, index1, index2=None): |
| 1173 | self.tk.call(self._w, 'delete', index1, index2) |
| 1174 | def get(self, index1, index2=None): |
| 1175 | return self.tk.call(self._w, 'get', index1, index2) |
| 1176 | def index(self, index): |
| 1177 | return self.tk.call(self._w, 'index', index) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1178 | def insert(self, index, chars, *args): |
| 1179 | apply(self.tk.call, (self._w, 'insert', index, chars)+args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1180 | def mark_names(self): |
| 1181 | return self.tk.splitlist(self.tk.call( |
| 1182 | self._w, 'mark', 'names')) |
| 1183 | def mark_set(self, markName, index): |
| 1184 | self.tk.call(self._w, 'mark', 'set', markName, index) |
| 1185 | def mark_unset(self, markNames): |
| 1186 | apply(self.tk.call, (self._w, 'mark', 'unset') + markNames) |
| 1187 | def scan_mark(self, y): |
| 1188 | self.tk.call(self._w, 'scan', 'mark', y) |
| 1189 | def scan_dragto(self, y): |
| 1190 | self.tk.call(self._w, 'scan', 'dragto', y) |
| 1191 | def tag_add(self, tagName, index1, index2=None): |
| 1192 | self.tk.call( |
| 1193 | self._w, 'tag', 'add', tagName, index1, index2) |
Guido van Rossum | ef8f881 | 1994-08-08 12:47:33 +0000 | [diff] [blame] | 1194 | def tag_unbind(self, tagName, sequence): |
| 1195 | self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1196 | def tag_bind(self, tagName, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1197 | if add: add='+' |
Guido van Rossum | 45853db | 1994-06-20 12:19:19 +0000 | [diff] [blame] | 1198 | name = self._register(func, self._substitute) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1199 | self.tk.call(self._w, 'tag', 'bind', |
| 1200 | tagName, sequence, |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1201 | (add + name,) + self._subst_format) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1202 | def tag_config(self, tagName, cnf={}, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1203 | apply(self.tk.call, |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1204 | (self._w, 'tag', 'configure', tagName) |
| 1205 | + self._options(cnf, kw)) |
Guido van Rossum | 2dcf529 | 1994-07-06 09:23:20 +0000 | [diff] [blame] | 1206 | def tag_delete(self, *tagNames): |
Guido van Rossum | 2a39031 | 1994-07-06 10:20:11 +0000 | [diff] [blame] | 1207 | apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1208 | def tag_lower(self, tagName, belowThis=None): |
Guido van Rossum | 97aeca1 | 1994-07-07 13:12:12 +0000 | [diff] [blame] | 1209 | self.tk.call(self._w, 'tag', 'lower', tagName, belowThis) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1210 | def tag_names(self, index=None): |
| 1211 | return self.tk.splitlist( |
| 1212 | self.tk.call(self._w, 'tag', 'names', index)) |
| 1213 | def tag_nextrange(self, tagName, index1, index2=None): |
| 1214 | return self.tk.splitlist(self.tk.call( |
Guido van Rossum | 903abee | 1995-03-20 15:09:13 +0000 | [diff] [blame] | 1215 | self._w, 'tag', 'nextrange', tagName, index1, index2)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1216 | def tag_raise(self, tagName, aboveThis=None): |
| 1217 | self.tk.call( |
| 1218 | self._w, 'tag', 'raise', tagName, aboveThis) |
| 1219 | def tag_ranges(self, tagName): |
| 1220 | return self.tk.splitlist(self.tk.call( |
| 1221 | self._w, 'tag', 'ranges', tagName)) |
| 1222 | def tag_remove(self, tagName, index1, index2=None): |
| 1223 | self.tk.call( |
Guido van Rossum | 5113569 | 1994-07-06 21:16:58 +0000 | [diff] [blame] | 1224 | self._w, 'tag', 'remove', tagName, index1, index2) |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1225 | def window_cget(self, index, option): |
| 1226 | return self.tk.call(self._w, 'window', 'cget', index, option) |
| 1227 | def window_config(self, index, cnf={}, **kw): |
| 1228 | apply(self.tk.call, |
| 1229 | (self._w, 'window', 'configure', index) |
| 1230 | + self._options(cnf, kw)) |
| 1231 | def window_create(self, index, cnf={}, **kw): |
| 1232 | apply(self.tk.call, |
| 1233 | (self._w, 'window', 'create', index) |
| 1234 | + self._options(cnf, kw)) |
| 1235 | def window_names(self): |
| 1236 | return self.tk.splitlist( |
| 1237 | self.tk.call(self._w, 'window', 'names')) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1238 | def yview(self, *what): |
| 1239 | apply(self.tk.call, (self._w, 'yview')+what) |
| 1240 | def yview_pickplace(self, *what): |
| 1241 | apply(self.tk.call, (self._w, 'yview', '-pickplace')+what) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1242 | |
Guido van Rossum | bf4d8f9 | 1995-09-01 20:35:37 +0000 | [diff] [blame] | 1243 | class OptionMenu(Widget): |
| 1244 | def __init__(self, master, variable, value, *values): |
| 1245 | self.widgetName = 'tk_optionMenu' |
| 1246 | Widget._setup(self, master, {}) |
| 1247 | self.menuname = apply( |
| 1248 | self.tk.call, |
| 1249 | (self.widgetName, self._w, variable, value) + values) |
| 1250 | |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1251 | class Image: |
| 1252 | def __init__(self, imgtype, name=None, cnf={}, **kw): |
| 1253 | self.name = None |
| 1254 | master = _default_root |
| 1255 | if not master: raise RuntimeError, 'Too early to create image' |
| 1256 | self.tk = master.tk |
| 1257 | if not name: name = `id(self)` |
| 1258 | if kw and cnf: cnf = _cnfmerge((cnf, kw)) |
| 1259 | elif kw: cnf = kw |
| 1260 | options = () |
| 1261 | for k, v in cnf.items(): |
| 1262 | if type(v) in CallableTypes: |
| 1263 | v = self._register(v) |
| 1264 | options = options + ('-'+k, v) |
| 1265 | apply(self.tk.call, |
| 1266 | ('image', 'create', imgtype, name,) + options) |
| 1267 | self.name = name |
| 1268 | def __str__(self): return self.name |
| 1269 | def __del__(self): |
| 1270 | if self.name: |
| 1271 | self.tk.call('image', 'delete', self.name) |
| 1272 | def height(self): |
| 1273 | return self.tk.getint( |
| 1274 | self.tk.call('image', 'height', self.name)) |
| 1275 | def type(self): |
| 1276 | return self.tk.call('image', 'type', self.name) |
| 1277 | def width(self): |
| 1278 | return self.tk.getint( |
| 1279 | self.tk.call('image', 'width', self.name)) |
| 1280 | |
| 1281 | class PhotoImage(Image): |
| 1282 | def __init__(self, name=None, cnf={}, **kw): |
| 1283 | apply(Image.__init__, (self, 'photo', name, cnf), kw) |
| 1284 | |
| 1285 | class BitmapImage(Image): |
| 1286 | def __init__(self, name=None, cnf={}, **kw): |
| 1287 | apply(Image.__init__, (self, 'bitmap', name, cnf), kw) |
| 1288 | |
| 1289 | def image_names(): return _default_root.tk.call('image', 'names') |
| 1290 | def image_types(): return _default_root.tk.call('image', 'types') |
| 1291 | |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1292 | ###################################################################### |
| 1293 | # Extensions: |
| 1294 | |
| 1295 | class Studbutton(Button): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1296 | def __init__(self, master=None, cnf={}, **kw): |
| 1297 | Widget.__init__(self, master, 'studbutton', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1298 | self.bind('<Any-Enter>', self.tkButtonEnter) |
| 1299 | self.bind('<Any-Leave>', self.tkButtonLeave) |
| 1300 | self.bind('<1>', self.tkButtonDown) |
| 1301 | self.bind('<ButtonRelease-1>', self.tkButtonUp) |
Guido van Rossum | aec5dc9 | 1994-06-27 07:55:12 +0000 | [diff] [blame] | 1302 | |
| 1303 | class Tributton(Button): |
Guido van Rossum | 35f67fb | 1995-08-04 03:50:29 +0000 | [diff] [blame] | 1304 | def __init__(self, master=None, cnf={}, **kw): |
| 1305 | Widget.__init__(self, master, 'tributton', cnf, kw) |
Guido van Rossum | 761c5ab | 1995-07-14 15:29:10 +0000 | [diff] [blame] | 1306 | self.bind('<Any-Enter>', self.tkButtonEnter) |
| 1307 | self.bind('<Any-Leave>', self.tkButtonLeave) |
| 1308 | self.bind('<1>', self.tkButtonDown) |
| 1309 | self.bind('<ButtonRelease-1>', self.tkButtonUp) |
| 1310 | self['fg'] = self['bg'] |
| 1311 | self['activebackground'] = self['bg'] |