Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete Tkinter widgets from a window?

Tags:

python

tkinter

I have a list of tkinter widgets that I want to change dynamically.

How to delete the widgets from the window?

like image 642
TheBeardedBerry Avatar asked Sep 11 '12 07:09

TheBeardedBerry


People also ask

How do I remove a widget from Python?

Step 1: First, import the library tkinter. Step 2: Now, create a GUI app using tkinter. Step 3: Then, create a function to remove widgets from the grid in tkinter. In the function created, we have used the inbuilt function grid_remove() to remove certain widgets.

How do I delete all widgets in a frame?

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 I delete an entry widget?

Tkinter Entry widgets are used to display a single line text that is generally taken in the form of user Input. We can clear the content of Entry widget by defining a method delete(0, END) which aims to clear all the content in the range.

How do I close a Tk window in Python?

Creating an application using tkinter is easy but sometimes, it becomes difficult to close the window or the frame without closing it through the button on the title bar. In such cases, we can use the . destroy() method to close the window.


4 Answers

You can call pack_forget to remove a widget (if you use pack to add it to the window).

Example:

from tkinter import *

root = Tk()

b = Button(root, text="Delete me", command=lambda: b.pack_forget())
b.pack()

root.mainloop()

If you use pack_forget, you can later show the widget again calling pack again. If you want to permanently delete it, call destroy on the widget (then you won't be able to re-add it).

If you use the grid method, you can use grid_forget or grid_remove to hide the widget.

like image 76
sloth Avatar answered Sep 19 '22 20:09

sloth


One way you can do it, is to get the slaves list from the frame that needs to be cleared and destroy or "hide" them according to your needs. To get a clear frame you can do it like this:

from tkinter import *

root = Tk()

def clear():
    list = root.grid_slaves()
    for l in list:
        l.destroy()

Label(root,text='Hello World!').grid(row=0)
Button(root,text='Clear',command=clear).grid(row=1)

root.mainloop()

You should call grid_slaves(), pack_slaves() or slaves() depending on the method you used to add the widget to the frame.

like image 20
Oziel Carneiro Avatar answered Sep 19 '22 20:09

Oziel Carneiro


You simply use the destroy() method to delete the specified widgets like this:

lbl = tk.Label(....)

btn = tk.Button(....., command=lambda: lbl.destroy())

Using this you can completely destroy the specific widgets.

like image 31
Feezan Khattak Avatar answered Sep 20 '22 20:09

Feezan Khattak


You say that you have a list of widgets to change dynamically. Do you want to reuse and reconfigure existing widgets, or create all new widgets and delete the old ones? It affects the answer.

If you want to reuse the existing widgets, just reconfigure them. Or, if you just want to hide some of them temporarily, use the corresponding "forget" method to hide them. If you mapped them with pack() calls, you would hide with pack_forget() (or just forget()) calls. Accordingly, grid_forget() to hide gridded widgets, and place_forget() for placed widgets.

If you do not intend to reuse the widgets, you can destroy them with a straight destroy() call, like widget.destroy(), to free up resources.

like image 34
Gary02127 Avatar answered Sep 20 '22 20:09

Gary02127