How to check if the Tkinter window has been closed Python?

Asked

Viewed 120 times

-1

I’m having a problem with the code, my code is bigger than that, this part is just the login and password part, my problem is that the code works if the window is closed by X, has how to check if the Tkinter window was closed by X and close the entire program if this was done?

#Criar janela de login
jan = Tk()
jan.title('Login System')
jan.geometry("250x150")
jan.resizable(width=False, height=False)

#Definir funções
def Logar():
    User = en1.get()
    Pass2 = en2.get()

    DataBase.cur.execute("""
    SELECT * FROM LoginUsers
    WHERE (User = ? and Password = ?)
    """, (User, Pass2))
    VerifyLogin = DataBase.cur.fetchone()
    try:
        if User in VerifyLogin and Pass2 in VerifyLogin:
            messagebox.showinfo(title='Login Info', message='Acesso Confirmado. Bem Vindo')
            jan.destroy()

    except:
        messagebox.showerror(title='Login Info', message='Acesso Negado. Verifique se você esta cadastrado no sistema.')

def RegisterData():
    Name = en1.get()
    Pass = en2.get()

    if Name == "" and Pass == "":
        messagebox.showerror(title="Register Error", message="Preencha todos os camos")
    else:
        DataBase.cur.execute("""
        INSERT INTO LoginUsers(User, Password) VALUES(?, ?)
        """, (Name, Pass))
        DataBase.conn.commit()
        messagebox.showinfo(title="Register Info", message="Registrado Com Sucesso")

#Criar Widgets
text1 = Label(jan, text='Login: ')
text2 = Label(jan, text='Passworld: ')
en1 = Entry(jan)
en2 = Entry(jan, show='*')
bt1 = Button(jan, text='Confirm', command=Logar)
bt2 = Button(jan, text='Register', command=RegisterData)

#Posicionar Widgets
text1.grid(row=0, column=0)
text2.grid(row=1, column=0)
en1.grid(row=0, column=1)
en2.grid(row=1, column=1)
bt1.grid(row=2, column=1)
bt2.grid(row=3, column=1)

#Deixar a janela em loop
jan.mainloop()

1 answer

0

And so I don’t know what are the other parts of the code but theoretically if it is closed by X the operating system for all related processes so it can be said that yes

PS: I understood that you were referring to the red X of the upper left corner and that’s what I was basing on

  • Yeah, the X is that red

  • https://github.com/VitorPL07/wizard These are the files.

  • I believe so, but the q and q was going wrong with the code?

  • If the user closes the login screen by X the program continues, and this is not supposed to happen. I want q if in case the user closes the window by X, the program exits

  • how and know that the program does not come out completely what and q gives you that indication

  • Because I tested, the program is I who am doing

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.