0

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

Baconflip
  • 47
  • 6

2 Answers2

1

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()
astqx
  • 2,058
  • 1
  • 10
  • 21
  • 1
    Thank you, that worked as I wanted it, it also removed "label_a1" as I wanted, perfect. You and jizhihaoSAMA were right and fast to help me. Thank you both! – Baconflip Jan 07 '21 at 07:46
1

Another way of doing it is by using root.winfo_children() to get the children of the root widget and checking if the child is an instance of the label using isinstance(). This might be suitable for you if you don't want to store every label in lists.

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')
JacksonPro
  • 3,135
  • 2
  • 6
  • 29