By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

tkinter question

P: n/a
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()

time.sleep(5)
print "script finished"

mainloop()

Mar 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 27 Mar 2006 15:15:05 -0800, <li********@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+-'])"
Mar 28 '06 #2

P: n/a
Ultimately what I am trying to is create a public computer session
manager.

1) User logs in and main application window is withdrawn (easy)
2) After (for example) 55 minutes - a warning window/dialoguebox
"session will end in 5 minutes"
3) With 30 seconds to go - a warning window/dialoguebox "session will
end in 30 seconds"
4) User logged out (easy)

I am having problems with steps 2 and 3
Either the window/dialoguebox will
(a) pause the program execution upon display waiting for user input
or (b) both warnings display together when application has finished.

What I want want is a window/dialogue box that will not halt
application logic, and will display immediately.

cheers David

Mar 29 '06 #3

P: n/a
(Please quote a part of the message you're replying to; I had problems
figuring out you were replying to my post...)

On 29 Mar 2006 14:08:02 -0800, <li********@yahoo.com.au> wrote:
Ultimately what I am trying to is create a public computer session
manager.

1) User logs in and main application window is withdrawn (easy)
2) After (for example) 55 minutes - a warning window/dialoguebox
"session will end in 5 minutes"
3) With 30 seconds to go - a warning window/dialoguebox "session will
end in 30 seconds"
4) User logged out (easy)

I am having problems with steps 2 and 3
Either the window/dialoguebox will
(a) pause the program execution upon display waiting for user input
or (b) both warnings display together when application has finished.

What I want want is a window/dialogue box that will not halt
application logic, and will display immediately.


But what does it do between steps 1 and 2? Is Python/Tkinter still in
control, or are you running some external program, or something else?

As for the dialogue box pausing the program execution waiting for user
input, you're probably using the "built-in" show* or ask* functions from
the tkMessageBox module that are designed to be modal. If you just create
a Toplevel and populate it with widgets, it will just display once the
control is returned to the Tkinter mainloop and no user input will be
needed. But the control *has* to return to the mainloop, or the window
won't show up. One acceptable workaround may be to call the "update"
method on the Toplevel, but it may depend on what you do between steps 1 &
2.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 30 '06 #4

P: n/a
>If you just create
a Toplevel and populate it with widgets, it will just display once the
control is returned to the Tkinter mainloop and no user input will be
needed.


Thanks for explaining that. I borrowed the "after" method that you used
in your first reply. It seems to do what I want (except for
root.destroy) Though any suggestions on a more elegant solution
welcome.

cheers David
from Tkinter import *

def message1():
top1 = Tk()
F1 = Frame(top1)
F1.pack()
Hello1 = Label(F1, text="step 2 you will be logged off in 5
seconds")
Hello1.pack()
top1.after(3000, message2)

def message2():
top2 = Tk()
F2 = Frame(top2)
F2.pack()
Hello2 = Label(F2, text="step 3 you will be logged off in 2
seconds")
Hello2.pack()
top2.after(2000, goodbye)

def goodbye():
print "step 4 you are logged out"
root.destroy

root = Tk()
root.withdraw()
print "step 1 login window withdrawn"

message1()

root.mainloop()

Mar 30 '06 #5

P: n/a
On 30 Mar 2006 15:11:19 -0800, <li********@yahoo.com.au> wrote:
If you just create
a Toplevel and populate it with widgets, it will just display once the
control is returned to the Tkinter mainloop and no user input will be
needed.
Thanks for explaining that. I borrowed the "after" method that you used
in your first reply. It seems to do what I want (except for
root.destroy) Though any suggestions on a more elegant solution
welcome.

cheers David

[snip start of code] def goodbye():
print "step 4 you are logged out"
root.destroy


This is not a method call. To do what you want, write:
root.destroy()
Methods are first class objects in Python, so root.destroy is just the
method object for the destroy method applied to root. Written like that,
it just gets the object, but does not call it.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Mar 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.