Search code examples
pythonmessagemessageboxpyqt5

Python PyQt5: How to show an error message with PyQt5


In normal Python (3.x) we always use showerror() from the tkinter module to display an error message but what should I do in PyQt5 to display exactly the same message type as well?


Solution

  • Qt includes an error-message specific dialog class QErrorMessage which you should use to ensure your dialog matches system standards. To show the dialog just create a dialog object, then call .showMessage(). For example:

    error_dialog = QtWidgets.QErrorMessage()
    error_dialog.showMessage('Oh no!')
    

    Here is a minimal working example script:

    import PyQt5
    from PyQt5 import QtWidgets
    
    app = QtWidgets.QApplication([])
    
    error_dialog = QtWidgets.QErrorMessage()
    error_dialog.showMessage('Oh no!')
    
    app.exec_()