Clearing inputText with a button release in Kivy

Question:

Let’s imagine we have a form where the person wrote some stuff on the inputs but need to clear them all, how would I do that in Kivy?

I tried this but it didn’t work:

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)

*Ignore the uraConfig on the on_press

I tried this way and it worked:

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

I’m quite new to kivy and Python, so I’m not sure if this was the best way to clear the texts, but what I did wrong? Or if you guys could give some light in the best way to do it.

Asked By: Caíck

||

Answers:

The problem in your case is passing the text, but not text property but a copy of the text, so even if you set it to empty is not reflected in the textinput. You have to pass the textinput as a list, iterate and set the property with an empty string:

*.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 = ""
Answered By: eyllanesc

I tried this and it work for me:

*kv

TextInput:
    id: my_text_input
    hint_text: "Your name..."
    size_hint: None, None
    height: "50dp"
    width: "100dp"
    multiline: False
    on_text_validate:root.on_text_validate(self)

Label:
    id: txt
    text: "Your name is: " + root.text_input_str

Button:
    text: "Reset details"
    on_press: root.on_button_reset_details()
    size_hint: None, None
    width: "100dp"
    height: "50dp"

*py

class WidgetGalaxy(GridLayout):

    text_input_str = StringProperty("")
    def on_text_validate(self, widget):
        self.text_input_str = widget.text

    def on_button_reset_details(self):
        self.text_input_str = ""
        self.ids.my_text_input.text = ""
        if self.ids.my_text_input.text == "":
            self.ids.my_text_input.hint_text = self.ids.my_text_input.hint_text

with all that it should work for your code…
If having issue check your code preference and some predefined local or global variable.