PyQt:退出时没有错误消息(回溯) [英] PyQt: No error msg (traceback) on exit

查看:52
本文介绍了PyQt:退出时没有错误消息(回溯)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PyQt 应用程序不再将错误(stderr?)打印到控制台.

My PyQt application no longer prints the error (stderr?) to the console.

我使用 QtDesigner 并像这样导入 UI:

I use QtDesigner and import the UI like this:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.uic import loadUiType
Ui_MainWindow, QMainWindow = loadUiType("test.ui")

class Main(QMainWindow, Ui_MainWindow):
    """Main window"""
    def __init__(self,parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.testfunc)

   def testfunc(self):
        print(9/0)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

test.ui 包含一个 QPushButton 和一个标签.当我在非 Qt 应用程序中调用 testfunc(显然会出现错误)时,我收到错误消息、回溯等.当我执行此代码时,它只是退出.

test.ui contains a QPushButton and a label. When I call testfunc (which obviously gives an error) in a non-Qt application, I get the error message, traceback, etc. When I execute this code, it just exits.

我之前编写了一个没有 QtDesigner 的 PyQt 应用程序,它按预期将错误打印到控制台.QtDesigner 和继承有什么区别?

I wrote a PyQt application without QtDesigner before and it printed the errors to the console as expected. What's the difference with QtDesigner and inheritance?

推荐答案

这可能是由于 PyQt-5.5 中处理异常的方式发生了变化.引用 PyQt5 文档:

This is probably due to changes in the way exceptions are dealt with in PyQt-5.5. To quote from the PyQt5 Docs:

在 PyQt v5.5 中,未处理的 Python 异常将导致调用Qt 的 qFatal() 函数.默认情况下,这将调用 abort() 和应用程序将终止.请注意,安装了一个应用程序异常钩子仍然优先.

In PyQt v5.5 an unhandled Python exception will result in a call to Qt’s qFatal() function. By default this will call abort() and the application will terminate. Note that an application installed exception hook will still take precedence.

当我在普通控制台中运行您的示例时,我看到的是:

When I run your example in a normal console, this is what I see:

$ python test.py
Traceback (most recent call last):
  File "test.py", line 213, in testfunc
    print(9/0)
ZeroDivisionError: division by zero
Aborted (core dumped)

因此,主要区别在于应用程序现在在遇到未处理的异常时会立即中止(即就像普通的 Python 脚本一样).当然,您仍然可以通过使用 try/except 块或通过覆盖 sys.excepthook.

So the main difference is that the application will now immediately abort when encountering an unhandled exception (i.e. just like a normal python script would). Of course, you can still control this behaviour by using a try/except block or globally by overriding sys.excepthook.

如果您没有看到任何回溯,这可能是由于您用来运行应用程序的 Python IDE 存在问题.

If you're not seeing any traceback, this may be due to an issue with the Python IDE you're using to run your application.

附注:

作为最低限度,简单地将回溯打印到 stdout/stderr 的旧 PyQt4 行为可以像这样恢复:

As a bare minimum, the old PyQt4 behaviour of simply printing the traceback to stdout/stderr can be restored like this:

def except_hook(cls, exception, traceback):
    sys.__excepthook__(cls, exception, traceback)

if __name__ == "__main__":

    import sys
    sys.excepthook = except_hook

这篇关于PyQt:退出时没有错误消息(回溯)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆