问题描述
我想使用装饰器处理我的PyQt5应用程序中的异常:
I wanted to use a decorator to handle exceptions in my PyQt5 application:
def handle_exceptions(func):
def func_wrapper(*args, **kwargs):
try:
print(args)
return func(*args, **kwargs)
except Exception as e:
print(e)
return None
return func_wrapper
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("main_window.ui",self)
self.connect_signals()
def connect_signals(self):
self.menu_action.triggered.connect(self.fun)
@handle_exceptions
def fun(self):
print("hello there!")
运行时,出现以下异常:
When I run I get the following exception:
fun() takes 1 positional argument but 2 were given
输出为 False
(装饰器中打印的args).
The output is False
(printed args in the decorator).
有趣的是,当我通过构造函数中的 self.fun()
直接运行 fun()
函数或对装饰器进行注释时,一切正常.似乎装饰器添加了一个附加参数,但仅在信号调用该函数时才添加.发生了什么事?
The interesting thing is that when I run the fun()
function directly by self.fun()
in the constructor or comment the decorator, everything works. Seems like the decorator adds an additional argument, but only when the function is called by the signal. What is going on?
推荐答案
由于 已触发
信号过载,也就是说,它具有2个签名:
The problem is caused because the triggered
signal is overload, that is to say it has 2 signatures:
void QAction::triggered(bool checked = false)
QAction.triggered()
QAction.triggered(bool checked)
因此,默认情况下,它会发送一个布尔值(false),该布尔值显然不接受导致错误的"fun"方法.
So by default it sends a boolean(false) that clearly does not accept the "fun" method causing the error.
在这种情况下,解决方案是使用 @pyqtSlot()
装饰器以指示您必须接受的签名:
In this case the solution is to use the @pyqtSlot()
decorator to indicate the signature that you must accept:
@pyqtSlot()
@handle_exceptions
def fun(self):
print("hello there!")
这篇关于装饰器添加了意外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!