Python Qtimer不工作

Python Qtimer不工作,python,pyqt,Python,Pyqt,我想使用QTimer用传感器值更新GUI 我尝试了以下非常简单的代码: from pyQt4 import QtCore def f(): try : print ("text") finally : QtCore.QTimer.singleShot(5000, f) f() 它不起作用了 import sys from PyQt4 import QtCore, QtGui class MyApp(QtGui.QWidget): def __

我想使用
QTimer
用传感器值更新GUI

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

 from pyQt4 import QtCore
 def f():
   try :
      print ("text")
   finally :
      QtCore.QTimer.singleShot(5000, f)
 f()
它不起作用了

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 object创建的…

你不需要使用QtCore.QTimer()这个例子应该完整吗?如果是这样的话,你为什么希望它能起作用?