How to Clear All Widgets from a Tkinter Window in One Go Without Referencing Them All Directly

(Python) Is there a way to clear all entry boxes in a Tkinter UI in one line?

This should be sufficient:

[widget.delete(0, tk.END) for widget in root.winfo_children() if isinstance(widget, tk.Entry)]

It uses list comprehension to loop through all the children of root and does .delete() if the child is an Entry

Implementation:

import tkinter as tk


root = tk.Tk()
root.geometry("300x300")

def create_entries(root, amount):
for _ in range(amount):
tk.Entry(root).pack(pady=2)


def clear():
[widget.delete(0, tk.END) for widget in root.winfo_children() if isinstance(widget, tk.Entry)]


create_entries(root, 5)

clear_btn = tk.Button(root, text="clear", padx=10, command=clear)
clear_btn.pack()

root.mainloop()

NOTE:

I should point out that this is probably not best practice but I dont really know.

It creates a list but doesnt assign it to anything so essentially it just does what you intended

EDIT:

Also consider the following if you want to clear every entry, even if it is inside a different widget. This is to be used only if you are sure that every single entry in the whole application should be cleared

def clear(root):
for widget in root.winfo_children():
if not isinstance(widget, tk.Entry):
clear(widget)
elif isinstance(widget, tk.Entry):
widget.delete(0, tk.END)

Also note that, I used a normal for loop for this, because as noted in the comments, it is better

How do I remove all elements in a tkinter GUI?

You can either remove or destroy all children of the root iteratively (eg: for child in root.winfo_children(): child.destroy()), or you can create a single frame directly in the root and make all other widgets children of that frame, and then just remove or delete the frame.

How to delete all children elements?

You can use winfo_children to get a list of all children of a particular widget, which you can then iterate over:

for child in infoFrame.winfo_children():
child.destroy()

How delete all children except 1 specific widget?

You can just see if the variable you've used to save the widget is equal to the child, ie.

for child in frame.winfo_children():
# Here's where I need help. Don't know how to single-out 'keep_me'
if child != keep_me:
child.destroy()

How to clear the Entry widget after a button is pressed in Tkinter?

After poking around a bit through the Introduction to Tkinter, I came up with the code below, which doesn't do anything except display a text field and clear it when the "Clear text" button is pushed:

import tkinter as tk

class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, height=42, width=42)
self.entry = tk.Entry(self)
self.entry.focus()
self.entry.pack()
self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)
self.clear_button.pack()

def clear_text(self):
self.entry.delete(0, 'end')

def main():
root = tk.Tk()
App(root).pack(expand=True, fill='both')
root.mainloop()

if __name__ == "__main__":
main()


Related Topics



Leave a reply



Submit