从 tkinter 中删除多个标签
Remove multiple label from tkinter
我正在尝试制作一个简单的 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 BaconFlip
您可以将所有标签实例添加到一个列表中,然后在销毁它们的同时遍历它,您的最终代码应该如下所示
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()
另一种方法是使用 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')
我正在尝试制作一个简单的 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 BaconFlip
您可以将所有标签实例添加到一个列表中,然后在销毁它们的同时遍历它,您的最终代码应该如下所示
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()
另一种方法是使用 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')