如何编写一个for循环来销毁标签列表

如何编写一个for循环来销毁标签列表

本文介绍了tkinter:如何编写一个for循环来销毁标签列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网格中删除这些元素.通过逐一写出,我可以删除所有这些文件.然后,我编写了一个for循环以使其可扩展,然后遇到此错误消息.

I am trying to remove these elements from the grid. I was able to delete all of them by writing out one by one. I then wrote a for loop to make it expandable, then I run into this error message.

"employee.destroy()
AttributeError: 'str' object has no attribute 'destroy'"

这是一个更大程序的一部分,但是我可以尽最大努力解决核心问题,这是我的代码:

This is part of a bigger program, but as much as I can reduce to the core problem, here is my code:

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循环存储了一个列表,并且我有一个字典.因此,我想我不知道如何破坏字典中的标签.

Please help me. I think the problem is my for loop is stored a list, and I have a dictionary. So, then I guess i don't know how to destroy labels in a dictionary.

推荐答案

您已经在这些评论中找到了问题!

You have already figured out the issue in those comments !

您已经知道labelemployee是字典,因此在默认情况下对其进行迭代将为您提供字典的键.因此,employee将是诸如 A B ...之类的字符串.销毁字符串对象显然会给您一个错误.您需要销毁相应的tkinter小部件.因此,您应该在for循环中将employee.destroy()替换为labelemployee[employee].destroy().

You already know that labelemployee is a dictionary, so iterating over it will give you, by default, the keys of the dictionary. So employee will be strings like A, B... and so on. And destroying a string object will obviously give you an error. You need to destroy the corresponding tkinter widget. So for that, you should replace employee.destroy() with labelemployee[employee].destroy() in the for loop.

这篇关于tkinter:如何编写一个for循环来销毁标签列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:08