Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
170 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...