使用鼠标滚轮和滚动条一起滚动多个Tkinter列表框

时间:2019-07-17 10:09:00

标签: tkinter listbox

我对Tkinter还是很陌生,我曾尝试为多个Tkinter Listbox创建一个程序,该程序可使用单个滚动条和鼠标滚轮一起滚动。当我运行该程序并开始使用鼠标滚轮滚动左侧的列表框时,右侧的列表框不会随之滚动。但是,当我开始使用鼠标滚轮滚动右侧列表框时,左侧列表框随之滚动。我找不到错误。

如何修复程序,以便当我使用鼠标滚轮在左侧或右侧列表框上开始滚动时,两个列表框将一起滚动?

try:
    from Tkinter import *
except ImportError:
    from tkinter import *


class MultipleScrollingListbox(Tk):

    def __init__(self):
        Tk.__init__(self)
        self.title('Scrolling Multiple Listboxes')

        self.scrollbar = Scrollbar(self, orient='vertical')

        self.list1 = Listbox(self, width = 40, height = 10, yscrollcommand = self.yscroll1)
        self.list1.grid(row = 2, sticky = W)

        self.list2 = Listbox(self, width = 40, height = 10, yscrollcommand = self.yscroll1)
        self.list2.grid(row = 2, column = 1, sticky = E)

        self.scrollbar.config(command=self.yview)
        self.scrollbar.grid(row = 2, column = 2, sticky = "nsw")

        #filling the listboxes with stuff

        for x in range(30):
            self.list1.insert('end', "  " + str(x))
            self.list2.insert('end', "  " + str(x))


    #The part where both Listbox scrolls together when scrolled 

    def yscroll1(self, *args):
        if self.list2.yview() != self.list1.yview():
            self.list2.yview_moveto(args[0])
        self.scrollbar.set(*args)

    def yscroll2(self, *args):
        if self.list1.yview() != self.list2.yview():
            self.list1.yview_moveto(args[0])
        self.scrollbar.set(*args)

    def yview(self, *args):
        self.list1.yview(*args)
        self.list2.yview(*args)


if __name__ == "__main__":
    root = MultipleScrollingListbox()
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以将MouseWheel事件绑定到根窗口,这将允许您滚动到任何位置。如果您不想绑定到根窗口,也可以指定所需的小部件。

try:
    from Tkinter import *
except ImportError:
    from tkinter import *


class MultipleScrollingListbox(Tk):

    def __init__(self):
        Tk.__init__(self)

        ... 

        self.bind_all("<MouseWheel>", self.mousewheel)

        ...

    def mousewheel(self, event):
        self.list1.yview_scroll(-1 * int(event.delta / 120), "units")
        self.list2.yview_scroll(-1 * int(event.delta / 120), "units")

if __name__ == "__main__":
    root = MultipleScrollingListbox()
    root.mainloop()

要详细了解为何需要进行delta/120的原因,可以阅读答案here