3

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.

Aaron
  • 10,133
  • 1
  • 24
  • 40
  • Are you saying you don't know how to call the `xview` command of the widget? – Bryan Oakley Jul 06 '15 at 20:24
  • more or less... I'm not sure where to pass the event and make it actually scroll the window. Tried: `self.bind('', self.log.xview)` got: `TclError: bad option "": must be moveto or scroll` – Aaron Jul 06 '15 at 20:35
  • If you want to enable regular horizontal scrolling, too, or if you get the error I got with `(event.delta/120)` as an argument, even though that still works, you might check out this related q/a: https://stackoverflow.com/questions/46194948/what-are-the-tkinter-events-for-horizontal-edge-scrolling-in-linux – Brōtsyorfuzthrāx Sep 13 '17 at 11:53

1 Answers1

5

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.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685