FAQ
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

Search Discussions

  • Eb303 at Oct 8, 2009 at 7:30 am

    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, textvariable=var).pack()
    Button(root, text='Go!', command=incr).pack()

    root.mainloop()
    ---

    HTH
  • Hendrik van Rooyen at Oct 8, 2009 at 7:39 am

    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
  • J Wolfe at Oct 8, 2009 at 5:15 pm
    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
  • Eb303 at Oct 9, 2009 at 8:29 am

    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?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 7, '09 at 10:40p
activeOct 9, '09 at 8:29a
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase