Tkinter -- the best way to make a realtime loop

J

J Wolfe

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
 
E

eb303

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

Hendrik van Rooyen

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

J Wolfe

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
 
E

eb303

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?
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top