Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 796
Ramón Wilhelm Avatar asked Oct 24 '16 20:10

Ramón Wilhelm


3 Answers

Don't forget to call .exec_() to display the error:

from PyQt5.QtWidgets import QMessageBox

msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText('More information')
msg.setWindowTitle("Error")
msg.exec_()
like image 132
NShiell Avatar answered Oct 21 '22 16:10

NShiell


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_()
like image 32
mfitzp Avatar answered Oct 21 '22 15:10

mfitzp


All above options didn't work for me using Komodo Edit 11.0. Just had returned "1" or if not implemented "-1073741819".

Usefull for me was: Vanloc's solution.

def my_exception_hook(exctype, value, traceback):
    # Print the error and traceback
    print(exctype, value, traceback)
    # Call the normal Exception hook after
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook

# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
like image 6
ZF007 Avatar answered Oct 21 '22 16:10

ZF007