I have a list of tkinter
widgets that I want to change dynamically.
How to delete the widgets from the window?
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.
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().
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With