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帧 index = 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
完整追踪:
追踪(最近一次呼叫最后一次):
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:&#39; int&#39;对象不可迭代
答案 0 :(得分: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):
。