【问题标题】:PyQt5 - TypeError "NoneType" when using *args w/o functoolsPyQt5 - 使用 *args w/o functools 时出现类型错误“NoneType”
【发布时间】:2017-11-20 01:20:08
【问题描述】:

在我的 maingui (PyQt5) 脚本中使用 *args 时出现两次错误,不知道为什么。我在做什么不是 pythonic/pyqt5 风格的?

第一次尝试:

从 GUI 中调用时出现错误的代码行:

def createActions(self):
    ...
    self.lineEdit.returnPressed.connect(self.updateUi('buttons'))

错误:

Traceback...(snippet)
...
self.lineEdit.returnPressed.connect(self.updateUi('buttons'))
TypeError: argument 1 has unexpected type 'NoneType'

它可能不是一个函数...所以我将其更改为我的第二次尝试,正如 Mark Summerfield 在 RapidGUI Programming with python and PyQt 中所述,第 133 页:

def createActions(self):
    ...
    self.lineEdit.returnPressed.connect(functools.partial(self.updateUi, 'buttons'))

 def updateUi(self, *args):

        if args == 'color_update':
            color = self.colorCh2comboBox.currentText()

            if color == 'Violet':
                print '%s is purple' % color
            else:
                print color

        elif args== 'buttons':

            try:
                print 'yes'
                ...
            except:
                print "no"
                ...
        else:
            print "Unknown action : %s" % args

结果:“未知操作:按钮

在我尝试的第三次尝试中:

def updateUi(self, *args):

    argument = str(args)   # just to be sure I'll parse a string and not something else.
    ...etc... # as above.

还能是什么?

【问题讨论】:

    标签: python-2.7 pyqt5


    【解决方案1】:

    那么我接下来做了什么...与type 核对args 的情况。

    我的代码:

    def updateUi(self, *args):
    
            argument = str(args)
            print type(argument), type(args)
    

    结果:

    <type 'str'> <type 'tuple'>
    yes  # from the   try... print 'yes'
    no   # from the except.. print 'no'
    

    所以..args 是一个元组(doh!)。

    代码修改如下,现在可以使用了:-)

    def updateUi(self, *args):
    
            argument = str(args)
            print type(argument), type(args)
    
            for item in args:
                if color == 'Violet':
                    color = ...etc.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多