tkinter question

Discussion in 'Python' started by linuxnooby@yahoo.com.au, Mar 28, 2006.

  1. Guest

    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 28, 2006
    #1
    1. Advertising

  2. Eric Brunel Guest

    On 27 Mar 2006 15:15:05 -0800, <> 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+-'])"
     
    Eric Brunel, Mar 28, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
  4. Eric Brunel Guest

    (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, <> 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+-'])"
     
    Eric Brunel, Mar 30, 2006
    #4
  5. Guest

    >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 31, 2006
    #5
  6. Eric Brunel Guest

    On 30 Mar 2006 15:11:19 -0800, <> 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+-'])"
     
    Eric Brunel, Mar 31, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    535
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    484
    Jeff Epler
    Aug 23, 2004
  3. Pierre Dagenais
    Replies:
    0
    Views:
    351
    Pierre Dagenais
    Aug 3, 2008
  4. Hidekazu IWAKI
    Replies:
    1
    Views:
    419
    Peter Otten
    Dec 14, 2009
  5. Hidekazu IWAKI
    Replies:
    0
    Views:
    555
    Hidekazu IWAKI
    Dec 15, 2009
Loading...

Share This Page