ViolationHandler.exe
ViolationHandler.exe

Reputation: 31

Python (3.10) TKinter: horizontal scrolling on touchpad (laptop)

Using Python 3.10 and TKinter Library

I had an issue with touchpad horizontal scrolling being received as vertical scrolling, searched online a fix, I did find a potential fix using StackOverflow here. However, for windows, or maybe just my laptop it didn't work. I then checked what I was allowed to do with the event object and 'state' existed.

Using print(event.state) you can figure out what value your horizontal scroll and vertical are. Using that you can fix your code to allow horizontal and vertical scrolling on a touchpad. Hope you enjoy. (I don't have enough karma for literally anything, and this is a real solution, but not a question unfortunately, so hopefully its okay.)

# Bind canvas to scrollbar
        self.canvas.config(yscrollcommand=self.scrollbar.set)
        self.canvas.config(xscrollcommand=self.horizontal_scrollbar.set)
        self.canvas.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
        self.canvas.bind_all("<MouseWheel>", self.on_mousewheel)

# on_mousewheel function below:

def on_mousewheel(self, event):
        # Check if Control key is held down
        if event.state & 0x4:
            # Horizontal scrolling
            self.canvas.xview_scroll(int(-1 * (event.delta / 80)), "units")
        else:
            # Vertical + Horizontal scrolling
            if event.state == 9:
                self.canvas.xview_scroll(int(-1 * (event.delta / 80)), "units")
            elif event.state == 8:
                self.canvas.yview_scroll(int(-1 * (event.delta / 100)), "units")

Tried to allow horizontal scrolling using a touchpad on a laptop, couldn't at first, it was received as vertical scrolling regardless of moving my hands left to right. Found a potential solution that didn't work, it made me think and I figured out a solution. This post is to inform people on how to find vertical and horizontal scrolling states for Python 3.10 with the TKinter library.

Upvotes: 2

Views: 251

Answers (0)

Related Questions