Marcelo Zárate
Marcelo Zárate

Marcelo Zárate

Simple alert dialog PyQt

Simple alert dialog PyQt

Reusability with the least effort

Marcelo Zárate
·Nov 28, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Good day, fellow reader. I was a Qt user long time ago, and I'm not a PyQt expert at all, in fact, when sometimes I find an answer in PyQt forum my first thought is: "Wow, this people really know their stuff", doing in three lines what probably takes me more than ten. But as I fiddle around with PyQt I didn't find a simple, yet customizable way to display annoying alert dialogs to the user. Of course, The property based API handles a lot of the issues I mentioned, but I wanted a one liner solution.

So, this is what we are going to achieve in this article:



The goal is to have an option to display an alert in PyQt by issuing

r = showMessageBox("Title", "All the body text", "Question", True, ["Confirm", "No way!"], returnTrue)

An experienced PyQt user might be wondering why not using the default constructor? Well, although some options are available in it, like setting the buttons text, title and icon, there are limited options regarding customization of the buttons and actions triggered by them. So we are building our own simple PyQt alert dialog.

Coding of the PyQt simple dialog

We will define some fixed strings for the available icons ["NoIcon", "Information", "Warning", "Critical", "Question"] and an option to allow for two button or single button mode. We are going to add the possibility for a callback to be executed whenever the uses clicks on OK button.

from PyQt5.QtWidgets import QMessageBox

def showMessageBox(title, text, icon="NoIcon", buttons=False, buttonsText=[], callback=None):
    qmb = QMessageBox()
    if icon == "NoIcon":
    if icon == "Information":
    if icon == "Warning":
    if icon == "Critical":
    if icon == "Question":

    if buttons == True:
        qmb.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        if len(buttonsText) == 2:
        if len(buttonsText) == 1:

    if qmb.exec() == QMessageBox.Ok:
        if callback:
            return callback()
            return None
        return None

PyQt simple message box examples

Minimal alert box

showMessageBox("Hey!", "How you doin'?")

Show error dialog


showMessageBox("Danger", "A critical error just happened", "Critical")

Confirmation dialog with default buttons


showMessageBox("Confirm action", "Are you comfortable with this action?", "Warning", True)

Confirmation dialog with custom text buttons and callback


returnedStuff = showMessageBox("Erasing records", "Are you absolutely sure you want to erase the selected records? This operation is not reversible", "Warning", True, ["Yes, erase them", "No, don't do it"], eraseSelected)

Full code

In case you just want to try it out, saving this code and running it with python

Wrapping up

That's it! Simple, yet kind of useful. If you have any suggestions or improvements, I beg you to do it directly in the comments. As I mentioned, I'm almost a trainee in PyQt, but I wanted to share this with you.

See you around!

Share this