Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用计时器时出错?_Python_Python 3.x - Fatal编程技术网

在python中使用计时器时出错?

在python中使用计时器时出错?,python,python-3.x,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%,在三秒钟后打印加载10%,但它会立即打印所有内容),并给出以下错误: Exception in thread Thread-

我想为我正在创建的游戏创建一个假加载条,并尝试使用以下计时器命令:

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%,在三秒钟后打印加载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
你能给我建议一些解决这个问题的方法或其他方法吗? 提前感谢。

加载(0)立即调用加载

t = Timer(1.0, lambda: loading(0))
应该解决你的问题。这只会创建一个被调用的
lambda
(匿名函数)。

加载(0)
立即调用加载

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

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

@user2311251使用这些加载消息可以做的另一件很酷的事情是使用回车(
\r
),它们会返回到行的开头打印出来,以使加载条成为提示!我会调查的it@user2311251使用这些加载消息可以做的另一件很酷的事情是使用回车符(
\r
),它们返回到行的开头在其上打印,以便为提示制作加载条!我会调查的