Python3 Tkinter method Stroy

Asked

Viewed 904 times

1

good night,

I was working on a project that involved a graphical interface and decided to use Tkinter to produce it

I read about a method called Destroy() but could not find a way to use it the goal was to destroy the frame that contains the word 'hello' (this is to test and get an idea) minha interface tkinter

I don’t know if anyone knows how to use this method or if there are more effective methods than this,

thank you and good night

1 answer

0


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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.