This article covers the PyQt5 widget, QMessageBox.

QMessageBox is a useful PyQt5 widget used to create dialogs. These dialogs can be used for a variety of purposes and customized in many ways to display different messages and buttons.

In this PyQt5 tutorial you’ll also find a complete list of all methods, buttons types and options for the QMessageBox widget.


Creating a QMessageBox

Below we are just going to create a simple QMessageBox widget. It’s going to be a bare-bone messagebox with no added features or customization.

msg = QMessageBox(win)
msg.setWindowTitle("Message Box")
msg.setText("This is some random text")

x = msg.exec_()

And below is the output. If you want to display a simple message on screen, this is the way to do so.

Simple QMessageBox

Now compare this simple messagebox to the advanced and feature-rich message-boxes we are going to create below.


QMessageBox Icons

QMessageBox has 4 different types of icons that can be used by calling the setIcon() function. Decide which one to use depending on type of message you wish to display.

  • QMessageBox.Question
  • QMessageBox.Information
  • QMessageBox.Warning
  • QMessageBox.Critical

All you have to do is pass one of the above to the setIcon() function as shown below.

msg.setIcon(QMessageBox.Question)

Below are all four different messages that can be displayed.

Different Types of QMessageBox Icons- PyQt5

QMessageBox Buttons

Uptil now we’ve been using the default “OK” button that QMessageBox offers. However, there are actually over a dozen different buttons that QMessageBox offers for our use. Below is an example of us utilizing some of the buttons.

Using the setStandardButtons() function, we can pass whichever button-type we want. (See the list of complete button types below)

def show_popup():
    msg = QMessageBox(win)
    msg.setWindowTitle("Message Box")
    msg.setText("This is some random text")
    msg.setIcon(QMessageBox.Question)
    msg.setStandardButtons(QMessageBox.Cancel|QMessageBox.Ok
                          |QMessageBox.Retry)
    msg.setInformativeText("This is some extra informative text")
    x = msg.exec_()
QMessageBox Buttons

You may have noticed this already, but the order the buttons has no impact on how they appear on the message box.

Default Button

Look at the above image again. You’l have noticed that there is a blue outline around the “OK” button. This is the symbol of the default button.

Using the setDefaultButton() function we can change the default button. Adding the following line to our code will cause the blue highlight to be around the “Cancel” button.

msg.setDefaultButton(QMessageBox.Cancel)

Here’s a complete list of different buttons types you can use with Messages.

  • QMessageBox.Ok
  • QMessageBox.No
  • QMessageBox.Yes
  • QMessageBox.Cancel
  • QMessageBox.Close
  • QMessageBox.Abort
  • QMessageBox.open
  • QMessageBox.Retry
  • QMessageBox.Ignore
  • QMessageBox.Save
  • QMessageBox.Retry
  • QMessageBox.Apply
  • QMessageBox.Help
  • QMessageBox.Reset
  • QMessageBox.SaveAll
  • QMessageBox.YesToAll
  • QMessageBox.NoToAll

Detailed and Informative Text

By default you only have one area in the QMessageBox where you display Text. However, there are two more additional areas that we can unlock to add more text.

The first is an additional text section on the messagebox window itself. It’s like an additional line of text you can add called informative text. You only need to call the setInformativeText() function to add this new area of text.

The second is displayed on an area that expands from the QMessageBox. This is called “Detailed Text”. Setting up this section will automatically create a button that is used to show this area. You only need the setDetailedText() function to create this area.

def show_popup():
    msg = QMessageBox(win)
    msg.setWindowTitle("Message Box")
    msg.setText("This is some random text")
    msg.setIcon(QMessageBox.Question)
    
    msg.setStandardButtons(QMessageBox.Cancel|QMessageBox.Ok)
    msg.setDefaultButton(QMessageBox.Ok)

    msg.setDetailedText("Extra details.....")
    msg.setInformativeText("This is some extra informative text")
    x = msg.exec_()

app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")

button = QtWidgets.QPushButton(win)
button.setText("A Button")
button.clicked.connect(show_popup)
button.move(100,100)

win.show()
sys.exit(app.exec_())

In case it didn’t make sense before, look the code and compare to how the output below has changed.

QMessageBox with Detail section

Clicking the Hide/Show Details button will hide/show the extra area below the messagebox respectively.


Retrieving QMessageBox Values

We’ve discussed alot of different customizations and features above, but we haven’t actually linked any of these different features and buttons to our code.

For example, if we have 3 different buttons on our messagebox, how will we know which one was pressed? This is what we’ll be exploring in this section. The code for this is shown below. Make sure to read it before moving forward.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
import sys

def show_popup():
    msg = QMessageBox()
    msg.setWindowTitle("Message Box")
    msg.setText("This is some random text")
    
    msg.setIcon(QMessageBox.Question)
    msg.setStandardButtons(QMessageBox.Cancel|QMessageBox.Ok)
    msg.buttonClicked.connect(popup)
    
    x = msg.exec_()

def popup(i):
    print(i.text())

app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")

button = QtWidgets.QPushButton(win)
button.setText("A Button")
button.clicked.connect(show_popup)
button.move(100,100)

win.show()
sys.exit(app.exec_())

Using the buttonClicked.connect() method, we can cause a function to be triggered whenever a button on the message box is clicked. In this case, we’ve linked our messagebox to a function called popup. Notice that popup() takes a parameter i.

When the messagebox button is clicked it automatically passes the button that was clicked to the function declared in buttonClicked.connect(). Calling the text() function on this button will return it’s text value.


Head over to our main PyQt5 section to learn more about the other great widgets!

This marks the end of the PyQt5 QMessageBox article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments