Bare
Bare

Reputation: 85

How to read text in kivy textinput or Label

imports:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
kv = '''
BoxLayout:
    text: your_textinput
    orientation: 'vertical'

    TextInput:
        id: your_textinput
    Button:
        text: 'click'
        on_press: app.clicked()

'''

MyApp class:

class MyApp(App):
    text = StringProperty('-.text')

    def build(self):
        return Builder.load_string(kv)

    def clicked(self):
        file = open('read.text', 'r')
        f = file.readlines()
        newList = []
        for line in f:
            newList.append(line.strip())
        print(newList)
        self.root.ids.your_textinput.text = (newList)


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

Upvotes: 1

Views: 723

Answers (1)

inclement
inclement

Reputation: 29450

Something like id: your_textinput in the TextInput kv, and self.root.ids.your_text_input.text = newList in the clicked method. Also see http://inclem.net/2019/06/20/kivy/widget_interactions_between_python_and_kv/ for other ways of passing references around.

Upvotes: 1

Related Questions