PyQt - QMessageBox



QMessageBox is a commonly used modal dialog to display some informational message and optionally ask the user to respond by clicking any one of the standard buttons on it. Each standard button has a predefined caption, a role and returns a predefined hexadecimal number.

Important methods and enumerations associated with QMessageBox class are given in the following table −

Sr.No. Methods & Description
1

setIcon()

Displays predefined icon corresponding to severity of the message

Question Question

Information Information

Warning Warning

Critical Critical

2

setText()

Sets the text of the main message to be displayed

3

setInformativeText()

Displays additional information

4

setDetailText()

Dialog shows a Details button. This text appears on clicking it

5

setTitle()

Displays the custom title of dialog

6

setStandardButtons()

List of standard buttons to be displayed. Each button is associated with

QMessageBox.Ok 0x00000400

QMessageBox.Open 0x00002000

QMessageBox.Save 0x00000800

QMessageBox.Cancel 0x00400000

QMessageBox.Close 0x00200000

QMessageBox.Yes 0x00004000

QMessageBox.No 0x00010000

QMessageBox.Abort 0x00040000

QMessageBox.Retry 0x00080000

QMessageBox.Ignore 0x00100000

7

setDefaultButton()

Sets the button as default. It emits the clicked signal if Enter is pressed

8

setEscapeButton()

Sets the button to be treated as clicked if the escape key is pressed

QMessageBox does not define predefined icons but rather provides them through the style. The default value is No Icon. Message boxes remain the same for all cases. It is recommended to use the standard icons as suggested in the table or as per the style guidelines for the platform.

In this case, none of the predefined icons is suitable for our message box, we can create a custom icon by setting the icon pixmap property instead of the icon property.

Example 1

In this example, illustrate the predefined icon using QMessageBox class and it's method.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
def window():
   app = QApplication(sys.argv)
   w = QWidget()

   # Create buttons
   b1 = QPushButton(w)
   b1.setText("Information")
   b1.move(45, 50)

   b2 = QPushButton(w)
   b2.setText("Warning")
   b2.move(150, 50)

   b3 = QPushButton(w)
   b3.setText("Question")
   b3.move(50, 150)

   b4 = QPushButton(w)
   b4.setText("Critical")
   b4.move(150, 150)

   # Connect button signals to functions
   b1.clicked.connect(show_info_messagebox)
   b2.clicked.connect(show_warning_messagebox)
   b3.clicked.connect(show_question_messagebox)
   b4.clicked.connect(show_critical_messagebox)

   # Set window title
   w.setWindowTitle("PyQt MessageBox")

   # Show all widgets
   w.show()

   # Start the app
   sys.exit(app.exec())

def show_info_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Information)
   msg.setText("Information")
   msg.setWindowTitle("Information MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()
def show_warning_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Warning)
   msg.setText("Warning")
   msg.setWindowTitle("Warning MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()
def show_question_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Question)
   msg.setText("Question")
   msg.setWindowTitle("Question MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()
def show_critical_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Critical)
   msg.setText("Critical")
   msg.setWindowTitle("Critical MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()

if __name__ == "__main__":
   window()

Output

On executing the code, we see four on-click buttons to get the specific icons.

QMessageBox Output example one

Example 2

In the following example, click signal of the button on the top level window, the connected function displays the messagebox dialog.

msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("This is a message box")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")

setStandardButton() function displays desired buttons.

msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

buttonClicked() signal is connected to a slot function, which identifies the caption of source of the signal.

msg.buttonClicked.connect(msgbtn)

The complete code for the example is as follows −

import sys
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *

def window():
   app = QApplication(sys.argv)
   w = QWidget()
   b = QPushButton(w)
   b.setText("Show message!")
   
   b.move(100,50)
   b.clicked.connect(showdialog)
   w.setWindowTitle("PyQt MessageBox demo")
   w.show()
   sys.exit(app.exec_())

def showdialog():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Information)
   
   msg.setText("This is a message box")
   msg.setInformativeText("This is additional information")
   msg.setWindowTitle("MessageBox demo")
   msg.setDetailedText("The details are as follows:")
   msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
   msg.buttonClicked.connect(msgbtn)

   retval = msg.exec_()

def msgbtn(i):
   print ("Button pressed is:",i.text())

if __name__ == '__main__':
   window()

Output

The above code produces the following output. Message Box pops up when main windows button is clicked −

QMessageBox Output

If you click on Ok or Cancel button on MessageBox, the following output is produced on the console −

Button pressed is: OK
Button pressed is: Cancel
Advertisements