2016-11-13 48 views
1

我是新來使用PyQt4 QTimer。我只是從某處複製代碼,但似乎不起作用。有人可以幫我弄這個嗎?PyQt4 QTimer不起作用

from PyQt4 import QtCore, QtGui 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 



def startCount(): 
    timer.start(1000) 



def showNum(): 
    global count 
    count = count + 1 
    return count 

timer = QtCore.QTimer() 
count = 0 
timer.timeout.connect(showNum) 
startCount() 

我希望看到計數增加了時間,但控制檯沒有顯示任何輸出。有人可以解釋這個嗎?

+0

忘了更正showNum函數的返回值,但它不是在屏幕上沒有顯示任何東西的問題。它使用打印時具有相同的結果。 – lwangreen

回答

3

如果沒有正在運行的事件循環,則QTimer無法工作。試試這個:

import sys 
from PyQt4 import QtCore, QtGui 

def startCount(): 
    timer.start(1000) 

def showNum(): 
    global count 
    count = count + 1 
    print(count) 
    if count > 10: 
     app.quit() 

app = QtCore.QCoreApplication(sys.argv) 

timer = QtCore.QTimer() 
count = 0 
timer.timeout.connect(showNum) 
startCount() 

app.exec_() 
+0

非常感謝您的幫助! – lwangreen