QMessageBox類提供了一個消息對話框,用於通知用戶或詢問用戶問題並接收答案。
消息對話框分為五種,分別是information,question,warning,critical,abort。
import sys from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox
class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.resize(250, 155) self.setWindowTitle('title') self.show() # 重寫closeEvent()事件方法 def closeEvent(self, event): # 顯示消息對話框 reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: event.accept() else: event.ignore() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
QMessageBox options: