Kivy- Python - 检查textField是否为空。

问题描述 投票:0回答:1

我需要做一些动作,在 on_release 按钮的一部分,但只有当TextField中的文本不是空的时候。

还有一个相关的问题是如何只插入数字?我试过用 input_filter (所以,如果它工作,我不需要检查空虚),但我得到的是如果没有INT,就会粉碎一个应用程序。还有一件事,我想在不创建一个特殊的类来检查所有属性的情况下进行,我想在主体的 MDTextField 或使用按钮。

我有文本字段。

MDTextField:
    id: times_amount
    hint_text: "Times a day"
    mode: "rectangle"
    helper_text_mode: "on_focus"
    helper_text: "Insert only numbers"


和一个按钮。

MDFloatingActionButton:
    id: ok_but
    pos_hint:{"center_x": .8, "center_y": .2}
    icon: 'check'
    on_release:     #should I use this?-> if times_amount.text!=''
        app.add_object(my_object)
        times_amount.text=''
python button kivy textfield
1个回答
0
投票

你可以使用一些逻辑在你的 kv,比如。

MDFloatingActionButton:
    id: ok_but
    pos_hint:{"center_x": .8, "center_y": .2}
    icon: 'check'
    on_release:     #should I use this?-> if times_amount.text!=''
        app.add_object(my_object) if times_amount.text != '' else None
        times_amount.text=''

0
投票

如果有人感兴趣的话,完整的代码是这样的:

MDTextField:
    id: times_amount
    hint_text: "Times a day"
    mode: "rectangle"
    required: True
    input_filter:'int'
    helper_text_mode: "on_error"
    helper_text: "Insert only numbers"

and

FloatLayout:
    MDFloatingActionButton:
        id: ok_but
        pos_hint:{"center_x": .8, "center_y": .2}
        icon: 'check'
        on_release:
            app.add_object(my_object) if times_amount.text!='' else None
            times_amount.text=''
© www.soinside.com 2019 - 2024. All rights reserved.