How do I remove all elements in a tkinter GUI?

Viewed 336

I am currectly building a Tkinter GUI using Python, and I want to see how I can remove all elements, creating a blank clean slate. I tried .destroy(), but that completely exits out the window. How do I do this?

Example code:

import tkinter as tk

root = tk.Tk()
mylabel = tk.Label(root, text='I want this to be removed')
mylabel.pack()
#I want to remove the mylabel Label.
1 Answers

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.

Related