Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How check value for text_input? #477

Open
Poleg44 opened this issue Jan 30, 2024 · 1 comment
Open

How check value for text_input? #477

Poleg44 opened this issue Jan 30, 2024 · 1 comment

Comments

@Poleg44
Copy link

Poleg44 commented Jan 30, 2024

I do time widget with text_input for sec. How can i use onchange event for control sec value <=59?
I need example with onchange event using.

@ppizarror
Copy link
Owner

ppizarror commented Jan 31, 2024

Hi @Poleg44. You have many tools for controlling the values of inputs. In this case, you can use a combination of maxchar, input_type, and an on_change callback.

Maxchar limits the user to enter numbers greater than 99. Input type restricts input chars only to numeric, and on_change validates the time and responds appropriately. See the following example:

import pygame_menu
from pygame_menu.examples import create_example_window
from pygame_menu.locals import INPUT_INT

surface = create_example_window('Time validator', (600, 400))

# Create the menu
menu = pygame_menu.Menu(height=300, theme=pygame_menu.themes.THEME_DARK, title='Test timer', width=400)

# Add the input time entry and validate
time_input = menu.add.text_input('Time: ', input_type=INPUT_INT, maxchar=2)
def validate_time(value, *args):
    if value < 0:
        time_input.set_value(0)
    elif value > 59:
        time_input.set_value(59)
time_input.set_onchange(validate_time)

menu.add.button('Quit', pygame_menu.events.EXIT)
menu.mainloop(surface)

In the example, the time is validated; if less than 0, set 0; if greater than 59, set 59. There are better solutions, like building your own Widget based on TextInput. But I guess this works, and it is quite simple.

Greetings,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants