在python中使用Timer时出错?

时间:2013-04-23 12:45:40

标签: python python-3.x

我想为我正在创建的游戏创建一个假装载栏,我尝试使用这样的计时器命令:

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

但是它不起作用(它应该在1秒后打印加载0%并在3之后加载10%但它立即打印它们)并且它给出了这个错误:

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

你能建议我解决这个问题或其他方法吗? 提前谢谢。

1 个答案:

答案 0 :(得分:4)

loading(0)会立即调用加载。

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

应该解决您的问题。这只是创建了一个lambda(匿名函数),它被调用,调用你的函数。