python,tkinter,如何使用for循环销毁两个不同选项卡中的标签? [英] python, tkinter, how to destroy labels in two different tabs with for loop?

查看:95
本文介绍了python,tkinter,如何使用for循环销毁两个不同选项卡中的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢@ Miraj50帮助我完成了.destroy() tkinter:如何编写for循环以销毁标签列表?在下一阶段,我试图销毁标签两个标签中的标签.我知道如何将同一列表共享到不同的选项卡,但是我不知道如何将它们链接"到选项卡.据我所知,我尝试过

Thanks to @Miraj50 that helped me with the .destroy() tkinter: how to write a for loop to destroy a list of labels? For the next stage, I am trying to destroy the labels in both tabs. I knew how to share the same list to different tabs, but I don't know how to "link" them to the tabs. With my limited knowledge I tried

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"]" 

只是看看它是否有效.它仍然有相同的错误.如果有人可以帮助我解决这个问题并消除我的困惑,请.这是完整的代码:

just to see if it works or not. It still has the same error. If someone can assist me with this hump and clear my confusion, please. Here is the complete code:

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()

推荐答案

首先,您需要跟踪所有标签.在这里,仅在 add_left_names 中分配标签将覆盖先前的标签.因此,我将标签存储在员工 value 列表中.现在,在删除功能中,您只需要遍历 labelemployee 中的所有这些标签并销毁它们.

First of all you need to keep track of all the labels. Here just assigning the labels in add_left_names will override the previous label. So I stored the labels in a list which is the value to the employee key. Now in the remove function, you just need to iterate on all these labels in labelemployee and destroy them.

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()

这篇关于python,tkinter,如何使用for循环销毁两个不同选项卡中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆