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

时光毁灭记忆、已成空白 提交于 2019-11-29 18:14:15

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. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!