tkinter question

L

linuxnooby

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()
 
E

Eric Brunel

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"
 
L

linuxnooby

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
 
E

Eric Brunel

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

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
 
L

linuxnooby

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()
 
E

Eric Brunel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top