PyQt5学习2-第一个对话框

QMessageBox

QtWidgets提供了一些默认的消息框 QMessageBox, 主要有

1
2
3
4
5
6
7
8
9
QMessageBox.question(self, 'message', 'Are you sure to quite?',QMessageBox.Yes, QMessageBox.No)
QMessageBox.about(self,'About this', 'This is a test program')
QMessageBox.warning(self,'warning','There is a warning',QMessageBox.Cancel)
QMessageBox.information(self,'Information', 'This is an information')
QMessageBox.critical(self, 'critical', 'error')

QMessageBox.question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python3
#-*- coding: utf-8 -*-
# shows message box when click close button
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox
class ex(QWidget):
def __init__(self):
super().__init__()
self.show()
def closeEvent(self,event):
reply = QMessageBox.question(self,'Message',"Are you sure to quit?")
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = ex()
sys.exit(app.exec_())

点击关闭按钮时,产生一个消息窗口如下

QMessageBox