Why does me trying to use pygame-textinput cause an error?

Viewed 32

I have the below pygame code, where I am trying to use the pygame-textinput library to allow the user of my program to input text

def Coursework():  # the application loop that all pygame programs require to function
    clock = pygame.time.Clock()  # this creates a variable, that, when a value is assigned to it, it will be the
    # refresh rate of the application
    run = True
    encrypt = False
    decrypt = False
    while run:  # everything in this loop will only happen when the window is open
        clock.tick(FPS)  # this refreshes the frame every 1/FPS seconds, setting a framerate for the program
        for event in pygame.event.get():  # the event loop that ensures that any events are properly managed
            if event.type == pygame.QUIT:
                run = False  # this closes the window if the user quits the program
            if encrypt_button.click(event) == 1:  # The two following loops create a text box on the screen when one
                # of the options is chosen
                encrypt_button.image.fill(TRANSPARENT)
                decrypt_button.image.fill(TRANSPARENT)
                encrypt = True
            if decrypt_button.click(event):
                encrypt_button.image.fill(TRANSPARENT)
                decrypt_button.image.fill(TRANSPARENT)
                decrypt = True
            if encrypt:
                events = pygame.event.get()
                textinput.update(events)
                WIN.blit(textinput.surface, (300, 250))
            elif decrypt:
                events = pygame.event.get()
                textinput.update(events)
                WIN.blit(textinput.surface, (300, 250))
        draw_window()  # this ensures that when this function is called the window is coloured grey by calling the
        # draw window function

In particular, when I reach the two loops at the bottom, instead of allowing me to input text the window closes and I receive the message "update() missing one required positional argument: events" despite me establishing what events is. I'm very new to pygame, so I'm not too sure what I am doing and any help would be appreciated.

1 Answers

I think you should handle the text input outside the event loop:

import pygame_textinput
import pygame
pygame.init()

# Create TextInput-object
textinput = pygame_textinput.TextInputVisualizer()

screen = pygame.display.set_mode((1000, 200))
clock = pygame.time.Clock()

run = True
encrypt = False
decrypt = False
while run:  # everything in this loop will only happen when the window is open
    clock.tick(FPS)  # this refreshes the frame every 1/FPS seconds, setting a framerate for the program
    events = pygame.event.get()
    # Feed it with events every frame
    textinput.update(events)
    # Blit its surface onto the screen
    screen.blit(textinput.surface, (10, 10))
    for event in events:  # the event loop that ensures that other events are properly managed
        if event.type == pygame.QUIT:
            run = False  # this closes the window if the user quits the program
        if encrypt_button.click(event) == 1:  # The two following loops create a text box on the screen when one
            # of the options is chosen
            encrypt_button.image.fill(TRANSPARENT)
            decrypt_button.image.fill(TRANSPARENT)
            encrypt = True
        if decrypt_button.click(event):
            encrypt_button.image.fill(TRANSPARENT)
            decrypt_button.image.fill(TRANSPARENT)
            decrypt = True
        if encrypt:
            events = pygame.event.get()
            textinput.update(events)
            WIN.blit(textinput.surface, (300, 250))
        elif decrypt:
            events = pygame.event.get()
            WIN.blit(textinput.surface, (300, 250))
    draw_window()  # this ensures that when this function is called the window is coloured grey by calling the
    # draw window function
Related