Thomas Weeks
Thomas Weeks

Reputation: 83

reversing mouse scroll direction on mac os

I've changed the mouse scrolling behavior on my mac by going to System Preferences/Mouse/Scroll direction: natural - deselect. However my python program is still using the natural scroll direction. How do I reverse it?

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

Upvotes: 0

Views: 824

Answers (1)

Thomas Weeks
Thomas Weeks

Reputation: 83

The solution is to modify the binding where sys.platform == "darwin". Change "event.delta" to "-1 * (event.delta)"

So this is natural scroll direction:

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

and this is reverse scroll direction:

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

Upvotes: 1

Related Questions