如何在不使用 while 循环的情况下重复 python3 中的函数?

How can I repeat a function in python3 without using a while loop?

我在 python 三个中有一个函数需要 运行 不断地检查一些变量。目前我有一个 while 循环,一切都变得很顺利 运行,直到我想使用 pyqt 小部件,结果却以灾难告终。

我在 Whosebug 的另一个线程上找到了 twisted.internet 模块,但我无法通过 python3 将它转到 运行。

是否有其他模块允许我在 python3 中每 0.01 秒重复某个功能到 运行 而不会空闲我的代码?

如果你使用的是 pyqt,这意味着有一个 运行 事件循环(你的 while 循环会阻塞)——你可以要求它在一定时间后安排一个函数调用,而不需要循环你自己。

使用 QTimer sounds like what you need. This 是一个更简单的示例。

从那一秒开始link:

timer = QTimer()
timer.timeout.connect(your_function)
timer.start(100)

这会做你想做的,只要 QTimer 保持活动状态(如果它被垃圾收集它就会停止)。