Как мне вернуться в предыдущее окно, которое я "уничтожил" в TkInter? - PullRequest
0 голосов
/ 11 июля 2020

В моем коде пока есть два windows. Первый - тот, который открывается при запуске кода. В первом окне есть кнопка, которая открывает новое окно и уничтожает первое окно. Как мне вернуться к этому первому разрушенному окну? Я попытался включить первую страницу в функцию, как и вторая страница, но это не сработало (вероятно, из-за порядка, в котором они помещены в код, они не могут видеть друг друга).

Вот мой код на данный момент , Я удалил функцию, в которой я поместил первую страницу:

(кнопка входа в функцию и кнопка регистрации в основном коде - это те, которые я хотел бы использовать для навигации)

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

Ответы [ 2 ]

2 голосов
/ 12 июля 2020

Невозможно вернуть окно Tk после того, как оно было уничтожено; это то же самое, что использовать del для переменной.

1 голос
/ 11 июля 2020

Хорошо, есть способ сделать это.

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

Когда вы нажмете эту кнопку, откроется окно window1. Вот весь код:

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

Надеюсь, это помогло

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...