【问题标题】:reversing mouse scroll direction on mac os在mac os上反转鼠标滚动方向
【发布时间】:2021-02-09 17:02:34
【问题描述】:

我通过转到系统偏好设置/鼠标/滚动方向更改了我的 mac 上的鼠标滚动行为:自然 - 取消选择。但是我的 python 程序仍然使用自然滚动方向。我该如何扭转它?

from tkinter import *

# initialize position and size for root window
position_x = 100
position_y = 50
size_x = 625
size_y = 600

root = Tk()
root.title("Mouse Kung Fu version {}".format("3.008"))
root.geometry("%dx%d+%d+%d" % (size_x, size_y, position_x, position_y))

F = Frame(root)
F.pack(fill=BOTH, side=LEFT)

# link up the canvas and scrollbar
S = Scrollbar(F)
C = Canvas(F, width=1600)
S.pack(side=RIGHT, fill=BOTH)
C.pack(side=LEFT, fill=BOTH, pady=10, padx=10)
S.configure(command=C.yview, orient="vertical")
C.configure(yscrollcommand=S.set)
if sys.platform == "win32":
    C.bind_all('<MouseWheel>', lambda event: C.yview_scroll(int(-1 * (event.delta / 120)), "units"))
elif sys.platform == "darwin":
    C.bind_all('<MouseWheel>', lambda event: C.yview_scroll(int(event.delta), "units"))
elif sys.platform == "linux":
    C.bind_all('<Button-4>', lambda event: C.yview('scroll', -1, 'units'))
    C.bind_all('<Button-5>', lambda event: C.yview('scroll', 1, 'units'))

#  create frame inside canvas
FF = Frame(C)
C.create_window((0, 0), window=FF, anchor=NW)

# create page content
for _ in range(100):
    Label(FF,text="foo").pack()
    Label(FF,text="bar").pack()

root.update()
C.config(scrollregion=C.bbox("all"))
mainloop()

【问题讨论】:

  • 注意到您的某个绑定中的-1 了吗?将其交换到另一个绑定。
  • 谢谢。成功了。

标签: python macos tkinter mousewheel macos-big-sur


【解决方案1】:

解决方法是修改 sys.platform == "darwin" 的绑定。将“event.delta”更改为“-1 * (event.delta)”

所以这是自然的滚动方向:

elif sys.platform == "darwin":
    C.bind_all('<MouseWheel>', lambda event: C.yview_scroll(int(event.delta), "units"))

这是反向滚动方向:

elif sys.platform == "darwin":
    C.bind_all('<MouseWheel>', lambda event: C.yview_scroll(int(-1 *(event.delta)), "units"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2023-03-24
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    相关资源
    最近更新 更多