目录:Python tkinter 从入门到放弃(1)_BBBBBBBBBBBBBBBUG的博客-CSDN博客_python tkinter

文章目录

一.窗口表面

1.标题

2.窗口大小和位置

(1).大小

(2).位置

3.消息循环

4.窗口外部

5.窗口的最大与最小

(1).窗口最大

(2).窗口最小

(3).固定窗口大小

二.窗口事件

1.刷新窗口

2.销毁窗口

3.自动触发事件

4.置顶窗口

欢迎补充

附本章所涉及的函数源码

一.窗口表面

1.标题

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.title('我是窗口')    #设置窗口标签root.mainloop()    #消息循环

细心的读者会发现在布局函数博客中,所有的窗口标题都是tk,因为如果在代码中没有声明窗口标题时,窗口标题就会自动设置成tk,如图:

如果我们设置了标题(如上方代码中的title函数),tkinter就会设置成我们输入的汉字或字符

但是设置的标题太长或窗口太小(以后会讲)时, 就会显示 '...'

2.窗口大小和位置

(1).大小

注:窗口大小只跟灰色的大小有关,与白色框无关

当我们在窗口内不放置任何控件时,窗口大小就是200×200 (单位:像素)

当我们窗口内有控件时,窗口大小就是这个控件(或一堆)的最小长和宽(除place函数)

如果我们要自定义大小时,函数geometry就可以帮助我们

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.geometry('600x400')    #设置窗口大小root.mainloop()    #消息循环

注意:'600x400'中间是小写x而不是乘号

写法:geometry(’长x宽')

(2).位置

如果读者想要在电脑屏幕的固定位置弹出这个窗口,就需要再添加两个参数

写法:geometry('长x宽+电脑位置(x轴)+电脑位置(y轴)')

或geometry('+电脑位置(x轴)+电脑位置(y轴)')

注:位置前必须要加’+‘号

3.消息循环

如果读者在之前看过或者知道tkinter可以做游戏,但不加循环的话是做不了游戏的,所以tkinter给我们了一个函数 mainloop() 消息循环

用法:窗口变量.mainloop()

注:mainloop必须在程序结尾,在中间就会运行不完

当消息循环在结尾:

#当mainloop在代码结尾时import tkinter as tk
root = tk.Tk()'''代码段'''root.mainloop()#       ↓
#  等价 ↓
#       ↓while True:'''代码段'''if 点击退出:break
return

当消息循环不在结尾:

#当mainloop不在在代码结尾时import tkinter as tk
root = tk.Tk()'''代码段'''root.mainloop()'''代码段'''#       ↓
#  等价 ↓
#       ↓while True:'''代码段'''if 点击退出:break
return
'''代码段'''    #运行不到

mainloop函数可以消息循环多个窗口,如:

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
window = tk.Tk()'''代码段'''root.mainloop()    #消息循环
window.mainloop()#         ↓
#  等价于 ↓
#         ↓while True:while True:'''代码段'''if 点击退出:breakif 点击退出:break
return 

4.窗口外部

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.geometry('+300+300')root.overrideredirect(True)    #隐藏外框#或1,False,0
root.mainloop()    #消息循环

运行:

如图所示,overrideredirect函数的作用是将外部去掉,也就是去掉标题,最小化,最大化和退出

注:使用overrideredirect后切换后台不显示该窗口且拖拽不了,并不推荐使用

5.窗口的最大与最小

(1).窗口最大

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.maxsize(300,300)root.mainloop()    #消息循环

运行:

效果:窗口最大不超过设置的大小

写法:maxsize(x最大,y最大)

(2).窗口最小

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.minsize(100,100)root.mainloop()    #消息循环

运行:

效果:窗口最小不超过设置的大小

写法:maxsize(x最小,y最小)

(3).固定窗口大小

固定窗口大小有两种方法:

#方法一
import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.maxsize(300,300)
root.minsize(300,300)root.mainloop()    #消息循环#方法二
import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.geometry('300x300')
root.resizable(False,False)root.mainloop()    #消息循环

方法一就不说了(作者懒)

方法二:

写法:resizable(窗口x轴是否可以改变,窗口y轴是否可以改变)

运行:

二.窗口事件

1.刷新窗口

窗口内事件较多时,mainloop函数可能反应不过来,就会导致一系列BUG,这时就可以使用刷新窗口update()

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口'''事件较多时'''
def back():'''n个事件'''root.update()root.mainloop()    #消息循环

2.销毁窗口

在实现登录系统之类的需要自动关闭窗口的程序时,就会使用销毁窗口函数destroy()

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口def Break():root.destroy()    #等价于del rootroot.mainloop()    #消息循环

注:使用destroy后就不能再调用窗口变量(root)了

3.自动触发事件

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口def back():'''事件'''root.after(1500,back)root.mainloop()    #消息循环

写法:after(时间(单位:毫秒),函数)

意义:在1.5秒后触发back这个函数

4.置顶窗口

import tkinter as tk    #导入tkinter模块
root = tk.Tk()    #创建窗口
root.wm_attributes('-topmost',True)    #置顶窗口root.mainloop()    #消息循环

写法:wm_attributes('-topmost',是否置顶)

欢迎补充

附本章所涉及的函数源码

后面的读者自行翻译(作者懒)

class Misc:"""定义内部小部件通用方法的基类"""# 定义内部小部件通用方法的基类。_last_child_ids = None# XXX 字体命令_tclCommands = Nonedef destroy(self):"""内部功能,删除在 tcl 解释器中为这个小部件创建的所有 tcl 命令"""if self._tclCommands is not None:for name in self._tclCommands:#print '- Tkinter: deleted command', nameself.tk.deletecommand(name)self._tclCommands = Nonedef deletecommand(self, name):"""内部函数,删除名称中提供的 tcl 命令"""#打印:Tkinter'已删除命令’,名称self.tk.deletecommand(name)try:self._tclCommands.remove(name)except ValueError:passdef tk_strictMotif(self, boolean=None):"""设置 tcl 内部变量,是否外观和感觉应该坚持主题。一个1的参数意味着坚持主题(例如,如果鼠标经过滑块就不会改变颜色)。返回设置的值"""return self.tk.getboolean(self.tk.call('set', 'tk_strictMotif', boolean))def tk_bisque(self):"""将配色方案改为浅棕色,如 tk3.6 及之前所用"""self.tk.call('tk_bisque')def tk_setPalette(self, *args, **kw):"""为所有小部件元素设置一个新的配色方案。作为参数的单一颜色将导致 tk 小部件元素的所有颜色都从。或者,可以给出几个关键字参数及其相关的颜色。以下关键字是有效的: activeBackground, foreground, selectColor,activeForeground, highlightBackground, selectBackground,background, highlightColor, selectForeground,disabledForeground, insertBackground, troughColor"""self.tk.call(('tk_setPalette',)+ _flatten(args) + _flatten(list(kw.items())))def wait_variable(self, name='PY_VAR'):"""等待变量被修改,必须给出intvar、 stringvar、 double levar 或 booleanvar 类型的参数"""self.tk.call('tkwait', 'variable', name)waitvar = wait_variable # XXX b/w compatdef wait_window(self, window=None):"""等待,直到小部件被销毁。如果没有参数给自己使用"""if window is None:window = selfself.tk.call('tkwait', 'window', window._w)def wait_visibility(self, window=None):"""等待,直到小部件的可见性改变(例如,它出现)。如果没有参数给自己使用"""if window is None:window = selfself.tk.call('tkwait', 'visibility', window._w)def setvar(self, name='PY_VAR', value='1'):"""将 tcl 变量名设置为 value"""self.tk.setvar(name, value)def getvar(self, name='PY_VAR'):"""Tcl 变量名的返回值"""return self.tk.getvar(name)def getint(self, s):try:return self.tk.getint(s)except TclError as exc:raise ValueError(str(exc))def getdouble(self, s):try:return self.tk.getdouble(s)except TclError as exc:raise ValueError(str(exc))def getboolean(self, s):"""返回 tcl 布尔值的布尔值 true 和 false 作为参数给定"""try:return self.tk.getboolean(s)except TclError:raise ValueError("invalid literal for getboolean()")def focus_set(self):"""将输入焦点直接指向这个小部件。如果应用程序当前没有焦点,那么如果应用程序通过窗口管理器获得焦点,这个小部件将获得焦点"""self.tk.call('focus', self._w)focus = focus_set # XXX b/w compat?def focus_force(self):"""将输入焦点直接指向这个小部件,即使应用程序没有这个焦点。小心使用!"""self.tk.call('focus', '-force', self._w)def focus_get(self):"""返回当前在应用程序中具有焦点的小部件。使用 focus _ displayof 允许处理多个显示器。如果应用程序没有焦点,则不返回任何值"""name = self.tk.call('focus')if name == 'none' or not name: return Nonereturn self._nametowidget(name)def focus_displayof(self):"""返回当前将焦点放在该小部件所在的显示器上的小部件。如果应用程序没有焦点,则不返回任何值"""name = self.tk.call('focus', '-displayof', self._w)if name == 'none' or not name: return Nonereturn self._nametowidget(name)def focus_lastfor(self):"""返回小部件,如果该小部件的顶层从窗口管理器获得焦点,则该小部件将拥有焦点"""name = self.tk.call('focus', '-lastfor', self._w)if name == 'none' or not name: return Nonereturn self._nametowidget(name)def tk_focusFollowsMouse(self):"""鼠标下的小部件会自动获得焦点,不能轻易关闭"""self.tk.call('tk_focusFollowsMouse')def tk_focusNext(self):"""按照焦点顺序返回下一个小部件,该小部件位于当前具有焦点的小部件之后。焦点顺序首先到达下一个子元素,然后递归地到达子元素的子元素,然后到达在堆叠顺序中位置较高的下一个兄弟姐妹。如果将 takefocus 资源设置为0,则忽略小部件"""name = self.tk.call('tk_focusNext', self._w)if not name: return Nonereturn self._nametowidget(name)def tk_focusPrev(self):"""按照焦点顺序返回上一个小部件"""name = self.tk.call('tk_focusPrev', self._w)if not name: return Nonereturn self._nametowidget(name)def after(self, ms, func=None, *args):"""给定时间后调用函数一次。Ms 指定以毫秒为单位的时间。Func 给出了应该调用的函数。附加参数作为函数调用的参数给出。返回标识符以使用 after _ Cancel 取消调度"""if not func:# 我宁愿用 time.sleep(ms*0.001)self.tk.call('after', ms)return Noneelse:def callit():try:func(*args)finally:try:self.deletecommand(name)except TclError:passcallit.__name__ = func.__name__name = self._register(callit)return self.tk.call('after', ms, name)def after_idle(self, func, *args):"""如果 tcl main 循环没有要处理的事件,则调用 func 一次。返回一个标识符,用 after _ Cancel 取消调度"""return self.after('idle', func, *args)def after_cancel(self, id):"""用 id 标识的函数的取消调度。必须给出 after 或 after _ 怠速返回的标识符作为第一个参数"""if not id:raise ValueError('id must be a valid identifier returned from ''after or after_idle')try:data = self.tk.call('after', 'info', id)script = self.tk.splitlist(data)[0]self.deletecommand(script)except TclError:passself.tk.call('after', 'cancel', id)def bell(self, displayof=0):"""敲响展览的钟"""self.tk.call(('bell',) + self._displayof(displayof))# 剪贴板处理:def clipboard_get(self, **kw):"""从窗口显示的剪贴板中检索数据。Window 关键字默认为 tkinter 应用程序的根窗口。Type 关键字指定返回数据的形式,并且应该是一个原子名,如 string 或 file _ name。Type 默认为 string,但 x11除外,其中默认为尝试 utf8 _ string 并回退到 string。这个命令相当于:selection_get(CLIPBOARD)"""if 'type' not in kw and self._windowingsystem == 'x11':try:kw['type'] = 'UTF8_STRING'return self.tk.call(('clipboard', 'get') + self._options(kw))except TclError:del kw['type']return self.tk.call(('clipboard', 'get') + self._options(kw))def clipboard_clear(self, **kw):"""清除 tk 剪贴板中的数据。为可选 displayof 关键字参数指定的小部件指定目标显示"""if 'displayof' not in kw: kw['displayof'] = self._wself.tk.call(('clipboard', 'clear') + self._options(kw))def clipboard_append(self, string, **kw):"""将字符串附加到 tk 剪贴板。在可选 displayof 关键字参数中指定的小部件指定目标显示。可以通过 select _ get 检索剪贴板"""if 'displayof' not in kw: kw['displayof'] = self._wself.tk.call(('clipboard', 'append') + self._options(kw)+ ('--', string))# 抓取当前窗口参数def grab_current(self):"""返回小部件,目前在这个应用程序抓取或没有"""name = self.tk.call('grab', 'current', self._w)if not name: return Nonereturn self._nametowidget(name)def grab_release(self):"""释放抓取这个小部件,如果当前设置"""self.tk.call('grab', 'release', self._w)def grab_set(self):"""为这个小部件设置抓取。一个 catch 将所有事件指向这个部件和应用程序中的后代小部件"""self.tk.call('grab', 'set', self._w)def grab_set_global(self):"""为这个小部件设置全局抓取。一个全局抓取将所有事件指向这个和显示器上的后代小部件。谨慎使用-其他应用程序不再获得事件"""self.tk.call('grab', 'set', '-global', self._w)def grab_status(self):"""如果此小部件没有返回任何值,则返回“ local”或“ global”"""status = self.tk.call('grab', 'status', self._w)if status == 'none': status = Nonereturn statusdef option_add(self, pattern, value, priority = None):"""为选项模式(第一个参数)设置一个值(第二个参数)。可选的第三个参数提供数字优先级(默认为80)"""self.tk.call('option', 'add', pattern, value, priority)def option_clear(self):"""Clear the option database.It will be reloaded if option_add is called."""self.tk.call('option', 'clear')def option_get(self, name, className):"""Return the value for an option NAME for this widgetwith CLASSNAME.Values with higher priority override lower values."""return self.tk.call('option', 'get', self._w, name, className)def option_readfile(self, fileName, priority = None):"""Read file FILENAME into the option database.An optional second parameter gives the numericpriority."""self.tk.call('option', 'readfile', fileName, priority)def selection_clear(self, **kw):"""Clear the current X selection."""if 'displayof' not in kw: kw['displayof'] = self._wself.tk.call(('selection', 'clear') + self._options(kw))def selection_get(self, **kw):"""Return the contents of the current X selection.A keyword parameter selection specifies the name ofthe selection and defaults to PRIMARY.  A keywordparameter displayof specifies a widget on the displayto use. A keyword parameter type specifies the form of data to befetched, defaulting to STRING except on X11, where UTF8_STRING is triedbefore STRING."""if 'displayof' not in kw: kw['displayof'] = self._wif 'type' not in kw and self._windowingsystem == 'x11':try:kw['type'] = 'UTF8_STRING'return self.tk.call(('selection', 'get') + self._options(kw))except TclError:del kw['type']return self.tk.call(('selection', 'get') + self._options(kw))def selection_handle(self, command, **kw):"""Specify a function COMMAND to call if the Xselection owned by this widget is queried by anotherapplication.This function must return the contents of theselection. The function will be called with thearguments OFFSET and LENGTH which allows the chunkingof very long selections. The following keywordparameters can be provided:selection - name of the selection (default PRIMARY),type - type of the selection (e.g. STRING, FILE_NAME)."""name = self._register(command)self.tk.call(('selection', 'handle') + self._options(kw)+ (self._w, name))def selection_own(self, **kw):"""Become owner of X selection.A keyword parameter selection specifies the name ofthe selection (default PRIMARY)."""self.tk.call(('selection', 'own') +self._options(kw) + (self._w,))def selection_own_get(self, **kw):"""Return owner of X selection.The following keyword parameter canbe provided:selection - name of the selection (default PRIMARY),type - type of the selection (e.g. STRING, FILE_NAME)."""if 'displayof' not in kw: kw['displayof'] = self._wname = self.tk.call(('selection', 'own') + self._options(kw))if not name: return Nonereturn self._nametowidget(name)def send(self, interp, cmd, *args):"""Send Tcl command CMD to different interpreter INTERP to be executed."""return self.tk.call(('send', interp, cmd) + args)def lower(self, belowThis=None):"""Lower this widget in the stacking order."""self.tk.call('lower', self._w, belowThis)def tkraise(self, aboveThis=None):"""Raise this widget in the stacking order."""self.tk.call('raise', self._w, aboveThis)lift = tkraisedef winfo_atom(self, name, displayof=0):"""Return integer which represents atom NAME."""args = ('winfo', 'atom') + self._displayof(displayof) + (name,)return self.tk.getint(self.tk.call(args))def winfo_atomname(self, id, displayof=0):"""Return name of atom with identifier ID."""args = ('winfo', 'atomname') \+ self._displayof(displayof) + (id,)return self.tk.call(args)def winfo_cells(self):"""Return number of cells in the colormap for this widget."""return self.tk.getint(self.tk.call('winfo', 'cells', self._w))def winfo_children(self):"""Return a list of all widgets which are children of this widget."""result = []for child in self.tk.splitlist(self.tk.call('winfo', 'children', self._w)):try:# Tcl sometimes returns extra windows, e.g. for# menus; those need to be skippedresult.append(self._nametowidget(child))except KeyError:passreturn resultdef winfo_class(self):"""Return window class name of this widget."""return self.tk.call('winfo', 'class', self._w)def winfo_colormapfull(self):"""Return True if at the last color request the colormap was full."""return self.tk.getboolean(self.tk.call('winfo', 'colormapfull', self._w))def winfo_containing(self, rootX, rootY, displayof=0):"""Return the widget which is at the root coordinates ROOTX, ROOTY."""args = ('winfo', 'containing') \+ self._displayof(displayof) + (rootX, rootY)name = self.tk.call(args)if not name: return Nonereturn self._nametowidget(name)def winfo_depth(self):"""Return the number of bits per pixel."""return self.tk.getint(self.tk.call('winfo', 'depth', self._w))def winfo_exists(self):"""Return true if this widget exists."""return self.tk.getint(self.tk.call('winfo', 'exists', self._w))def winfo_fpixels(self, number):"""Return the number of pixels for the given distance NUMBER(e.g. "3c") as float."""return self.tk.getdouble(self.tk.call('winfo', 'fpixels', self._w, number))def winfo_geometry(self):"""Return geometry string for this widget in the form "widthxheight+X+Y"."""return self.tk.call('winfo', 'geometry', self._w)def winfo_height(self):"""Return height of this widget."""return self.tk.getint(self.tk.call('winfo', 'height', self._w))def winfo_id(self):"""Return identifier ID for this widget."""return int(self.tk.call('winfo', 'id', self._w), 0)def winfo_interps(self, displayof=0):"""Return the name of all Tcl interpreters for this display."""args = ('winfo', 'interps') + self._displayof(displayof)return self.tk.splitlist(self.tk.call(args))def winfo_ismapped(self):"""Return true if this widget is mapped."""return self.tk.getint(self.tk.call('winfo', 'ismapped', self._w))def winfo_manager(self):"""Return the window manager name for this widget."""return self.tk.call('winfo', 'manager', self._w)def winfo_name(self):"""Return the name of this widget."""return self.tk.call('winfo', 'name', self._w)def winfo_parent(self):"""Return the name of the parent of this widget."""return self.tk.call('winfo', 'parent', self._w)def winfo_pathname(self, id, displayof=0):"""Return the pathname of the widget given by ID."""args = ('winfo', 'pathname') \+ self._displayof(displayof) + (id,)return self.tk.call(args)def winfo_pixels(self, number):"""Rounded integer value of winfo_fpixels."""return self.tk.getint(self.tk.call('winfo', 'pixels', self._w, number))def winfo_pointerx(self):"""Return the x coordinate of the pointer on the root window."""return self.tk.getint(self.tk.call('winfo', 'pointerx', self._w))def winfo_pointerxy(self):"""Return a tuple of x and y coordinates of the pointer on the root window."""return self._getints(self.tk.call('winfo', 'pointerxy', self._w))def winfo_pointery(self):"""Return the y coordinate of the pointer on the root window."""return self.tk.getint(self.tk.call('winfo', 'pointery', self._w))def winfo_reqheight(self):"""Return requested height of this widget."""return self.tk.getint(self.tk.call('winfo', 'reqheight', self._w))def winfo_reqwidth(self):"""Return requested width of this widget."""return self.tk.getint(self.tk.call('winfo', 'reqwidth', self._w))def winfo_rgb(self, color):"""Return tuple of decimal values for red, green, blue forCOLOR in this widget."""return self._getints(self.tk.call('winfo', 'rgb', self._w, color))def winfo_rootx(self):"""Return x coordinate of upper left corner of this widget on theroot window."""return self.tk.getint(self.tk.call('winfo', 'rootx', self._w))def winfo_rooty(self):"""Return y coordinate of upper left corner of this widget on theroot window."""return self.tk.getint(self.tk.call('winfo', 'rooty', self._w))def winfo_screen(self):"""Return the screen name of this widget."""return self.tk.call('winfo', 'screen', self._w)def winfo_screencells(self):"""Return the number of the cells in the colormap of the screenof this widget."""return self.tk.getint(self.tk.call('winfo', 'screencells', self._w))def winfo_screendepth(self):"""Return the number of bits per pixel of the root window of thescreen of this widget."""return self.tk.getint(self.tk.call('winfo', 'screendepth', self._w))def winfo_screenheight(self):"""Return the number of pixels of the height of the screen of this widgetin pixel."""return self.tk.getint(self.tk.call('winfo', 'screenheight', self._w))def winfo_screenmmheight(self):"""Return the number of pixels of the height of the screen ofthis widget in mm."""return self.tk.getint(self.tk.call('winfo', 'screenmmheight', self._w))def winfo_screenmmwidth(self):"""Return the number of pixels of the width of the screen ofthis widget in mm."""return self.tk.getint(self.tk.call('winfo', 'screenmmwidth', self._w))def winfo_screenvisual(self):"""Return one of the strings directcolor, grayscale, pseudocolor,staticcolor, staticgray, or truecolor for the defaultcolormodel of this screen."""return self.tk.call('winfo', 'screenvisual', self._w)def winfo_screenwidth(self):"""Return the number of pixels of the width of the screen ofthis widget in pixel."""return self.tk.getint(self.tk.call('winfo', 'screenwidth', self._w))def winfo_server(self):"""Return information of the X-Server of the screen of this widget inthe form "XmajorRminor vendor vendorVersion"."""return self.tk.call('winfo', 'server', self._w)def winfo_toplevel(self):"""Return the toplevel widget of this widget."""return self._nametowidget(self.tk.call('winfo', 'toplevel', self._w))def winfo_viewable(self):"""Return true if the widget and all its higher ancestors are mapped."""return self.tk.getint(self.tk.call('winfo', 'viewable', self._w))def winfo_visual(self):"""Return one of the strings directcolor, grayscale, pseudocolor,staticcolor, staticgray, or truecolor for thecolormodel of this widget."""return self.tk.call('winfo', 'visual', self._w)def winfo_visualid(self):"""Return the X identifier for the visual for this widget."""return self.tk.call('winfo', 'visualid', self._w)def winfo_visualsavailable(self, includeids=False):"""Return a list of all visuals available for the screenof this widget.Each item in the list consists of a visual name (see winfo_visual), adepth and if includeids is true is given also the X identifier."""data = self.tk.call('winfo', 'visualsavailable', self._w,'includeids' if includeids else None)data = [self.tk.splitlist(x) for x in self.tk.splitlist(data)]return [self.__winfo_parseitem(x) for x in data]def __winfo_parseitem(self, t):"""Internal function."""return t[:1] + tuple(map(self.__winfo_getint, t[1:]))def __winfo_getint(self, x):"""Internal function."""return int(x, 0)def winfo_vrootheight(self):"""Return the height of the virtual root window associated with thiswidget in pixels. If there is no virtual root window return theheight of the screen."""return self.tk.getint(self.tk.call('winfo', 'vrootheight', self._w))def winfo_vrootwidth(self):"""Return the width of the virtual root window associated with thiswidget in pixel. If there is no virtual root window return thewidth of the screen."""return self.tk.getint(self.tk.call('winfo', 'vrootwidth', self._w))def winfo_vrootx(self):"""Return the x offset of the virtual root relative to the rootwindow of the screen of this widget."""return self.tk.getint(self.tk.call('winfo', 'vrootx', self._w))def winfo_vrooty(self):"""Return the y offset of the virtual root relative to the rootwindow of the screen of this widget."""return self.tk.getint(self.tk.call('winfo', 'vrooty', self._w))def winfo_width(self):"""Return the width of this widget."""return self.tk.getint(self.tk.call('winfo', 'width', self._w))def winfo_x(self):"""Return the x coordinate of the upper left corner of this widgetin the parent."""return self.tk.getint(self.tk.call('winfo', 'x', self._w))def winfo_y(self):"""Return the y coordinate of the upper left corner of this widgetin the parent."""return self.tk.getint(self.tk.call('winfo', 'y', self._w))def update(self):"""Enter event loop until all pending events have been processed by Tcl."""self.tk.call('update')def update_idletasks(self):"""Enter event loop until all idle callbacks have been called. Thiswill update the display of windows but not process events caused bythe user."""self.tk.call('update', 'idletasks')def bindtags(self, tagList=None):"""Set or get the list of bindtags for this widget.With no argument return the list of all bindtags associated withthis widget. With a list of strings as argument the bindtags areset to this list. The bindtags determine in which order events areprocessed (see bind)."""if tagList is None:return self.tk.splitlist(self.tk.call('bindtags', self._w))else:self.tk.call('bindtags', self._w, tagList)def _bind(self, what, sequence, func, add, needcleanup=1):"""Internal function."""if isinstance(func, str):self.tk.call(what + (sequence, func))elif func:funcid = self._register(func, self._substitute,needcleanup)cmd = ('%sif {"[%s %s]" == "break"} break\n'%(add and '+' or '',funcid, self._subst_format_str))self.tk.call(what + (sequence, cmd))return funcidelif sequence:return self.tk.call(what + (sequence,))else:return self.tk.splitlist(self.tk.call(what))def bind(self, sequence=None, func=None, add=None):"""Bind to this widget at event SEQUENCE a call to function FUNC.SEQUENCE is a string of concatenated eventpatterns. An event pattern is of the form<MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is oneof Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,B3, Alt, Button4, B4, Double, Button5, B5 Triple,Mod1, M1. TYPE is one of Activate, Enter, Map,ButtonPress, Button, Expose, Motion, ButtonReleaseFocusIn, MouseWheel, Circulate, FocusOut, Property,Colormap, Gravity Reparent, Configure, KeyPress, Key,Unmap, Deactivate, KeyRelease Visibility, Destroy,Leave and DETAIL is the button number for ButtonPress,ButtonRelease and DETAIL is the Keysym for KeyPress andKeyRelease. Examples are<Control-Button-1> for pressing Control and mouse button 1 or<Alt-A> for pressing A and the Alt key (KeyPress can be omitted).An event pattern can also be a virtual event of the form<<AString>> where AString can be arbitrary. Thisevent can be generated by event_generate.If events are concatenated they must appear shortlyafter each other.FUNC will be called if the event sequence occurs with aninstance of Event as argument. If the return value of FUNC is"break" no further bound function is invoked.An additional boolean parameter ADD specifies whether FUNC willbe called additionally to the other bound function or whetherit will replace the previous function.Bind will return an identifier to allow deletion of the bound function withunbind without memory leak.If FUNC or SEQUENCE is omitted the bound function or listof bound events are returned."""return self._bind(('bind', self._w), sequence, func, add)def unbind(self, sequence, funcid=None):"""Unbind for this widget for event SEQUENCE  thefunction identified with FUNCID."""self.tk.call('bind', self._w, sequence, '')if funcid:self.deletecommand(funcid)def bind_all(self, sequence=None, func=None, add=None):"""Bind to all widgets at an event SEQUENCE a call to function FUNC.An additional boolean parameter ADD specifies whether FUNC willbe called additionally to the other bound function or whetherit will replace the previous function. See bind for the return value."""return self._bind(('bind', 'all'), sequence, func, add, 0)def unbind_all(self, sequence):"""Unbind for all widgets for event SEQUENCE all functions."""self.tk.call('bind', 'all' , sequence, '')def bind_class(self, className, sequence=None, func=None, add=None):"""Bind to widgets with bindtag CLASSNAME at eventSEQUENCE a call of function FUNC. An additionalboolean parameter ADD specifies whether FUNC will becalled additionally to the other bound function orwhether it will replace the previous function. See bind forthe return value."""return self._bind(('bind', className), sequence, func, add, 0)def unbind_class(self, className, sequence):"""Unbind for all widgets with bindtag CLASSNAME for event SEQUENCEall functions."""self.tk.call('bind', className , sequence, '')def mainloop(self, n=0):"""Call the mainloop of Tk."""self.tk.mainloop(n)def quit(self):"""Quit the Tcl interpreter. All widgets will be destroyed."""self.tk.quit()def _getints(self, string):"""Internal function."""if string:return tuple(map(self.tk.getint, self.tk.splitlist(string)))def _getdoubles(self, string):"""Internal function."""if string:return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))def _getboolean(self, string):"""Internal function."""if string:return self.tk.getboolean(string)def _displayof(self, displayof):"""Internal function."""if displayof:return ('-displayof', displayof)if displayof is None:return ('-displayof', self._w)return ()@propertydef _windowingsystem(self):"""Internal function."""try:return self._root()._windowingsystem_cachedexcept AttributeError:ws = self._root()._windowingsystem_cached = \self.tk.call('tk', 'windowingsystem')return wsdef _options(self, cnf, kw = None):"""Internal function."""if kw:cnf = _cnfmerge((cnf, kw))else:cnf = _cnfmerge(cnf)res = ()for k, v in cnf.items():if v is not None:if k[-1] == '_': k = k[:-1]if callable(v):v = self._register(v)elif isinstance(v, (tuple, list)):nv = []for item in v:if isinstance(item, int):nv.append(str(item))elif isinstance(item, str):nv.append(_stringify(item))else:breakelse:v = ' '.join(nv)res = res + ('-'+k, v)return resdef nametowidget(self, name):"""Return the Tkinter instance of a widget identified byits Tcl name NAME."""name = str(name).split('.')w = selfif not name[0]:w = w._root()name = name[1:]for n in name:if not n:breakw = w.children[n]return w_nametowidget = nametowidgetdef _register(self, func, subst=None, needcleanup=1):"""Return a newly created Tcl function. If thisfunction is called, the Python function FUNC willbe executed. An optional function SUBST canbe given which will be executed before FUNC."""f = CallWrapper(func, subst, self).__call__name = repr(id(f))try:func = func.__func__except AttributeError:passtry:name = name + func.__name__except AttributeError:passself.tk.createcommand(name, f)if needcleanup:if self._tclCommands is None:self._tclCommands = []self._tclCommands.append(name)return nameregister = _registerdef _root(self):"""Internal function."""w = selfwhile w.master: w = w.masterreturn w_subst_format = ('%#', '%b', '%f', '%h', '%k','%s', '%t', '%w', '%x', '%y','%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D')_subst_format_str = " ".join(_subst_format)def _substitute(self, *args):"""Internal function."""if len(args) != len(self._subst_format): return argsgetboolean = self.tk.getbooleangetint = self.tk.getintdef getint_event(s):"""Tk changed behavior in 8.4.2, returning "??" rather more often."""try:return getint(s)except (ValueError, TclError):return snsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D = args# Missing: (a, c, d, m, o, v, B, R)e = Event()# serial field: valid for all events# number of button: ButtonPress and ButtonRelease events only# height field: Configure, ConfigureRequest, Create,# ResizeRequest, and Expose events only# keycode field: KeyPress and KeyRelease events only# time field: "valid for events that contain a time field"# width field: Configure, ConfigureRequest, Create, ResizeRequest,# and Expose events only# x field: "valid for events that contain an x field"# y field: "valid for events that contain a y field"# keysym as decimal: KeyPress and KeyRelease events only# x_root, y_root fields: ButtonPress, ButtonRelease, KeyPress,# KeyRelease, and Motion eventse.serial = getint(nsign)e.num = getint_event(b)try: e.focus = getboolean(f)except TclError: passe.height = getint_event(h)e.keycode = getint_event(k)e.state = getint_event(s)e.time = getint_event(t)e.width = getint_event(w)e.x = getint_event(x)e.y = getint_event(y)e.char = Atry: e.send_event = getboolean(E)except TclError: passe.keysym = Ke.keysym_num = getint_event(N)try:e.type = EventType(T)except ValueError:e.type = Ttry:e.widget = self._nametowidget(W)except KeyError:e.widget = We.x_root = getint_event(X)e.y_root = getint_event(Y)try:e.delta = getint(D)except (ValueError, TclError):e.delta = 0return (e,)def _report_exception(self):"""Internal function."""exc, val, tb = sys.exc_info()root = self._root()root.report_callback_exception(exc, val, tb)def _getconfigure(self, *args):"""Call Tcl configure command and return the result as a dict."""cnf = {}for x in self.tk.splitlist(self.tk.call(*args)):x = self.tk.splitlist(x)cnf[x[0][1:]] = (x[0][1:],) + x[1:]return cnfdef _getconfigure1(self, *args):x = self.tk.splitlist(self.tk.call(*args))return (x[0][1:],) + x[1:]def _configure(self, cmd, cnf, kw):"""Internal function."""if kw:cnf = _cnfmerge((cnf, kw))elif cnf:cnf = _cnfmerge(cnf)if cnf is None:return self._getconfigure(_flatten((self._w, cmd)))if isinstance(cnf, str):return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))# These used to be defined in Widget:def configure(self, cnf=None, **kw):"""Configure resources of a widget.The values for resources are specified as keywordarguments. To get an overview aboutthe allowed keyword arguments call the method keys."""return self._configure('configure', cnf, kw)config = configuredef cget(self, key):"""Return the resource value for a KEY given as string."""return self.tk.call(self._w, 'cget', '-' + key)__getitem__ = cgetdef __setitem__(self, key, value):self.configure({key: value})def keys(self):"""Return a list of all resource names of this widget."""splitlist = self.tk.splitlistreturn [splitlist(x)[0][1:] for x insplitlist(self.tk.call(self._w, 'configure'))]def __str__(self):"""Return the window path name of this widget."""return self._wdef __repr__(self):return '<%s.%s object %s>' % (self.__class__.__module__, self.__class__.__qualname__, self._w)# Pack methods that apply to the master_noarg_ = ['_noarg_']def pack_propagate(self, flag=_noarg_):"""Set or get the status for propagation of geometry information.A boolean argument specifies whether the geometry informationof the slaves will determine the size of this widget. If no argumentis given the current setting will be returned."""if flag is Misc._noarg_:return self._getboolean(self.tk.call('pack', 'propagate', self._w))else:self.tk.call('pack', 'propagate', self._w, flag)propagate = pack_propagatedef pack_slaves(self):"""Return a list of all slaves of this widgetin its packing order."""return [self._nametowidget(x) for x inself.tk.splitlist(self.tk.call('pack', 'slaves', self._w))]slaves = pack_slaves# Place method that applies to the masterdef place_slaves(self):"""Return a list of all slaves of this widgetin its packing order."""return [self._nametowidget(x) for x inself.tk.splitlist(self.tk.call('place', 'slaves', self._w))]# Grid methods that apply to the masterdef grid_anchor(self, anchor=None): # new in Tk 8.5"""The anchor value controls how to place the grid within themaster when no row/column has any weight.The default anchor is nw."""self.tk.call('grid', 'anchor', self._w, anchor)anchor = grid_anchordef grid_bbox(self, column=None, row=None, col2=None, row2=None):"""Return a tuple of integer coordinates for the boundingbox of this widget controlled by the geometry manager grid.If COLUMN, ROW is given the bounding box applies fromthe cell with row and column 0 to the specifiedcell. If COL2 and ROW2 are given the bounding boxstarts at that cell.The returned integers specify the offset of the upper leftcorner in the master widget and the width and height."""args = ('grid', 'bbox', self._w)if column is not None and row is not None:args = args + (column, row)if col2 is not None and row2 is not None:args = args + (col2, row2)return self._getints(self.tk.call(*args)) or Nonebbox = grid_bboxdef _gridconvvalue(self, value):if isinstance(value, (str, _tkinter.Tcl_Obj)):try:svalue = str(value)if not svalue:return Noneelif '.' in svalue:return self.tk.getdouble(svalue)else:return self.tk.getint(svalue)except (ValueError, TclError):passreturn valuedef _grid_configure(self, command, index, cnf, kw):"""Internal function."""if isinstance(cnf, str) and not kw:if cnf[-1:] == '_':cnf = cnf[:-1]if cnf[:1] != '-':cnf = '-'+cnfoptions = (cnf,)else:options = self._options(cnf, kw)if not options:return _splitdict(self.tk,self.tk.call('grid', command, self._w, index),conv=self._gridconvvalue)res = self.tk.call(('grid', command, self._w, index)+ options)if len(options) == 1:return self._gridconvvalue(res)def grid_columnconfigure(self, index, cnf={}, **kw):"""Configure column INDEX of a grid.Valid resources are minsize (minimum size of the column),weight (how much does additional space propagate to this column)and pad (how much space to let additionally)."""return self._grid_configure('columnconfigure', index, cnf, kw)columnconfigure = grid_columnconfiguredef grid_location(self, x, y):"""Return a tuple of column and row which identify the cellat which the pixel at position X and Y inside the masterwidget is located."""return self._getints(self.tk.call('grid', 'location', self._w, x, y)) or Nonedef grid_propagate(self, flag=_noarg_):"""Set or get the status for propagation of geometry information.A boolean argument specifies whether the geometry informationof the slaves will determine the size of this widget. If no argumentis given, the current setting will be returned."""if flag is Misc._noarg_:return self._getboolean(self.tk.call('grid', 'propagate', self._w))else:self.tk.call('grid', 'propagate', self._w, flag)def grid_rowconfigure(self, index, cnf={}, **kw):"""Configure row INDEX of a grid.Valid resources are minsize (minimum size of the row),weight (how much does additional space propagate to this row)and pad (how much space to let additionally)."""return self._grid_configure('rowconfigure', index, cnf, kw)rowconfigure = grid_rowconfiguredef grid_size(self):"""Return a tuple of the number of column and rows in the grid."""return self._getints(self.tk.call('grid', 'size', self._w)) or Nonesize = grid_sizedef grid_slaves(self, row=None, column=None):"""Return a list of all slaves of this widgetin its packing order."""args = ()if row is not None:args = args + ('-row', row)if column is not None:args = args + ('-column', column)return [self._nametowidget(x) for x inself.tk.splitlist(self.tk.call(('grid', 'slaves', self._w) + args))]# Support for the "event" command, new in Tk 4.2.# By Case Roole.def event_add(self, virtual, *sequences):"""Bind a virtual event VIRTUAL (of the form <<Name>>)to an event SEQUENCE such that the virtual event is triggeredwhenever SEQUENCE occurs."""args = ('event', 'add', virtual) + sequencesself.tk.call(args)def event_delete(self, virtual, *sequences):"""Unbind a virtual event VIRTUAL from SEQUENCE."""args = ('event', 'delete', virtual) + sequencesself.tk.call(args)def event_generate(self, sequence, **kw):"""Generate an event SEQUENCE. Additionalkeyword arguments specify parameter of the event(e.g. x, y, rootx, rooty)."""args = ('event', 'generate', self._w, sequence)for k, v in kw.items():args = args + ('-%s' % k, str(v))self.tk.call(args)def event_info(self, virtual=None):"""Return a list of all virtual events or the informationabout the SEQUENCE bound to the virtual event VIRTUAL."""return self.tk.splitlist(self.tk.call('event', 'info', virtual))# Image related commandsdef image_names(self):"""Return a list of all existing image names."""return self.tk.splitlist(self.tk.call('image', 'names'))def image_types(self):"""Return a list of all available image types (e.g. photo bitmap)."""return self.tk.splitlist(self.tk.call('image', 'types'))

(一个座位上是我,另一个座位上也是我,因为我裂开了)

Python tkinter 从入门到放弃(窗口函数)相关推荐

  1. python从入门到放弃-掌握这个学习方法,让 Python 不再从入门到放弃

    随着数据科学概念的普及,Python 这门并不算新的语言火得一塌糊涂.因为写了几篇用 Python 做数据分析的 文章,经常有读者和学生在留言区问我,想学习 Python,该如何入手?我经常需要根据他 ...

  2. python爬虫从入门到放弃,含案例分析,超详细讲解(一)

    Python爬虫 1.认识爬虫 1.1.概述 网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者) 按照一定规律,自动地抓取万维网信息的程序或者脚本. 1.2.应用场景 ...

  3. Python爬虫从入门到放弃(十二)之 Scrapy框架的架构和原理

    原文地址https://www.cnblogs.com/zhaof/p/7173397.html 这一篇文章主要是为了对scrapy框架的工作流程以及各个组件功能的介绍 Scrapy目前已经可以很好的 ...

  4. python爬虫从入门到放弃(三)之 Urllib库的基本使用

    官方文档地址:https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的HTTP请求库 包括以下模块 urllib ...

  5. 掌握这些!让Python不再从入门到放弃,初学者容易忽略的一些细节

    1.Python中当让给变量1赋值上变量2的值时,变量1直接指向值在内存中存储的地址,即变量1存储的是变量2的值而不是指向变量 这里有我自己整理了一套最新的python系统学习教程,包括从基础的pyt ...

  6. python爬虫从入门到放弃(六)之 BeautifulSoup库的使用

    上一篇文章的正则,其实对很多人来说用起来是不方便的,加上需要记很多规则,所以用起来不是特别熟练,而这节我们提到的beautifulsoup就是一个非常强大的工具,爬虫利器. beautifulSoup ...

  7. Python爬虫从入门到放弃(十三)之 Scrapy框架的命令行详解

    这篇文章主要是对的scrapy命令行使用的一个介绍 创建爬虫项目 scrapy startproject 项目名 例子如下: localhost:spider zhaofan$ scrapy star ...

  8. python爬虫从入门到放弃(八)之 Selenium库的使用

    原文地址https://www.cnblogs.com/zhaof/p/6953241.html 一.什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(s ...

  9. Python爬虫从入门到放弃(十一)之 Scrapy框架整体的一个了解

    这里是通过爬取伯乐在线的全部文章为例子,让自己先对scrapy进行一个整理的理解 该例子中的详细代码会放到我的github地址:https://github.com/pythonsite/spider ...

最新文章

  1. python websocket例程_python 实现websocket
  2. php laravel 框架 APP_KEY 的作用
  3. MySQL创建视图的语法格式
  4. 数据库连接池的工作原理
  5. 如何给U盘设置一张妖娆又骚气的图标
  6. MAC使用homeBrew安装Redis
  7. linux使用ftp命令_如何使用Linux FTP命令
  8. R语言-实用功能性语句1
  9. 字符串转码中文乱码问题的进一步理解 UTF-8 GBK转码
  10. 喜讯 | 大势智慧获得全球软件领域最高权威CMMI5认证
  11. 中望CAD工具栏消失不见的解决方案
  12. 项目管理如何真正实现降本增效?
  13. 实用前端可视化工具库
  14. 《财富》2022全球500强榜单,沃尔玛九连冠,沙特阿美最赚钱,中国公司上榜最多 | 美通社头条...
  15. [Qt]QLabel的显示圆形
  16. nfc卡模式与标准模式_全功能NFC是什么意思?点对点/读写卡/卡模拟三种模式介绍...
  17. 本地数据下,radiobutton和图片组合,利用adapter+listview进行单选
  18. Win10 Bash搭建嵌入式Qt开发平台
  19. python培训湖南
  20. Minecraft正版账号获得方法

热门文章

  1. 403ByPass总结
  2. 对于菜鸟网络,马云只要做到三件事:拉投资、布平台、造势
  3. 华3如何使用ftp和tftp进行文件传输与保存
  4. Hexo博客添加评论功能
  5. vcastr.swf 网络视频播放器,参数设置
  6. 温故而知新——CSS3小技巧:width:fit-content
  7. NOD32与VS2005冲突问题
  8. 【情态动词练习题】may not 和 can‘t / might 的区别
  9. cnn系列文章四 --池化层和简单卷积网络示例
  10. GEE中影像镶嵌的mosaic函数的应用