Home > front end >  how to print all textinputs values in a python/kivy app, added by a for cycle?
how to print all textinputs values in a python/kivy app, added by a for cycle?

Time:02-04

I add some TextInputs in a Python3/kivy app, using a for cycle. I'd need to get all updated values after clicking a Button:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class app(App):
    def build(self):
        self.box=BoxLayout(orientation='vertical')
        for n in range(5):
            self.box.add_widget(TextInput())
        def doet(instance):
            print('values: ')
            #print all TextInputs values
        self.box.add_widget(Button(text='DOET',on_press=doet))
        return self.box
app().run()

CodePudding user response:

    def doet(instance):
        print('values: ')
        # for loop:
        for t in instance.parent.children:
            if isinstance(t, TextInput):
                print(t.text)

        # using list comprehension
        print('\n'.join([t.text for t in instance.parent.children if isinstance(t, TextInput)]))
  • Related