Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - Remove multiple label from tkinter

I'm trying to make a simple tkinter script that creates 10 numbers in the GUI after clicking a button. The problem is removing the numbers from the GUI. I can only remove one number. The numbers is beeing created via a loop. I've printed out the name of the .pack() variable "self.label_a2" and the names are: .!label2, .!label3 and so on. I have not figured out how I can use the ".!label2" names to remove the lables. Nor How to remove more than just one lable.

Here is the code:

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

When running the script it's only lable whit number 9 that gets removed. How to make all of the labes go away? Help please.

BR BaconFlip


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add all the label instances to a list and then iterate through it while destroying them, your final code should look something like this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...