tkinter question

Eric Brunel eric_brunel at despammed.com
Tue Mar 28 02:18:01 EST 2006


On 27 Mar 2006 15:15:05 -0800, <linuxnooby at yahoo.com.au> wrote:
> Hi
>
> I have a tkinter question. In the following script the window will not
> display until the script has finished executing. It appears at the same
> time as the output "script finished". How can I make it appear
> immediately,  with the output "script finished" appearing 5 seconds
> later.
>
> cheers Dave
>
> from Tkinter import *
> import time
>
> print "script started"
>
> top = Tk()
> F = Frame(top)
> F.pack()
> Hello = Label(F, text="hello world")
> Hello.pack()

Here, the window is built with its contents, but the application is not  
started until you've done a top.mainloop() (or just mainloop()). This is  
required to be able to treat user requests. Until then, the window may  
actually not show up (depends on the platform, in fact).

> time.sleep(5)

Now you wait for 5 seconds, but the application is still not started.

> print "script finished"
> mainloop()

Now the application starts and begins to treat user requests.

What are you trying to do exactly? If you just want to automatically quit  
the application after 5 seconds, here is a way to do it (untested):

--------------------------------------------
## The script starts exactly as yours do
 from Tkinter import *

print "script started"

top = Tk()
F = Frame(top)
F.pack()
Hello = Label(F, text="hello world")
Hello.pack()

## Now, we'll register an event to call top.quit
## after 5 seconds (= 5000 milliseconds)
top.after(5000, top.quit)

## Now we start the main loop
top.mainloop()

## This should happen after top.quit() is
## executed by the event registered via after
print "script finished"
--------------------------------------------

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"



More information about the Python-list mailing list