简体   繁体   English

为什么我的代码应该工作时却出现属性错误

[英]Why am I getting an Attribute Error for my code when it should be working

I have a class ScrollingCredits.我有一个 class ScrollingCredits。 In that, I have a method load_credits.在那里,我有一个方法 load_credits。 Please have a look at the code请看一下代码

class ScrollingCredits:
    def __init__(self):


        self.load_credits("end_credits.txt")

        (self.background, self.background_rect) = load_image("starfield.gif", True)

        self.font = pygame.font.Font(None, FONT_SIZE)

        self.scroll_speed = SCROLL_SPEED

        self.scroll_pause = SCROLL_PAUSE

        self.end_wait = END_WAIT

        self.reset()

        def load_credits(self, filename):

            f = open(filename)

            credits = []

            while 1:

                line = f.readline()

                if not line:

                    break
            line = string.rstrip(line)

            credits.append(line)

            f.close()

            self.lines = credits

The first line after defining the function is where my attribute problem occurs I get this brought up when I try to run it: AttributeError: 'ScrollingCredits' object has no attribute 'load_credits'定义 function 后的第一行是我的属性问题发生的地方,当我尝试运行它时出现了这个问题: AttributeError: 'ScrollingCredits' object has no attribute 'load_credits'

If anyone would be able to help me it would be much appreciated如果有人能够帮助我,将不胜感激

There is function definition and calling issue for load_credits , if you want to access the function with self Make the load_credits outside the __init__ function like below. load_credits 有load_credits定义和调用问题,如果您想使用self访问 function 在__init__ load_credits之外创建 load_credits,如下所示。

class ScrollingCredits:
    def __init__(self):
        self.load_credits("end_credits.txt")
............

    def load_credits(self, filename):
............

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM