Denio
Denio

Reputation: 15

How to close tkinter window without a button and not closing Python completely?

I am aware that there are commands such as .destroy() , .exit() etc. However, when taking these out of the 'command' from the button parameter of what action to take when pressed, they don't work.

My scenario is when a user logins successfully, the Tkinter window including its widgets should close whilst short after a GUI in pygame opens. I just don't want the Tkinter window to be there once I don't need it ever again whilst also not exiting Python. I don't want a button as I want the process to be automatic.

The thing that baffles me is why when taking out this command to be on its own, it does not work:

Button(root, text="Quit", command=root.destroy).pack() #works
root.destroy() #don't works

Upvotes: 1

Views: 3257

Answers (1)

pixelistik
pixelistik

Reputation: 7820

Without seeing more of the source code, I guess the problem is based on where you call root.destroy()

If it comes after the blocking tk.mainloop(), it will never be reached. Please read Tkinter understanding mainloop regarding this issue.

A possible solution based on the above:

while True:
    tk.update_idletasks()
    tk.update()
    if login_successful: # or whatever your login check looks like
        root.destroy()

You replace the mainloop with your custom loop, which includes a check for a successful login.

Upvotes: 1

Related Questions