python - 在接受输入之前将输入的 Kivy TextInput 验证为数据类型整数

标签 python python-3.x kivy

在将计算数据更新到 ListView 之前,我正在尝试在对输入值进行计算之前验证 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

第二次编辑:我一定是错过了,但是另外两个 Stack Overflow 问答让我找到了正确答案, Stack_Overflow_Answer1 ; Stack_Overflow_Answer2 . 我还浏览了 Kivy 文档,该文档显示了在插入文本时仅允许 float 和 TextInput 中的一个点的示例 Kivy1.11.0_TextInput_Documentation .所以我将能够解决它。 @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()

这是我的 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: []

最佳答案

详情请引用说明和例子。

TextInput - input_filter

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

input_filter

input_filter

Filters the input according to the specified mode, if not None. If None, no filtering is applied.

input_filter is an ObjectProperty and defaults to None. Can be one of None, ‘int’ (string), or ‘float’ (string), or a callable. If it is ‘int’, it will only accept numbers. If it is ‘float’ it will also accept a single period. Finally, if it is a callable it will be called with two parameters; the string to be added and a bool indicating whether the string is a result of undo (True). The callable should return a new substring that will be used instead.

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")

Declaration of a Property

To declare properties, you must declare them at the class level.

例子

主.py
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()

校准.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: []

输出

Img01

关于python - 在接受输入之前将输入的 Kivy TextInput 验证为数据类型整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51679570/

相关文章:

python - 使用 "src"布局在 Python 项目中使用 PyTest

python-3.x - 如何在 Selenium Python 中更改 FireFox 的 Remote ?

python - pygame.mixer.music,我想在 while 循环运行时播放轨道

python - float 布局 KIVY 中的动画

python - Kivy 弹出窗口还是 Eventloop 交互?

Python 3 readline() 不起作用

python - python中的嵌套字典

python-3.x - 通过计算数组中的字符串并添加数组中的值来填充 pandas 数据框

kivy:将背景颜色更改为白色

python - 使用 Python 替换数据框中的值