Simple alert dialog PyQt

Simple alert dialog PyQt

Reusability with the least effort

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:

pyqt-like-confirmation-dialog.PNG

Objective

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()
    qmb.setText(text)
    qmb.setWindowTitle(title)
    if icon == "NoIcon":
        qmb.setIcon(QMessageBox.NoIcon)
    if icon == "Information":
        qmb.setIcon(QMessageBox.Information)
    if icon == "Warning":
        qmb.setIcon(QMessageBox.Warning)
    if icon == "Critical":
        qmb.setIcon(QMessageBox.Critical)
    if icon == "Question":
        qmb.setIcon(QMessageBox.Question)

    if buttons == True:
        qmb.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        if len(buttonsText) == 2:
            qmb.button(QMessageBox.Ok).setText(buttonsText[0])
            qmb.button(QMessageBox.Cancel).setText(buttonsText[1])
    else:
        if len(buttonsText) == 1:
            qmb.setStandardButtons(QMessageBox.Ok)
            qmb.button(QMessageBox.Ok).setText(buttonsText[0])

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

PyQt simple message box examples

Minimal alert box

pyqt-minimal-dialog.PNG
showMessageBox("Hey!", "How you doin'?")

Show error dialog

pyqt-error-dialog-alert.PNG

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

Confirmation dialog with default buttons

pyqt-confirmation-warning-dialog-message-box.PNG

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

Confirmation dialog with custom text buttons and callback

pyqt-custom-message-box-confirmation-callback.PNG

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 simplealertdialog.py

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!