Tkinter将<Shift-MouseWheel>绑定到水平滚动

Aar*_*ron 3 python events user-interface scroll tkinter

我有一个带有水平和垂直滚动条的文本小部件。我希望能够正常上下滚动,并在按住shift的同时左右滚动。我不知道将<Shift-MouseWheel>事件绑定到什么,或者回调应该是什么。这是mainWindow(Tk.TopLevel)的代码段__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?
Run Code Online (Sandbox Code Playgroud)

我已成功将shift-scroll绑定到简单的打印功能等,但是我不确定如何将其绑定到文本框滚动。

Bry*_*ley 5

您需要让绑定调用您自己的函数,然后使该函数调用小部件的xview_scroll方法:

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

def scrollHorizontally(self, event):
    self.log.xview_scroll((event.delta/120), "units")
Run Code Online (Sandbox Code Playgroud)

您可能需要调整每次点击滚轮时要滚动的单位(或“页面”)的数量。

该答案具有有关delta事件属性的平台差异的更多信息。