2

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
1
  • After successful login in the same function call root.destroy() Commented Sep 9, 2018 at 16:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was missing. I left the mainloop() which like you said didn't allow me to reach to .destroy() . Thank you smart sir! (quick response too I must say)
0
import tkinter as tk

root = tk.Tk()

root.title("Auto Close")

root.geometry("300x100")

tk.Label(root, text="This window will close in 5 seconds.").pack(pady=20)

# The window closes after 5000 milliseconds (5 seconds)

root.after(5000, lambda: root.destroy())

root

.mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.