QMessageBox是一种通用的弹出框对话框;包含:提示、警告、错误、咨询、关于等对话框;只是显示图标不同,其他功能类似;
QMessageBox类常用方法如下:
按钮类型:
常用对话框demo
例如:
1 #QMessageBox消息框使用 2 from PyQt5.QtWidgets import QVBoxLayout,QWidget,QApplication ,QHBoxLayout,QDialog,QPushButton,QMessageBox 3 4 from PyQt5.QtGui import QIcon,QPixmap,QFont 5 from PyQt5.QtCore import Qt 6 7 import sys 8 9 class WindowClass(QWidget): 10 11 def __init__(self,parent=None): 12 13 super(WindowClass, self).__init__(parent) 14 layout=QVBoxLayout() 15 self.btn=QPushButton() 16 self.btn.setText("显示消息框") 17 self.btn.clicked.connect(self.showMessageBox) 18 self.resize(500,500) 19 layout.addWidget(self.btn) 20 21 self.setLayout(layout) 22 23 def showMessageBox(self): 24 res_1= QMessageBox.information(self,"标题","消息对话框正文!",QMessageBox.Yes|QMessageBox.No,QMessageBox.Yes) 25 print(res_1) 26 res_2 = QMessageBox.question(self, "标题", "消息对话框正文!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) 27 print(res_2) 28 res_3 = QMessageBox.warning(self, "标题", "消息对话框正文!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) 29 print(res_3) 30 res_4 = QMessageBox.critical(self, "标题", "消息对话框正文!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) 31 print(res_4) 32 res_5 = QMessageBox.about(self, "标题", "消息对话框正文!" ) 33 print(res_5) 34 35 if __name__=="__main__": 36 app=QApplication(sys.argv) 37 win=WindowClass() 38 win.show() 39 sys.exit(app.exec_())