PyQt5--MessageBox


 1 # -*- coding:utf-8 -*-
 2 '''
 3 Created on Sep 13, 2018
 4 
 5 @author: SaShuangYiBing
 6 '''
 7 import sys
 8 from PyQt5.QtWidgets import QApplication,QWidget,QMessageBox,QPushButton
 9 from PyQt5.QtCore import QCoreApplication
10 
11 class New_test(QWidget):
12     def __init__(self):
13         super().__init__()
14         self.initUI()
15         
16     def initUI(self):
17         qbtn = QPushButton('Quit',self) # 創建了一個按鈕。按鈕是一個QPushButton類的實例。
18         # 構造方法的第一個參數是顯示在button上的標簽文本。第二個參數是父組件。
19         # 父組件是Example組件,它繼承了QWiget類。
20         qbtn.clicked.connect(QCoreApplication.instance().quit)
21         qbtn.resize(qbtn.sizeHint())
22         qbtn.move(100,50)
23         self.setGeometry(300,300,250,150)
24         self.setWindowTitle('Message box')
25         self.show()
26         
27     def closeEvent(self, event):
28         reply = QMessageBox.question(self, 'Message', 'Are you want to quit?', QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
29         if reply == QMessageBox.Yes:
30             event.accept()
31         else:
32             event.ignore()
33 
34 if __name__ == "__main__":
35     app = QApplication(sys.argv)
36     ex = New_test()
37     sys.exit(app.exec_())
38         

點擊窗口的 關閉 按鈕,彈出messageBox

 

注:第28行解釋:QMessageBox.question(self, 'Message', 'Are you want to quit?', QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)

顯示一個消息框,兩個按鈕:“Yes”和“No”。第一個字符串出現在titlebar。第二個字符串消息對話框中顯示的文本。第三個參數指定按鈕的組合出現在對話框中。最后一個參數是默認按鈕,這個是默認的按鈕焦點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM