Tkinter: Making a window disappear

  • Thread starter Claus Tondering
  • Start date
C

Claus Tondering

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?
 
F

Fredrik Lundh

Claus said:
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>
 
J

James Stroud

Claus said:
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?

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/
 
E

Eric Brunel

I just solved the problem myself:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top