Textfield Link in Kivy

Viewed 46

I have two kivy TextFields A and B, I would like to link both fields in such a way that the the result of multiplying the Text in A by an integer will be displayed on B.

1 Answers

I assume that by TextFields you mean a TextInput and a Label. Here is a code sample that does what you are looking for.

main.py

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


class MyGrid(GridLayout):
    def Multiply(self, input_text):
        my_integer = 100
        try:
            integer_input_text = int(input_text)
            return str(integer_input_text * my_integer)
        except ValueError:
            return "Please, insert a valid number"


class MyApp(App):
    def build(self):
        return MyGrid()


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

my.kv

<MyGrid>:
    cols: 1

    TextInput:
        multiline: False
        on_text_validate:
            my_label.text = root.Multiply(self.text)
            self.text = ""

    Label:
        id: my_label
        text: "Insert a number and press enter to know the result of multiplying it by one hundred"
Related