装饰器添加了一个意想不到的参数

2022-01-12 00:00:00 python pyqt pyqt5 decorator

问题描述

我想在我的 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!")

相关文章