Michael G
Michael G

Reputation: 1

calling a class in some functions is working but in others it gives an error, even though its called the same way

I am trying to make menu screens for a pygame project and have made a class so I can create buttons.


class button():

    def __init__(self, colour, x, y, width, height, text = ""):
        self.colour = colour
        self.x = x        
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        
    def draw_button(self, screen):
        pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height), 0)
        
        button_text = font.render(self.text, True, black)
        
        screen.blit(button_text, (self.x + (self.width/2 - button_text.get_width()/2), self.y + (self.height/2 - button_text.get_height()/2)))
        
    def over_button(self,pos):
        ##pos is the co ordinates of the mouse
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True            
        return False

the class is callable in all my other functions apart from when i try to move between these two

two different menus

def lb_menu():
    running = True
    while running:
        clock.tick(30)
        print("4")
        option = 0

        pygame.display.update()
        
        ##fills the screen in white
        screen.fill(alice_blue)

        ##calls the draw text function
        draw_text("Leaderboards", title_font, auro_metal_saurus, screen, screen_width/2, screen_width/10)

        
        ##creates the buttons
        lb_easy = button(air_force_blue, (screen_width-300)/2, screen_height*0.25, 300, 50, "Leaderboard - Easy")
        lb_medium = button(air_force_blue, ((screen_width-300)/2), screen_height*0.4, 300, 50, "Leaderboard - Medium")
        lb_hard = button(air_force_blue, ((screen_width-300)/2), screen_height*0.55, 300, 50, "Leaderboard - Hard")
        button_back = button(air_force_blue, ((screen_width-300)/2), screen_height*0.7, 300, 50, "Back")

        #draws the buttons
        
        lb_easy.draw_button(screen)
        lb_medium.draw_button(screen)
        lb_hard.draw_button(screen)
        button_back.draw_button(screen)

        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
                
            if event.type == pygame.MOUSEBUTTONDOWN:
                if lb_easy.over_button(pos):
                    option = 1
                    running = False
                    

                elif button_back.over_button(pos):
                    option = 4
                    running = False
                    

    if option == 1:
        lb_easy()
    elif option == 2:
        pass
    elif option == 3:
        pass
    elif option == 4:
        main_menu()

    
                
def lb_easy():
    running = True
    while running:
        clock.tick(30)
        print("5")
        pygame.display.update()
        
        ##fills the screen in white
        screen.fill(alice_blue)

        ##calls the draw text function
        draw_text("Leaderboard - Easy", title_font, auro_metal_saurus, screen, screen_width/2, screen_width/10)
        
        ##creates the buttons
        button_back = button(air_force_blue, ((screen_width-300)/2), screen_height*0.7, 300, 50, "Back")

        #draws the buttons

        button_back.draw_button(screen)

        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
                
            if event.type == pygame.MOUSEBUTTONDOWN:
                if button_back.over_button(pos):
                    running = False
    lb_menu()

'''Traceback (most recent call last): File "D:\ProgrammingProject\minesweeper.py", line 351, in main_menu() File "D:\ProgrammingProject\minesweeper.py", line 151, in main_menu lb_menu() File "D:\ProgrammingProject\minesweeper.py", line 308, in lb_menu lb_easy() TypeError: 'create_button' object is not callable'''

this is the error message i am receiving, It only happens when i go two menus deep

Upvotes: -1

Views: 54

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

You have used lb_easy both as the name of a button, and the name of a function. It can't be both of those things, whichever one was most recently assigned overwrites the previous value.

Upvotes: 1

Related Questions