Preface
In this section, we will introduce how to use messagebox, which can be used in message prompt box, warning box, inquiry box, error, about and other conversation boxes.
1, Basic knowledge
1. Introduction to MessageBox
Message box is a message session box, which can prompt the user with key messages and obtain the user's selection, so as to control the execution of the program in special circumstances.
The standard button types are as follows
Two. Example
1. QT Designer design UI
Open designer.exe, use the default Main Window to Create, and directly click Create
The design UI is as follows and saved as messagebox.ui
2 convert messagebox.ui to UI? Messagebox.py
Enter the messagebox.py directory, enter pyuic5 - O UI ﹣ messagebox.py messagebox.ui
3 write python program
# encoding=utf-8 import sys import PyQt5.QtWidgets as qw import ui_msgbox class myForm(qw.QWidget, ui_msgbox.Ui_Form): def __init__(self): super().__init__() self.setupUi(self) self.btn_info.clicked.connect(self.btn_info_cb) self.btn_warn.clicked.connect(self.btn_warn_cb) self.btn_critical.clicked.connect(self.btn_critical_cb) self.btn_question.clicked.connect(self.btn_question_cb) self.btn_about.clicked.connect(self.btn_about_cb) def btn_info_cb(self): print("ready to show messagebox.") res = qw.QMessageBox.information(self, "Tips", "I am info Type MessageBox!", qw.QMessageBox.Yes | qw.QMessageBox.No) if (qw.QMessageBox.Yes == res): print("[info] you clicked yes button!") elif (qw.QMessageBox.No == res): print("[info] you clicked no button!") def btn_warn_cb(self): res = qw.QMessageBox.warning(self, "warning", "I am warn Type MessageBox!", qw.QMessageBox.Yes | qw.QMessageBox.No) if (qw.QMessageBox.Yes == res): print("[warn] you clicked yes button!") elif (qw.QMessageBox.No == res): print("[warn] you clicked no button!") def btn_critical_cb(self): res = qw.QMessageBox.critical(self, "error", "I am critical Type MessageBox!", qw.QMessageBox.Abort | qw.QMessageBox.Cancel) if (qw.QMessageBox.Abort == res): print("[critical] you clicked Abort button!") elif (qw.QMessageBox.Cancel == res): print("[critical] you clicked Cancel button!") def btn_question_cb(self): res = qw.QMessageBox.question(self, "inquiry", "I am critical Type MessageBox", qw.QMessageBox.Retry | qw.QMessageBox.Ok) if (qw.QMessageBox.Retry == res): print("[question] you clicked Retry button!") elif (qw.QMessageBox.Ok == res): print("[question] you clicked Ok button!") def btn_about_cb(self): qw.QMessageBox.about(self, "about", "I am critical Type MessageBox!") if __name__ == '__main__': app = qw.QApplication(sys.argv) w = myForm() w.show() sys.exit(app.exec_())
First, set it to the button designed by QT Designer in the previous step, add the corresponding message box, and get the user's point information, which will be displayed through the console log.
Three, operation
Enter the file directory, enter python3 run.py, and the above page designed with QT Designer will pop up.
Click info button:
Click warn button:
Click critical button:
Click the question button:
Click about button:
Four, conclusion
If you have any questions during use, please add QQ group for further communication, or github for Issue.
QQ communication group: 906015840 (Note: Internet of things project communication)
Get source code: pay attention to the public number, reply to pyqt5 can be
One sand, one world, one leaf, one Bodhi