在python 3中按下X窗口按钮时如何返回上一个窗口

时间:2019-07-19 14:58:09

标签: python python-3.x tkinter window

我对python还是很陌生,目前正在学习如何创建登录系统。这样做时,遇到一个问题,当按下当前窗口的x按钮时,我需要前一个窗口打开才能备份。

我曾尝试编辑它,但是当我通过将其写为'on_close():'在其他函数上调用它时,它给了我错误:"NameError: name 'on_close' is not defined"


    def on_close():

         #custom close options, here's one example:

         close = messagebox.askokcancel("Close", "Would you like to close the program?")
         if close:
              root.destroy()

    root.protocol("WM_DELETE_WINDOW",  on_close)

from tkinter import*
from tkinter import messagebox

w = 300
h = 2

def register_user():
    global username_info
    global password_info

    username_info = username.get()
    password_info = password.get()

    file = open(username_info+".txt", "w")
    file.write(username_info)
    file.write(password_info)
    file.close()

    username_entry.delete(0, END)
    password_entry.delete(0, END)

    Label(text = "Registration Success")

def register():
    global screen1
    screen.withdraw()
    screen1 = Toplevel(screen)
    screen1.title("Registration")
    screen1.geometry("300x250")

    global username
    global password
    global username_entry
    global password_entry
    username = StringVar()
    password = StringVar()

    Label(screen1, text = " Please Enter Your Details Below*").pack()
    Label(screen1, text = "").pack()
    Label(screen1, text = "Username *").pack()
    username_entry = Entry(screen1, textvariable = username)
    username_entry.pack()
    Label(screen1, text = "Password *").pack()
    password_entry = Entry(screen1, textvariable = password)
    password_entry.pack()
    Label(screen1, text = "").pack()
    Button(screen1, text= "Register", height="1", width="10", command = register_user).pack()

def login():
    global screen2

    screen.withdraw()
    screen2 = Toplevel(screen)
    screen2.title("HOT or SUPER HOT")
    screen2.geometry("500x400")
    on_close():

def main_Screen():

    global screen

    screen = Tk()
    screen.geometry("600x300")
    screen.title("Login System")

    Label(text = "Login System" , bg = "black", width = w , height = h, font = ("Calibri", 20) , fg = "white").pack()
    Label(text = "").pack()
    Button(text = "Login", height = h, width = "30", command = login).place(x=50 , y=200)
    Label(text = "").pack()
    Button(text = "Register" ,height = h, width = "30", command = register).place(x=320 , y=200)

    screen.mainloop()

main_Screen()

print("Hello World")




def on_close():
    screen2.withdraw()
    screen.update()
    screen.deiconify


screen2.protocol("WM_DELETE_WINDOW", on_close)

解决了,谢谢您的帮助 解决问题的方法是,我将'screen2.protocol("WM_DELETE_WINDOW", on_close)添加到def login()

1 个答案:

答案 0 :(得分:0)

您可以尝试通过预先定义on_close()

from tkinter import*

w = 300
h = 2


def on_close():
    screen2.withdraw()
    screen.update()
    screen.deiconify()


def register_user():
    global username_info
    global password_info

    username_info = username.get()
    password_info = password.get()

    file = open(username_info+".txt", "w")
    file.write(username_info)
    file.write(password_info)
    file.close()

    username_entry.delete(0, END)
    password_entry.delete(0, END)

    Label(text = "Registration Success")


def register():
    global screen1
    screen.withdraw()
    screen1 = Toplevel(screen)
    screen1.title("Registration")
    screen1.geometry("300x250")

    global username
    global password
    global username_entry
    global password_entry
    username = StringVar()
    password = StringVar()

    Label(screen1, text = " Please Enter Your Details Below*").pack()
    Label(screen1, text = "").pack()
    Label(screen1, text = "Username *").pack()
    username_entry = Entry(screen1, textvariable = username)
    username_entry.pack()
    Label(screen1, text = "Password *").pack()
    password_entry = Entry(screen1, textvariable = password)
    password_entry.pack()
    Label(screen1, text = "").pack()
    Button(screen1, text= "Register", height="1", width="10", command = register_user).pack()


def login():
    global screen2

    screen.withdraw()
    screen2 = Toplevel(screen)
    screen2.title("NO IDEA")
    screen2.geometry("500x400")
    on_close()


def main_Screen():

    global screen

    screen = Tk()
    screen.geometry("600x300")
    screen.title("Login System")

    Label(text = "Login System" , bg = "black", width = w , height = h, font = ("Calibri", 20) , fg = "white").pack()
    Label(text = "").pack()
    Button(text = "Login", height = h, width = "30", command = login).place(x=50 , y=200)
    Label(text = "").pack()
    Button(text = "Register" ,height = h, width = "30", command = register).place(x=320 , y=200)

    screen.mainloop()


main_Screen()

print("Hello World")

screen2.protocol("WM_DELETE_WINDOW", on_close)

不提示错误可能会有所帮助