Yair
Yair

Reputation: 909

kivy - save user input into a python list

I`m having some difficulties understanding the interrelation between kivy and python.

I`m trying to do something super simple, as a first step, and it would be great if anybody could show an example: How can I store input data into a python list once the user feeds-in data and press "enter"? Thanks

Upvotes: 0

Views: 1347

Answers (1)

Gábor Erdős
Gábor Erdős

Reputation: 3689

An example of this. User can input 3 things and they get stored in an array. If you want the user to input one thing and store it in an array you will have to split the input.

result = []
for i in range(3):
    answer = input()
    result.append(answer)

print(result)

NOTE: input() is raw_input() in python2.x

In kivy:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput


class Screen(GridLayout):
    def __init__(self, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self.input = TextInput(multiline=False)
        self.add_widget(self.input)
        self.input.bind(on_text_validate=self.print_input)
    def print_input(self, value):
        print(value.text)


class MyApp(App):

    def build(self):
        return Screen()


if __name__ == '__main__':
    MyApp().run()

This simple script will give you an input box and when you press enter it will print its text to terminal. You can easily store it in a list if you wish.

Upvotes: 2

Related Questions