【问题标题】:Clearing MDTextField from kivymd after submission提交后从 kivymd 清除 MDTextField
【发布时间】:2020-04-02 05:58:23
【问题描述】:

我正在尝试创建一个需要用户输入坐标的游戏。但是,我意识到即使单击“Enter”按钮并更改屏幕,字段也不会被清除。通常使用普通的文本输入字段,我可以插入一个self.shootdepthinput.text='' 的python 行,这将清除字段中的文本输入,因为它是一个字符串。但是,对于 MDTextField,输入是元组的形式,它是不可变的。有谁知道我清除 MDtextfield 的功能?谢谢!

这是我的 kivy 文件的摘录,我的 MDTextField 位于:

<ComputerScreen>:

    shootxinput: shootxinput_id
    shootyinput: shootyinput_id
    shootdepthinput: shootdepthinput_id

    id: "computerscreen_id"
    name: "computer"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "battleshipgame.png"

    GridLayout:
        cols: 3
        id: s_input
        pos_hint: {"x":0.03,"y":0.72}
        size_hint : 0.7, 0.1

        MDTextField:
            id: shootxinput_id
            hint_text: "X-Coordinate"
            required: True
            helper_text_mode: "on_focus"
            color_mode: "custom"
            helper_text: "Enter a number from 1 to 10"
            line_color_focus: 0, 0, 0, 1

        MDTextField:
            id: shootyinput_id
            hint_text: "Y-Coordinate"
            required: True
            color_mode: "custom"
            helper_text_mode: "on_focus"
            helper_text: "Enter an alphabet from A to J"
            line_color_focus: 0, 0, 0, 1

        MDTextField:
            id: shootdepthinput_id
            hint_text: "Depth"
            required: True
            color_mode: "custom"
            helper_text_mode: "on_focus"
            helper_text: "0: Surface Level, 1: Sub-sea Level"
            line_color_focus: 0, 0, 0, 1

    FloatLayout:
        id: buttonarea
        Button:
            id: shootsubmitbtn
            size_hint:0.1,0.05
            on_press: root.shoot()
            text: "Enter"
            pos_hint:{"x": 0.75, "y": 0.75}

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    shoot 方法中创建一个清除输入字段的语句

    def shoot(self, *args):
        shootxinput_id.text, shootyinput_id.text = ""
        # do some other things
    

    【讨论】:

    • 您好!我已经尝试过了,但是对于 MDTextFields,shootxinput_id.text 作为一个元组出现,它是不可变的......有没有办法清除一个元组?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2018-07-23
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多