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

纵然是瞬间 提交于 2019-11-28 12:33:10

问题


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 know the event name(s) to allow me to do this. The vertical events work fine (so does pressing shift and using the vertical edge scroll to do horizontal scrolling). What are the event names I'm looking for? They don't appear to be "<Button-6>" and "<Button-7>", as these give errors; e.g.

_tkinter.TclError: specified keysym "6" for non-key event

I'm not sure what "<Button-6>" has to do with keys, but okay.

I'm using Linux (I say this because the events for vertical scrolling are different on Linux than Windows). I know how to discover unknown event names from key presses, but I'm not sure how to do that with the touchpad, too.

Looking at the list of events for a Text widget doesn't seem particularly insightful (other than that I don't see an event that looks like it's for the horizontal mousewheel).

I'm using Python 3.5.3 on Linux (Xubuntu 17.04, 64-bit).


回答1:


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



来源:https://stackoverflow.com/questions/46194948/what-are-the-tkinter-events-for-horizontal-edge-scrolling-in-linux

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