【问题标题】:PyQt4 QTimer doesn't workPyQt4 QTimer 不起作用
【发布时间】:2016-11-13 11:17:49
【问题描述】:

我是使用 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()

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

【问题讨论】:

  • 忘记更正showNum函数的返回,但不是屏幕不显示的问题。使用 print 时,结果相同。

标签: python pyqt pyqt4 qtimer


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多