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