Decorator adds an unexpected argument(装饰器添加了一个意想不到的参数)
问题描述
我想在我的 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!")
运行时出现以下异常:
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?
推荐答案
问题是因为triggered
信号超载,也就是说它有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)
所以默认情况下它会发送一个 boolean(false) 显然不接受导致错误的有趣"方法.
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!")
这篇关于装饰器添加了一个意想不到的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:装饰器添加了一个意想不到的参数
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01