As the name says, the method destroy
serves to destroy the created widget. If you use the method in a Label
, only this object will be destroyed. If you use the method in a Frame
containing two labels
, it will be destroyed along with the labels
.
This happens because the method destroy
, destroys the widget along with other widgets within it ( child widgets ). It is important to say that once called the method destroy
, the widget never more can be used.
There is another method called forget
. By far, this method is similar to the destroy
, but it is completely different. The method forget
will simply hide the widget, but it will not be destroyed. This is good because you can "delete" the widget temporarily and then re-use it.
See some examples below:
USING THE DESTROY METHOD:
from tkinter import Tk,Button
def destroy():
window.destroy()
print("FINALIZADO!")
window = Tk()
Button(window,text="Destroy",command=destroy,bg="white").pack()
window.mainloop()
USING THE FORGET METHOD:
from tkinter import Tk,Button,Label
def hide_or_show():
global show
if show:
show = False
text.forget()
button["text"] = "Show"
else:
show = True
text.pack()
button["text"] = "Hide"
show = True
window = Tk()
button = Button(window,text="Hide",command=hide_or_show,bg="white")
button.pack()
text = Label(window,text="Hello World",fg="red")
text.pack()
window.mainloop()