简体   繁体   English

从放置在网格中的Tkinter窗口中删除标签列表

[英]Deleting a List of Labels From The Tkinter Window Placed In The Grid

a. 一种。 Have a scenario where I had created a list of labels as shown below. 在我创建了标签列表的情况下,如下所示。

class test_template:
def __init__(self, master):
    self.master = master

...

def nb_code(self):
    if nb_cnt == 0:
        for i in range (int(no_of_fs)):
            self.enul = Label(root, text="Enter The Number Of Fruits In Basket%d\n"%i)
            self.enul.grid(row=i+1)

    # Trying To Delete The List Of Labels  
    elif nb_cnt == 2:
        for i in range (int(no_of_fs)):
            self.enul.grid_forget() 

b. b。 Say if I have a list of 3 Labels, when I try to delete them by putting in a loop, only they first one gets deleted which makes sense because that was holding the Label information for the last Label assigned. 假设我有一个3个标签的列表,当我尝试通过循环删除它们时,只有第一个标签被删除,这是有意义的,因为这保存着最后分配的标签的标签信息。

c. C。 But then in this scenario what I need to do to delete the complete list of Labels ? 但是在这种情况下,我需要怎么做才能删除标签的完整列表? Can be it be done by searching for the "Label" name in the total grid and delete them ? 是否可以通过在整个网格中搜索“标签”名称并将其删除来完成? or how can it be done ? 或怎么做?

Please share in your comments !! 请分享您的评论!

You can get root children and search for your labels. 您可以获取root子代并搜索标签。

for child in root.children.values():
    info = child.grid_info()
    if info['column'] == 0:
        child.grid_forget()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM