【问题标题】:'int' object is not iterable in class __init__ definition'int' 对象在类 __init__ 定义中不可迭代
【发布时间】:2016-08-13 18:25:16
【问题描述】:
class MenuItem():
def __init__(self, width, height, (posX, posY)=(0,0)):
    #self.label = self.render(self.text, 1, self.font_color)
    self.width = width
    self.height = height
    self.posX = posX
    self.posY = posY
    self.position = posX, posY

def set_position(self, x, y):
    self.position = (x, y)
    self.posX = x
    self.posY = y

def is_mouse_selection(self, (posx, posy)):
    if (posx >= self.posX and posx <= self.posX + self.width) and (posy >= self.posY and posy <= self.posY + self.height):
        return True
    return False

def draw(self):
    raise MissingFunction("Child of MenuItem class has no draw function")    

class RadioButton(MenuItem):
def __init__(width, (posX, posY) = (0, 0)):
    super(RadioButton, self).__init__(width, width, (posX, posY))
    self.active = False

def draw(screen):
    pygame.draw.rect(screen. GREY, pygame.Rect(posX, posY, self.width, self.height))
    if self.active:
        pygame.draw.circle(screen, BLACK, (posX + (self.width / 2), posY + (self.height / 2)), self.width)
    else:
        pygame.draw.circle(screen, BLACK, (posX + (self.width / 2), posY + (self.height / 2)), self.width, 1)

class RadioFrame(MenuItem):
def __init__(numButtons, buttonWidth, (posX, posY)=(0, 0)):
    super(RadioFrame, self).__init__(numButtons * buttonWidth, width)
    self.set_position(posX, posY)
    self.numButtons = numButtons
    self.buttons = []
    self.activeButton = None
    curX = self.posX
    for i in enumerate(self.numButtons):
        self.buttons.append(RadioButton(buttonWidth, (curX, self.posY)))
        curX += buttonWidth

我正在尝试创建我的 MenuItem 类的子类 RadioFrame,其中包含 RadioButton 对象的列表。我已经有其他以类似方式初始化的成功运行的子节点。

我得到了 int 对象不可迭代的错误,我知道,但我很难看到我在哪里犯了这个错误。它被触发: line 44, in __init__ def __init__(numButtons, buttonWidth, (posX, posY)=(0, 0)):

对于更多上下文,这里是我创建类的新实例的地方: numRadios = 2 # 2 帧 索引 = 0

    # First Num Players
    ## PosX = middle of screen width - middle of (numButtons * buttonWidth) [aka RadioFrame width]
    ## PosY = middle of screen height - middle of (buttonWidth) [aka RadioFrame height]
    playerRadio = RadioFrame(3, 50)
    posx = (self.scr_width / 2) - ((50 * 3) / 2)
    posy = (self.scr_height / 2) - (50 / 2) + ((index * 2) + index * 50) # Copied from text button position, index starts at zero
    playerRadio.set_position(posx, posy)
    self.items.append(playerRadio)
    index += 1

完整跟踪:

Traceback(最近一次通话最后一次):

 File "./main.py", line 61, in <module>

   2: PreGameMenu(screen, clock, preGameFuncs.keys(), preGameFuncs),

 File "/home/rhartman/Documents/pitch/menus.py", line 113, in __init__

   playerRadio = RadioFrame(3, 50)

 File "/home/rhartman/Documents/pitch/menus.py", line 44, in __init__

   def __init__(numButtons, buttonWidth, (posX, posY)=(0, 0)):

TypeError: 'int' 对象不可迭代

【问题讨论】:

  • 为什么不posX=0, posY=0
  • 发布完整的ErrorTrace,但我认为这是有问题的部分:` for i in enumerate(self.numButtons):` 不是self.numButtonsint 吗?此外,您的 __init__ 方法缺少 self 参数。
  • @jonrsharpe 在哪里?
  • @juanpa.arrivillaga self.numButtons 是一个整数,我最初有 for i in range(0, numButtons) 但在尝试调试时更改了它,具有讽刺意味的是,我可能已经倒退了。将 self 添加到 init 函数确实改变了错误。 super(RadioFrame, self).__init__(numButtons * buttonWidth, width) TypeError: must be type, not classobj
  • 在方法的定义中。如果您有要添加的代码,请使用edit,因为在 cmets 中很难阅读。

标签: python class pygame


【解决方案1】:

您正在尝试迭代一个整数。

class RadioFrame(MenuItem):
    def __init__(numButtons, buttonWidth, (posX, posY)=(0, 0)):
        super(RadioFrame, self).__init__(numButtons * buttonWidth, width)
        self.set_position(posX, posY)
        self.numButtons = numButtons
        self.buttons = []
        self.activeButton = None
        curX = self.posX
        for i in enumerate(self.numButtons):
            self.buttons.append(RadioButton(buttonWidth, (curX, self.posY)))
            curX += buttonWidth

playerRadio = RadioFrame(3, 50)

根据您的代码self.numButtons 是一个整数3。然后您尝试在for i in enumerate(self.numButtons): 这一行中迭代它。应该是for i in xrange(self.numButtons):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多