Oliver Rhodes
Oliver Rhodes

Reputation: 23

i want to destroy a window and then re open it (tkinter)

I want to open a window from my main window and then be able to destroy and open the original window again from the window I created

Here is my code so far but i just get: tkinter.TclError: can't invoke "wm" command: application has been destroyed

Any help to fix this would be much appreciated :)

Here is my code :

    from tkinter import*

    root = Tk()
    root.title("Using Frames")
    root.geometry("400x600")

    frame = LabelFrame(root, text="pages",
    padx=5,pady=5)
    frame.pack(padx=10,pady=10)
    def create_window():
        window1 = Tk()
        btn = Button(window1,text="destroy main page",command=root.destroy)
        btn.pack()
        btn2 = Button(window1,text="open main page",command=root.deiconify)
        btn2.pack()
        window1.mainloop()

    b1 = Button(frame,text="create window 2",command=create_window)
    b1.pack()
    root.mainloop()

Upvotes: 0

Views: 3352

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

After you destroy this window,you couldn't open it again unless you create a new window.

You should use .withdraw() and .deiconify() to make it hide or show.

This code maybe solve your problem:

from tkinter import *

root = Tk()
root.title("Using Frames")
root.geometry("400x600")

frame = LabelFrame(root, text="pages",
                   padx=5, pady=5)
frame.pack(padx=10, pady=10)


def create_window():
    window1 = Toplevel()
    btn = Button(window1, text="destroy main page", command=root.withdraw)
    btn.pack()
    btn2 = Button(window1, text="open main page", command=root.deiconify)
    btn2.pack()
    window1.mainloop()


b1 = Button(frame, text="create window 2", command=create_window)
b1.pack()
root.mainloop()

Upvotes: 4

Related Questions