How to delete Tkinter widgets from a window?

TheBeardedBerry picture TheBeardedBerry · Sep 11, 2012 · Viewed 149.1k times · Source

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

How to delete the widgets from the window?

Answer

sloth picture sloth · Sep 11, 2012

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.