【问题标题】:Kivy- Python - check if textField is not emptyKivy- Python - 检查文本字段是否不为空
【发布时间】:2020-05-10 14:17:25
【问题描述】:

我需要在按钮的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】:

    您可以在 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=''
    

    【讨论】:

    • 谢谢!这就是决定
    【解决方案2】:

    如果有人感兴趣,完整代码如下所示:

    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"
    

    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=''
    

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 2021-07-01
      • 2015-12-28
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多