如何删除未保存的tkinker标签? [英] How to delete unsaved tkinker label?

查看:110
本文介绍了如何删除未保存的tkinker标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了这个程序,在其中将标签放置在网格上而不将其保存在变量中.之所以这样做,是因为这样我就可以for循环遍历所有类,并从每个类中获取数据并将它们添加到一行中.这只是其中的一小部分:

I made this program where I am putting labels on a grid without saving them in a variable. I do this because then I can for loop through a list of classes and get the data from each class in and add them to a row. This is a small piece of it:

self.collum = 0
for i in self.gui_resource_list:
   Label(text=i.get_name(), relief="groove", width=15).grid(column=self.column, row=0)
   Label(text=i.get_buyPrice(), relief="groove", width=15).grid(column=self.column, row=1)
   Label(text=i.get_salePrice(), relief="groove", width=15).grid(column=self.column, row=2)
   Label(text=i.arrow, relief="groove", width=15).grid(column=self.column,row=3)
   self.column += 1

因此,这将生成类似表格的布局.然后有一个按钮将更新所有值,并再次运行for循环.因此,它基本上在较旧的标签上绘制了新标签.这不好,因为在转弯300时,gui_resource列表中的所有资源实例乘以300个标签.解决此问题的一种方法是删除旧标签.

So this will generate a table-like layout. Then there is a button that updates all the values runs that for loop again. So it basically draws the new labels on top of the older ones. This is not good because when you are on the turn 300 there are 300 labels times the all the resource instances in gui_resource list. A way to fix this is to delete the old labels.

是否可以删除未保存的标签?像这样:

Is there a way to delete an unsaved label? Something like:

delete_grid(column=2,row=3) 

那会删除网格中2,3位的所有东西吗?

And that would delete all of the things in the grid at position 2,3?

推荐答案

from pprint import pprint
from tkinter import Tk, Label

root = Tk()
Label(root, text='MyLabel').pack()
Label(root, text='MyLabel').pack()
Label(root, text='MyLabel').pack()

# as you did not kept references to the labels
# you have to look into the childrens of root

pprint(root.children) # show root children names
print()

root.children['!label2'].destroy() # do what you asked on the second Label
pprint(root.children) # check that it's gone

这篇关于如何删除未保存的tkinker标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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