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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…