How to create a self-destructing Tkinter dialog box?

John Posner jjposner at optimum.net
Thu Dec 17 21:28:37 CET 2009


On Thu, 17 Dec 2009 02:09:03 -0500, Martin P. Hellwig  
<martin.hellwig at dcuktec.org> wrote:

> mrstevegross wrote:
>> 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?
>>  Thanks,
>>  --Steve
>
> Just, thinking aloud, I probably would do something like registering the  
> [place|grid|pack]_forget() function by using the alarm callback  
> 'after()' function of that frame.
>

Yup, after() is your friend:

#---------------------------
 from Tkinter import *
 from functools import partial

def RemoveWindow(win):
     win.destroy()

# root window
root = Tk()
Label(root, text="this is the main window").pack()

# another top-level window, to be removed in 2 seconds
top = Toplevel()
Label(top, text="this is the window to be removed").pack()
root.after(2000, partial(RemoveWindow, top))

# go
root.mainloop()
#---------------------------

HTH,
John



More information about the Python-list mailing list