Tkinter: Making a window disappear

James Stroud jstroud at mbi.ucla.edu
Mon Oct 9 23:19:02 CEST 2006


Claus Tondering wrote:
> I am trying to make a Tkinter main window appear and disappear, but I
> have problems with that.
> 
> Here is a small code sample:
> 
>     class MyDialog(Frame):
>         def __init__(self):
>             Frame.__init__(self, None)
>             Label(self, text="Hello").pack()
>             Button(self, text="OK", command=self.ok).pack()
>             self.grid()
> 
>         def ok(self):
>             self.destroy()
>             self.quit()
> 
>     MyDialog().mainloop()
>     print "Now waiting 5 seconds"
>     time.sleep(5)
>     MyDialog().mainloop()
> 
> The first mainloop() shows the dialog nicely, and I press the "OK"
> button. The system then sleeps for 5 seconds, and a new dialog is
> displayed. This is all very nice.
> 
> But during the 5 seconds of sleeping, remnants of the old window are
> still visible. It is not redrawn, but it just lies there as an ugly box
> on the display. I thought that destroy()/quit() would completely remove
> the window, but obviously I am wrong.
> 
> What should I do instead?
> 
> --
> Claus Tondering
> 

Maybe think about using the Toplevel.withdraw() method. This way you 
don't have to re-instantiate your window every time. This is the 
technique used by the PMW library. Use deiconify() to get it back.

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list