[Python] Kivy - Limit Values on InputText

Discussão em 'Python' iniciado por Stack, Novembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to make an input text that only accept float values. In addition, the value entered must be between two values.

    I created a class that contain a 'validate' method. If the value is not between two values, a Popup is showed.

    But I have a problem. The method is only called when the user hit 'Enter'. I tried call the method when the text is changed, but it is annoying for the user, because the Popup appears all the time while the user is entering the data.

    There is another approach to do something like this?

    Python file:

    class BoundedInput(BoxLayout):
    value = NumericProperty()

    def validate(self, min_value, max_value):
    status = min_value <= self.value <= max_value
    if not status:
    message = f'Value must be between {min_value} and {max_value}'
    popup = Popup(title='Warning', content=Label(text=message),
    size_hint=(None, None), size=(300, 200))
    popup.open()


    Kv file:

    <NumericInput@TextInput>:
    input_filter: 'float'
    multiline: False

    <BoundedInput>:
    orientation: 'horizontal'
    Label:
    text: 'Value'
    NumericInput:
    text: str(root.value)
    on_text_validate:
    root.value = float(self.text)
    root.validate(5, 100)

    Continue reading...

Compartilhe esta Página