简体   繁体   中英

Kivy: Can the insert_text() function of TextInput be overrided in kv language?

Is there a way to override the insert_text() function of TextInput only using kv language? I would like to restrict the text to numeric values only. I know there is a way to do it through python code, such as the following example I found:

class NumericInput(TextInput):

def insert_text(self, substring, from_undo=False):
    if not from_undo:
        try:
            int(substring)
        except ValueError:
            return
    super(NumericInput, self).insert_text(substring, from_undo)

But is there a way to do the same to the TextInput widget inside of the kv file:

    <Temperature>:
        name: "Temperature"
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: "Farenheit"
            TextInput:
                id: user_input
                font_size: 50
                size_hint_y: None
                insert_text:

If not, how could I integrate the python code so that it restricts the input text of my TextInput widget?

You can't override the method from kv language. If you want to do that, just override the method of your Temperature widget in its python definition.

TextInput also has an input_filter property that can be set to 'int' or 'float' to accept only numbers. You can set this in kv.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM