简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM