1

下面的代码只是做一个QTimerwith QLCDNumber。我想在程序启动后 4 秒后显示一个消息对话框。但是,当消息对话框弹出时,时间QLCDNumber不会改变。即使弹出消息对话框,我应该怎么做才能使该程序保持计时?

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(800,600)

        self.lcdNumber = QLCDNumber()
        self.lcdNumber.setNumDigits(8)

        layout =  QVBoxLayout(self)
        layout.addWidget(self.lcdNumber)

        self.currentTime = QTime(0,0,0)
        self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateLcdNumberContent)
        self.timer.start(1000)

    def updateLcdNumberContent(self):

        self.currentTime = self.currentTime.addSecs(1)
        self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))


        if self.currentTime == QTime(0,0,4) :
            msgBox = QMessageBox()
            msgBox.setWindowTitle('iTimer')
            msgBox.setIcon (QMessageBox.Information)
            msgBox.setText("Time Out !!")

            stopButton = msgBox.addButton("Stop", QMessageBox.ActionRole)
            ignoreButton = msgBox.addButton(QMessageBox.Ignore)

            stopButton.clicked.connect(self.timer.stop)


            msgBox.show()
#            msgBox.exec_()



if __name__ == '__main__':
    app =QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    sys.exit(app.exec_())
4

1 回答 1

0

将您的代码更改为

msgBox = QMessageBox(self)

创建一个非阻塞消息框。您可以删除 exec_() 方法。

于 2013-03-15T06:53:08.593 回答