首页 > 解决方案 > 如何返回到我在 TkInter 中“销毁”的上一个窗口?

问题描述

到目前为止,在我的代码中有两个窗口。第一个是运行代码时打开的。在第一个窗口中有一个按钮,可以打开一个新窗口并销毁第一个窗口。我怎样才能回到被破坏的第一个窗口?我尝试将第一页放在第二页的功能中,但它不起作用(可能是因为它们在代码中的放置顺序,它们彼此看不到)。

到目前为止,这是我的代码,我删除了放入第一页的函数:

(函数中的登录按钮和主代码中的注册按钮是我想用于导航的按钮)

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

标签: pythontkinter

解决方案


Tk 窗口被破坏后无法恢复;del它与在变量上使用相同。


推荐阅读