tkinter滚动事件详解

在tkinter中,由<Mousewheel>提供绑定信息。

但是同其它绑定事件一样,滚动事件同样提供的event参数返回值,也拥有特殊的属性,比如send_eventdeltastate等。

现在来具体看一下。

测试代码

因为不涉及具体应用,这里使用的代码很简短。

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()

  

具体操作测试

其实也没有多具体,就是三种滚动方式:

  1. 纵向滚动
  2. 横向滚动
  3. 触摸板缩放(缩放滚动)

可以得到以下返回值:

<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)缩放

posted on 2023-03-19 15:07  帅胡  阅读(124)  评论(0编辑  收藏  举报

导航