【问题标题】:Validate a Kivy TextInput on Input as Data Type Integer Before Accepting Input在接受输入之前验证输入上的 Kivy TextInput 作为数据类型整数
【发布时间】:2018-08-03 20:16:07
【问题描述】:

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

编辑:我注意到我做错了什么:我在没有声明类的情况下导入了 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 Q&A 让我得到了正确答案, Stack_Overflow_Answer1; Stack_Overflow_Answer2。 我还浏览了 Kivy 文档,该文档显示了在插入文本时仅允许浮点数和 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 中这是值“12”,现在用户按下“s”然后应该显示在 TextInput
  • 请改进您在 .kv 中的缩进
  • 请在这里回答,不在您的帖子中,您找到解决方案了吗?
  • 是的,我在文档中给出的 kivy 文件“input_filter = 'float'”中添加了一行,它解决了它。谢谢。

标签: python python-3.x kivy


【解决方案1】:

详情请参阅说明和示例。

TextInput - 输入过滤器

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

input_filter

input_filter

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

input_filter 是一个 ObjectProperty,默认为 None。可以是其中之一 无、“int”(字符串)或“float”(字符串)或可调用对象。如果是 ‘int’,它只接受数字。如果它是“浮动”的,它也会 接受一个时期。最后,如果它是可调用的,它将被调用 有两个参数;要添加的字符串和一个布尔值指示 字符串是否是撤消的结果 (True)。可调用的应该 返回一个将被使用的新子字符串。

kv文件

定义两个根

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

TextInput - 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

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

示例

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

输出

【讨论】:

  • 你的回答比我的单行和很好的解释要好。还是要多多学习。谢谢。
  • 请记得投票或接受答案。 点击解决您问题的答案左侧的绿色勾勒勾号。
  • 绿色。我知道这是题外话,我还有六个用于计算的文本输入。我是否使用 ListProperty 来获取这些对象的列表,以便我可以遍历它们并检查它们是否为空?我还注意到 textinputs 总是返回一个字符串,我必须将它们转换为 int 或 float。这是默认的文本输入行为还是有一个属性可以做到这一点?
  • 使用for child in reversed(self.container.children):。请查看我的解决方案TextInputs Validation
  • 再次感谢 ikolim 非常有帮助的评论。
猜你喜欢
  • 2018-12-31
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 2020-01-21
相关资源
最近更新 更多