473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 6237
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 is this so? I notice that I can display a GIF...
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, we have it in DISABLED mode except when the...
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 hanging the GUI (and without having to write the...
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 toplevel window) for displaying images and another...
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 shrink/resize the window the scrollbars adjust...
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 current window showing other text. I tryed: ...
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 buttons seems to disappear after the __init__...
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 are the buttons, however I want these button to be...
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 Tkinter import Toplevel, Button, Label from PIL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.