2

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.

  • Welcome to Stack Overflow, I'm glad you've solved the problem! However if you want to [answer your own question](https://stackoverflow.com/help/self-answer), you should post a separate answer. Putting the answer inside the question is against the SO guidelines. See [answer] for more information on writing answers. – Sylvester Kruin Apr 16 '23 at 19:39
  • Good point, I didn't think of that at the time, I assumed the reputation requirements were also included in your own posts tbh. I'm honestly surprised to hear that its against the guidelines of Stack Overflow too. I also just read the portion you linked to 'answer your own question' in which I actually don't believe I would've been able to due to my lack of reputation, I only would've been able to do so AFTER (depending on how much you get from posting) I posted said question, and then waited for my reputation to go up from upvotes/etc. Thank you for mentioning this though, I appreciate it! – ViolationHandler.exe Apr 18 '23 at 23:37

0 Answers0