使用单独的线程进行循环。
import threading
self.connect(self.pushButton,SIGNAL("toggled(bool)"),self.on_testLoop)
def on_testLoop(self, bool):
threading.Thread(target=self.testLoop, args=(bool,)).start()
def testLoop(self, bool):
while bool:
print "looping"
我发现另一种非常有用的方法是将函数变成生成器。这使您能够在每次迭代后做一些事情。
self.connect(self.pushButton,SIGNAL("toggled(bool)"),self.on_testLoop)
def on_testLoop(self, bool):
for _ in testLoop(bool):
# do some stuff, update UI elements etc.
def testLoop(self, bool):
while bool:
print "looping"
yield
我不知道为此使用生成器有多好,因为我对 PyQt 和 gui 编程很陌生,但这对我来说效果很好。