tkinter 可以单独处理鼠标滚轮前进和后退事件吗?

Can tkinter handle mouse wheel forward and backward events separately?

我正在尝试使用 canvas 在 tkinter 上编写一个简单的放大和缩小程序,我已经完成了大部分工作,但我被一个问题阻止了:绑定事件时使用 master.bind(<Control-MouseWheel>, func) 我无法以不同方式处理鼠标滚轮前进事件和后退事件。有什么解决办法吗?

P.S。绑定事件的时候,我被迫使用master.bind(<Control-MouseWheel>, lambda func: "other code"),否则当程序运行时,事件绑定的函数被瞬间执行,还有其他问题的解决方案吗?

事件对象有一个名为 delta 的属性,它告诉您要移动多少个单位。 delta 可以是正数(向前移动)或负数(向后移动)。

def func(event):
    if event.delta > 0:
        print("scroll forward")
    else:
        print("scroll backward")

来自规范文档:

The delta value represents the rotation units the mouse wheel has been moved. On Windows 95 & 98 systems the smallest value for the delta is 120. Future systems may support higher resolution values for the delta. The sign of the value represents the direction the mouse wheel was scrolled.

在 windows 和 *nix 系统上,如果您想使用该值来确定要缩放多少,通常需要将 delta 除以 120(或其他值,具体取决于您想要缩放的速度)滚动或缩放。在 OSX 上,您可以使用原始增量值。

有关使用鼠标滚轮进行缩放的完整示例,请参阅 to the question