472,342 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Tkinter: Making a window disappear

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
4 5959
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why...
4
by: Patrick L. Nolan | last post by:
Our Tkinter application has a big ScrolledText widget which is a log of everything that happens. In order to prevent people from making changes,...
2
by: Russell E. Owen | last post by:
I want to support execution of simple user-written scripts in a Tkinter application. The scripts should be able to wait for data and such without...
2
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you...
4
by: greenflame | last post by:
I have a script that will make a window that shows the text I want using Tkinter. What I need to do is to make another window popup above the...
2
by: Bellum | last post by:
I'm rather new to both Tkinter and Classes, so whatever I'm doing wrong probably seems really stupid. Just bare with me, here. The reference to the...
4
by: Miki | last post by:
Hello, I have a simple Tkinter window with and buttons at the bottom. When I resize the window to be shorter, the first thing to disappear...
2
by: Pedro | last post by:
Hi I'm trying to build a small application that can display some images in a toplevel window. I have this code: def Results(master): from...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.