How to delete unsaved tkinker label?

2024/11/15 21:44:00

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?

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 tkdef 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.

https://en.xdnf.cn/q/119609.html

Related Q&A

Adjust every other row of a data frame

I would like to change every second row of my data frame.I have a df like this:Node | Feature | Indicator | Value | Class | Direction -------------------------------------------------------- 1 | …

Why is the list index out of range?

Im new at programing and Im trying to check a piece of code that keeps giving me this error: t[i] = t[i - 1] + dt IndexError: list index out of rangeThe code is the following: dt = 0.001t = [0] for i i…

Stopping a while loop mid-way - Python

What is the best way to stop a while loop in Python mid-way through the statement? Im aware of break but I thought using this would be bad practice.For example, in this code below, I only want the pro…

Click on element in dropdown with Selenium and Python

With Selenium and Chrome webdriver on MacOS need to click dropdown element. But always have an error that cant find. Have this html code on a page where it located:<select id="periodoExtrato&qu…

Send cv2 video stream for face recognition

Im struggling with a problem to send a cv2 videostream (webcam) to a server (which shall be used later for face recognition). I keep getting the following error for the server: Traceback (most recent c…

Generate all possible lists from the sublist in python [duplicate]

This question already has answers here:How to get the Cartesian product of multiple lists(20 answers)Closed 7 years ago.Suppose I have list [[a, b, c], [d, e], [1, 2]]I want to generate list where on t…

Time/frequency color map in python

Is there in native Python 3.X library or in scipy/numpy/matplolib libraries a function or their short set which could help me to draw a plot similar to this one(?):What would be an efficient way to ac…

ImageMagick is splitting the NASAs [.IMG] file by a black line upon converting to JPG

I have some raw .IMG format files which Im converting to .jpg using ImageMagick to apply a CNN Classifier. The converted images, however have a black vertical line splitting the image into two. The par…

CV2 - rectangular detecting issue

Im trying to implement an OMR using pythons CV2. As part of the code I need to find the corners of the choices box (rectangulars) however I ran into difficulty causes by the grade sheet template. In th…

Keras/TensorFlow - high acc, bad prediction

Im new to machine learning and Im trying to train a model which detects Prague city in a sentence. It can be in many word forms.Prague, PRAHA, Z Prahy etc...So I have a train dataset which consists of …