Python PyQt5: How to show an error message with PyQt5
Question:
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?
Answers:
The following should work:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText(e)
msg.setWindowTitle("Error")
It is not the exact same message type (different GUI’s) but fairly close.
e
is the expression for an Error in python3
Hope that helped,
Narusan
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_()
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
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_()
To show a message box, you can call this def:
from PyQt5.QtWidgets import QMessageBox, QWidget
MainClass(QWidget):
def __init__(self):
super().__init__()
def clickMethod(self):
QMessageBox.about(self, "Title", "Message")
Assuming you are in a QWidget from which you want to display an error message, you can simply use QMessageBox.critical(self, "Title", "Message")
, replace self by another (main widget for example) if you are not is a QWidget class.
Edit: even if you are not in a QWidget (or don’t want to inherit from it), you can just use None as parent with for instance QMessageBox.critical(None, "Title", "Message")
.
Edit, here is an example of how to use it:
# -*-coding:utf-8 -*
from PyQt5.QtWidgets import QApplication, QMessageBox
import sys
# In this example, success is False by default, and
# - If you press Cancel, it will ends with False,
# - If you press Retry until i = 3, it will end with True
expectedVal = 3
def MyFunction(val: int) -> bool:
return val == expectedVal
app = QApplication(sys.argv)
i = 1
success = MyFunction(i)
while not success:
# Popup with several buttons, manage below depending on choice
choice = QMessageBox.critical(None,
"Error",
"i ({}) is not expected val ({})".format(i, expectedVal),
QMessageBox.Retry | QMessageBox.Cancel)
if choice == QMessageBox.Retry:
i += 1
print("Retry with i = {}".format(i))
success = MyFunction(i)
else:
print("Cancel")
break
if success:
# Standard popup with only OK button
QMessageBox.information(None, "Result", "Success is {}".format(success))
else:
# Standard popup with only OK button
QMessageBox.critical(None, "Result", "Success is {}".format(success))
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?
The following should work:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText(e)
msg.setWindowTitle("Error")
It is not the exact same message type (different GUI’s) but fairly close.
e
is the expression for an Error in python3
Hope that helped,
Narusan
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_()
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
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_()
To show a message box, you can call this def:
from PyQt5.QtWidgets import QMessageBox, QWidget
MainClass(QWidget):
def __init__(self):
super().__init__()
def clickMethod(self):
QMessageBox.about(self, "Title", "Message")
Assuming you are in a QWidget from which you want to display an error message, you can simply use QMessageBox.critical(self, "Title", "Message")
, replace self by another (main widget for example) if you are not is a QWidget class.
Edit: even if you are not in a QWidget (or don’t want to inherit from it), you can just use None as parent with for instance QMessageBox.critical(None, "Title", "Message")
.
Edit, here is an example of how to use it:
# -*-coding:utf-8 -*
from PyQt5.QtWidgets import QApplication, QMessageBox
import sys
# In this example, success is False by default, and
# - If you press Cancel, it will ends with False,
# - If you press Retry until i = 3, it will end with True
expectedVal = 3
def MyFunction(val: int) -> bool:
return val == expectedVal
app = QApplication(sys.argv)
i = 1
success = MyFunction(i)
while not success:
# Popup with several buttons, manage below depending on choice
choice = QMessageBox.critical(None,
"Error",
"i ({}) is not expected val ({})".format(i, expectedVal),
QMessageBox.Retry | QMessageBox.Cancel)
if choice == QMessageBox.Retry:
i += 1
print("Retry with i = {}".format(i))
success = MyFunction(i)
else:
print("Cancel")
break
if success:
# Standard popup with only OK button
QMessageBox.information(None, "Result", "Success is {}".format(success))
else:
# Standard popup with only OK button
QMessageBox.critical(None, "Result", "Success is {}".format(success))