【问题标题】:Clearing inputText with a button release in Kivy在 Kivy 中使用按钮释放清除 inputText
【发布时间】:2018-10-03 18:18:45
【问题描述】:

假设我们有一个表单,该人在输入上写了一些东西,但需要全部清除,我将如何在 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 很陌生,所以我不确定这是否是清除文本的最佳方法,但我做错了什么?或者,如果你们能以最好的方式提供一些启示。

【问题讨论】:

    标签: python kivy python-3.6 kivy-language


    【解决方案1】:

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

    *.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 = ""
    

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多