Spencer
Spencer

Reputation: 23

Bind TextInput to Label in .kv File

I was following the tutorial video "Kivy crash course 3: More interesting widget interactions" by Alexander Taylor, but he is writing the code in python rather than in a .kv file. I was trying to follow the tutorial using a .kv file instead, but I am confused on how to bind the TextInput text to the label text. Is it possible to write it in the .kv file or does it have to be written in the .py file. Can you give an example?

.py

from kivy.app import App

from kivy.lang import Builder
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout


class MainApp(App):

    def build(self):
        return pres    

pres = Builder.load_file("main.kv")

if __name__ == "__main__":
    MainApp().run()

.kv

BoxLayout:
    orientation: 'vertical'
    TextInput:
        size_hint_y: None
        height: 200
        font_size: 150
        hint_text: "Enter Text"
        text: ""
    FloatLayout:
        Scatter:
            Label:
                text: ""
                font_size: 150

Upvotes: 2

Views: 860

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

the binding in .kv are simpler due to the fact that it is a declarative language, it is enough with an assignment but for that the element that has the information must have an id.

*.kv

BoxLayout:
    orientation: 'vertical'
    TextInput:
        id: ti # <---
        size_hint_y: None
        height: 200
        font_size: 150
        hint_text: "Enter Text"
    FloatLayout:
        Scatter:
            Label:
                text: ti.text # <---
                font_size: 150

Upvotes: 1

Related Questions