Home > Software engineering >  How do I change the text in a Button class for Pygame?
How do I change the text in a Button class for Pygame?

Time:12-20

I've made a Button class for my game and I wanted to keep assinging different texts from a file to the displayed buttons whenever I click on them, but I can't even get the text to change after assinging the initial assignment. Whenever I run b.text = "New text" and print it out, the console does display the new text, but the game window still blits out "placeholder text".

The code:

import pygame, sys
pygame.init()

screen = pygame.display.set_mode((900,600),0,32) 
clock = pygame.time.Clock() 
font = pygame.font.Font('freesansbold.ttf', 32) 


class Button():
    def __init__(self, text, x, y):
        self.text = text       
        self.render = font.render(self.text, True, 'white')
        self.text_width = self.render.get_width()
        self.text_height = self.render.get_height()
        
        self.box = pygame.Surface((self.text_width, self.text_height)) 
        self.box.set_alpha(0) 
        screen.blit(self.box, (x, y)) 

        self.rect = self.render.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False
        
        
    def draw(self):
        screen.blit(self.box, (self.rect.x, self.rect.y))
        screen.blit(self.render, (self.rect.x, self.rect.y))   
        
        pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(pos):
            self.box.set_alpha(100) 
            self.box.fill((255, 255, 255))
            
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
        
            if pygame.mouse.get_pressed()[0] == 0:
                self.clicked = False
        else:
            self.box.set_alpha(0) 
            
        return self.clicked


b = Button("placeholder text", 100, 100)

while True:
    clock.tick(30)  

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit() 
            sys.exit() 
        

    screen.fill('blue')
    b.text = "new text"
    b.draw()

    pygame.display.update()

CodePudding user response:

As @Rabbid76 said:

You have to render the text again

What you can also do is use @property decorator for easier changing text:

class Button():
    render: pygame.font.Font
    text_width: int
    text_height: int
    box: pygame.Surface
    rect: pygame.Rect


    def __init__(self, text, x, y):
        self._text = ""
        self.text = text       
        self.clicked = False
    
    @property
    def text(self):
        return self._text
    
    @text.setter
    def text(self, value):
        self._text = value
        self.render = font.render(self.text, True, 'white')
        self.text_width = self.render.get_width()
        self.text_height = self.render.get_height()
        
        self.box = pygame.Surface((self.text_width, self.text_height)) 
        self.box.set_alpha(0) 
        screen.blit(self.box, (x, y)) 

        self.rect = self.render.get_rect()
        self.rect.topleft = (x, y)

    # Rest of your code goes here

CodePudding user response:

You have to render the text again. Add a method to the class that updates the text. Draw the text and the box after updating the highlighting:

class Button():
    def __init__(self, text, x, y):
         self.updateText(text)
         self.rect = self.render.get_rect()
         self.rect.topleft = (x, y)
         self.clicked = False  

    def updateText(self, text):
         self.text = text   
         self.render = font.render(self.text, True, 'white')
         self.text_width = self.render.get_width()
         self.text_height = self.render.get_height()
         self.box = pygame.Surface((self.text_width, self.text_height)) 
        
    def draw(self):       
        pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(pos):
            self.box.set_alpha(100) 
            self.box.fill((255, 255, 255))
            
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
        
            if pygame.mouse.get_pressed()[0] == 0:
                self.clicked = False
        else:
            self.box.set_alpha(0) 

        screen.blit(self.box, (self.rect.x, self.rect.y))
        screen.blit(self.render, (self.rect.x, self.rect.y))   
            
        return self.clicked 

Invoke updateText:

b.text = "New text"

b.updateText("New text")
  • Related