【问题标题】:How bind mousewheel to tk inter _listbox?如何将鼠标滚轮绑定到 tkinter 列表框?
【发布时间】:2014-05-30 14:17:14
【问题描述】:

我有一个多列表框界面,但是当我使用鼠标滚轮(向上或向下)时,列独立移动。

如何同时移动所有列?

(希望你能帮助我,我是初学者,我已经在这方面工作了几个星期)

这是我的代码:

from tkinter import *

class MultiListbox(Frame):
    def __init__(self, master, lists):
        Frame.__init__(self, master)
        self.lists = []
        for l,w in lists:
            frame = Frame(self); frame.pack(side=LEFT, expand=YES, fill=BOTH)
            Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X)
            lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0,
                 relief=FLAT, exportselection=FALSE)
            lb.pack(expand=YES, fill=BOTH)
            self.lists.append(lb)
            lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y))
            lb.bind("<MouseWheel>")
            lb.bind('<Button-1>', lambda e, s=self: s._select(e.y))
            lb.bind('<Leave>', lambda e: 'break')
            lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
            lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
        frame = Frame(self); frame.pack(side=LEFT, fill=Y)
        Label(frame, borderwidth=1, relief=RAISED).pack(fill=X)
        sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll)
        sb.pack(expand=YES, fill=Y)

    def _select(self, y):
        row = self.lists[0].nearest(y)
        self.selection_clear(0, END)
        self.selection_set(row)
        return 'break'

    def _button2(self, x, y):
        for l in self.lists: l.scan_mark(x, y)
        return 'break'

    def _b2motion(self, x, y):
        for l in self.lists: l.scan_dragto(x, y)
        return 'break'''

    def _scroll(self, *args):
        for l in self.lists:
            l.yview(*args)
    def curselection(self):
        return self.lists[0].curselection()

    def delete(self, first, last=None):
        for l in self.lists:
            l.delete(first, last)

    def get(self, first, last=None):
        result = []
        for l in self.lists:
            result.append(l.get(first,last))
        if last: return map(None, *result)
        return result

    def index(self, index):
        self.lists[0].index(index)

    def insert(self, index, *elements):
        for e in elements:
                i = 0
        for l in self.lists:
            l.insert(index, e[i])
            i = i + 1

    def size(self):
        return self.lists[0].size()

    def see(self, index):
        for l in self.lists:
            l.see(index)

    def selection_anchor(self, index):
        for l in self.lists:
            l.selection_anchor(index)

    def selection_clear(self, first, last=None):
        for l in self.lists:
            l.selection_clear(first, last)

    def selection_includes(self, index):
        return self.lists[0].selection_includes(index)

    def selection_set(self, first, last=None):
        for l in self.lists:
            l.selection_set(first, last)

if __name__ == '__main__':
        tk = Tk()
        Label(tk, text='MultiListbox').pack()
        mlb = MultiListbox(tk, (('Clave', 20), ('Descripcion', 20), ('Existencia', 20)))


        lineas = len(open("productos-doc1.txt").readlines())
        totalLineas=(int(lineas))

        try:
            x = 0
            while 0 != totalLineas:
                abrir = open("productos-doc1.txt","r+")#ABRE EL ARCHIVO
                leer = abrir.readlines()
                renglon = leer[x]
                splitRenglon = renglon.split("'")
                clave = splitRenglon[0]
                descripcion = splitRenglon[1]
                existencia = splitRenglon[3]
                mlb.insert(END, (clave, descripcion, existencia))
                mlb.pack(expand=YES,fill=BOTH)

                x = x+1
        except IndexError:
            mlb.insert(END,"FINISH" )


tk.mainloop()

【问题讨论】:

    标签: python tkinter mouseevent mousewheel python-3.4


    【解决方案1】:

    仅通过查看代码,因为没有您的文本文件我无法运行它,您似乎没有为列表的 xscrollcommand 选项分配任何功能,该选项指定了滚动列表时要做什么。我认为没有它,您的滚动条将无法按预期工作。您可以按如下方式添加选项:

        sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll)
    
        [l.config(yscrollcommand=sb.set) for l in self.lists]
    

    但是,不确定这是否是您的问题的原因。

    【讨论】:

    • 谢谢你!它有效,我的卷轴以前不动,但现在可以了。现在的问题是:我那里有 3 列,当将鼠标放在每个人身上时,它们都独立移动,我使用了鼠标滚轮。我想用鼠标滚轮移动所有列。注意:当我推动滚动时,所有动作都一起移动。对不起我的英语不好,再次感谢你!
    猜你喜欢
    • 2013-06-25
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多