PyQt:退出时没有错误消息(回溯)
PyQt: No error msg (traceback) on exit
我的 PyQt 应用程序不再将错误(stderr?)打印到控制台。
我使用 QtDesigner 并像这样导入 UI:
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(这显然会出错)时,我会收到错误消息、回溯等。当我执行这段代码时,它就退出了。
我之前在没有 QtDesigner 的情况下编写了一个 PyQt 应用程序,它按预期将错误打印到控制台。 QtDesigner和继承有什么区别?
这可能是由于 PyQt-5.5 中处理异常的方式发生了变化。引用自PyQt5 Docs:
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.
当我在普通控制台中 运行 你的例子时,这是我看到的:
$ 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.
全局控制此行为
如果您没有看到任何追溯,这可能是由于您用于 运行 您的应用程序的 Python IDE 存在问题。
PS:
作为最低限度,简单地将回溯打印到 stdout/stderr 的旧 PyQt4 行为可以像这样恢复:
def except_hook(cls, exception, traceback):
sys.__excepthook__(cls, exception, traceback)
if __name__ == "__main__":
import sys
sys.excepthook = except_hook
我的 PyQt 应用程序不再将错误(stderr?)打印到控制台。
我使用 QtDesigner 并像这样导入 UI:
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(这显然会出错)时,我会收到错误消息、回溯等。当我执行这段代码时,它就退出了。
我之前在没有 QtDesigner 的情况下编写了一个 PyQt 应用程序,它按预期将错误打印到控制台。 QtDesigner和继承有什么区别?
这可能是由于 PyQt-5.5 中处理异常的方式发生了变化。引用自PyQt5 Docs:
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.
当我在普通控制台中 运行 你的例子时,这是我看到的:
$ 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.
如果您没有看到任何追溯,这可能是由于您用于 运行 您的应用程序的 Python IDE 存在问题。
PS:
作为最低限度,简单地将回溯打印到 stdout/stderr 的旧 PyQt4 行为可以像这样恢复:
def except_hook(cls, exception, traceback):
sys.__excepthook__(cls, exception, traceback)
if __name__ == "__main__":
import sys
sys.excepthook = except_hook