What are the Tkinter events for horizontal edge scrolling (in Linux)?

后端 未结 2 1411
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 08:32

I have a Python Tkinter Text widget with scrollbars. I would like to define my own method for using horizontal edge scrolling on my laptop\'s touchpad. However, I don\'t kno

相关标签:
2条回答
  • 2020-12-22 09:17

    I just wanted to add on that on MacOS as no where tells this and I spent quite a bit of time experimenting and searching. The event.state changes from 0 for vertical mouse scroll and 1 for horizontal mouse scroll.

    def mouseScroll(event):
        if event.state == 0:
            canvas.y_viewscroll(-event.delta, "units")
        elif event.state == 1: # you can use just an else as it can only be 0 or 1
            canvas.x_viewscroll(-event.delta, "units")
    
    0 讨论(0)
  • 2020-12-22 09:21

    I investigated code relating to printing general events, made code to do that for button presses (not just key presses) and I saw what event.num is for the horizontal mousewheel. So, I did the following solution:

    I don't think there are event names for horizontal mouse scrolling (tell me if I'm wrong), but there do appear to be event numbers (which are 6 for scrolling left and 7 for scrolling right). While "<Button-6>" and "<Button-7>" don't seem to work, horizontal edge scrolling is still possible in Tkinter in Python 3.5.3 on Linux (I haven't tested anything else).

    You'll need to bind "<Button>" instead of "<Button-6>" and "<Button-7>", and make a handler something like this:

    ...
        self.bind("<Button>", self.touchpad_events)
    ...
    
    def touchpad_events(self, event):
        if event.num==6:
            self.xview_scroll(-10, "units")
            return "break"
        elif event.num==7:
            self.xview_scroll(10, "units")
            return "break"
    

    I do a return "break" in each section of the if/elif statement because we don't want to return "break" all the time and interrupt other button events (only when the horizontal mousewheel is scrolled); I mean, if you break all the time regular left-clicking won't work anymore (unless you program it in), and stuff like that. I just do 10 and -10 instead of event.delta/120 because it gives me the following error otherwise for some odd reason, and just putting in numbers manually seems to work great:

    tkinter.TclError: expected integer but got "0.0"

    Anyway, I tested this solution and it works. Problem solved. :)

    0 讨论(0)
提交回复
热议问题