0

我正在尝试制作一个简单的 tkinter 脚本,在单击按钮后在 GUI 中创建 10 个数字。问题是从 GUI 中删除数字。我只能删除一个号码。这些数字是通过循环创建的。我打印了 .pack() 变量“self.label_a2”的名称,名称为:.!label2、.!label3 等等。我还没有弄清楚如何使用“.!label2”名称来删除标签。也不是如何删除多个标签。

这是代码:

from tkinter import *
root = Tk()

class the_GUI:
    def __init__(self, master):
        self.master = master
        master.title("Numbers")
        self.start_prim_button = Button(master, text="Get numbers", command=self.main_prim, width=30)
        self.start_prim_button.pack()   
        self.start_secu_button = Button(master, text="Remove numbers", command=self.main_secu, width=30)
        self.start_secu_button.pack()   
    def main_prim(self):
        self.label_a1 = Label(text='Numbers:', bg = "orange", width=30)
        self.label_a1.pack()
        numbers(self)
    def main_secu(self):
        remove_numbers(self)
def numbers(self):
    print('Printing numbers:')
    # Loop to create numbers
    for i in range(10):
        print(i)
        self.label_a2 = Label(text=i, bg = "light green", width=30)
        self.label_a2.pack()
        print(self.label_a2)
def remove_numbers(self):
    print('Removing numbers:')
    try:
        for i in range(10):
            self.label_a2.destroy()
            root.update_idletasks()
            print('Removing', i)
    except:
        print('Generate numbers first')
my_gui = the_GUI(root)
root.mainloop()

运行脚本时,只有 9 号标签会被删除。如何让所有的实验室消失?请帮忙。

BR培根翻转

4

2 回答 2

1

您可以将所有标签实例添加到列表中,然后在销毁它们时遍历它,您的最终代码应如下所示

from tkinter import *
root = Tk()

class the_GUI:
    def __init__(self, master):
        self.master = master
        master.title("Numbers")
        self.start_prim_button = Button(master, text="Get numbers", command=self.main_prim, width=30)
        self.start_prim_button.pack()   
        self.start_secu_button = Button(master, text="Remove numbers", command=self.main_secu, width=30)
        self.start_secu_button.pack()   
        self.label_list=[]
    def main_prim(self):
        self.label_a1 = Label(text='Numbers:', bg = "orange", width=30)
        self.label_a1.pack()
        numbers(self)
    def main_secu(self):
        remove_numbers(self)
def numbers(self):
    print('Printing numbers:')
    # Loop to create numbers
    for i in range(10):
        print(i)
        self.label_a2 = Label(text=i, bg = "light green", width=30)
        self.label_a2.pack()
        self.label_list.append(self.label_a2)
        print(self.label_a2)
def remove_numbers(self):
    print('Removing numbers:')
    try:
        for i in range(10):
            self.label_list[i].destroy()
            root.update_idletasks()
            print('Removing', i)
        self.label_a1.destroy()
        self.label_list=[]
    except:
        print('Generate numbers first')
my_gui = the_GUI(root)
root.mainloop()
于 2021-01-07T07:37:46.917 回答
1

另一种方法是使用root.winfo_children() 获取根小部件的子级并检查子级是否是标签的实例,使用isinstance(). 如果您不想将每个标签都存储在列表中,这可能适合您。

def remove_numbers(self):
    print('Removing numbers:')
    try:

        for i in self.master.winfo_children():
            if isinstance(i, Label):
                i.destroy()
                root.update_idletasks()
                print('Removing', i)
    except:
        print('Generate numbers first')
于 2021-01-07T07:51:16.813 回答