Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1 | # Tkinter.py -- Tk/Tcl widget wrappers |
| 2 | import tkinter |
| 3 | from tkinter import TclError |
| 4 | |
| 5 | class _Dummy: |
| 6 | def meth(self): return |
| 7 | |
| 8 | def _isfunctype(func): |
| 9 | return type(func) in (type(_Dummy.meth), type(_isfunctype)) |
| 10 | |
| 11 | FunctionType = type(_isfunctype) |
| 12 | ClassType = type(_Dummy) |
| 13 | MethodType = type(_Dummy.meth) |
| 14 | |
| 15 | def tkerror(err): |
| 16 | pass |
| 17 | |
| 18 | class Event: |
| 19 | pass |
| 20 | |
| 21 | class Misc: |
| 22 | def tk_strictMotif(self, boolean=None): |
| 23 | self.tk.getboolean(self.tk.call( |
| 24 | 'set', 'tk_strictMotif', boolean)) |
| 25 | def waitvar(self, name='VAR'): |
| 26 | self.tk.call('tkwait', 'variable', name) |
| 27 | def setvar(self, name='VAR', value='1'): |
| 28 | self.tk.setvar(name, value) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 29 | def getvar(self, name='VAR'): |
| 30 | return self.tk.getvar(name) |
| 31 | def getint(self, s): |
| 32 | return self.tk.getint(s) |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame^] | 33 | def getdouble(self, s): |
| 34 | return self.tk.getdouble(s) |
| 35 | def getboolean(self, s): |
| 36 | return self.tk.getboolean(s) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 37 | def focus(self): |
| 38 | self.tk.call('focus', self._w) |
| 39 | def focus_default(self): |
| 40 | self.tk.call('focus', 'default', self._w) |
| 41 | def focus_none(self): |
| 42 | self.tk.call('focus', 'none') |
| 43 | #XXX focus_get? |
| 44 | def after(self, ms, func=None, *args): |
| 45 | if not func: |
| 46 | self.tk.call('after', ms) |
| 47 | else: |
| 48 | name = self._register(func) |
| 49 | apply(self.tk.call, ('after', ms, name) + args) |
| 50 | #XXX grab_current |
| 51 | def grab_release(self): |
| 52 | self.tk.call('grab', 'release', self._w) |
| 53 | def grab_set(self): |
| 54 | self.tk.call('grab', 'set', self._w) |
| 55 | def grab_set_global(self): |
| 56 | self.tk.call('grab', 'set', '-global', self._w) |
| 57 | def grab_status(self): |
| 58 | self.tk.call('grab', 'status', self._w) |
| 59 | def lower(self, belowThis=None): |
| 60 | self.tk.call('lower', self._w, belowThis) |
| 61 | def selection_clear(self): |
| 62 | self.tk.call('selection', 'clear', self._w) |
| 63 | def selection_get(self, type=None): |
| 64 | self.tk.call('selection', 'get', type) |
| 65 | def selection_handle(self, func, type=None, format=None): |
| 66 | name = self._register(func) |
| 67 | self.tk.call('selection', 'handle', |
| 68 | self._w, name, type, format) |
| 69 | #XXX def selection_own(self): |
| 70 | # self.tk.call('selection', 'own', self._w) |
| 71 | def send(self, interp, cmd, *args): #XXX |
| 72 | return apply(self.tk.call, ('send', interp, cmd) + args) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 73 | def lower(self, belowThis=None): |
| 74 | self.tk.call('lift', self._w, belowThis) |
| 75 | def tkraise(self, aboveThis=None): |
| 76 | self.tk.call('raise', self._w, aboveThis) |
| 77 | lift = tkraise |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 78 | def colormodel(self, value=None): |
| 79 | return self.tk.call('tk', 'colormodel', self._w, value) |
| 80 | def winfo_atom(self, name): |
| 81 | return self.tk.getint(self.tk.call('winfo', 'atom', name)) |
| 82 | def winfo_atomname(self, id): |
| 83 | return self.tk.call('winfo', 'atomname', id) |
| 84 | def winfo_cells(self): |
| 85 | return self.tk.getint( |
| 86 | self.tk.call('winfo', 'cells', self._w)) |
| 87 | #XXX winfo_children |
| 88 | def winfo_class(self): |
| 89 | return self.tk.call('winfo', 'class', self._w) |
| 90 | def winfo_containing(self, rootX, rootY): |
| 91 | return self.tk.call('winfo', 'containing', rootx, rootY) |
| 92 | def winfo_depth(self): |
| 93 | return self.tk.getint(self.tk.call('winfo', 'depth', self._w)) |
| 94 | def winfo_exists(self): |
| 95 | return self.tk.getint( |
| 96 | self.tk.call('winfo', 'exists', self._w)) |
| 97 | def winfo_fpixels(self, number): |
| 98 | return self.tk.getdouble(self.tk.call( |
| 99 | 'winfo', 'fpixels', self._w, number)) |
| 100 | def winfo_geometry(self): |
| 101 | return self.tk.call('winfo', 'geometry', self._w) |
| 102 | def winfo_height(self): |
| 103 | return self.tk.getint( |
| 104 | self.tk.call('winfo', 'height', self._w)) |
| 105 | def winfo_id(self): |
| 106 | return self.tk.getint( |
| 107 | self.tk.call('winfo', 'id', self._w)) |
| 108 | def winfo_interps(self): |
| 109 | return self.tk.splitlist( |
| 110 | self.tk.call('winfo', 'interps')) |
| 111 | def winfo_ismapped(self): |
| 112 | return self.tk.getint( |
| 113 | self.tk.call('winfo', 'ismapped', self._w)) |
| 114 | def winfo_name(self): |
| 115 | return self.tk.call('winfo', 'name', self._w) |
| 116 | def winfo_parent(self): |
| 117 | return self.tk.call('winfo', 'parent', self._w) |
| 118 | def winfo_pathname(self, id): |
| 119 | return self.tk.call('winfo', 'pathname', id) |
| 120 | def winfo_pixels(self, number): |
| 121 | return self.tk.getint( |
| 122 | self.tk.call('winfo', 'pixels', self._w, number)) |
| 123 | def winfo_reqheight(self): |
| 124 | return self.tk.getint( |
| 125 | self.tk.call('winfo', 'reqheight', self._w)) |
| 126 | def winfo_reqwidth(self): |
| 127 | return self.tk.getint( |
| 128 | self.tk.call('winfo', 'reqwidth', self._w)) |
| 129 | def winfo_rgb(self, color): |
| 130 | return self._getints( |
| 131 | self.tk.call('winfo', 'rgb', self._w, color)) |
| 132 | def winfo_rootx(self): |
| 133 | return self.tk.getint( |
| 134 | self.tk.call('winfo', 'rootx', self._w)) |
| 135 | def winfo_rooty(self): |
| 136 | return self.tk.getint( |
| 137 | self.tk.call('winfo', 'rooty', self._w)) |
| 138 | def winfo_screen(self): |
| 139 | return self.tk.call('winfo', 'screen', self._w) |
| 140 | def winfo_screencells(self): |
| 141 | return self.tk.getint( |
| 142 | self.tk.call('winfo', 'screencells', self._w)) |
| 143 | def winfo_screendepth(self): |
| 144 | return self.tk.getint( |
| 145 | self.tk.call('winfo', 'screendepth', self._w)) |
| 146 | def winfo_screenheight(self): |
| 147 | return self.tk.getint( |
| 148 | self.tk.call('winfo', 'screenheight', self._w)) |
| 149 | def winfo_screenmmheight(self): |
| 150 | return self.tk.getint( |
| 151 | self.tk.call('winfo', 'screenmmheight', self._w)) |
| 152 | def winfo_screenmmwidth(self): |
| 153 | return self.tk.getint( |
| 154 | self.tk.call('winfo', 'screenmmwidth', self._w)) |
| 155 | def winfo_screenvisual(self): |
| 156 | return self.tk.call('winfo', 'screenvisual', self._w) |
| 157 | def winfo_screenwidth(self): |
| 158 | return self.tk.getint( |
| 159 | self.tk.call('winfo', 'screenwidth', self._w)) |
| 160 | def winfo_toplevel(self): |
| 161 | return self.tk.call('winfo', 'toplevel', self._w) |
| 162 | def winfo_visual(self): |
| 163 | return self.tk.call('winfo', 'visual', self._w) |
| 164 | def winfo_vrootheight(self): |
| 165 | return self.tk.getint( |
| 166 | self.tk.call('winfo', 'vrootheight', self._w)) |
| 167 | def winfo_vrootwidth(self): |
| 168 | return self.tk.getint( |
| 169 | self.tk.call('winfo', 'vrootwidth', self._w)) |
| 170 | def winfo_vrootx(self): |
| 171 | return self.tk.getint( |
| 172 | self.tk.call('winfo', 'vrootx', self._w)) |
| 173 | def winfo_vrooty(self): |
| 174 | return self.tk.getint( |
| 175 | self.tk.call('winfo', 'vrooty', self._w)) |
| 176 | def winfo_width(self): |
| 177 | return self.tk.getint( |
| 178 | self.tk.call('winfo', 'width', self._w)) |
| 179 | def winfo_x(self): |
| 180 | return self.tk.getint( |
| 181 | self.tk.call('winfo', 'x', self._w)) |
| 182 | def winfo_y(self): |
| 183 | return self.tk.getint( |
| 184 | self.tk.call('winfo', 'y', self._w)) |
| 185 | def update(self): |
| 186 | self.tk.call('update') |
| 187 | def update_idletasks(self): |
| 188 | self.tk.call('update', 'idletasks') |
| 189 | def bind(self, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 190 | if add: add = '+' |
| 191 | name = self._register(func, _substitute) |
| 192 | self.tk.call('bind', self._w, sequence, |
| 193 | (add + name,) + _subst_prefix) |
| 194 | def bind_all(self, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 195 | if add: add = '+' |
| 196 | name = self._register(func, _substitute) |
| 197 | self.tk.call('bind', 'all' , sequence, |
| 198 | (add + `name`,) + _subst_prefix) |
| 199 | def bind_class(self, className, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 200 | if add: add = '+' |
| 201 | name = self._register(func, _substitute) |
| 202 | self.tk.call('bind', className , sequence, |
| 203 | (add + name,) + _subst_prefix) |
| 204 | def mainloop(self): |
| 205 | self.tk.mainloop() |
| 206 | def quit(self): |
| 207 | self.tk.quit() |
| 208 | # Utilities |
| 209 | def _getints(self, string): |
| 210 | if string: |
| 211 | res = () |
| 212 | for v in self.tk.split(string): |
| 213 | res = res + (self.tk.getint(v),) |
| 214 | return res |
| 215 | else: |
| 216 | return string |
| 217 | def _getboolean(self, string): |
| 218 | if string: |
| 219 | return self.tk.getboolean(string) |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame^] | 220 | # else return None |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 221 | def _options(self, cnf): |
| 222 | res = () |
| 223 | for k, v in cnf.items(): |
| 224 | if _isfunctype(v): |
| 225 | v = self._register(v) |
| 226 | res = res + ('-'+k, v) |
| 227 | return res |
| 228 | def _register(self, func, subst=None): |
| 229 | f = func |
| 230 | f = _CallSafely(func, subst).__call__ |
| 231 | name = `id(f)` |
| 232 | if hasattr(func, 'im_func'): |
| 233 | func = func.im_func |
| 234 | if hasattr(func, 'func_name') and \ |
| 235 | type(func.func_name) == type(''): |
| 236 | name = name + func.func_name |
| 237 | self.tk.createcommand(name, f) |
| 238 | return name |
| 239 | |
| 240 | _subst_prefix = ('%#', '%b', '%f', '%h', '%k', |
| 241 | '%s', '%t', '%w', '%x', '%y', |
| 242 | '%A', '%E', '%K', '%N', '%T', '%X', '%Y') |
| 243 | |
| 244 | def _substitute(*args): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 245 | tk = default_root.tk |
| 246 | if len(args) != len(_subst_prefix): return args |
| 247 | nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, T, X, Y = args |
| 248 | # Missing: (a, c, d, m, o, v, B, R, W) |
| 249 | #XXX Convert %W (_w) to class instance? |
| 250 | e = Event() |
| 251 | e.serial = tk.getint(nsign) |
| 252 | e.num = tk.getint(b) |
| 253 | try: e.focus = tk.getboolean(f) |
| 254 | except TclError: pass |
| 255 | e.height = tk.getint(h) |
| 256 | e.keycode = tk.getint(k) |
| 257 | e.state = tk.getint(s) |
| 258 | e.time = tk.getint(t) |
| 259 | e.width = tk.getint(w) |
| 260 | e.x = tk.getint(x) |
| 261 | e.y = tk.getint(y) |
| 262 | e.char = A |
| 263 | try: e.send_event = tk.getboolean(E) |
| 264 | except TclError: pass |
| 265 | e.keysym = K |
| 266 | e.keysym_num = tk.getint(N) |
| 267 | e.type = T |
| 268 | #XXX %W stuff |
| 269 | e.x_root = tk.getint(X) |
| 270 | e.y_root = tk.getint(Y) |
| 271 | return (e,) |
| 272 | |
| 273 | class _CallSafely: |
| 274 | def __init__(self, func, subst=None): |
| 275 | self.func = func |
| 276 | self.subst = subst |
| 277 | def __call__(self, *args): |
| 278 | if self.subst: |
| 279 | args = self.apply_func(self.subst, args) |
| 280 | args = self.apply_func(self.func, args) |
| 281 | def apply_func(self, func, args): |
| 282 | import sys |
| 283 | try: |
| 284 | return apply(func, args) |
| 285 | except: |
| 286 | try: |
| 287 | try: |
| 288 | t = sys.exc_traceback |
| 289 | while t: |
| 290 | sys.stderr.write( |
| 291 | ' %s, line %s\n' % |
| 292 | (t.tb_frame.f_code, |
| 293 | t.tb_lineno)) |
| 294 | t = t.tb_next |
| 295 | finally: |
| 296 | sys.stderr.write('%s: %s\n' % |
| 297 | (sys.exc_type, |
| 298 | sys.exc_value)) |
| 299 | except: |
| 300 | print '*** Error in error handling ***' |
| 301 | print sys.exc_type, ':', sys.exc_value |
| 302 | |
| 303 | class Wm: |
| 304 | def aspect(self, |
| 305 | minNumer=None, minDenom=None, |
| 306 | maxNumer=None, maxDenom=None): |
| 307 | return self._getints( |
| 308 | self.tk.call('wm', 'aspect', self._w, |
| 309 | minNumer, minDenom, |
| 310 | maxNumer, maxDenom)) |
| 311 | def client(self, name=None): |
| 312 | return self.tk.call('wm', 'client', self._w, name) |
| 313 | def command(self, value=None): |
| 314 | return self.tk.call('wm', 'command', self._w, value) |
| 315 | def deiconify(self): |
| 316 | return self.tk.call('wm', 'deiconify', self._w) |
| 317 | def focusmodel(self, model=None): |
| 318 | return self.tk.call('wm', 'focusmodel', self._w, model) |
| 319 | def frame(self): |
| 320 | return self.tk.call('wm', 'frame', self._w) |
| 321 | def geometry(self, newGeometry=None): |
| 322 | return self.tk.call('wm', 'geometry', self._w, newGeometry) |
| 323 | def grid(self, |
| 324 | baseWidht=None, baseHeight=None, |
| 325 | widthInc=None, heightInc=None): |
| 326 | return self._getints(self.tk.call( |
| 327 | 'wm', 'grid', self._w, |
| 328 | baseWidht, baseHeight, widthInc, heightInc)) |
| 329 | def group(self, pathName=None): |
| 330 | return self.tk.call('wm', 'group', self._w, pathName) |
| 331 | def iconbitmap(self, bitmap=None): |
| 332 | return self.tk.call('wm', 'iconbitmap', self._w, bitmap) |
| 333 | def iconify(self): |
| 334 | return self.tk.call('wm', 'iconify', self._w) |
| 335 | def iconmask(self, bitmap=None): |
| 336 | return self.tk.call('wm', 'iconmask', self._w, bitmap) |
| 337 | def iconname(self, newName=None): |
| 338 | return self.tk.call('wm', 'iconname', self._w, newName) |
| 339 | def iconposition(self, x=None, y=None): |
| 340 | return self._getints(self.tk.call( |
| 341 | 'wm', 'iconposition', self._w, x, y)) |
| 342 | def iconwindow(self, pathName=None): |
| 343 | return self.tk.call('wm', 'iconwindow', self._w, pathName) |
| 344 | def maxsize(self, width=None, height=None): |
| 345 | return self._getints(self.tk.call( |
| 346 | 'wm', 'maxsize', self._w, width, height)) |
| 347 | def minsize(self, width=None, height=None): |
| 348 | return self._getints(self.tk.call( |
| 349 | 'wm', 'minsize', self._w, width, height)) |
| 350 | def overrideredirect(self, boolean=None): |
| 351 | return self._getboolean(self.tk.call( |
| 352 | 'wm', 'overrideredirect', self._w, boolean)) |
| 353 | def positionfrom(self, who=None): |
| 354 | return self.tk.call('wm', 'positionfrom', self._w, who) |
| 355 | def protocol(self, name=None, func=None): |
| 356 | if _isfunctype(func): |
| 357 | command = self._register(func) |
| 358 | else: |
| 359 | command = func |
| 360 | return self.tk.call( |
| 361 | 'wm', 'protocol', self._w, name, command) |
| 362 | def sizefrom(self, who=None): |
| 363 | return self.tk.call('wm', 'sizefrom', self._w, who) |
| 364 | def state(self): |
| 365 | return self.tk.call('wm', 'state', self._w) |
| 366 | def title(self, string=None): |
| 367 | return self.tk.call('wm', 'title', self._w, string) |
| 368 | def transient(self, master=None): |
| 369 | return self.tk.call('wm', 'transient', self._w, master) |
| 370 | def withdraw(self): |
| 371 | return self.tk.call('wm', 'withdraw', self._w) |
| 372 | |
| 373 | class Tk(Misc, Wm): |
| 374 | _w = '.' |
| 375 | def __init__(self, screenName=None, baseName=None, className='Tk'): |
| 376 | if baseName is None: |
| 377 | import sys, os |
| 378 | baseName = os.path.basename(sys.argv[0]) |
| 379 | if baseName[-3:] == '.py': baseName = baseName[:-3] |
| 380 | self.tk = tkinter.create(screenName, baseName, className) |
| 381 | self.tk.createcommand('tkerror', tkerror) |
| 382 | def __del__(self): |
| 383 | self.tk.call('destroy', '.') |
| 384 | def __str__(self): |
| 385 | return '.' |
| 386 | |
| 387 | class Pack: |
| 388 | def config(self, cnf={}): |
| 389 | apply(self.tk.call, |
| 390 | ('pack', 'configure', self._w) |
| 391 | + self._options(cnf)) |
| 392 | pack = config |
| 393 | def __setitem__(self, key, value): |
| 394 | Pack.config({key: value}) |
| 395 | def forget(self): |
| 396 | self.tk.call('pack', 'forget', self._w) |
| 397 | def newinfo(self): |
| 398 | return self.tk.call('pack', 'newinfo', self._w) |
| 399 | info = newinfo |
| 400 | def propagate(self, boolean=None): |
| 401 | if boolean: |
| 402 | self.tk.call('pack', 'propagate', self._w) |
| 403 | else: |
| 404 | return self._getboolean(self.tk.call( |
| 405 | 'pack', 'propagate', self._w)) |
| 406 | def slaves(self): |
| 407 | return self.tk.splitlist(self.tk.call( |
| 408 | 'pack', 'slaves', self._w)) |
| 409 | |
| 410 | class Place: |
| 411 | def config(self, cnf={}): |
| 412 | apply(self.tk.call, |
| 413 | ('place', 'configure', self._w) |
| 414 | + self._options(cnf)) |
| 415 | place = config |
| 416 | def __setitem__(self, key, value): |
| 417 | Place.config({key: value}) |
| 418 | def forget(self): |
| 419 | self.tk.call('place', 'forget', self._w) |
| 420 | def info(self): |
| 421 | return self.tk.call('place', 'info', self._w) |
| 422 | def slaves(self): |
| 423 | return self.tk.splitlist(self.tk.call( |
| 424 | 'place', 'slaves', self._w)) |
| 425 | |
| 426 | default_root = None |
| 427 | |
| 428 | class Widget(Misc, Pack, Place): |
| 429 | def __init__(self, master, widgetName, cnf={}, extra=()): |
| 430 | global default_root |
| 431 | if not master: |
| 432 | if not default_root: |
| 433 | default_root = Tk() |
| 434 | master = default_root |
| 435 | if not default_root: |
| 436 | default_root = master |
| 437 | self.master = master |
| 438 | self.tk = master.tk |
| 439 | if cnf.has_key('name'): |
| 440 | name = cnf['name'] |
| 441 | del cnf['name'] |
| 442 | else: |
| 443 | name = `id(self)` |
| 444 | if master._w=='.': |
| 445 | self._w = '.' + name |
| 446 | else: |
| 447 | self._w = master._w + '.' + name |
| 448 | self.widgetName = widgetName |
| 449 | apply(self.tk.call, (widgetName, self._w) + extra) |
| 450 | Widget.config(self, cnf) |
| 451 | def config(self, cnf={}): |
| 452 | for k in cnf.keys(): |
| 453 | if type(k) == ClassType: |
| 454 | k.config(self, cnf[k]) |
| 455 | del cnf[k] |
| 456 | apply(self.tk.call, (self._w, 'configure') |
| 457 | + self._options(cnf)) |
| 458 | def __getitem__(self, key): |
| 459 | v = self.tk.split(self.tk.call( |
| 460 | self._w, 'configure', '-' + key)) |
| 461 | return v[4] |
| 462 | def __setitem__(self, key, value): |
| 463 | Widget.config(self, {key: value}) |
| 464 | def __str__(self): |
| 465 | return self._w |
| 466 | def __del__(self): |
| 467 | self.tk.call('destroy', self._w) |
| 468 | destroy = __del__ |
| 469 | def _do(self, name, args=()): |
| 470 | apply(self.tk.call, (self._w, name) + args) |
| 471 | |
| 472 | class Toplevel(Widget, Wm): |
| 473 | def __init__(self, master=None, cnf={}): |
| 474 | extra = () |
| 475 | if cnf.has_key('screen'): |
| 476 | extra = ('-screen', cnf['screen']) |
| 477 | del cnf['screen'] |
| 478 | if cnf.has_key('class'): |
| 479 | extra = extra + ('-class', cnf['class']) |
| 480 | del cnf['class'] |
| 481 | Widget.__init__(self, master, 'toplevel', cnf, extra) |
| 482 | self.iconname(self.tk.call('wm', 'iconname', '.')) |
| 483 | self.title(self.tk.call('wm', 'title', '.')) |
| 484 | |
| 485 | class Button(Widget): |
| 486 | def __init__(self, master=None, cnf={}): |
| 487 | Widget.__init__(self, master, 'button', cnf) |
| 488 | def tk_butEnter(self): |
| 489 | self.tk.call('tk_butEnter', self._w) |
| 490 | def tk_butLeave(self): |
| 491 | self.tk.call('tk_butLeave', self._w) |
| 492 | def tk_butDown(self): |
| 493 | self.tk.call('tk_butDown', self._w) |
| 494 | def tk_butUp(self): |
| 495 | self.tk.call('tk_butUp', self._w) |
| 496 | def flash(self): |
| 497 | self.tk.call(self._w, 'flash') |
| 498 | def invoke(self): |
| 499 | self.tk.call(self._w, 'invoke') |
| 500 | |
| 501 | # Indices: |
| 502 | def AtEnd(): |
| 503 | return 'end' |
Guido van Rossum | 1e9e400 | 1994-06-20 09:09:51 +0000 | [diff] [blame^] | 504 | def AtInsert(*args): |
| 505 | s = 'insert' |
| 506 | for a in args: |
| 507 | if a: s = s + (' ' + a) |
| 508 | return s |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 509 | def AtSelFirst(): |
| 510 | return 'sel.first' |
| 511 | def AtSelLast(): |
| 512 | return 'sel.last' |
| 513 | def At(x, y=None): |
| 514 | if y: |
| 515 | return '@' + `x` + ',' + `y` |
| 516 | else: |
| 517 | return '@' + `x` |
| 518 | |
| 519 | class Canvas(Widget): |
| 520 | def __init__(self, master=None, cnf={}): |
| 521 | Widget.__init__(self, master, 'canvas', cnf) |
| 522 | def addtag(self, *args): |
| 523 | self._do('addtag', args) |
| 524 | def bbox(self, *args): |
| 525 | return self._getints(self._do('bbox', args)) |
| 526 | def bind(self, tagOrId, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 527 | if add: add='+' |
| 528 | name = self._register(func, _substitute) |
| 529 | self.tk.call(self._w, 'bind', tagOrId, sequence, |
| 530 | (add + name,) + _subst_prefix) |
| 531 | def canvasx(self, screenx, gridspacing=None): |
| 532 | return self.tk.getint(self.tk.call( |
| 533 | self._w, 'canvasx', screenx, gridspacing)) |
| 534 | def canvasy(self, screeny, gridspacing=None): |
| 535 | return self.tk.getint(self.tk.call( |
| 536 | self._w, 'canvasy', screeny, gridspacing)) |
| 537 | def coords(self, *args): |
| 538 | return self._do('coords', args) |
| 539 | def _create(self, itemType, args): # Args: (value, value, ..., cnf={}) |
| 540 | cnf = args[-1] |
| 541 | if type(cnf) == type({}): |
| 542 | args = args[:-1] |
| 543 | else: |
| 544 | cnf = {} |
| 545 | v = (self._w, 'create', itemType) + args |
| 546 | for k in cnf.keys(): |
| 547 | v = v + ('-' + k, cnf[k]) |
| 548 | return self.tk.getint(apply(self.tk.call, v)) |
| 549 | def create_arc(self, *args): |
| 550 | Canvas._create(self, 'arc', args) |
| 551 | def create_bitmap(self, *args): |
| 552 | Canvas._create(self, 'bitmap', args) |
| 553 | def create_line(self, *args): |
| 554 | Canvas._create(self, 'line', args) |
| 555 | def create_oval(self, *args): |
| 556 | Canvas._create(self, 'oval', args) |
| 557 | def create_polygon(self, *args): |
| 558 | Canvas._create(self, 'polygon', args) |
| 559 | def create_rectangle(self, *args): |
| 560 | Canvas._create(self, 'rectangle', args) |
| 561 | def create_text(self, *args): |
| 562 | Canvas._create(self, 'text', args) |
| 563 | def create_window(self, *args): |
| 564 | Canvas._create(self, 'window', args) |
| 565 | def dchars(self, *args): |
| 566 | self._do('dchars', args) |
| 567 | def delete(self, *args): |
| 568 | self._do('delete', args) |
| 569 | def dtag(self, *args): |
| 570 | self._do('dtag', args) |
| 571 | def find(self, *args): |
| 572 | self.tk.splitlist(self._do('find', args)) |
| 573 | def focus(self, *args): |
| 574 | return self._do('focus', args) |
| 575 | def gettags(self, *args): |
| 576 | return self.tk.splitlist(self._do('gettags', args)) |
| 577 | def icursor(self, *args): |
| 578 | self._do('icursor', args) |
| 579 | def index(self, *args): |
| 580 | return self.tk.getint(self._do('index', args)) |
| 581 | def insert(self, *args): |
| 582 | self._do('insert', args) |
| 583 | def itemconfig(self, tagOrId, cnf={}): |
| 584 | self._do('itemconfigure', (tagOrId,) + self._options(cnf)) |
| 585 | def lower(self, *args): |
| 586 | self._do('lower', args) |
| 587 | def move(self, *args): |
| 588 | self._do('move', args) |
| 589 | def postscript(self, cnf={}): |
| 590 | return self._do('postscript', self._options(cnf)) |
| 591 | def tkraise(self, *args): |
| 592 | self._do('raise', args) |
Guido van Rossum | 5e8d372 | 1994-06-20 08:12:01 +0000 | [diff] [blame] | 593 | lift = tkraise |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 594 | def scale(self, *args): |
| 595 | self._do('scale', args) |
| 596 | def scan_mark(self, x, y): |
| 597 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 598 | def scan_dragto(self, x, y): |
| 599 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 600 | def select_adjust(self, tagOrId, index): |
| 601 | self.tk.call( |
| 602 | self._w, 'select', 'adjust', tagOrId, index) |
| 603 | def select_clear(self): |
| 604 | self.tk.call(self._w, 'select', 'clear') |
| 605 | def select_from(self, tagOrId, index): |
| 606 | self.tk.call( |
| 607 | self._w, 'select', 'from', tagOrId, index) |
| 608 | def select_item(self): |
| 609 | self.tk.call(self._w, 'select', 'item') |
| 610 | def select_to(self, tagOrId, index): |
| 611 | self.tk.call( |
| 612 | self._w, 'select', 'to', tagOrId, index) |
| 613 | def type(self, tagOrId): |
| 614 | return self.tk.splitlist(self.tk.call( |
| 615 | self._w, 'type', tagOrId)) |
| 616 | def xview(self, index): |
| 617 | self.tk.call(self._w, 'xview', index) |
| 618 | def yview(self, index): |
| 619 | self.tk.call(self._w, 'yview', index) |
| 620 | |
| 621 | class Checkbutton(Widget): |
| 622 | def __init__(self, master=None, cnf={}): |
| 623 | Widget.__init__(self, master, 'checkbutton', cnf) |
| 624 | def deselect(self): |
| 625 | self.tk.call(self._w, 'deselect') |
| 626 | def flash(self): |
| 627 | self.tk.call(self._w, 'flash') |
| 628 | def invoke(self): |
| 629 | self.tk.call(self._w, 'invoke') |
| 630 | def select(self): |
| 631 | self.tk.call(self._w, 'select') |
| 632 | def toggle(self): |
| 633 | self.tk.call(self._w, 'toggle') |
| 634 | |
| 635 | class Entry(Widget): |
| 636 | def __init__(self, master=None, cnf={}): |
| 637 | Widget.__init__(self, master, 'entry', cnf) |
| 638 | def tk_entryBackspace(self): |
| 639 | self.tk.call('tk_entryBackspace', self._w) |
| 640 | def tk_entryBackword(self): |
| 641 | self.tk.call('tk_entryBackword', self._w) |
| 642 | def tk_entrySeeCaret(self): |
| 643 | self.tk.call('tk_entrySeeCaret', self._w) |
| 644 | def delete(self, first, last=None): |
| 645 | self.tk.call(self._w, 'delete', first, last) |
| 646 | def get(self): |
| 647 | return self.tk.call(self._w, 'get') |
| 648 | def icursor(self, index): |
| 649 | self.tk.call(self._w, 'icursor', index) |
| 650 | def index(self, index): |
| 651 | return self.tk.getint(self.tk.call( |
| 652 | self._w, 'index', index)) |
| 653 | def insert(self, index, string): |
| 654 | self.tk.call(self._w, 'insert', index, string) |
| 655 | def scan_mark(self, x): |
| 656 | self.tk.call(self._w, 'scan', 'mark', x) |
| 657 | def scan_dragto(self, x): |
| 658 | self.tk.call(self._w, 'scan', 'dragto', x) |
| 659 | def select_adjust(self, index): |
| 660 | self.tk.call(self._w, 'select', 'adjust', index) |
| 661 | def select_clear(self): |
| 662 | self.tk.call(self._w, 'select', 'clear') |
| 663 | def select_from(self, index): |
| 664 | self.tk.call(self._w, 'select', 'from', index) |
| 665 | def select_to(self, index): |
| 666 | self.tk.call(self._w, 'select', 'to', index) |
| 667 | def select_view(self, index): |
| 668 | self.tk.call(self._w, 'select', 'view', index) |
| 669 | |
| 670 | class Frame(Widget): |
| 671 | def __init__(self, master=None, cnf={}): |
| 672 | extra = () |
| 673 | if cnf.has_key('class'): |
| 674 | extra = ('-class', cnf['class']) |
| 675 | del cnf['class'] |
| 676 | Widget.__init__(self, master, 'frame', cnf, extra) |
| 677 | def tk_menuBar(self, *args): |
| 678 | apply(self.tk.call, ('tk_menuBar', self._w) + args) |
| 679 | |
| 680 | class Label(Widget): |
| 681 | def __init__(self, master=None, cnf={}): |
| 682 | Widget.__init__(self, master, 'label', cnf) |
| 683 | |
| 684 | class Listbox(Widget): |
| 685 | def __init__(self, master=None, cnf={}): |
| 686 | Widget.__init__(self, master, 'listbox', cnf) |
| 687 | def tk_listboxSingleSelect(self): |
| 688 | self.tk.call('tk_listboxSingleSelect', self._w) |
| 689 | def curselection(self): |
| 690 | return self.tk.splitlist(self.tk.call( |
| 691 | self._w, 'curselection')) |
| 692 | def delete(self, first, last=None): |
| 693 | self.tk.call(self._w, 'delete', first, last) |
| 694 | def get(self, index): |
| 695 | return self.tk.call(self._w, 'get', index) |
| 696 | def insert(self, index, *elements): |
| 697 | apply(self.tk.call, |
| 698 | (self._w, 'insert', index) + elements) |
| 699 | def nearest(self, y): |
| 700 | return self.tk.getint(self.tk.call( |
| 701 | self._w, 'nearest', y)) |
| 702 | def scan_mark(self, x, y): |
| 703 | self.tk.call(self._w, 'scan', 'mark', x, y) |
| 704 | def scan_dragto(self, x, y): |
| 705 | self.tk.call(self._w, 'scan', 'dragto', x, y) |
| 706 | def select_adjust(self, index): |
| 707 | self.tk.call(self._w, 'select', 'adjust', index) |
| 708 | def select_clear(self): |
| 709 | self.tk.call(self._w, 'select', 'clear') |
| 710 | def select_from(self, index): |
| 711 | self.tk.call(self._w, 'select', 'from', index) |
| 712 | def select_to(self, index): |
| 713 | self.tk.call(self._w, 'select', 'to', index) |
| 714 | def size(self): |
| 715 | return self.tk.getint(self.tk.call(self._w, 'size')) |
| 716 | def xview(self, index): |
| 717 | self.tk.call(self._w, 'xview', index) |
| 718 | def yview(self, index): |
| 719 | self.tk.call(self._w, 'yview', index) |
| 720 | |
| 721 | class Menu(Widget): |
| 722 | def __init__(self, master=None, cnf={}): |
| 723 | Widget.__init__(self, master, 'menu', cnf) |
| 724 | def tk_menuBar(self, *args): |
| 725 | apply(self.tk.call, ('tk_menuBar', self._w) + args) |
| 726 | def tk_bindForTraversal(self): |
| 727 | self.tk.call('tk_bindForTraversal', self._w) |
| 728 | def tk_mbPost(self): |
| 729 | self.tk.call('tk_mbPost', self._w) |
| 730 | def tk_mbUnpost(self): |
| 731 | self.tk.call('tk_mbUnpost') |
| 732 | def tk_traverseToMenu(self, char): |
| 733 | self.tk.call('tk_traverseToMenu', self._w, char) |
| 734 | def tk_traverseWithinMenu(self, char): |
| 735 | self.tk.call('tk_traverseWithinMenu', self._w, char) |
| 736 | def tk_getMenuButtons(self): |
| 737 | return self.tk.call('tk_getMenuButtons', self._w) |
| 738 | def tk_nextMenu(self, count): |
| 739 | self.tk.call('tk_nextMenu', count) |
| 740 | def tk_nextMenuEntry(self, count): |
| 741 | self.tk.call('tk_nextMenuEntry', count) |
| 742 | def tk_invokeMenu(self): |
| 743 | self.tk.call('tk_invokeMenu', self._w) |
| 744 | def tk_firstMenu(self): |
| 745 | self.tk.call('tk_firstMenu', self._w) |
| 746 | def tk_mbButtonDown(self): |
| 747 | self.tk.call('tk_mbButtonDown', self._w) |
| 748 | def activate(self, index): |
| 749 | self.tk.call(self._w, 'activate', index) |
| 750 | def add(self, itemType, cnf={}): |
| 751 | apply(self.tk.call, (self._w, 'add', itemType) |
| 752 | + self._options(cnf)) |
| 753 | def delete(self, index1, index2=None): |
| 754 | self.tk.call(self._w, 'delete', index1, index2) |
| 755 | def entryconfig(self, index, cnf={}): |
| 756 | apply(self.tk.call, (self._w, 'entryconfigure', index) |
| 757 | + self._options(cnf)) |
| 758 | def index(self, index): |
| 759 | return self.tk.call(self._w, 'index', index) |
| 760 | def invoke(self, index): |
| 761 | return self.tk.call(self._w, 'invoke', index) |
| 762 | def post(self, x, y): |
| 763 | self.tk.call(self._w, 'post', x, y) |
| 764 | def unpost(self): |
| 765 | self.tk.call(self._w, 'unpost') |
| 766 | def yposition(self, index): |
| 767 | return self.tk.getint(self.tk.call( |
| 768 | self._w, 'yposition', index)) |
| 769 | |
| 770 | class Menubutton(Widget): |
| 771 | def __init__(self, master=None, cnf={}): |
| 772 | Widget.__init__(self, master, 'menubutton', cnf) |
| 773 | |
| 774 | class Message(Widget): |
| 775 | def __init__(self, master=None, cnf={}): |
| 776 | Widget.__init__(self, master, 'message', cnf) |
| 777 | |
| 778 | class Radiobutton(Widget): |
| 779 | def __init__(self, master=None, cnf={}): |
| 780 | Widget.__init__(self, master, 'radiobutton', cnf) |
| 781 | def deselect(self): |
| 782 | self.tk.call(self._w, 'deselect') |
| 783 | def flash(self): |
| 784 | self.tk.call(self._w, 'flash') |
| 785 | def invoke(self): |
| 786 | self.tk.call(self._w, 'invoke') |
| 787 | def select(self): |
| 788 | self.tk.call(self._w, 'select') |
| 789 | |
| 790 | class Scale(Widget): |
| 791 | def __init__(self, master=None, cnf={}): |
| 792 | Widget.__init__(self, master, 'scale', cnf) |
| 793 | def get(self): |
| 794 | return self.tk.getint(self.tk.call(self._w, 'get')) |
| 795 | def set(self, value): |
| 796 | self.tk.call(self._w, 'set', value) |
| 797 | |
| 798 | class Scrollbar(Widget): |
| 799 | def __init__(self, master=None, cnf={}): |
| 800 | Widget.__init__(self, master, 'scrollbar', cnf) |
| 801 | def get(self): |
| 802 | return self.tk.getints(self.tk.call(self._w, 'get')) |
| 803 | def set(self, totalUnits, windowUnits, firstUnit, lastUnit): |
| 804 | self.tk.call(self._w, 'set', |
| 805 | totalUnits, windowUnits, firstUnit, lastUnit) |
| 806 | |
| 807 | class Text(Widget): |
| 808 | def __init__(self, master=None, cnf={}): |
| 809 | Widget.__init__(self, master, 'text', cnf) |
| 810 | def tk_textSelectTo(self, index): |
| 811 | self.tk.call('tk_textSelectTo', self._w, index) |
| 812 | def tk_textBackspace(self): |
| 813 | self.tk.call('tk_textBackspace', self._w) |
| 814 | def tk_textIndexCloser(self, a, b, c): |
| 815 | self.tk.call('tk_textIndexCloser', self._w, a, b, c) |
| 816 | def tk_textResetAnchor(self, index): |
| 817 | self.tk.call('tk_textResetAnchor', self._w, index) |
| 818 | def compare(self, index1, op, index2): |
| 819 | return self.tk.getboolean(self.tk.call( |
| 820 | self._w, 'compare', index1, op, index2)) |
| 821 | def debug(self, boolean=None): |
| 822 | return self.tk.getboolean(self.tk.call( |
| 823 | self._w, 'debug', boolean)) |
| 824 | def delete(self, index1, index2=None): |
| 825 | self.tk.call(self._w, 'delete', index1, index2) |
| 826 | def get(self, index1, index2=None): |
| 827 | return self.tk.call(self._w, 'get', index1, index2) |
| 828 | def index(self, index): |
| 829 | return self.tk.call(self._w, 'index', index) |
| 830 | def insert(self, index, chars): |
| 831 | self.tk.call(self._w, 'insert', index, chars) |
| 832 | def mark_names(self): |
| 833 | return self.tk.splitlist(self.tk.call( |
| 834 | self._w, 'mark', 'names')) |
| 835 | def mark_set(self, markName, index): |
| 836 | self.tk.call(self._w, 'mark', 'set', markName, index) |
| 837 | def mark_unset(self, markNames): |
| 838 | apply(self.tk.call, (self._w, 'mark', 'unset') + markNames) |
| 839 | def scan_mark(self, y): |
| 840 | self.tk.call(self._w, 'scan', 'mark', y) |
| 841 | def scan_dragto(self, y): |
| 842 | self.tk.call(self._w, 'scan', 'dragto', y) |
| 843 | def tag_add(self, tagName, index1, index2=None): |
| 844 | self.tk.call( |
| 845 | self._w, 'tag', 'add', tagName, index1, index2) |
| 846 | def tag_bind(self, tagName, sequence, func, add=''): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 847 | if add: add='+' |
| 848 | name = self._register(func, _substitute) |
| 849 | self.tk.call(self._w, 'tag', 'bind', |
| 850 | tagName, sequence, |
| 851 | (add + name,) + _subst_prefix) |
| 852 | def tag_config(self, tagName, cnf={}): |
| 853 | apply(self.tk.call, |
| 854 | (self._w, 'tag', 'configure', tagName) |
| 855 | + self._options(cnf)) |
| 856 | def tag_delete(self, tagNames): |
| 857 | apply(self.tk.call, (self._w, 'tag', 'delete') |
| 858 | + tagNames) |
| 859 | def tag_lower(self, tagName, belowThis=None): |
| 860 | self.tk.call(self._w, 'tag', 'lower', |
| 861 | tagName, belowThis) |
| 862 | def tag_names(self, index=None): |
| 863 | return self.tk.splitlist( |
| 864 | self.tk.call(self._w, 'tag', 'names', index)) |
| 865 | def tag_nextrange(self, tagName, index1, index2=None): |
| 866 | return self.tk.splitlist(self.tk.call( |
| 867 | self._w, 'tag', 'nextrange', index1, index2)) |
| 868 | def tag_raise(self, tagName, aboveThis=None): |
| 869 | self.tk.call( |
| 870 | self._w, 'tag', 'raise', tagName, aboveThis) |
| 871 | def tag_ranges(self, tagName): |
| 872 | return self.tk.splitlist(self.tk.call( |
| 873 | self._w, 'tag', 'ranges', tagName)) |
| 874 | def tag_remove(self, tagName, index1, index2=None): |
| 875 | self.tk.call( |
| 876 | self._w, 'tag', 'remove', index1, index2) |
| 877 | def yview(self, what): |
| 878 | self.tk.call(self._w, 'yview', what) |
| 879 | def yview_pickplace(self, what): |
| 880 | self.tk.call(self._w, 'yview', '-pickplace', what) |
| 881 | |
| 882 | #class Dialog: |
| 883 | |