0

I have a menu. In this menu, I have a "Name" field where the user enters his name. I need to take this name for further processing. How should I do it? I have some code:

menu.add.text_input('name:', default='player') # I NEED THIS VALUE.
menu.add.selector('difficulty', [('easy', "EASY"), ('medium', 'MEDIUM'), ('hard', 'HARD')], onchange=change_difficulty, style='fancy', style_fancy_arrow_margin=(0, 0, 0), style_fancy_bgcolor=(0, 0, 0, 0), style_fancy_bordercolor=(0, 0, 0, 0), style_fancy_arrow_color=(220, 132, 201))
menu.add.button('play', start_the_game, DIFFICULTY)
menu.add.button('quit', pygame_menu.events.EXIT)

while True:

    screen.blit(bg_image, (0, 0))

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

    if menu.is_enabled():
        menu.update(events)
        menu.draw(screen)
    
    pygame.display.update()

1 Answer 1

1

Like other event driven languages e.g. C# you need to bind a function to the on change event of your input field that you added in the menu so that your code will be like this

menu.add.text_input('name:', default='player', onchange= MyTextValue) # Function is bind here
menu.add.selector('difficulty', [('easy', "EASY"), ('medium', 'MEDIUM'), ('hard', 'HARD')], onchange=change_difficulty, style='fancy', style_fancy_arrow_margin=(0, 0, 0), style_fancy_bgcolor=(0, 0, 0, 0), style_fancy_bordercolor=(0, 0, 0, 0), style_fancy_arrow_color=(220, 132, 201))
menu.add.button('play', start_the_game, DIFFICULTY)
menu.add.button('quit', pygame_menu.events.EXIT)

def MyTextValue(name):
    #on input change your value is returned here
    print('Player name is', name)

while True:

    screen.blit(bg_image, (0, 0))

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

    if menu.is_enabled():
        menu.update(events)
        menu.draw(screen)
    
    pygame.display.update()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.