简体   繁体   中英

'Button' object has no attribute 'prep_msg'

Working through python crash course, and for some reason I cannot figure out what I have done wrong at this portion of the first project, Alien invasion. It says that the Button object has no attribute 'prep_msg'. any help would be appreciated!

Attached is the traceback and module:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "alien_invasion.py", line 63, in <module>
    run_game()
  File "alien_invasion.py", line 25, in run_game
    play_button = Button(ai_settings, screen, "Play")
  File "C:\Users\eslog\OneDrive\Desktop\alien_invasion\button.py", line 21, in __init__
    self.prep_msg(msg)
AttributeError: 'Button' object has no attribute 'prep_msg'

and button mod

import pygame.font

class Button():

    def __init__(self, ai_settings, screen, msg):
        """initialize button attrributes"""
        self.screen = screen
        self.screen_rect = screen.get_rect()

        #set the dimesions and properties of the button
        self.width, self.height = 200, 50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48)

        #build the button's rect object and center it
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        #the button message needs to be prepped only once
        self.prep_msg(msg)

        def prep_msg(self, msg):
            """turn msg into a rendered image and center text on the button"""
            self.msg_image = self.font.render(msg, True, self.text_color,
                                              self.button_color)
            self.msg_image_rect = self.msg_image.get_rect()
            self.msg_image.center = self.rect.center


        def draw_button(self):
            #draw blank button and then draw message
            self.screen.fill(self.button_color, self.rect)
            self.screen.blit(self.msg_image, self.msg_image_rect)

Looks like you meant for prep_msg() to be a method of the Button class. The problem is your indentation. both the prep_msg() and draw_button() methods are indented inside the init() method, making them essentially nested functions.

This should fix the problem:

import pygame.font

class Button():

    def __init__(self, ai_settings, screen, msg):
        """initialize button attrributes"""
        self.screen = screen
        self.screen_rect = screen.get_rect()

        #set the dimesions and properties of the button
        self.width, self.height = 200, 50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48)

        #build the button's rect object and center it
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        #the button message needs to be prepped only once
        self.prep_msg(msg)

    def prep_msg(self, msg):
        """turn msg into a rendered image and center text on the button"""
        self.msg_image = self.font.render(msg, True, self.text_color,
                                          self.button_color)
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image.center = self.rect.center


    def draw_button(self):
        #draw blank button and then draw message
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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