4

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()
2
  • What happens when you run now? Do you get some sort of error? Commented Sep 16, 2015 at 16:27
  • Yes. When I write something in TextInput and click 'print' I get: File "D:/Documents/Python\simple.kv", line 9, in <module> on_press: root.printTxt(txt.text) TypeError: printTxt() takes exactly 1 argument (2 given) Commented Sep 16, 2015 at 16:40

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I did, but now it returns this error: File "D:/Documents/Python\simple.kv", line 9, in <module> on_press: root.printTxt(txt.text) File "D:/Documents/Python/textinput.py", line 8, in printTxt print txt.text NameError: global name 'txt' is not defined
Look at my new edit and see if that works. I got rid of the txt.text and replaced with just text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.