1、事件app
在Tkinter中事件使用格式的字符串描述的。其中type是事件类型,好比键盘按下(Key)、鼠标(Motion/Enter/Leave/Relase)等。modifier事件修饰符,最多见的是Alt、Shit组合键和Double事件。detail描述哪一个键盘按钮或者鼠标按钮。less
:鼠标左击事件函数
:双击事件spa
:鼠标移动事件code
:鼠标释放事件事件
:鼠标进入事件ci
:鼠标离开时产生的事件字符串
:特殊键事件it
:全部的键按键事件io
:A、空格、
组合键事件
大小改变事件
2、事件处理
事件处理是当事件发生时控件的响应函数。
# 函数
def handle(event):
pass
# 或者类的一个方法
def handle(self, event):
pass
event是事件的一个实例,经常使用的属性有:
.type:事件类型,字符串,即事件字符串中的type。
.char:用于键盘事件,表示按下键的ascii码。
.keycode:用于键盘事件,表示键盘代码。
.delta:用于描述鼠标滚动事件,表示滚轮滚动的距离。
3、自定义事件
自定义事件字符串,是包含在书名号中,好比<< Complete >>。在自定义事件中,须要用代码触发事件,使用self.generate_event('<>'
四、事件绑定
事件绑定是当一个事件发生时某个控件可以作出响应。能够将多个事件绑定到同的组件。
共有三种不一样层次的绑定方式。
实例绑定bind:绑定在某一个控件上。
类型绑定bind_class:绑定在某一个类型控件上。
应用绑定bind_all:绑定在全部控件上。
# 在instance级别与printEvent绑定
bt1 = Button(root,text = 'instance event')
bt1.bind('',printEvent)
# 在bt1的Toplevel级别与printToplevel绑定
bt1.winfo_toplevel().bind('',printToplevel)
# 在class级别绑定事件printClass
root.bind_class('Button','',printClass)
# 在application all级别绑定printAppAll
bt1.bind_all('',printAppAll)
#使用protocal绑定
# 使用protocol将WM_DELETE_WINDOW与printProtocol绑定
root.protocol('WM_DELETE_WINDOW',printProtocol)