2016-08-13 73 views
-2
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'對象在類中不可迭代__init__定義

我得到的錯誤,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 (most recent call last):

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' object is not iterable

+2

爲什麼不'posX = 0,posY = 0'? – jonrsharpe

+2

發佈完整的ErrorTrace,但我認爲這是有問題的部分:'我在枚舉(self.numButtons):'不是'self.numButtons'和'int'?另外,你的'__init__'方法缺少'self'參數。 –

+0

@jonrsharpe在哪裏? – RyHartAttack

回答

0

您正在嘗試迭代整數。

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