alvindraper
alvindraper

Reputation: 65

How do I return to a previous window I "destroyed" in TkInter?

In my code there are two windows so far. The first one is the one that opens when the code is ran. In the first window there is a button that opens a new window and destroys the first window. How can I return to this first window that was destroyed? I tried putting the first page in function like the second page but it did not work (probably because of the order in which they are put in the code they cannot see each other).

Here is my code so far, I removed the function where I put the first page in:

(the log in button in the function and the register button in the main code are the ones I would like to use for navigation)

from tkinter import *

def register():
    RP = Tk()
    RP.title("Registration Page")
    RP.geometry('1000x850')  # creates dimensions of page
    RP.resizable(0, 0)  # will disable max/min tab of window

    # seperating the page into sections
    header = LabelFrame(RP, bg="#12a8e3")
    content = LabelFrame(RP, bg="white")

    RP.columnconfigure(0, weight=1)  # 100%
    RP.rowconfigure(0, weight=2)  # 2%
    RP.rowconfigure(1, weight=98)  # 98%

    # creating the title of the page to be displayed in the header
    title = Label(header, text="School Subjects Quiz", bg="#12a8e3", fg="white", font=("Ariel", 65, "bold"), padx=10,
                  pady=10)
    title.pack(expand=TRUE)

    # username input box
    usernameLabel = Label(content, width=60, borderwidth=5, font=("Ariel", 22), text="Enter a new username", bg="white",
                          anchor="w", padx=100)
    usernameLabel.pack(ipady=8, pady=(35, 0))
    usernameBox = Entry(content, width=60, borderwidth=5, font=("HelvLight", 18))
    usernameBox.pack(ipady=10)

    # password input box
    passwordLabel = Label(content, width=60, borderwidth=5, font=("Ariel", 22), text="Enter a new password", bg="white",
                          anchor="w", padx=100)
    passwordLabel.pack(ipady=8, pady=(35, 0))
    passwordLabel = Entry(content, width=60, borderwidth=5, font=("HelvLight", 18))
    passwordLabel.pack(ipady=10)

    # password confirm input box
    passwordLabel = Label(content, width=60, borderwidth=5, font=("Ariel", 22), text="Re-enter your password",
                          bg="white", anchor="w", padx=100)
    passwordLabel.pack(ipady=8, pady=(35, 0))
    passwordLabel = Entry(content, width=60, borderwidth=5, font=("HelvLight", 18))
    passwordLabel.pack(ipady=10)

    # submit buttons for inputs
    submitButton = Button(content, width=20, borderwidth=5, font=("Ariel", 22), text="Submit", bg="#b5b5b5", fg="black",
                          activebackground="#b5b5b5")
    submitButton.pack(ipady=5, pady=(15, 0))

    # button to return to log in page
    loginButton = Button(content, width=40, borderwidth=5, font=("Ariel", 24),
                         text="Already have an account? Click here to log in", bg="#12a8e3", fg="white",
                         activebackground="#12a8e3") #this is the button i would like to be able to return to the first page with
    loginButton.pack(ipady=20, pady=(35, 0))

    header.grid(row=0, sticky='news')
    content.grid(row=1, sticky='news')


#Creating the log in page
root = Tk()
root.title("Log-in page")
root.geometry('1000x850')   #creates dimensions of page
root.resizable(0,0)      #will disable max/min tab of window

#seperating the page into sections
header = LabelFrame(root, bg="#12a8e3")
content = LabelFrame(root, bg="white")

root.columnconfigure(0, weight=1) # 100%
root.rowconfigure(0, weight=2) # 2%
root.rowconfigure(1, weight=98) # 98%

#creating the title of the page to be displayed in the header
title = Label(header, text="School Subjects Quiz", bg="#12a8e3", fg="white", font=("Ariel",65, "bold"), padx=10, pady=10)
title.pack(expand=TRUE)

#username input box
usernameLabel = Label(content, width = 60, borderwidth=5, font=("Ariel", 22), text="Enter Username", bg="white", anchor="w", padx=100)
usernameLabel.pack(ipady = 8,  pady=(55,0))
usernameBox = Entry(content, width = 60, borderwidth=5, font=("HelvLight", 18))
usernameBox.pack(ipady = 10)

#password input box
passwordLabel = Label(content, width = 60, borderwidth=5, font=("Ariel", 22), text="Enter Password", bg="white", anchor="w", padx=100)
passwordLabel.pack(ipady = 8, pady=(55,0))
passwordLabel = Entry(content, width = 60, borderwidth=5, font=("HelvLight", 18))
passwordLabel.pack(ipady = 10)

#submit buttons for inputs
submitButton = Button(content, width = 20, borderwidth=5, font=("Ariel", 22), text="Submit", bg="#b5b5b5", fg="black", activebackground="#b5b5b5")
submitButton.pack(ipady = 5, pady=(15,0))

#button to click on to make an account
registerButton = Button(content, width = 40, borderwidth=5, font=("Ariel", 24), text="New? Click here to register", bg="#12a8e3", fg="white", activebackground="#12a8e3", command=lambda:[register(), root.destroy()])
#this button above opens the registration page and destroys the log in page
registerButton.pack(ipady = 20, pady=(100,0))

header.grid(row=0, sticky='news')
content.grid(row=1, sticky='news')

root.mainloop()

Upvotes: 0

Views: 2016

Answers (2)

Maimas2
Maimas2

Reputation: 961

There is no way to bring back a Tk window after it has been destroyed; it is the same as using del on a variable.

Upvotes: 2

user13645394
user13645394

Reputation:

Ok so there is a way of doing this.

btn = Button(window1, text="destroy main page", command=root.withdraw)

When you click that button it will open window1 back up. Here is the entire code:

from tkinter import *
root = Tk()


#here is the function to open and close the window
def create_window():
    window1 = Toplevel()
    #destroy
    btn = Button(window1, text="destroy main page", command=root.withdraw)
    btn.pack()
    #open
    btn2 = Button(window1, text="open main page", command=root.deiconify)
    btn2.pack()
    window1.mainloop()


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

Hoped this helped

Upvotes: 1

Related Questions