2014-11-08 152 views
1

我想使用QTimer来更新带有传感器值的GUI。Qtimer无法正常工作

我尝试下面的代码非常简单:

from pyQt4 import QtCore 
def f(): 
    try : 
     print ("text") 
    finally : 
     QtCore.QTimer.singleShot(5000, f) 
f() 

它不工作。

+0

你不需要使用QtCore.QTimer() – neiesc 2014-11-08 13:15:44

+0

这个例子应该是完整的吗?如果是这样,为什么你会期望它的工作? – ekhumoro 2014-11-08 15:51:23

回答

2
import sys 
from PyQt4 import QtCore, QtGui 

class MyApp(QtGui.QWidget): 
    def __init__(self): 
     self.print_hello() 

    def print_hello(self): 
     print 'hello' 
     QtCore.QTimer.singleShot(3000, self.print_hello) 

qapp = QtGui.QApplication(sys.argv) 
app = MyApp() 
qapp.exec_() 

我不确定这是为什么可行,但它与计时器需要在运行线程中有关。我想它是用app对象创建的...

+0

它现在正在工作:)谢谢 – 2014-11-13 18:56:46

+1

@KyawLin考虑接受解决方案,然后 – chwi 2015-12-01 10:02:16