2014-11-08 27 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