python - 如何返回到我在 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()
解决方案
Tk 窗口被破坏后无法恢复;del
它与在变量上使用相同。
推荐阅读
- php - scandir/opendir 不能与 ssh2.sftp 一起使用?
- php - 在 OceanWP 主题的 WooCommerce 中删除主商店的简短产品描述
- powershell - 文件夹名称 - 用空格替换句号
- c++ - c ++在期望out_of_range时没有抛出异常
- c - read() 在有效的文件描述符上返回错误的文件描述符
- angular - 无法在 Ionic 应用程序中设置和删除存储的参数
- javascript - JS - 检测设备是否在某个轴(X、Y、Z)上旋转超过阈值度数
- python - Json上循环的列表索引超出范围错误
- python - 由于方法中的“默认参数值是可变的”,Python/Django 随机数据库查询结果
- linux - 如何在我的 Linux 系统上安全地重新配置 gcc?