Tkappa
Tkappa

Reputation: 25

Error when using Timer in python?

i wanted to create a fake loading bar for game i'm creating and I tried to use the timer command like this:

def loading(time):
    print("loading "+str(time)+"%..")
t = Timer(1.0, loading(0))
t.start()
a = Timer(3.0, loading(10))
a.start()

But it doesn't work ( It should print loading 0% afer 1 second and loading 10% after three but it prints them all immediatly) and it gives this error:

Exception in thread Thread-1:
Traceback (most recent call last):
File "H:\Python\python\App\lib\threading.py", line 736, in _bootstrap_inner
self.run()
File "H:\Python\python\App\lib\threading.py", line 942, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

Could you suggest me some solution for this problem or other methods to do this? Thanks in advance.

Upvotes: 2

Views: 1630

Answers (1)

jamylak
jamylak

Reputation: 133504

loading(0) calls loading right then and there.

t = Timer(1.0, lambda: loading(0))

should fix your issue. This just creates a lambda(anonymous function) that gets called instead, calling your function.

Upvotes: 4

Related Questions