【发布时间】:2019-04-20 07:52:15
【问题描述】:
我正在尝试从网格中删除这些元素。我能够通过一一写出将它们全部删除。然后我编写了一个 for 循环以使其可扩展,然后遇到此错误消息。
"employee.destroy()
AttributeError: 'str' object has no attribute 'destroy'"
这是一个更大程序的一部分,但我可以尽可能减少核心问题,这是我的代码:
import tkinter as tk
from tkinter import ttk
labelemployee={}
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.buttons()
self.create_grid()
self.add_left_names()
def remove(self):
#labelemployee["A"].destroy()
#labelemployee["B"].destroy()
#labelemployee["C"].destroy()
#labelemployee["D"].destroy()
for employee in labelemployee:
employee.destroy()
def create_grid(self):
for i in range (7):
for j in range(7):
self.label = tk.Label(self, relief="ridge", width=12,
height=3)
self.label.grid(row=i, column=j, sticky='nsew')
def buttons(self):
self.button=tk.Button(self, text="Clear", bg="salmon", command
= self.remove)
self.button.grid(row=7, column=6, sticky='e')
def add_left_names(self):
#--------add in name labels on the side--------------
i=2
for employee in self.Employees:
self.label=tk.Label(self, text=employee , fg="red",
bg="snow")
self.label.grid(row=i,column=0)
labelemployee[employee]=self.label
i +=1
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()
请帮助我。我认为问题是我的 for 循环存储了一个列表,并且我有一本字典。所以,那么我想我不知道如何销毁字典中的标签。
【问题讨论】:
标签: python-3.x for-loop tkinter label destroy