感谢@ Miraj50帮助我完成了.destroy() tkinter: how to write a for loop to destroy a list of labels?在下一阶段,我试图破坏两个标签中的标签。我知道如何将同一列表共享到不同的选项卡,但是我不知道如何将它们“链接”到选项卡。据我所知,我尝试过
def remove(self, name):
for name in tabs[name]:
for employee in labelemployee:
labelemployee[employee].destroy()
它给了我这个错误:
TypeError: remove() missing 1 required positional argument: 'name'
然后我尝试了
"for name in tabs["Requests"]"
只是看看它是否有效。它仍然有相同的错误。如果有人可以帮助我解决这个问题并消除我的困惑,请。这是完整的代码:
import tkinter as tk
from tkinter import ttk
labelemployee={}
upper_tabs = ["Final", "Requests", "Control"]
tabs = {}
class Application(ttk.Frame): #inherent from frame.
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="LightBlue4")
self.parent = parent
self.Employees = ["A", "B", "C", "D"]
self.pack(fill=tk.BOTH, expand=1)
self.GUI()
def GUI(self): #the function that runs all the GUI functions.
self.create_tabs()
self.buttons("Control")
for name in upper_tabs[:2]:
self.create_grid(name)
self.add_left_names("Requests")
self.add_left_names("Final")
def create_tabs(self):
self.tabControl = ttk.Notebook(self, width=1000, height=400)
for name in upper_tabs:
self.tab=tk.Frame(self.tabControl, bg='thistle')
self.tabControl.add(self.tab, text=name)
tabs[name] = self.tab
self.tabControl.grid(row=0, column=0, sticky='nsew')
def create_grid(self, name):
for i in range (7):
for j in range(7):
self.label = tk.Label(tabs[name], relief="ridge",
width=12, height=3)
self.label.grid(row=i, column=j, sticky='nsew')
def buttons(self, name):
self.button=tk.Button(tabs[name], text="Clear", bg="salmon",
command = self.remove)
self.button.pack()
def add_left_names(self, name):
#--------add in name labels on the side--------------
i=2
for employee in self.Employees:
self.label=tk.Label(tabs[name], text=employee , fg="red",
bg="snow")
self.label.grid(row=i,column=0)
labelemployee[employee]=self.label
i +=1
**def remove(self, name):
for name in tabs[name]:
for employee in labelemployee:
labelemployee[employee].destroy()**
def main():
root = tk.Tk()
root.title("class basic window")
root.geometry("1000x500")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
首先,您需要跟踪所有标签。在这里,仅在 add_left_names 中分配标签将覆盖先前的标签。因此,我将标签存储在员工键的 value 列表中。现在,在删除功能中,您只需要迭代 labelemployee 中的所有这些标签并销毁它们。
import tkinter as tk
from tkinter import ttk
from collections import defaultdict
labelemployee=defaultdict(list)
upper_tabs = ["Final", "Requests", "Control"]
tabs = {}
class Application(ttk.Frame): #inherent from frame.
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="LightBlue4")
self.parent = parent
self.Employees = ["A", "B", "C", "D"]
self.pack(fill=tk.BOTH, expand=1)
self.GUI()
def GUI(self): #the function that runs all the GUI functions.
self.create_tabs()
self.buttons("Control")
for name in upper_tabs[:2]:
self.create_grid(name)
self.add_left_names("Requests")
self.add_left_names("Final")
def create_tabs(self):
self.tabControl = ttk.Notebook(self, width=1000, height=400)
for name in upper_tabs:
self.tab=tk.Frame(self.tabControl, bg='thistle')
self.tabControl.add(self.tab, text=name)
tabs[name] = self.tab
self.tabControl.grid(row=0, column=0, sticky='nsew')
def create_grid(self, name):
for i in range (7):
for j in range(7):
self.label = tk.Label(tabs[name], relief="ridge", width=12, height=3)
self.label.grid(row=i, column=j, sticky='nsew')
def buttons(self, name):
self.button=tk.Button(tabs[name], text="Clear", bg="salmon", command=self.remove)
self.button.pack()
def add_left_names(self, name):
#--------add in name labels on the side--------------
i=2
for employee in self.Employees:
self.label=tk.Label(tabs[name], text=employee, fg="red", bg="snow")
self.label.grid(row=i,column=0)
labelemployee[employee].append(self.label)
i +=1
def remove(self):
for employee in labelemployee:
for label in labelemployee[employee]:
label.destroy()
def main():
root = tk.Tk()
root.title("class basic window")
root.geometry("1000x500")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
if __name__ == '__main__':
main()