Python 'called with the wrong argument type' 错误

Python 'called with the wrong argument type' error

我明白为什么会收到此错误,它正在寻找我的对象作为参数,并接收到一个字符串值。但我对解决方案感到困惑?

下面的代码片段只是试图运行这个命令;

self.buttonGroup.addButton(self.ui.m001)

x 次数:

num = 0
range_ = 10
prefix = "m"

for i in range (range_):
    if num <(range_-1):
        numString = "00"+str(num)
        if (num >9):
            numString = "0"+str(num)

        button = "self.ui."+prefix+numString

        self.buttonGroup.addButton(button)
        num +=1

print self.buttonGroup

问题是按钮是一个字符串,一个可能的解决方案是使用getattr

变化:

button = "self.ui."+prefix+numString

button = getattr(self.ui, prefix+numString)