📄 tkinter.py
字号:
def select(self): self.tk.call(self._w, 'select')class Scale(Widget): def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'scale', cnf, kw) def get(self): value = self.tk.call(self._w, 'get') try: return getint(value) except ValueError: return getdouble(value) def set(self, value): self.tk.call(self._w, 'set', value) def coords(self, value=None): return self._getints(self.tk.call(self._w, 'coords', value)) def identify(self, x, y): return self.tk.call(self._w, 'identify', x, y)class Scrollbar(Widget): def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'scrollbar', cnf, kw) def activate(self, index): self.tk.call(self._w, 'activate', index) def delta(self, deltax, deltay): return getdouble( self.tk.call(self._w, 'delta', deltax, deltay)) def fraction(self, x, y): return getdouble(self.tk.call(self._w, 'fraction', x, y)) def identify(self, x, y): return self.tk.call(self._w, 'identify', x, y) def get(self): return self._getdoubles(self.tk.call(self._w, 'get')) def set(self, *args): self.tk.call((self._w, 'set') + args)class Text(Widget): # XXX Add dump() def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'text', cnf, kw) def bbox(self, *args): return self._getints( self.tk.call((self._w, 'bbox') + args)) or None def tk_textSelectTo(self, index): self.tk.call('tk_textSelectTo', self._w, index) def tk_textBackspace(self): self.tk.call('tk_textBackspace', self._w) def tk_textIndexCloser(self, a, b, c): self.tk.call('tk_textIndexCloser', self._w, a, b, c) def tk_textResetAnchor(self, index): self.tk.call('tk_textResetAnchor', self._w, index) def compare(self, index1, op, index2): return self.tk.getboolean(self.tk.call( self._w, 'compare', index1, op, index2)) def debug(self, boolean=None): return self.tk.getboolean(self.tk.call( self._w, 'debug', boolean)) def delete(self, index1, index2=None): self.tk.call(self._w, 'delete', index1, index2) def dlineinfo(self, index): return self._getints(self.tk.call(self._w, 'dlineinfo', index)) def get(self, index1, index2=None): return self.tk.call(self._w, 'get', index1, index2) # (Image commands are new in 8.0) def image_cget(self, index, option): if option[:1] != "-": option = "-" + option if option[-1:] == "_": option = option[:-1] return self.tk.call(self._w, "image", "cget", index, option) def image_configure(self, index, cnf={}, **kw): if not cnf and not kw: cnf = {} for x in self.tk.split( self.tk.call( self._w, "image", "configure", index)): cnf[x[0][1:]] = (x[0][1:],) + x[1:] return cnf apply(self.tk.call, (self._w, "image", "configure", index) + self._options(cnf, kw)) def image_create(self, index, cnf={}, **kw): return apply(self.tk.call, (self._w, "image", "create", index) + self._options(cnf, kw)) def image_names(self): return self.tk.call(self._w, "image", "names") def index(self, index): return self.tk.call(self._w, 'index', index) def insert(self, index, chars, *args): self.tk.call((self._w, 'insert', index, chars) + args) def mark_gravity(self, markName, direction=None): return self.tk.call( (self._w, 'mark', 'gravity', markName, direction)) def mark_names(self): return self.tk.splitlist(self.tk.call( self._w, 'mark', 'names')) def mark_set(self, markName, index): self.tk.call(self._w, 'mark', 'set', markName, index) def mark_unset(self, *markNames): self.tk.call((self._w, 'mark', 'unset') + markNames) def mark_next(self, index): return self.tk.call(self._w, 'mark', 'next', index) or None def mark_previous(self, index): return self.tk.call(self._w, 'mark', 'previous', index) or None def scan_mark(self, x, y): self.tk.call(self._w, 'scan', 'mark', x, y) def scan_dragto(self, x, y): self.tk.call(self._w, 'scan', 'dragto', x, y) def search(self, pattern, index, stopindex=None, forwards=None, backwards=None, exact=None, regexp=None, nocase=None, count=None): args = [self._w, 'search'] if forwards: args.append('-forwards') if backwards: args.append('-backwards') if exact: args.append('-exact') if regexp: args.append('-regexp') if nocase: args.append('-nocase') if count: args.append('-count'); args.append(count) if pattern[0] == '-': args.append('--') args.append(pattern) args.append(index) if stopindex: args.append(stopindex) return self.tk.call(tuple(args)) def see(self, index): self.tk.call(self._w, 'see', index) def tag_add(self, tagName, index1, *args): self.tk.call( (self._w, 'tag', 'add', tagName, index1) + args) def tag_unbind(self, tagName, sequence, funcid=None): self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '') if funcid: self.deletecommand(funcid) def tag_bind(self, tagName, sequence, func, add=None): return self._bind((self._w, 'tag', 'bind', tagName), sequence, func, add) def tag_cget(self, tagName, option): if option[:1] != '-': option = '-' + option if option[-1:] == '_': option = option[:-1] return self.tk.call(self._w, 'tag', 'cget', tagName, option) def tag_configure(self, tagName, cnf={}, **kw): if type(cnf) == StringType: x = self.tk.split(self.tk.call( self._w, 'tag', 'configure', tagName, '-'+cnf)) return (x[0][1:],) + x[1:] self.tk.call( (self._w, 'tag', 'configure', tagName) + self._options(cnf, kw)) tag_config = tag_configure def tag_delete(self, *tagNames): self.tk.call((self._w, 'tag', 'delete') + tagNames) def tag_lower(self, tagName, belowThis=None): self.tk.call(self._w, 'tag', 'lower', tagName, belowThis) def tag_names(self, index=None): return self.tk.splitlist( self.tk.call(self._w, 'tag', 'names', index)) def tag_nextrange(self, tagName, index1, index2=None): return self.tk.splitlist(self.tk.call( self._w, 'tag', 'nextrange', tagName, index1, index2)) def tag_prevrange(self, tagName, index1, index2=None): return self.tk.splitlist(self.tk.call( self._w, 'tag', 'prevrange', tagName, index1, index2)) def tag_raise(self, tagName, aboveThis=None): self.tk.call( self._w, 'tag', 'raise', tagName, aboveThis) def tag_ranges(self, tagName): return self.tk.splitlist(self.tk.call( self._w, 'tag', 'ranges', tagName)) def tag_remove(self, tagName, index1, index2=None): self.tk.call( self._w, 'tag', 'remove', tagName, index1, index2) def window_cget(self, index, option): if option[:1] != '-': option = '-' + option if option[-1:] == '_': option = option[:-1] return self.tk.call(self._w, 'window', 'cget', index, option) def window_configure(self, index, cnf={}, **kw): if type(cnf) == StringType: x = self.tk.split(self.tk.call( self._w, 'window', 'configure', index, '-'+cnf)) return (x[0][1:],) + x[1:] self.tk.call( (self._w, 'window', 'configure', index) + self._options(cnf, kw)) window_config = window_configure def window_create(self, index, cnf={}, **kw): self.tk.call( (self._w, 'window', 'create', index) + self._options(cnf, kw)) def window_names(self): return self.tk.splitlist( self.tk.call(self._w, 'window', 'names')) def xview(self, *what): if not what: return self._getdoubles(self.tk.call(self._w, 'xview')) self.tk.call((self._w, 'xview') + what) def yview(self, *what): if not what: return self._getdoubles(self.tk.call(self._w, 'yview')) self.tk.call((self._w, 'yview') + what) def yview_pickplace(self, *what): self.tk.call((self._w, 'yview', '-pickplace') + what)class _setit: def __init__(self, var, value, callback=None): self.__value = value self.__var = var self.__callback = callback def __call__(self, *args): self.__var.set(self.__value) if self.__callback: apply(self.__callback, (self.__value,)+args)class OptionMenu(Menubutton): def __init__(self, master, variable, value, *values, **kwargs): kw = {"borderwidth": 2, "textvariable": variable, "indicatoron": 1, "relief": RAISED, "anchor": "c", "highlightthickness": 2} Widget.__init__(self, master, "menubutton", kw) self.widgetName = 'tk_optionMenu' menu = self.__menu = Menu(self, name="menu", tearoff=0) self.menuname = menu._w # 'command' is the only supported keyword callback = kwargs.get('command') if kwargs.has_key('command'): del kwargs['command'] if kwargs: raise TclError, 'unknown option -'+kwargs.keys()[0] menu.add_command(label=value, command=_setit(variable, value, callback)) for v in values: menu.add_command(label=v, command=_setit(variable, v, callback)) self["menu"] = menu def __getitem__(self, name): if name == 'menu': return self.__menu return Widget.__getitem__(self, name) def destroy(self): Menubutton.destroy(self) self.__menu = Noneclass Image: def __init__(self, imgtype, name=None, cnf={}, master=None, **kw): self.name = None if not master: master = _default_root if not master: raise RuntimeError, 'Too early to create image' self.tk = master.tk if not name: name = `id(self)` # The following is needed for systems where id(x) # can return a negative number, such as Linux/m68k: if name[0] == '-': name = '_' + name[1:] if kw and cnf: cnf = _cnfmerge((cnf, kw)) elif kw: cnf = kw options = () for k, v in cnf.items(): if callable(v): v = self._register(v) options = options + ('-'+k, v) self.tk.call(('image', 'create', imgtype, name,) + options) self.name = name def __str__(self): return self.name def __del__(self): if self.name: try: self.tk.call('image', 'delete', self.name) except TclError: # May happen if the root was destroyed pass def __setitem__(self, key, value): self.tk.call(self.name, 'configure', '-'+key, value) def __getitem__(self, key): return self.tk.call(self.name, 'configure', '-'+key) def configure(self, **kw): res = () for k, v in _cnfmerge(kw).items(): if v is not None: if k[-1] == '_': k = k[:-1] if callable(v): v = self._register(v) res = res + ('-'+k, v) self.tk.call((self.name, 'config') + res) config = configure def height(self): return getint( self.tk.call('image', 'height', self.name)) def type(self): return self.tk.call('image', 'type', self.name) def width(self): return getint( self.tk.call('image', 'width', self.name))class PhotoImage(Image): def __init__(self, name=None, cnf={}, master=None, **kw): apply(Image.__init__, (self, 'photo', name, cnf, master), kw) def blank(self): self.tk.call(self.name, 'blank') def cget(self, option): return self.tk.call(self.name, 'cget', '-' + option) # XXX config def __getitem__(self, key): return self.tk.call(self.name, 'cget', '-' + key) # XXX copy -from, -to, ...? def copy(self): destImage = PhotoImage() self.tk.call(destImage, 'copy', self.name) return destImage def zoom(self,x,y=''): destImage = PhotoImage() if y=='': y=x self.tk.call(destImage, 'copy', self.name, '-zoom',x,y) return destImage def subsample(self,x,y=''): destImage = PhotoImage() if y=='': y=x self.tk.call(destImage, 'copy', self.name, '-subsample',x,y) return destImage def get(self, x, y): return self.tk.call(self.name, 'get', x, y) def put(self, data, to=None): args = (self.name, 'put', data) if to: if to[0] == '-to': to = to[1:] args = args + ('-to',) + tuple(to) self.tk.call(args) # XXX read def write(self, filename, format=None, from_coords=None): args = (self.name, 'write', filename) if format: args = args + ('-format', format) if from_coords: args = args + ('-from',) + tuple(from_coords) self.tk.call(args)class BitmapImage(Image): def __init__(self, name=None, cnf={}, master=None, **kw): apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)def image_names(): return _default_root.tk.call('image', 'names')def image_types(): return _default_root.tk.call('image', 'types')####################################################################### Extensions:class Studbutton(Button): def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'studbutton', cnf, kw) self.bind('<Any-Enter>', self.tkButtonEnter) self.bind('<Any-Leave>', self.tkButtonLeave) self.bind('<1>', self.tkButtonDown) self.bind('<ButtonRelease-1>', self.tkButtonUp)class Tributton(Button): def __init__(self, master=None, cnf={}, **kw): Widget.__init__(self, master, 'tributton', cnf, kw) self.bind('<Any-Enter>', self.tkButtonEnter) self.bind('<Any-Leave>', self.tkButtonLeave) self.bind('<1>', self.tkButtonDown) self.bind('<ButtonRelease-1>', self.tkButtonUp) self['fg'] = self['bg'] self['activebackground'] = self['bg']####################################################################### Test:def _test(): root = Tk() label = Label(root, text="Proof-of-existence test for Tk") label.pack() test = Button(root, text="Click me!", command=lambda root=root: root.test.configure( text="[%s]" % root.test['text'])) test.pack() root.test = test quit = Button(root, text="QUIT", command=root.destroy) quit.pack() # The following three commands are needed so the window pops # up on top on Windows... root.iconify() root.update() root.deiconify() root.mainloop()if __name__ == '__main__': _test()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -