PYQT QTimer未输入处理程序D

2024-05-18 21:24:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码,在我的主窗口工作,但我需要复制它在弹出窗口。当运行时,它不会进入处理程序def,我不知道为什么。我想尽一切办法都试过了。有人能告诉我我做错了什么吗?你知道吗

from PyQt4 import QtGui, QtCore
import sys

CurrentTime = 0

class widgetWindow(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self,parent)
        super(widgetWindow, self).__init__()
        widgetWindow.start(self)

    def start(self):
        window = QtGui.QMainWindow(self)
        window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        CentralWidget = QtGui.QWidget()
        timeSlider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        CentralWidgetLayout = QtGui.QHBoxLayout()
        VBox = QtGui.QVBoxLayout()
        CentralWidgetLayout.addWidget(timeSlider)
        VBox.addLayout(CentralWidgetLayout)
        CentralWidget.setLayout(VBox)
        window.setCentralWidget(CentralWidget)
        timeSlider.setValue(0)
        window.show()

        self.runTimer()

    def runTimer(self):

        timer = QtCore.QTimer()
        timer.timeout.connect(self.updateTime)
        timer.start(1000)

    def updateTime(self):
        global CurrentTime
        CurrentTime = CurrentTime + 1
        print("Current Timer = ", CurrentTime)



def main():
    app = QtGui.QApplication(sys.argv)
    win = widgetWindow()
    win.show()
    win.resize(800,450)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Tags: selfinitdefsyswindowstartqtguivbox
1条回答
网友
1楼 · 发布于 2024-05-18 21:24:09

这里有两个问题

(一)

main函数中,行

win.show()

是多余的。删除它,您将看到您的QMainWindow对象

(二)

您没有保存对计时器对象的引用。你知道吗

runTimer改成这个,它就可以工作了

def runTimer(self):

    self.timer = QtCore.QTimer()
    self.timer.timeout.connect(self.updateTime)
    self.timer.start(1000)

相关问题 更多 >

    热门问题