2013-03-15 46 views
1

下面的代碼只是讓一個QTimerQLCDNumber。從程序啓動後經過4秒後,我想顯示消息對話框。然而,當消息對話框彈出,在QLCDNumber時間不會改變。我應該怎麼做,使這個節目保留時間,即使在消息對話框彈出?線程,使一個計時器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_()) 

回答

0

更改您的代碼

msgBox = QMessageBox(self) 

創建非阻塞消息框。您可以刪除exec_()方法。

+0

感謝您的回覆,但現在我有我的代碼中的其他2個問題都。 1)如果QmessageBox的父項爲None,則show()僅顯示QmessageBox一秒,然後立即消失。爲什麼會出現此問題? 2)爲什麼QMessageBox的QMainWindow作爲父項創建一個非阻塞消息框? – iMath 2013-03-16 03:50:56

+0

1)我想設置父無觸發垃圾收集。 2.)它在文檔中是這樣說的。 請接受答案,如果它解決了您的問題 – Thomas 2013-03-17 18:35:41

+0

請給我你在答案中提到的鏈接。我剛剛檢查了文檔,只發現「如果不是無論如何,父母的論點,導致自己歸Qt所有,而不是PyQt的「。你是這個意思嗎 ?我也不明白這一點 – iMath 2013-03-18 12:30:58