Python 'called with the wrong argument type' error

こ雲淡風輕ζ 提交于 2020-05-23 21:36:10

问题


I understand why I am getting this error, it's looking for my object as an argument, and receiving a string value. But I'm confused as to what the solution would be?

The following code snippet is simply trying to run this command;

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

x number of times:

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

回答1:


The problem is that button is a string, a possible solution is to use getattr.

Change:

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

to

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


来源:https://stackoverflow.com/questions/49020267/python-called-with-the-wrong-argument-type-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!