Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch a Python/Tkinter dialog box that self-destructs?

Tags:

python

tkinter

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?

like image 247
Stephen Gross Avatar asked Dec 16 '09 19:12

Stephen Gross


2 Answers

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.

like image 71
luc Avatar answered Oct 16 '22 14:10

luc


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()
like image 37
Corey Goldberg Avatar answered Oct 16 '22 14:10

Corey Goldberg