0

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()
asked Feb 9, 2021 at 17:02
2
  • 1
    Notice the -1 in one of your bindings? Swap it to the other binding. Commented Feb 9, 2021 at 17:52
  • Thanks. That did the trick. Commented Feb 9, 2021 at 20:49

1 Answer 1

1

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"))
answered Feb 9, 2021 at 20:49
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.