使用Kivy中的按钮释放来清除inputText

时间:2018-10-03 18:18:45

标签: python kivy python-3.6 kivy-language

让我们假设我们有一个表格,该人在输入中写了一些东西,但需要清除所有内容,我将如何在Kivy中做到这一点?

我尝试过,但是没有用:

main.py:

class CreateUra(GridLayout):

    def clear_inputs(self, *args):
            for item in args:
                item = ''


class UraApp(App):

    def build(self):
        return CreateUra()

if __name__ == "__main__":
    UraApp().run()

Ura.kv:

<CreateUra>:
    cols: 1
    padding: 10
    spacing: 10
    row_force_default: True
    row_default_height: '32dp'

      BoxLayout:
            Label: 
                text: 'Contexto:'
                TextInput: 
                hint_text: "Contexto da ura"
                focus: True
                multiline: False
                cursor_color: (0, 0, 0, 0.8)
                id: context_input

        BoxLayout:
            Label: 
                text: 'Nome da ura:'
                TextInput: 
                hint_text: "Nome do arquivo da ura"
                multiline: False
                cursor_color: (0, 0, 0, 0.8)
                id: ura_file_name

        Button:
            text: 'Create Ura'
            id: bt_create
            on_press: root.uraConfig()
            on_release: root.clear_inputs(context_input.text, ura_file_name.text)

*忽略on_press上的uraConfig

我尝试过这种方法,并且有效:

Button:
    text: 'Create Ura'
    id: bt_create
    on_press: root.uraConfig()
    on_release: context_input.text = ''

我对kivy和Python还是很陌生,所以我不确定这是否是清除文本的最佳方法,但是我做错了什么?或者,如果你们能以最好的方式给他们一些启发。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是传递文本,但不是传递text属性而是传递文本的副本,因此即使将其设置为空也不会反映在textinput中。您必须将textinput作为列表传递,迭代并使用空字符串设置属性:

*。kv

Button:
    text: 'Create Ura'
    id: bt_create
    on_press: root.uraConfig()
    on_release: root.clear_inputs([context_input, ura_file_name]) # <-- add brackets

*。py

class CreateUra(GridLayout):
    def clear_inputs(self, text_inputs):
        for text_input in text_inputs:
            text_input.text = ""