Tkinter: Making a window disappear

Fredrik Lundh fredrik at pythonware.com
Mon Oct 9 05:18:37 EDT 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.

your program can (quite obviously) not process any events when it's stuck
inside time.sleep().  adding a call to self.update() before you quit the event
loop should do the trick:

        def ok(self):
            self.destroy()
            self.update() # process all queued events
            self.quit()

</F> 






More information about the Python-list mailing list