【问题标题】:Make Button change content of Listbox使 Button 改变 Listbox 的内容
【发布时间】:2015-06-28 13:13:47
【问题描述】:

我正在尝试通过按下按钮使列表框的选定元素移动到另一个列表框。但是,我不明白如何让按钮知道它正在获取和发送数据到哪些列表框。

这是我的代码:

import tkinter as tk
from tkinter import ttk

class MoveItems(ttk.Button):
#the button should move things from the origin listbox to the destiny
#listbox
    def __init__(self, parent, origin, destiny, label):
        ttk.Button.__init__(self, parent, text = label)
        self.parent = parent
        self.origin = origin #is this correct?
        self.destiny = destiny
        self.command = self.moveToDestiny

    def moveToDestiny(self):
        selected_items = self.origin.curselection()
        selected = [self.origin.get(i) for i in selected_items]
        self.destiny.list_update(selected)


class ListOfThings(tk.Listbox):
#here are my listboxes
#list_update adds a list to the listbox
        def __init__(self, parent, list_to_show):
            tk.Listbox.__init__(self, parent, selectmode = tk.EXTENDED)
            self.list_to_show = list_to_show
            self.parent = parent
            self.list_update(list_to_show)

        def list_update(self, list):
            for item in list:
                self.insert(tk.END, item)


class SelectFromList(ttk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        list1 = ["A", "B", "C"]
        list2 = ["D", "E", "F"]

        self.leftList = ListOfThings(self, list1)
        self.rightList = ListOfThings(self, list2)
        self.moveRight = MoveItems(self, self.leftList, self.rightList, "-->")

        self.leftList.pack(side = "left")
        self.moveRight.pack(side = "left")
        self.rightList.pack(side = "right")

if __name__ == "__main__":
    root = tk.Tk()
    SelectFromList(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

我不确定我的 MoveItems 按钮的 __init__ 是否真的跟踪起源和命运列表框。我尝试只打印 selected 项目,但终端上没有显示任何内容。

【问题讨论】:

    标签: python tkinter listbox


    【解决方案1】:

    我认为self.command = self.moveToDestiny 不会做你想做的事。我们知道command 是Button 的init 方法中的参数名称,但这并不一定意味着该值随后会存储在command 属性中。

    尝试在调用Button.__init__ 时指定command

    class MoveItems(ttk.Button):
    #the button should move things from the origin listbox to the destiny
    #listbox
        def __init__(self, parent, origin, destiny, label):
            ttk.Button.__init__(self, parent, text = label, command=self.moveToDestiny)
    

    现在您的按钮应该成功地将项目添加到右侧的列表框中。


    我敢打赌,您还希望从左侧框中删除选定的项目。您必须创建另一个ListOfThings 方法,从列表中删除 项。比如:

        def list_remove(self, items_to_remove):
            items = self.get(0, tk.END)
            remaining_items = [item for item in items if item not in items_to_remove]
            self.delete(0, tk.END)
            self.list_update(remaining_items)
    

    (可能不是最好的方法,但这只是一个例子)

    那你可以在moveToDestiny调用它。

    def moveToDestiny(self):
        selected_items = self.origin.curselection()
        selected = [self.origin.get(i) for i in selected_items]
        self.destiny.list_update(selected)
        self.origin.list_remove(selected)
    

    【讨论】:

    • 我在错误的地方寻找问题。将命令放在按钮的 init 中解决了这个问题。感谢您提供从列表框中删除项目的后续示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多