使用python中textinput的值

我想创建一个应用程序,当我按下Print时,它将打印在TextInput中输入的文本.

经过几个小时的网络搜索后,我仍然无法理解如何在
Python脚本中将TextInput的值赋予变量.

这是Kivy代码:

<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脚本:

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()

最佳答案 尝试改变这个:

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

对此

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