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()

我希望看到计数随时间增加,但是控制台没有显示任何输出。有人能解释一下吗?

没有运行的事件循环,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_()

最新更新