Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Tkinter -- the best way to make a realtime loop (http://www.velocityreviews.com/forums/t700822-tkinter-the-best-way-to-make-a-realtime-loop.html)

J Wolfe 10-07-2009 10:40 PM

Tkinter -- the best way to make a realtime loop
 
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

eb303 10-08-2009 07:30 AM

Re: Tkinter -- the best way to make a realtime loop
 
On Oct 8, 12:40 am, J Wolfe <vorticitywo...@gmail.com> 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

Hendrik van Rooyen 10-08-2009 07:39 AM

Re: Tkinter -- the best way to make a realtime loop
 
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 10-08-2009 05:15 PM

Re: Tkinter -- the best way to make a realtime loop
 
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 10-09-2009 08:29 AM

Re: Tkinter -- the best way to make a realtime loop
 
On Oct 8, 7:15 pm, J Wolfe <vorticitywo...@gmail.com> 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?


All times are GMT. The time now is 10:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.