By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Tkinter: Making a window disappear

P: n/a
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

Oct 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I just solved the problem myself:

I wrote:
self.destroy()
Writing "self.master.destroy()" instead does the trick.

Sorry for the inconvenience.

--
Claus Tondering

Oct 9 '06 #2

P: n/a
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>

Oct 9 '06 #3

P: n/a
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/
Oct 9 '06 #4

P: n/a
On Mon, 09 Oct 2006 11:08:39 +0200, Claus Tondering
<cl*************@gmail.comwrote:
I just solved the problem myself:

I wrote:
> self.destroy()

Writing "self.master.destroy()" instead does the trick.
As an alternative (which is better IMHO), you may consider specializing
Toplevel instead of Frame for your dialog:

class MyDialog(Toplevel):
...

In tk/Tkinter, a Frame is a generic container for widgets; it is not what
is usually called a window. Creating an instance of Frame when there is no
window to contain it happens to create a new one, but it is a side-effect,
and you should not rely on it.

Once you've done that, you can simply write:

self.destroy()

to delete the window.
Sorry for the inconvenience.
No problem.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Oct 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.