Python PyQt5:如何使用 PyQt5 显示错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40227047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:19:24  来源:igfitidea点击:

Python PyQt5: How to show an error message with PyQt5

pythonmessagemessageboxpyqt5

提问by Ramón Wilhelm

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?

在普通的 Python (3.x) 中,我们总是使用 tkinter 模块中的 showerror() 来显示错误消息,但是在 PyQt5 中我应该怎么做才能显示完全相同的消息类型?

回答by mfitzp

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

Qt 包含一个错误消息特定对话框类QErrorMessage,您应该使用它来确保您的对话框符合系统标准。要显示对话框,只需创建一个对话框对象,然后调用.showMessage(). 例如:

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_()

回答by NShiell

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

不要忘记调用.exec_()以显示错误:

from PyQt5.QtWidgets import QMessageBox

msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText('More information')
msg.setWindowTitle("Error")
msg.exec_()

回答by ZF007

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

以上所有选项对我使用 Komodo Edit 11.0 都不起作用。刚刚返回“1”或者如果没有实现“-1073741819”。

Usefull for me was: Vanloc'ssolution.

对我有用的是:Vanloc 的解决方案。

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

回答by Karam Qusai

To show a message box, you can call this def:

要显示消息框,您可以调用此 def:

from PyQt5.QtWidgets import QMessageBox, QWidget

MainClass(QWidget):
    def __init__(self):
        super().__init__()

    def clickMethod(self):
        QMessageBox.about(self, "Title", "Message")

回答by Narusan

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. eis the expression for an Error in python3

它不是完全相同的消息类型(不同的 GUI),但相当接近。 e是python3中错误的表达式

Hope that helped, Narusan

希望有帮助,Narusan

回答by gluttony

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.

假设您在要显示错误消息QMessageBox.critical(self, "Title", "Message")的 QWidget 中,如果您不是 QWidget 类,则可以简单地使用, 将 self 替换为另一个(例如主小部件)。