Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to close the tkinter window

I want to close the current window 2 seconds after it appears

if you insert time.sleep(2) before win.mainloop(), the window will not be displayed for 2 seconds, and I need it to open and close after 2 seconds

    global win
    win = Tk()

    win.attributes('-alpha', 0.0)
    win.iconify()

    window = Toplevel(win)
    window.geometry("300x300+" + str(randint(0, 1400)) + "+" + str(randint(0, 700)))
    window.overrideredirect(1)

    photo = PhotoImage(file="classes/bin/starters/spaceStorage/popUps/" + rand + r".png")

    label = Label(window, image=photo)
    label.pack()

    win.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use .after() instead of time.sleep():

...
# destroy root window after 2 seconds
win.after(2000, win.destroy)
win.mainloop()

Why don’t you just use the root window instead of hiding it and create another toplevel?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading