Ok, I would like to put together a Python/Tkinter dialog box that displays a simple message and self-destructs after N seconds. Is there a simple way to do this?
You can use the after
function to call a function after a delay elapsed and the destroy
to close the window.
Here is an example
from Tkinter import Label, Tk
root = Tk()
prompt = 'hello'
label1 = Label(root, text=prompt, width=len(prompt))
label1.pack()
def close_after_2s():
root.destroy()
root.after(2000, close_after_2s)
root.mainloop()
Update: The after docstring says:
Call function once after given time. MS specifies the time in milliseconds. FUNC gives the function which shall be called. Additional parameters are given as parameters to the function call. Return identifier to cancel scheduling with after_cancel.
you could also use a thread.
this example uses a Timer to call destroy() after a specified amount of time.
import threading
import Tkinter
root = Tkinter.Tk()
Tkinter.Frame(root, width=250, height=100).pack()
Tkinter.Label(root, text='Hello').place(x=10, y=10)
threading.Timer(3.0, root.destroy).start()
root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With