I need to delete a widget, example:
button1 = Button(root, text="start", command=self.cc).pack()
How do I make another widget which has the command to delete button1? or even just a function which when called, deletes button1.
I couldnt find the answer any where :/
there is another similar question (How to delete Tkinter widgets from a window?) BUT its 8 years old so the solutions may be outdated
Every widget has a function called destroy() and you can call it from another button-command, like this:
import tkinter as tk
root = tk.Tk()
button = tk.Button(root,text="Btn1")
button.grid(row=0,column=0)
button2 = tk.Button(root,text="Delete",command=button.destroy)
button2.grid(row=1,column=0)
root.mainloop()
If you want to destroy all widgets inside a frame or "root", you can use a function called winfo_children which selects all children widgets and then with a loop destroy each of them:
def destroyall():
for widget in root.winfo_children():
widget.destroy()
button3 = tk.Button(root,text="Delete All",command=destroyall)
button3.grid(row=2,column=0)
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