Python PyQt5:如何使用 PyQt5 显示错误消息
Python PyQt5: How to show an error message with PyQt5
在正常情况下 Python (3.x) 我们总是使用 tkinter 模块中的 showerror() 来显示错误消息但是我应该在 PyQt5 中做什么来显示完全相同的消息类型?
以下应该有效:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText(e)
msg.setWindowTitle("Error")
它不是完全相同的消息类型(不同的 GUI),但非常接近。
e
是 python3
中错误的表达式
希望对您有所帮助,
鸣山
Qt 包含一个 error-message specific dialog class QErrorMessage
,您应该使用它来确保您的对话符合系统标准。要显示对话框,只需创建一个对话框对象,然后调用 .showMessage()
。例如:
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Oh no!')
这是一个最小的工作示例脚本:
import PyQt5
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Oh no!')
app.exec_()
以上所有选项对使用 Komodo Edit 11.0 的我都不起作用。刚刚返回“1”或者如果没有实现“-1073741819”。
对我有用的是: 解决方案。
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
别忘了调用.exec_()
显示错误:
from PyQt5.QtWidgets import QMessageBox
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText('More information')
msg.setWindowTitle("Error")
msg.exec_()
要显示消息框,您可以调用此 def:
from PyQt5.QtWidgets import QMessageBox, QWidget
MainClass(QWidget):
def __init__(self):
super().__init__()
def clickMethod(self):
QMessageBox.about(self, "Title", "Message")
假设您在 QWidget 中,您希望从中显示错误消息,您可以简单地使用 QMessageBox.critical(self, "Title", "Message")
,如果您不是 QWidget,则将自己替换为另一个(例如主小部件)class.
在正常情况下 Python (3.x) 我们总是使用 tkinter 模块中的 showerror() 来显示错误消息但是我应该在 PyQt5 中做什么来显示完全相同的消息类型?
以下应该有效:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText(e)
msg.setWindowTitle("Error")
它不是完全相同的消息类型(不同的 GUI),但非常接近。
e
是 python3
希望对您有所帮助, 鸣山
Qt 包含一个 error-message specific dialog class QErrorMessage
,您应该使用它来确保您的对话符合系统标准。要显示对话框,只需创建一个对话框对象,然后调用 .showMessage()
。例如:
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Oh no!')
这是一个最小的工作示例脚本:
import PyQt5
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Oh no!')
app.exec_()
以上所有选项对使用 Komodo Edit 11.0 的我都不起作用。刚刚返回“1”或者如果没有实现“-1073741819”。
对我有用的是:
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
别忘了调用.exec_()
显示错误:
from PyQt5.QtWidgets import QMessageBox
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText('More information')
msg.setWindowTitle("Error")
msg.exec_()
要显示消息框,您可以调用此 def:
from PyQt5.QtWidgets import QMessageBox, QWidget
MainClass(QWidget):
def __init__(self):
super().__init__()
def clickMethod(self):
QMessageBox.about(self, "Title", "Message")
假设您在 QWidget 中,您希望从中显示错误消息,您可以简单地使用 QMessageBox.critical(self, "Title", "Message")
,如果您不是 QWidget,则将自己替换为另一个(例如主小部件)class.