Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tkinter question

Reply
Thread Tools

tkinter question

 
 
linuxnooby@yahoo.com.au
Guest
Posts: n/a
 
      03-27-2006
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()

 
Reply With Quote
 
 
 
 
Eric Brunel
Guest
Posts: n/a
 
      03-28-2006
On 27 Mar 2006 15:15:05 -0800, <(E-Mail Removed)> 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+-'])"
 
Reply With Quote
 
 
 
 
linuxnooby@yahoo.com.au
Guest
Posts: n/a
 
      03-29-2006
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

 
Reply With Quote
 
Eric Brunel
Guest
Posts: n/a
 
      03-30-2006
(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, <(E-Mail Removed)> 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+-'])"
 
Reply With Quote
 
linuxnooby@yahoo.com.au
Guest
Posts: n/a
 
      03-30-2006
>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()

 
Reply With Quote
 
Eric Brunel
Guest
Posts: n/a
 
      03-31-2006
On 30 Mar 2006 15:11:19 -0800, <(E-Mail Removed)> 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+-'])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the differences between tkinter in windows and Tkinter inthe other platform? Hidekazu IWAKI Python 0 12-15-2009 05:58 AM
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
from Tkinter import *,win = Tk() "from Tkinter import *" Pierre Dagenais Python 0 08-03-2008 10:33 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments