How can I pull and save strings from text input boxes?

Viewed 32

I am working on a python pygame program and need to collect user text inputs and save them as strings. The text input boxes the user will input data into are part of a single class as shown below. I am new to programming and am unsure how to collect a string from the 6 input boxes created from the class as it seems like it will always save any output as a single variable but I know there is a simple solution that I am overlooking. Any help would be appreciated.

COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
FONT = pygame.font.Font(None, 32)

class InputBox:
    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                # Toggle the active variable.
                self.active = not self.active
            else:
                self.active = False
            # Change the current color of the input box.
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    print(self.text)
                    self.text = ''
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # Resize the box if the text is too long.
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        # Blit the rect.
        pygame.draw.rect(screen, self.color, self.rect, 2)



def main():
    clock = pygame.time.Clock()
    input_box1 = InputBox(100, 150, 140, 32)
    input_box2 = InputBox(100, 250, 140, 32)
    input_box3 = InputBox(100, 350, 140, 32)
    input_box4 = InputBox(100, 450, 140, 32)
    input_box5 = InputBox(100, 550, 140, 32)
    input_box6 = InputBox(100, 650, 140, 32)

    input_boxes = [input_box1, input_box2, input_box3, input_box4, input_box5, input_box6]
    done = False

    title_font = pygame.font.Font(None,50)
    text_font = pygame.font.Font(None,40)
    title_text = title_font.render('Geotechnically Write V.1', False, 'Red')
    
    text3 = text_font.render('Client Name', False, 'Red')
    text4 = text_font.render('Client Address Line 1, i.e 123 Fake St', False, 'Red')
    text5 = text_font.render('Client Address Line 2, i.e Ice Cream Island, Alaska 12345', False, 'Red')
    text6 = text_font.render('Project Number', False, 'Red')
    text7 = text_font.render('Project Address Line 1, i.e 123 Fake St', False, 'Red')
    text8 = text_font.render('Project Address Line 2, i.e Ice Cream Island, Alaska 12345', False, 'Red')
    screen.blit(title_text,(250,50))
       
    screen.blit(text3,(50,100))   
    screen.blit(text4,(50,200))   
    screen.blit(text5,(50,300))
    screen.blit(text6,(50,400))  
    screen.blit(text7,(50,500))  
    screen.blit(text8,(50,600))

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)

        for box in input_boxes:
            box.update()

        
        for box in input_boxes:
            box.draw(screen)

        pygame.display.flip()
        clock.tick(30)
if __name__ == '__main__':
    main()
    pygame.quit()

1 Answers

You can get the current value in each box via its text attribute. So you could do stuff with input_box1.text or input_box6.text, or if you want to handle all the inputs at once, something like [box.text for box in input_boxes], which builds a list to hold the strings from all six input boxes.

Related