Catching exceptions raised in QApplication(捕获 QApplication 中引发的异常)
问题描述
我正在尝试使用 PyQt5 编写一个在系统托盘中运行的应用程序.代码有时会引发异常,我需要能够捕获它们.
I'm trying to write an app that works in system tray using PyQt5. The code is sometimes raising exceptions, and I need to be able to catch them.
我希望当应用程序中发生异常时,主事件循环会退出,所以像这样捕获它应该可以工作:
I would expect that when an exception occurs in an app, the main event loop is exited, so catching it like that should work:
try:
application.exec()
except:
do_stuff()
在下面的例子中,当我按下Raise"按钮时,我只看到回溯,但我从来没有看到 error catched!
打印出来.
In the following example, when I press the "Raise" button, I only see the traceback, but I never see the error catched!
printed.
from PyQt5 import QtWidgets, QtGui, QtCore
class ErrorApp():
def __init__(self):
# Init QApplication, QWidet and QMenu
self.app = QtWidgets.QApplication([])
self.widget = QtWidgets.QWidget()
self.menu = QtWidgets.QMenu("menu", self.widget)
# Add items to menu
self.menu_action_raise = self.menu.addAction("Raise")
self.menu_action_raise.triggered.connect(self.raise_error)
self.menu_action_exit = self.menu.addAction("Exit")
self.menu_action_exit.triggered.connect(self.app.exit)
# Create the tray app
self.tray = QtWidgets.QSystemTrayIcon(QtGui.QIcon("logo.png"), self.widget)
self.tray.setContextMenu(self.menu)
# Show app
self.tray.show()
def raise_error(self):
assert False
e = ErrorApp()
try:
e.app.exec()
except:
print("error catched!")
有 2 个类似的问题,但那里的答案没有做我需要做的事情:
There are 2 similar questions, but the answers there don't do what I need to do:
抓取 PyQt 中的任何异常:OP 想要监控 异常,偶数循环不退出防止 PyQt 静默插槽中发生的异常:装饰器回答根本行不通;将 sys.exit(1)
添加到 sys.excepthook
只会关闭整个程序,而不会打印 error catched!
Grab any exception in PyQt: the OP wants to monitor the exceptions, the even loop isn't exited
Preventing PyQt to silence exceptions occurring in slots: the decorator answer simply doesn't work; adding sys.exit(1)
to sys.excepthook
just closes the whole program, without printing error catched!
推荐答案
你必须使用异常,如果你想要事件循环结束,那么你必须调用 quit()
(或 exit()
) 方法.
You must use the exception and, if you want the event loop to end then you must call the quit()
(or exit()
) method.
import sys
import traceback
from PyQt5 import QtWidgets, QtGui, QtCore
class ErrorApp:
# ...
def raise_error(self):
assert False
def excepthook(exc_type, exc_value, exc_tb):
tb = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
print("error catched!:")
print("error message:
", tb)
QtWidgets.QApplication.quit()
# or QtWidgets.QApplication.exit(0)
sys.excepthook = excepthook
e = ErrorApp()
ret = e.app.exec_()
print("event loop exited")
sys.exit(ret)
输出:
error catched!:
error message:
Traceback (most recent call last):
File "main.py", line 28, in raise_error
assert False
AssertionError
event loop exited
这篇关于捕获 QApplication 中引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:捕获 QApplication 中引发的异常
基础教程推荐
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01