Close Tkinter window after few seconds

Asked

Viewed 132 times

-2

Whoa, guys, I’m having a very simple doubt.

I need to close the Tkinter window after a few seconds automatically, someone can help me?

Follows the code:

from tkinter import *
from PIL import ImageTk,Image
import os

root = Tk()
root.iconbitmap('1.jpeg')

root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.focus_set()  # <-- move focus to this widget
root.bind("<Escape>", lambda e: e.widget.quit())
my_img = ImageTk.PhotoImage(Image.open("1.jpeg"))
my_label = Label(image=my_img)
my_label.pack()
root.mainloop()
exit()

1 answer

0

Add that line to your code: root.after(ms,root.destroy)

Example:

from tkinter import *
from PIL import ImageTk,Image
import os

root = Tk()

root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.focus_set()  # <-- move focus to this widget
root.bind("<Escape>", lambda e: e.widget.quit())
my_img = ImageTk.PhotoImage(Image.open("1.jpeg"))
my_label = Label(image=my_img)
my_label.pack()
root.after(1000,root.destroy) # destroy after 1 second

The method after receives a time in milliseconds and a function that will be executed after the set time passes.

Browser other questions tagged

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