• <legend id='TVnGK'><style id='TVnGK'><dir id='TVnGK'><q id='TVnGK'></q></dir></style></legend>
      <bdo id='TVnGK'></bdo><ul id='TVnGK'></ul>
    <tfoot id='TVnGK'></tfoot>
      <i id='TVnGK'><tr id='TVnGK'><dt id='TVnGK'><q id='TVnGK'><span id='TVnGK'><b id='TVnGK'><form id='TVnGK'><ins id='TVnGK'></ins><ul id='TVnGK'></ul><sub id='TVnGK'></sub></form><legend id='TVnGK'></legend><bdo id='TVnGK'><pre id='TVnGK'><center id='TVnGK'></center></pre></bdo></b><th id='TVnGK'></th></span></q></dt></tr></i><div id='TVnGK'><tfoot id='TVnGK'></tfoot><dl id='TVnGK'><fieldset id='TVnGK'></fieldset></dl></div>
    1. <small id='TVnGK'></small><noframes id='TVnGK'>

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

        时间:2023-08-04
        <tfoot id='pE548'></tfoot>

          • <small id='pE548'></small><noframes id='pE548'>

              <bdo id='pE548'></bdo><ul id='pE548'></ul>
                <tbody id='pE548'></tbody>
              <legend id='pE548'><style id='pE548'><dir id='pE548'><q id='pE548'></q></dir></style></legend>

                • <i id='pE548'><tr id='pE548'><dt id='pE548'><q id='pE548'><span id='pE548'><b id='pE548'><form id='pE548'><ins id='pE548'></ins><ul id='pE548'></ul><sub id='pE548'></sub></form><legend id='pE548'></legend><bdo id='pE548'><pre id='pE548'><center id='pE548'></center></pre></bdo></b><th id='pE548'></th></span></q></dt></tr></i><div id='pE548'><tfoot id='pE548'></tfoot><dl id='pE548'><fieldset id='pE548'></fieldset></dl></div>
                  本文介绍了装饰器添加了一个意想不到的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  这篇关于装饰器添加了一个意想不到的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在右侧 cilck 上的 Qdoublespinbox 上向 QtCore.Qt. 下一篇:从不同文件中的其他类更改标签

                  相关文章

                  最新文章

                  <i id='I8sgf'><tr id='I8sgf'><dt id='I8sgf'><q id='I8sgf'><span id='I8sgf'><b id='I8sgf'><form id='I8sgf'><ins id='I8sgf'></ins><ul id='I8sgf'></ul><sub id='I8sgf'></sub></form><legend id='I8sgf'></legend><bdo id='I8sgf'><pre id='I8sgf'><center id='I8sgf'></center></pre></bdo></b><th id='I8sgf'></th></span></q></dt></tr></i><div id='I8sgf'><tfoot id='I8sgf'></tfoot><dl id='I8sgf'><fieldset id='I8sgf'></fieldset></dl></div>
                  • <bdo id='I8sgf'></bdo><ul id='I8sgf'></ul>

                    <small id='I8sgf'></small><noframes id='I8sgf'>

                      <legend id='I8sgf'><style id='I8sgf'><dir id='I8sgf'><q id='I8sgf'></q></dir></style></legend>
                    1. <tfoot id='I8sgf'></tfoot>