Brōtsyorfuzthrāx
Brōtsyorfuzthrāx

Reputation: 4749

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

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

Upvotes: 2

Views: 1471

Answers (3)

ViolationHandler.exe
ViolationHandler.exe

Reputation: 31

For those who may be here years later, on Windows (10 if it matters), Python 3.10, Touchpad scrolling values are determined by event.state. With my laptop, the values are 8 for vertical and 9 for horizontal. Code Example:

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")
# Way to test YOUR horizontal & vertical scroll:
print(event.state)

This means you can find out which value your vertical scrolling and horizontal scrolling is for you by using print(event.state) when you try to scroll, then it will print which scrolling you are doing, allowing you to check your values. The values provided by Brōtsyorfuzthrāx did not work for me, that's why I am sharing. Hope it helps!

Upvotes: 1

Marfung37
Marfung37

Reputation: 1

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")

Upvotes: 0

Brōtsyorfuzthrāx
Brōtsyorfuzthrāx

Reputation: 4749

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

Upvotes: 2

Related Questions