📄 tkinter.py
字号:
command = wm_command def wm_deiconify(self): return self.tk.call('wm', 'deiconify', self._w) deiconify = wm_deiconify def wm_focusmodel(self, model=None): return self.tk.call('wm', 'focusmodel', self._w, model) focusmodel = wm_focusmodel def wm_frame(self): return self.tk.call('wm', 'frame', self._w) frame = wm_frame def wm_geometry(self, newGeometry=None): return self.tk.call('wm', 'geometry', self._w, newGeometry) geometry = wm_geometry def wm_grid(self, baseWidth=None, baseHeight=None, widthInc=None, heightInc=None): return self._getints(self.tk.call( 'wm', 'grid', self._w, baseWidth, baseHeight, widthInc, heightInc)) grid = wm_grid def wm_group(self, pathName=None): return self.tk.call('wm', 'group', self._w, pathName) group = wm_group def wm_iconbitmap(self, bitmap=None): return self.tk.call('wm', 'iconbitmap', self._w, bitmap) iconbitmap = wm_iconbitmap def wm_iconify(self): return self.tk.call('wm', 'iconify', self._w) iconify = wm_iconify def wm_iconmask(self, bitmap=None): return self.tk.call('wm', 'iconmask', self._w, bitmap) iconmask = wm_iconmask def wm_iconname(self, newName=None): return self.tk.call('wm', 'iconname', self._w, newName) iconname = wm_iconname def wm_iconposition(self, x=None, y=None): return self._getints(self.tk.call( 'wm', 'iconposition', self._w, x, y)) iconposition = wm_iconposition def wm_iconwindow(self, pathName=None): return self.tk.call('wm', 'iconwindow', self._w, pathName) iconwindow = wm_iconwindow def wm_maxsize(self, width=None, height=None): return self._getints(self.tk.call( 'wm', 'maxsize', self._w, width, height)) maxsize = wm_maxsize def wm_minsize(self, width=None, height=None): return self._getints(self.tk.call( 'wm', 'minsize', self._w, width, height)) minsize = wm_minsize def wm_overrideredirect(self, boolean=None): return self._getboolean(self.tk.call( 'wm', 'overrideredirect', self._w, boolean)) overrideredirect = wm_overrideredirect def wm_positionfrom(self, who=None): return self.tk.call('wm', 'positionfrom', self._w, who) positionfrom = wm_positionfrom def wm_protocol(self, name=None, func=None): if callable(func): command = self._register(func) else: command = func return self.tk.call( 'wm', 'protocol', self._w, name, command) protocol = wm_protocol def wm_resizable(self, width=None, height=None): return self.tk.call('wm', 'resizable', self._w, width, height) resizable = wm_resizable def wm_sizefrom(self, who=None): return self.tk.call('wm', 'sizefrom', self._w, who) sizefrom = wm_sizefrom def wm_state(self): return self.tk.call('wm', 'state', self._w) state = wm_state def wm_title(self, string=None): return self.tk.call('wm', 'title', self._w, string) title = wm_title def wm_transient(self, master=None): return self.tk.call('wm', 'transient', self._w, master) transient = wm_transient def wm_withdraw(self): return self.tk.call('wm', 'withdraw', self._w) withdraw = wm_withdrawclass Tk(Misc, Wm): _w = '.' def __init__(self, screenName=None, baseName=None, className='Tk'): global _default_root self.master = None self.children = {} if baseName is None: import sys, os baseName = os.path.basename(sys.argv[0]) baseName, ext = os.path.splitext(baseName) if ext not in ('.py', '.pyc', '.pyo'): baseName = baseName + ext self.tk = _tkinter.create(screenName, baseName, className) if _MacOS: # Disable event scanning except for Command-Period _MacOS.SchedParams(1, 0) # Work around nasty MacTk bug # XXX Is this one still needed? self.update() # Version sanity checks tk_version = self.tk.getvar('tk_version') if tk_version != _tkinter.TK_VERSION: raise RuntimeError, \ "tk.h version (%s) doesn't match libtk.a version (%s)" \ % (_tkinter.TK_VERSION, tk_version) tcl_version = self.tk.getvar('tcl_version') if tcl_version != _tkinter.TCL_VERSION: raise RuntimeError, \ "tcl.h version (%s) doesn't match libtcl.a version (%s)" \ % (_tkinter.TCL_VERSION, tcl_version) if TkVersion < 4.0: raise RuntimeError, \ "Tk 4.0 or higher is required; found Tk %s" \ % str(TkVersion) self.tk.createcommand('tkerror', _tkerror) self.tk.createcommand('exit', _exit) self.readprofile(baseName, className) if _support_default_root and not _default_root: _default_root = self self.protocol("WM_DELETE_WINDOW", self.destroy) def destroy(self): for c in self.children.values(): c.destroy() self.tk.call('destroy', self._w) Misc.destroy(self) global _default_root if _support_default_root and _default_root is self: _default_root = None def readprofile(self, baseName, className): import os if os.environ.has_key('HOME'): home = os.environ['HOME'] else: home = os.curdir class_tcl = os.path.join(home, '.%s.tcl' % className) class_py = os.path.join(home, '.%s.py' % className) base_tcl = os.path.join(home, '.%s.tcl' % baseName) base_py = os.path.join(home, '.%s.py' % baseName) dir = {'self': self} exec 'from Tkinter import *' in dir if os.path.isfile(class_tcl): print 'source', `class_tcl` self.tk.call('source', class_tcl) if os.path.isfile(class_py): print 'execfile', `class_py` execfile(class_py, dir) if os.path.isfile(base_tcl): print 'source', `base_tcl` self.tk.call('source', base_tcl) if os.path.isfile(base_py): print 'execfile', `base_py` execfile(base_py, dir) def report_callback_exception(self, exc, val, tb): import traceback, sys sys.stderr.write("Exception in Tkinter callback\n") sys.last_type = exc sys.last_value = val sys.last_traceback = tb traceback.print_exception(exc, val, tb)# Ideally, the classes Pack, Place and Grid disappear, the# pack/place/grid methods are defined on the Widget class, and# everybody uses w.pack_whatever(...) instead of Pack.whatever(w,# ...), with pack(), place() and grid() being short for# pack_configure(), place_configure() and grid_columnconfigure(), and# forget() being short for pack_forget(). As a practical matter, I'm# afraid that there is too much code out there that may be using the# Pack, Place or Grid class, so I leave them intact -- but only as# backwards compatibility features. Also note that those methods that# take a master as argument (e.g. pack_propagate) have been moved to# the Misc class (which now incorporates all methods common between# toplevel and interior widgets). Again, for compatibility, these are# copied into the Pack, Place or Grid class.class Pack: def pack_configure(self, cnf={}, **kw): self.tk.call( ('pack', 'configure', self._w) + self._options(cnf, kw)) pack = configure = config = pack_configure def pack_forget(self): self.tk.call('pack', 'forget', self._w) forget = pack_forget def pack_info(self): words = self.tk.splitlist( self.tk.call('pack', 'info', self._w)) dict = {} for i in range(0, len(words), 2): key = words[i][1:] value = words[i+1] if value[:1] == '.': value = self._nametowidget(value) dict[key] = value return dict info = pack_info propagate = pack_propagate = Misc.pack_propagate slaves = pack_slaves = Misc.pack_slavesclass Place: def place_configure(self, cnf={}, **kw): for k in ['in_']: if kw.has_key(k): kw[k[:-1]] = kw[k] del kw[k] self.tk.call( ('place', 'configure', self._w) + self._options(cnf, kw)) place = configure = config = place_configure def place_forget(self): self.tk.call('place', 'forget', self._w) forget = place_forget def place_info(self): words = self.tk.splitlist( self.tk.call('place', 'info', self._w)) dict = {} for i in range(0, len(words), 2): key = words[i][1:] value = words[i+1] if value[:1] == '.': value = self._nametowidget(value) dict[key] = value return dict info = place_info slaves = place_slaves = Misc.place_slavesclass Grid: # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu) def grid_configure(self, cnf={}, **kw): self.tk.call( ('grid', 'configure', self._w) + self._options(cnf, kw)) grid = configure = config = grid_configure bbox = grid_bbox = Misc.grid_bbox columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure def grid_forget(self): self.tk.call('grid', 'forget', self._w) forget = grid_forget def grid_remove(self): self.tk.call('grid', 'remove', self._w) def grid_info(self): words = self.tk.splitlist( self.tk.call('grid', 'info', self._w)) dict = {} for i in range(0, len(words), 2): key = words[i][1:] value = words[i+1] if value[:1] == '.': value = self._nametowidget(value) dict[key] = value return dict info = grid_info def grid_location(self, x, y): return self._getints( self.tk.call( 'grid', 'location', self._w, x, y)) or None location = grid_location propagate = grid_propagate = Misc.grid_propagate rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure size = grid_size = Misc.grid_size slaves = grid_slaves = Misc.grid_slavesclass BaseWidget(Misc): def _setup(self, master, cnf): if _support_default_root: global _default_root if not master: if not _default_root: _default_root = Tk() master = _default_root self.master = master self.tk = master.tk name = None if cnf.has_key('name'): name = cnf['name'] del cnf['name'] if not name: name = `id(self)` self._name = name if master._w=='.': self._w = '.' + name else: self._w = master._w + '.' + name self.children = {} if self.master.children.has_key(self._name): self.master.children[self._name].destroy() self.master.children[self._name] = self def __init__(self, master, widgetName, cnf={}, kw={}, extra=()): if kw: cnf = _cnfmerge((cnf, kw)) self.widgetName = widgetName BaseWidget._setup(self, master, cnf) classes = [] for k in cnf.keys(): if type(k) is ClassType: classes.append((k, cnf[k])) del cnf[k] self.tk.call( (widgetName, self._w) + extra + self._options(cnf)) for k, v in classes: k.configure(self, v) def destroy(self): for c in self.children.values(): c.destroy() if self.master.children.has_key(self._name): del self.master.children[self._name] self.tk.call('destroy', self._w) Misc.destroy(self) def _do(self, name, args=()): # XXX Obsolete -- better use self.tk.call directly! return self.tk.call((self._w, name) + args)class Widget(BaseWidget, Pack, Place, Grid): passclass Toplevel(BaseWidget, Wm): def __init__(self, master=None, cnf={}, **kw): if kw: cnf = _cnfmerge((cnf, kw)) extra = () for wmkey in ['screen', 'class_', 'class', 'visual', 'colormap']: if cnf.has_key(wmkey): val = cnf[wmkey] # TBD: a hack needed because some keys # are not valid as keyword arguments if wmkey[-1] == '_': opt = '-'+wmkey[:-1] else: opt = '-'+wmkey extra = extra + (opt, val) del cnf[wmkey] BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra) root = self._root() self.iconname(root.iconname()) self.title(root.title()) self.protocol("WM_DELETE_WINDOW", self.destroy)class Button(Widget): def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'button', cnf, kw) def tkButtonEnter(self, *dummy): self.tk.call('tkButtonEnter', self._w) def tkButtonLeave(self, *dummy): self.tk.call('tkButtonLeave', self._w) def tkButtonDown(self, *dummy): self.tk.call('tkButtonDown', self._w) def tkButtonUp(self, *dummy): self.tk.call('tkButtonUp', self._w) def tkButtonInvoke(self, *dummy): self.tk.call('tkButtonInvoke', self._w) def flash(self): self.tk.call(self._w, 'flash') def invoke(self): return self.tk.call(self._w, 'invoke')# Indices:# XXX I don't like these -- take them awaydef AtEnd(): return 'end'def AtInsert(*args): s = 'insert' for a in args: if a: s = s + (' ' + a) return sdef AtSelFirst(): return 'sel.first'def AtSelLast(): return 'sel.last'def At(x, y=None): if y is None: return '@' + `x` else: return '@' + `x` + ',' + `y`class Canvas(Widget): def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'canvas', cnf, kw) def addtag(self, *args): self.tk.call((self._w, 'addtag') + args) def addtag_above(self, newtag, tagOrId): self.addtag(newtag, 'above', tagOrId) def addtag_all(self, newtag): self.addtag(newtag, 'all') def addtag_below(self, newtag, tagOrId): self.addtag(newtag, 'below', tagOrId) def addtag_closest(self, newtag, x, y, halo=None, start=None): self.addtag(newtag, 'closest', x, y, halo, start) def addtag_enclosed(self, newtag, x1, y1, x2, y2): self.addtag(newtag, 'enclosed', x1, y1, x2, y2) def addtag_overlapping(self, newtag, x1, y1, x2, y2): self.addtag(newtag, 'overlapping', x1, y1, x2, y2) def addtag_withtag(self, newtag, tagOrId): self.addtag(newtag, 'withtag', tagOrId) def bbox(self, *args): return self._getints( self.tk.call((self._w, 'bbox') + args)) or None def tag_unbind(self, tagOrId, sequence, funcid=None):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -