本文介绍了Python“使用错误的参数类型调用"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白为什么我会收到这个错误,它正在寻找我的对象作为参数,并接收一个字符串值.但我不知道解决方案是什么?

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 次:

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

推荐答案

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

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

变化:

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

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

这篇关于Python“使用错误的参数类型调用"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:57