In this article, we will discuss the Message Box Widget of the PyQT5 module. It is used to display the message boxes. PyQt5 is a library used to create GUI using the Qt GUI framework. Qt is originally written in C++ but can be used in Python. The latest version of PyQt5 can be installed using the command:
pip install PyQt5
What is a Message Box?
Message Boxes are usually used for declaring a small piece of information to the user. It gives users a pop-up box, that cannot be missed, to avoid important errors and information being missed by the users and in some cases, the user cannot continue without acknowledging the message box.
Based on the applications there are four types of message boxes. The following is the syntax for creating a message box. For any of the boxes, instantiation needs to be done.
Syntax:
msg_box_name = QMessageBox()
Now according to the requirement an appropriate message box is created.
Types of Message Box
Information Message Box
This type of message box is used when related information needs to be passed to the user.
Syntax:
msg_box_name.setIcon(QMessageBox.Information)

Question Message Box
This message box is used to get an answer from a user regarding some activity or action to be performed.
Syntax:
msg_box_name.setIcon(QMessageBox.Question)

Warning Message Box
This triggers a warning regarding the action the user is about to perform.
Syntax:
msg_box_name.setIcon(QMessageBox.Warning)

Critical Message Box
This is often used for getting the user's opinion for a critical action.
Syntax:
msg_box_name.setIcon(QMessageBox.Critical)

Creating a simple Message Box using PyQt5
Now to create a program that produces a message box first import all the required modules, and create a widget with four buttons, on clicking any of these a message box will be generated.
Now for each button associate a message box that pops when the respective button is clicked. For this first, instantiate a message box and add a required icon. Now set appropriate attributes for the pop that will be generated. Also, add buttons to deal with standard mechanisms.
Given below is the complete implementation.
Program:
Python
#import libraries
import sys
from PyQt5.QtWidgets import *
def window():
# create pyqt5 app
app = QApplication(sys.argv)
w = QWidget()
# create buttons
# b1- Information button
b1 = QPushButton(w)
b1.setText("Information")
b1.move(45, 50)
# b2- Warning button
b2 = QPushButton(w)
b2.setText("Warning")
b2.move(150, 50)
# b3- Question button
b3 = QPushButton(w)
b3.setText("Question")
b3.move(50, 150)
# b4- Critical button
b4 = QPushButton(w)
b4.setText("Critical")
b4.move(150, 150)
# declaring command when button clicked
b1.clicked.connect(show_info_messagebox)
b2.clicked.connect(show_warning_messagebox)
b3.clicked.connect(show_question_messagebox)
b4.clicked.connect(show_critical_messagebox)
# setting title of the window
w.setWindowTitle("PyQt MessageBox")
# showing all the widgets
w.show()
# start the app
sys.exit(app.exec_())
def show_info_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
# setting message for Message Box
msg.setText("Information ")
# setting Message box window title
msg.setWindowTitle("Information MessageBox")
# declaring buttons on Message Box
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
# start the app
retval = msg.exec_()
def show_warning_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
# setting message for Message Box
msg.setText("Warning")
# setting Message box window title
msg.setWindowTitle("Warning MessageBox")
# declaring buttons on Message Box
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
# start the app
retval = msg.exec_()
def show_question_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Question)
# setting message for Message Box
msg.setText("Question")
# setting Message box window title
msg.setWindowTitle("Question MessageBox")
# declaring buttons on Message Box
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
# start the app
retval = msg.exec_()
def show_critical_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
# setting message for Message Box
msg.setText("Critical")
# setting Message box window title
msg.setWindowTitle("Critical MessageBox")
# declaring buttons on Message Box
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
# start the app
retval = msg.exec_()
if __name__ == '__main__':
window()
Output
Similar Reads
Python EasyGUI - Message Box Message Box : It is used to display a window having a message or information in EasyGUI, it can be used where there is a need to display some message or some important information, it contains message and a "Ok" button which when pressed closes the message, below is how the message box looks like
2 min read
Python EasyGUI â Enter Box Enter Box : It is used to get the input from the user, input can be any keyboard input, it takes input in form of string. It displays the title, message to be displayed, place to enter a text and a pair of "Ok", "Cancel" button which is used confirm the input. Also we can set some default text to th
2 min read
Python Tkinter - Message Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applicat
3 min read
Python EasyGUI â Integer Box Integer Box : It is used to get the integer input from the user, input should be integer input not string which happens in enter box. It displays the title, message to be displayed, place to enter a integer input and a pair of "Ok", "Cancel" button which is used confirm the input. We can set some de
3 min read
PyQt5 â Set status bar message in window In this article, we will see how to set message to status bar of window. A status bar is a graphical control element which poses an information area typically found at the window's bottom. It can be divided into sections to group information. Its job is primarily to display information about the cur
1 min read