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