感谢@ Miraj50,帮助我与.destroy() Tkinter的:如何编写一个for循环破坏标签列表? 下一阶段,我试图摧毁这两个选项卡的标签。 我知道如何共享同一份名单,不同的标签,但我不知道如何“链接”他们的标签。 用我有限的知识,我试过
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()