473,573 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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").p ack()
Button(self, text="OK", command=self.ok ).pack()
self.grid()

def ok(self):
self.destroy()
self.quit()

MyDialog().main loop()
print "Now waiting 5 seconds"
time.sleep(5)
MyDialog().main loop()

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 6311
I just solved the problem myself:

I wrote:
self.destroy()
Writing "self.master.de stroy()" 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").p ack()
Button(self, text="OK", command=self.ok ).pack()
self.grid()

def ok(self):
self.destroy()
self.quit()

MyDialog().main loop()
print "Now waiting 5 seconds"
time.sleep(5)
MyDialog().main loop()

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").p ack()
Button(self, text="OK", command=self.ok ).pack()
self.grid()

def ok(self):
self.destroy()
self.quit()

MyDialog().main loop()
print "Now waiting 5 seconds"
time.sleep(5)
MyDialog().main loop()

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.withdr aw() 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.comwrot e:
I just solved the problem myself:

I wrote:
> self.destroy()

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

class MyDialog(Toplev el):
...

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.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"
Oct 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6323
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 image in the central area of a simple menu-bar app as shown below in the first code sample. But, when I set up my app with a class, as shown below in...
4
5281
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 program wants to write a new entry. This works OK, except that sometimes we want to copy out of piece of the contents and paste it in another window. ...
2
3859
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 script as a bunch of asynchronously called subroutines). I decided to use Tkinter's wait_variable. I built a "script runner" object that has...
2
4376
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 thread for recording the frame rates (using a progress bar for visulization). The whole application worked very well once it received image data...
0
3571
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 accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the...
4
2190
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: ----------- from Tkinter imprt * root = Tk() L = Label(root, text="Blah")
2
1677
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__ method, so when I try to call them and change them in another method, I get an attribute error. def __init__(self): self.window = Tk() ...
4
4785
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 visible at all times. Is there a way to make sure that these buttons are always visible?
2
5512
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 import ImageTk figures =
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7796
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1310
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.