2

I have a very basic Tkinter application that I'm trying to set to automatically refresh. I am using threading.Timer to trigger a function called go at the end of which cocalls the timer to reset it to run again.

def SetTimer():
    t=threading.Timer(5,go())
    t.start
    SetTimer()

I've spent most of my afternoon trying to resolve this but I can't seem to figure out what I'm doing wrong. Based upon the other questions I've read I understand that each instance creates a separate thread with the timer, but why isn't it waiting for the time to elapse prior to triggering the function.

1
  • 1
    If you're using Tkinter, using a thread to automatically refresh is the wrong way to solve the problem. You can avoid the complexities of threads by calling the after method of the root widget (or any widget) to schedule a function to run in the future. Commented Oct 6, 2014 at 11:08

1 Answer 1

4

Use:

t=threading.Timer(5,go)

Note the go rather than go(). The parentheses tell Python to call the function. In contrast, go is a function object. Functions are "first-class" objects in Python; they can be passed to functions just like any other object.

Python evaluates the arguments to a function before calling the function. So using go() calls the go before calling threading.Timer. The value returned by go() gets sent to Timer, which is not what you want.


Note that all Tkinter UI code should be called from a single thread. For this reason, using Tkinter's root.after method is probably a better choice for what you want to do. The after method schedules a function to be called by Tkinter's mainloop. Here are some examples using after:

Sign up to request clarification or add additional context in comments.

1 Comment

That makes perfect sense! Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.