简体   繁体   English

Kivy:可以用kv语言覆盖TextInput的insert_text()函数吗?

[英]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? 有没有一种方法只能使用kv语言覆盖TextInputinsert_text()函数? 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: 我知道有一种方法可以通过python代码来完成,例如我发现的以下示例:

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: 但是有没有办法对kv文件中的TextInput小部件执行相同的操作:

    <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? 如果没有,我如何集成python代码,以便限制TextInput小部件的输入文本?

You can't override the method from kv language. 您不能从kv语言覆盖该方法。 If you want to do that, just override the method of your Temperature widget in its python definition. 如果要这样做,只需在其python定义中覆盖Temperature小部件的方法。

TextInput also has an input_filter property that can be set to 'int' or 'float' to accept only numbers. TextInput还具有input_filter属性,可以将其设置为'int''float'以仅接受数字。 You can set this in kv. 您可以在kv中设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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