Tkinter -- the best way to make a realtime loop

Discussion in 'Python' started by J Wolfe, Oct 7, 2009.

  1. J Wolfe

    J Wolfe Guest

    What's the best way to make a realtime loop in Tkinter? I know in
    perl you can use "repeat" and it will call a function every x seconds,
    in python it seems like "after" may be the equivalent though it
    doesn't seem to behave like the perl repeat function. Any ideas?

    Thanks,
    Jonathan
     
    J Wolfe, Oct 7, 2009
    #1
    1. Advertising

  2. J Wolfe

    eb303 Guest

    On Oct 8, 12:40 am, J Wolfe <> wrote:
    > What's the best way to make a realtime loop in Tkinter? I know in
    > perl you can use "repeat" and it will call a function every x seconds,
    > in python it seems like "after" may be the equivalent though it
    > doesn't seem to behave like the perl repeat function. Any ideas?
    >
    > Thanks,
    > Jonathan


    Well, I don't know the Perl 'repeat' function, but AFAICT, after seems
    to be the way to go in Tkinter. Maybe something like this:
    ---
    from Tkinter import *

    root = Tk()

    var = IntVar()

    def incr():
    var.set(1 + var.get())
    root.after(1000, incr)

    Label(root, width=10, textvariable=var).pack()
    Button(root, text='Go!', command=incr).pack()

    root.mainloop()
    ---

    HTH
     
    eb303, Oct 8, 2009
    #2
    1. Advertising

  3. On Thursday, 8 October 2009 00:40:42 J Wolfe wrote:
    > What's the best way to make a realtime loop in Tkinter? I know in
    > perl you can use "repeat" and it will call a function every x seconds,
    > in python it seems like "after" may be the equivalent though it
    > doesn't seem to behave like the perl repeat function. Any ideas?


    What do you mean by "real time"?

    The after method in tkinter will call the routine after the specified time.

    If you want to do it again, you must arrange for the routine to call itself
    again with a further after.

    So it is perfectly possible to make a stutter thread that executes
    periodically.

    In which way does this not work for you?

    - Hendrik
     
    Hendrik van Rooyen, Oct 8, 2009
    #3
  4. J Wolfe

    J Wolfe Guest

    Thank you both for your replies. I had something similar to this:

    def incr():
    var.set(1 + var.get())
    root.after(1000, incr)

    except I had an extra set of parenthesis...
    def incr():
    var.set(1 + var.get())
    root.after(1000, incr())

    on the function which was screwing it up. Also needed to have a
    root.update() to refresh the GUI.

    Thanks again,
    Jon
     
    J Wolfe, Oct 8, 2009
    #4
  5. J Wolfe

    eb303 Guest

    On Oct 8, 7:15 pm, J Wolfe <> wrote:
    > Thank you both for your replies. I had something similar to this:
    >
    > def incr():
    > var.set(1 + var.get())
    > root.after(1000, incr)
    >
    > except I had an extra set of parenthesis...
    > def incr():
    > var.set(1 + var.get())
    > root.after(1000, incr())
    >
    > on the function which was screwing it up. Also needed to have a
    > root.update() to refresh the GUI.


    Mmmmm, no? The root.update() should not be necessary: the triggering
    of the action specified in after(...) is done by the tk mainloop when
    it's idle, and if it's idle, it means that it already has updated the
    display. So no update() should be needed. What happens if you remove
    it?
     
    eb303, Oct 9, 2009
    #5
    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. Henrik H
    Replies:
    1
    Views:
    664
    Scott Allen
    Oct 25, 2004
  2. Stan
    Replies:
    3
    Views:
    507
    Brock Allen
    May 5, 2005
  3. rhmd
    Replies:
    9
    Views:
    708
    Pat Bills
    Oct 27, 2003
  4. Kevin
    Replies:
    16
    Views:
    47,560
    Roedy Green
    Jan 30, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page