Aaron
Aaron

Reputation: 11075

Tkinter bind <Shift-MouseWheel> to horizontal scroll

I have a text widget with a horizontal and vertical scroll bar. I would like to be able to scroll up and down normally, and scroll side to side while holding shift. I can't figure out what to bind the <Shift-MouseWheel> event to, or what the callback ought to be. Here's a code snippet of the mainWindow(Tk.TopLevel)'s __init__():

    # console text
    self.yScroll = Tk.Scrollbar(self)
    self.yScroll.pack(side=Tk.RIGHT, fill=Tk.Y)
    self.xScroll = Tk.Scrollbar(self)
    self.xScroll.pack(side=Tk.BOTTOM, fill=Tk.X)
    self.log = Tk.Text(self,
                       wrap=Tk.NONE,
                       width=80,
                       height=24,
                       yscrollcommand=self.yScroll.set,
                       xscrollcommand=self.xScroll.set)
    self.log.pack()
    self.yScroll.config(command=self.log.yview)
    self.xScroll.config(command=self.log.xview, orient=Tk.HORIZONTAL)

    # shift scroll binding
    self.bind('<Shift-MouseWheel>', ) # what do I need here?

I have successfully bound the shift-scroll to simple print functions, etc. but I'm not sure how to bind it to the text box scrolling.

Upvotes: 4

Views: 2873

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386240

You need to have the binding call your own function, and then have that function call the xview_scroll method of the widget:

self.bind('<Shift-MouseWheel>', self.scrollHorizontally) 
...

def scrollHorizontally(self, event):
    self.log.xview_scroll((event.delta/120), "units")

You may need to adjust the number of units (or "pages") you want to scroll with each click of the wheel.

This answer has more information about platform differences with respect to the delta attribute of the event.

Upvotes: 6

Related Questions