Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove widgets from grid in tkinter

I have a grid in a tkinter frame which displays query results. It has a date field that is changed manually and then date is used as a parameter on the query for those results. every time the date is changed, obviously the results change, giving a different amount of rows. The problem is that if you get less rows the second time you do it, you will still have results from the first query underneath those and will be very confusing.

My question is, how can i remove all rows from the frame that have a row number greater than 6 (regardless of what's in it)?

By the way, I'm running Python 3.3.3. Thanks in advance!

like image 546
rodrigocf Avatar asked Apr 21 '14 01:04

rodrigocf


People also ask

Is Pack or grid better Tkinter?

pack is the easiest layout manager to use with Tkinter. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning. For simple positioning of widgets vertically or horizontally in relation to each other, pack() is the layout manager of choice.

What does pack () do in Tkinter?

The pack() fill option is used to make a widget fill the entire frame. The pack() expand option is used to expand the widget if the user expands the frame.

How do you destroy a frame in Python?

If we want to clear the frame content or delete all the widgets inside the frame, we can use the destroy() method. This method can be invoked by targeting the children of the frame using winfo_children().

How do you clear the screen in Tkinter?

While creating a canvas in tkinter, it will effectively eat some memory which needs to be cleared or deleted. In order to clear a canvas, we can use the delete() method. By specifying “all”, we can delete and clear all the canvas that are present in a tkinter frame.


1 Answers

Calling the method grid_forget on the widget will remove it from the window - this example uses the call grid_slaves on the parent to findout all widgets mapped to the grid, and then the grid_info call to learn about each widget's position:

>>> import tkinter
# create: 
>>> a = tkinter.Tk()
>>> for i in range(10):
...    label = tkinter.Label(a, text=str(i))
...    label.grid(column=0, row=i)
# remove from screen:
>>> for label in a.grid_slaves():
...    if int(label.grid_info()["row"]) > 6:
...       label.grid_forget()
like image 65
jsbueno Avatar answered Oct 06 '22 19:10

jsbueno