In this Python PyQt5 lesson, we want to learn about Python PyQt5 QMessageBox , so the the QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer. there are different types of pyqt messagebox that you can us, for example we have about messagebox,information messagebox, warning messagebox and multichoice messagebox.
This is the complete code for Python PyQt5 QMessageBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
from PyQt5.QtWidgets import QApplication, QWidget,\ QHBoxLayout, QMessageBox, QPushButton, QVBoxLayout, QLabel import sys from PyQt5.QtGui import QIcon, QFont class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200,400,200) self.setWindowTitle("Creating MessageBox") self.setWindowIcon(QIcon("python.png")) self.create_messagebox() def create_messagebox(self): hbox = QHBoxLayout() btn1 = QPushButton("Warning") btn1.clicked.connect(self.warning_msg) btn2 = QPushButton("Information") btn2.clicked.connect(self.info_msg) btn3 = QPushButton("About") btn3.clicked.connect(self.about_msg) btn4 = QPushButton("Yes/No") btn4.clicked.connect(self.multi_choice_msg) hbox.addWidget(btn1) hbox.addWidget(btn2) hbox.addWidget(btn3) hbox.addWidget(btn4) vbox = QVBoxLayout() self.label = QLabel("Text") self.label.setFont(QFont("Sanserif", 14)) vbox.addLayout(hbox) vbox.addWidget(self.label) self.setLayout(vbox) def warning_msg(self): QMessageBox.warning(self, "Warning", "This is a warning message") def info_msg(self): QMessageBox.information(self, "Information", "This is information message") def about_msg(self): QMessageBox.about(self, "About", "This is about message") def multi_choice_msg(self): message = QMessageBox.question(self, "Choice Message", "Do You Like Python ?", QMessageBox.Yes | QMessageBox.No) if message == QMessageBox.Yes: self.label.setText("Yes I Like Python") else: self.label.setText("No I Dont Like Python") App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.
1 2 3 |
self.setGeometry(200,200,400,200) self.setWindowTitle("Creating MessageBox") self.setWindowIcon(QIcon("python.png")) |
We want to add our widgets in the HBoxlayout.
1 |
hbox = QHBoxLayout() |
These are our four buttons and we have already connected the clicked signal of these buttons with the slots or methods that we want to create.
1 2 3 4 5 6 7 8 9 10 11 |
btn1 = QPushButton("Warning") btn1.clicked.connect(self.warning_msg) btn2 = QPushButton("Information") btn2.clicked.connect(self.info_msg) btn3 = QPushButton("About") btn3.clicked.connect(self.about_msg) btn4 = QPushButton("Yes/No") btn4.clicked.connect(self.multi_choice_msg) |
When you create layouts and widgets in your pyqt applications, you need to add all your widgets in the layout.
1 2 3 4 |
hbox.addWidget(btn1) hbox.addWidget(btn2) hbox.addWidget(btn3) hbox.addWidget(btn4) |
Also you need to set layout for the main window, if you don’t do this you will not see any widgets in the window.
1 |
self.setLayout(vbox) |
You can create pyqt5 messagebox by creating the object of QMessageBox class. so you can see that we have created four types of QMessageBox.
1 2 3 4 5 6 7 8 9 |
QMessageBox.warning(self, "Warning", "This is a warning message") QMessageBox.information(self, "Information", "This is information message") QMessageBox.about(self, "About", "This is about message") message = QMessageBox.question(self, "Choice Message", "Do You Like Python ?", QMessageBox.Yes | QMessageBox.No) |
For every Python PyQt5 application you need to create the object of QApplication, and the argument is optional.
1 |
App = QApplication(sys.argv) |
Run the complete code and this is the result.