【问题标题】:tkinter multilistbox lists are independent of each othertkinter multilistbox 列表相互独立
【发布时间】:2019-12-20 09:05:45
【问题描述】:

enter image description here我想用这段代码创建多个列表,并让我创建的列表相互配合。当我制作 mlb.get (ACTIVE) 时,单击列表中的值。 get 命令不与其他列表同步。 info 函数在侧面创建一个部分并列出单击行上的信息。 MULTILISTBOX 类需要制作列表并协同工作。

import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import apply as apply

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('<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)
        #self.lists[0]['yscrollcommand'] = sb.set

    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:
            apply(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 apply(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)

def info():
    yazi.after(2000,info)
    if mlb.curselection():
        yazi.config(text=mlb.get(ACTIVE))
        print(mlb.get(ACTIVE))



if __name__ == '__main__':
    tk = Tk()
    frame=Frame(tk)
    frame2=Frame(tk)
    screen_width = tk.winfo_screenwidth()
    screen_height = tk.winfo_screenheight()
    sol=int(screen_width*60/100)
    frame.config(width=sol)
    frame2.config(width=screen_width-sol)

    Label(tk, text='MultiListbox').pack()
    mlb = MultiListbox(frame, (('Bağlantı Noktası', 15), ('Cihaz Adı', 50), ('Seri Numarası', 20), ('Boyut', 10), ('İlerleme', 25), ('Durum', 20)))
    for i in range(20):
        mlb.insert(END, ('sda%d' % i, 'Sandisk bla bla bla %d' % i, '85545696324 54112','1000GB','//////////////%d'% i , 'Hazırmıyızzzz %d' % i))
    mlb.pack(expand=YES, fill=BOTH)
    yazi=Label(frame2)
    yazi.pack()
    info()
    frame.pack(side=LEFT)
    frame2.pack(side=RIGHT)
    tk.mainloop()

【问题讨论】:

  • ListBox中选择元素时使用lb.bind("&lt;&lt;ListboxSelect&gt;&gt;", function)运行函数,然后可以更改其他Listbox中的内容
  • 也许您应该将lb 发送到绑定函数,以便它们可以处理不同的列表。

标签: python tkinter listbox


【解决方案1】:

问题MultiListbox 类中的多个 Listbox 应该通过单击一个 Listbox 来同步。

更改/添加以下内容:

  1. 绑定到事件'&lt;&lt;ListboxSelect&gt;&gt;'
    class MultiListbox(Frame):
        def __init__(self, master, lists):
            ...
            for l, w in lists:
                ...
                lb.bind('<<ListboxSelect>>', self.on_select)
    
  2. 把你的全局def info():改成类方法def on_select(...

    def on_select(self, event):
        w = event.widget
        curselection = w.curselection()
    
        if curselection:
            self.selection_clear(0, self.size())
            self.selection_set(curselection)
    
            mlb_get = mlb.get(curselection)
            yazi.config(text=mlb_get)
            print(mlb_get)
    
  3. __main__ 中删除语句:info()
    # info()
    

【讨论】:

  • 我做了你写的。该程序没有输出我更改的任何内容但没有得到结果。
  • @MuhammetOnurAslan:编辑您的问题代码以显示您的尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 2011-01-29
  • 2011-01-22
相关资源
最近更新 更多