2

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.

1 Answer 1

4

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.

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

1 Comment

@user2311251 another cool thing you can do with these loading messages is use carriage returns (\r), they go back to the start of the line to print over it, to make loading bars

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.