在接受输入之前验证输入上的 Kivy TextInput 作为数据类型整数

Hme*_*006 2 python python-3.x kivy

在将计算的数据更新到 ListView 之前,在对 Input 的值进行计算之前,我试图在 Kivy 中验证 TextInput。但是当我通过打印出来测试第一个 TextInput 值时,它什么也没有,没有错误也没有结果。我在我的 Kivy 文件中引用了 on_press root.calvariable 方法 AddKalibrasieForm Class,但仍然没有。有人可以告诉我我做错了什么吗?

编辑:我注意到我做错了什么:我在没有声明类的情况下导入了 TextInput(它已被删除)并且没有以正确的方法声明 val_lewerha TextInput 对象(修复它),所以它打印到控制台。我的问题已更改为您可以在输入时验证用户输入吗?这叫 on_focus 吗?例如我认为应该达到预期结果的方法:

def validate_input(self):
    if isinstance(self.textinput, (int, float)):
        accept self.textinput
    else:
        make self.textinput color red as incorrect data type
Run Code Online (Sandbox Code Playgroud)

第二次编辑:我一定是错过了,但是另外两个 Stack Overflow Q&A 让我得到了正确答案 Stack_Overflow_Answer1Stack_Overflow_Answer2。我还浏览了 Kivy 文档,该文档显示了在插入文本Kivy1.11.0_TextInput_Documentation时只允许浮点数和 TextInput 中的一个点的 示例。所以我将能够解决它。@eyllanesc:我只想允许用户在 TextInput 中插入浮点“0-9”,没有字符串。谢谢。我如何将其标记为已回答?

这是我的 Python3 代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty

class AddKalibrasieForm(BoxLayout):
    calculated_results = ObjectProperty()

    def convert_calvariables(self):
        val_lewerha = ObjectProperty()
        not_filled_in = ""
        flowha = float(self.val_lewerha.text)
        if flowha != not_filled_in and isinstance(flowha, (int, float)):
            print(format(self.val_lewerha.text))
        else:
            print("Error")

class CalibrationApp(App):
    pass

if __name__ == '__main__':
    CalibrationApp().run()
Run Code Online (Sandbox Code Playgroud)

这是我的 Kivy 文件的一部分:calibration.kv

AddKalibrasieForm:

<AddKalibrasieForm>:
    orientation: "vertical"
    val_lewerha: volha_box
    calculated_results: calculated_results_table
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        Label:
            text: "Lewering per ha"
            size_hint_x: 25
        TextInput:
            id: volha_box #TextInput object name
            size_hint_x: 50
        Label:
            text: "liter"
            size_hint_x: 25
            text_size: self.size
            halign: "left"
    BoxLayout:
        height: "60dp"
        size_hint_y: None
        Label:
            size_hint_x: 35
        Button:
            text: "Bereken"
            size_hint_x: 30
            on_press: root.convert_calvariables() #method called on_press
        Label:
            size_hint_x: 35
    ListView:
        id: calculated_results_table
        table_items: []
Run Code Online (Sandbox Code Playgroud)

iko*_*lim 8

有关详细信息,请参阅说明和示例。

文本输入 - input_filter

使用input_filter: 'float'. 使用 input_filter,您不必在方法中检查“init”或“float”。

输入过滤器

input_filter
Run Code Online (Sandbox Code Playgroud)

根据指定的模式过滤输入,如果不是无。如果没有,则不应用过滤。

input_filter 是一个 ObjectProperty,默认为 None。可以是 None、'int'(字符串)或 'float'(字符串)之一,或者是可调用的。如果它是'int',它将只接受数字。如果它是“浮动”,它也将接受一个时期。最后,如果它是可调用的,它将使用两个参数进行调用;要添加的字符串和一个布尔值,指示该字符串是否是撤消的结果 (True)。callable 应该返回一个新的子字符串来代替。

kv文件

定义了两个根

在您的 kv 文件中,您有为同一个根小部件定义的根规则AddKalibrasieForm:和类规则<AddKalibrasieForm>:。由于您没有使用def build()方法,因此<AddKalibrasieForm>:在 kv 文件中删除类规则。

文本输入 - volha_box

添加以下内容:

  • multiline: False # disable multiline
  • hint_text: "Slegs nommers" # Numbers only
  • input_filter: "float"

Python代码

class AddKalibrasieForm(BoxLayout):
    calculated_results = ObjectProperty(None)
    val_lewerha = ObjectProperty(None)

    def convert_calvariables(self):
        if len(self.val_lewerha.text) > 0:   # if text is not empty
            print(format(self.val_lewerha.text))
        else:
            print("Error: Empty string")
Run Code Online (Sandbox Code Playgroud)

财产声明

要声明属性,您必须在类级别声明它们。

例子

主文件

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty


class AddKalibrasieForm(BoxLayout):
    calculated_results = ObjectProperty(None)
    val_lewerha = ObjectProperty(None)

    def convert_calvariables(self):
        if len(self.val_lewerha.text) > 0:   # if text is not empty
            print(format(self.val_lewerha.text))
        else:
            print("Error: Empty string")


class CalibrationApp(App):
    pass


if __name__ == '__main__':
    CalibrationApp().run()
Run Code Online (Sandbox Code Playgroud)

校准.kv

#:kivy 1.11.0

AddKalibrasieForm:  # root rule
    orientation: "vertical"
    val_lewerha: volha_box
    calculated_results: calculated_results_table

    BoxLayout:
        height: "40dp"
        size_hint_y: None

        Label:
            text: "Lewering per ha"
            size_hint_x: 25

        TextInput:
            id: volha_box   # TextInput object name
            size_hint_x: 50
            hint_text: "Slegs nommers"
            multiline: False
            input_filter: "float"

        Label:
            text: "liter"
            size_hint_x: 25
            text_size: self.size
            halign: "left"

    BoxLayout:
        height: "60dp"
        size_hint_y: None

        Label:
            size_hint_x: 35

        Button:
            text: "Bereken"
            size_hint_x: 30
            on_press: root.convert_calvariables()   # method called on_press

        Label:
            size_hint_x: 35

    ListView:
        id: calculated_results_table
        table_items: []
Run Code Online (Sandbox Code Playgroud)

输出

图像01