非常奇怪的 AttributeError

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月6日 01:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定