How to delete unsaved tkinker label?

淺唱寂寞╮ 提交于 2019-12-13 08:17:57

问题


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

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) 

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


回答1:


You can ask grid to give a list of widgets that it manages. You could then iterate over that list to find out which widget is in each row and column.

For example, if you wanted to be able to modify the text in the widget at a specific row or column, you could do something like this:

def set_item_text(master, row, column, text):
    for child in master.grid_slaves():
        grid_info = child.grid_info()
        if grid_info['row'] == row and grid_info['column'] == column:
            child.configure(text=text)

Here's an example that will change the text in row 2 column 2 to "hello, world":

import Tkinter as tk

def set_item_text(master, row, column, text):
    for child in master.grid_slaves():
        grid_info = child.grid_info()
        if grid_info['row'] == row and grid_info['column'] == column:
            child.configure(text=text)

root = tk.Tk()

for row in range(4):
    for col in range(5):
        tk.Label(
            root, text="row %s col %s" % (row, col)
        ).grid(row=row, column=col, padx=8)

set_item_text(root, 2,2, "hello, world")

root.mainloop()

You could just as easily delete the widget, though if you're just wanting to refresh a "table-like thing" it's more efficient just to change the data than to delete and recreate all of the widgets.




回答2:


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


来源:https://stackoverflow.com/questions/48550578/how-to-delete-unsaved-tkinker-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!