tkinter滚动事件详解
在tkinter中,由<Mousewheel>
提供绑定信息。
但是同其它绑定事件一样,滚动事件同样提供的event
参数返回值,也拥有特殊的属性,比如send_event
,delta
,state
等。
现在来具体看一下。
测试代码
因为不涉及具体应用,这里使用的代码很简短。
from tkinter import * def test(event): print(event) a=Tk() c=Canvas(a) c.pack(fill='both',expand=True) c.bind('<MouseWheel>',test) a.mainloop()
具体操作测试
其实也没有多具体,就是三种滚动方式:
- 纵向滚动
- 横向滚动
- 触摸板缩放(缩放滚动)
可以得到以下返回值:
<MouseWheel event send_event=True delta=-120 x=202 y=115>
<MouseWheel event send_event=True delta=-120 x=202 y=115>
<MouseWheel event send_event=True delta=-120 x=202 y=115>
<MouseWheel event send_event=True delta=120 x=202 y=115>
<MouseWheel event send_event=True delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=-120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=-120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=-120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Shift delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Control delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Control delta=120 x=202 y=115>
<MouseWheel event send_event=True state=Control delta=-120 x=202 y=115>
<MouseWheel event send_event=True state=Control delta=-120 x=202 y=115>
<MouseWheel event send_event=True state=Control delta=-120 x=202 y=115>
<MouseWheel event send_event=True state=Control delta=-120 x=202 y=115>
结论
不难看出,我们可以知道除了网上常说的delta
属性以外,还有state
属性。对应关系如下:
- None(0)纵向滚动
- Shift(1)横向滚动
- Control(2)缩放