英文:
Very strange AttributeError
问题
I have a class Menu with append_option Method:
class Menu:
def __init__(self):
self._option_surfaces = []
self._callbacks = []
self._current_option_index = 0
def append_option(self, option, callback, font):
self._option_surfaces.append(font.render(option, True, (255, 255, 255)))
self._callbacks.append(callback)
I create an instance of this class:
menu = Menu()
font = pygame.font.Font('Rostov.ttf', 60)
menu.append_option('Play', lambda: print('Oh hell nah man'), font)
But the python has a different opinion:
menu.append_option('Play', lambda: print('Oh hell nah man'), font)
^^^^^^^^^^^^^^^^^^
AttributeError: 'Menu' object has no attribute 'append_option'
I tried to rename the method and replace lines, but nothing changed.
How can I solve this problem?
英文:
I have a class Menu with append_option Method:
class Menu:
def __init__(self):
self._option_surfaces = []
self._callbacks = []
self._current_option_index = 0
def append_option(self, option, callback, font):
self._option_surfaces.append(font.render(option, True, (255, 255, 255)))
self._callbacks.append(callback)
I create an instance of this class.
menu = Menu()
font = pygame.font.Font('Rostov.ttf', 60)
menu.append_option('Play', lambda: print('Oh hell nah man'), font)
But the python has a different opinion
menu.append_option('Play', lambda: print('Oh hell nah man'), font)
^^^^^^^^^^^^^^^^^^
AttributeError: 'Menu' object has no attribute 'append_option'
I tried to rename method, replace lines but nothing changed.
How can I solve this problem?
答案1
得分: 1
I ran your code as follows, and I didn't get any errors:
import pygame
pygame.init()
class Menu:
def __init__(self):
self._option_surfaces = []
self._callbacks = []
self._current_option_index = 0
def append_option(self, option, callback, font):
self._option_surfaces.append(font.render(option, True, (255, 255, 255)))
self._callbacks.append(callback)
menu = Menu()
menu.append_option('Play', lambda: print('Oh, hell nah man'), pygame.font.Font('Rostov.ttf', 60))
Check your IDE, or something else is wrong.
英文:
I ran your code as follows, and I didn't get any errors:
import pygame
pygame.init()
class Menu:
def __init__(self):
self._option_surfaces = []
self._callbacks = []
self._current_option_index = 0
def append_option(self, option, callback, font):
self._option_surfaces.append(font.render(option, True, (255, 255, 255)))
self._callbacks.append(callback)
menu = Menu()
menu.append_option('Play', lambda: print('Oh, hell nah man'), pygame.font.Font('Rostov.ttf', 60))
Check your IDE, or something else is wrong.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论