Search code examples
pythonkivytextinput

Using value from textinput in python


I want to make an app which would print text entered in TextInput when I press Print. After a couple of hours of searching the web I still can't understand how to assing value of TextInput to variable in python script.

This is Kivy code:

<SimpleRoot>:
    orientation:"vertical"
    padding: root.width * .02, root.height * .02
    spacing: "10dp"
    TextInput:
        id: txt
    Button:
        text: 'Print'
        on_press: root.printTxt(txt.text)

Python script:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

class SimpleRoot(BoxLayout): # 2
    def printTxt(text):
        print txt.text


    pass

class SimpleApp(App):  # 1  
    def build(self):
        # Return root widget 
        return SimpleRoot()

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

Solution

  • Try changing this:

    class SimpleRoot(BoxLayout): # 2
        def printTxt(text):
            print txt.text
    

    To this

    class SimpleRoot(BoxLayout): # 2
        def printTxt(self, text):
            print text