2016-11-13 97 views
0

我想用这个在以后的项目越来越投入,我只是想通过点击MainScreen按钮打印从TextInput用户的输入,但是当我运行,并单击文本“打印按钮文本“没有发生任何错误和没有输出。从kivy的TextInput

的.kv文件:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition 

ScreenManagement: 
    transition: FadeTransition() 
    MainScreen: 
    SecondScreen: 

<MainScreen>: 
    name: "main" 
    Button: 
     on_release: root.get_text 
     text: "Print Text" 
     font_size: 50 
     size_hint:0.3,0.1 

    TextInput: 
     text:"Hello World" 
     size_hint: 0.35,0.25 
     pos_hint:{"right":1, "top":1} 
     color:1,0,0,1 
     id: user_text 

    Button: 
      color: 0,1,0,1 
      font_size: 25 
      size_hint: 0.3,0.25 
      text: "Continue" 
      on_release: app.root.current = "other" 
      pos_hint:{"right":1, "top":0} 


<SecondScreen>: 
    name: "other" 
    FloatLayout: 
     Button: 
      color: 0,1,0,1 
      font_size: 25 
      size_hint: 0.3,0.25 
      text: "Back Home" 
      on_release: app.root.current = "main" 
      pos_hint:{"right":1, "top":1} 

的Python代码:

from kivy.app import App 
#kivy.require("1.9.1") 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen , FadeTransition 
from kivy.uix.widget import Widget 
from kivy.graphics import Line 
from kivy.uix.textinput import TextInput 

class MainScreen(Screen): 
    def get_text(self,*args): 
     textinput= self.ids.user_text 
     user= TextInput.text 
     print(user) 



class SecondScreen(Screen): 
    pass 

class ScreenManagement(ScreenManager): 
    pass 


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


class MainApp(App): 
    def build(self): 
    return gui 


MainApp().run() 
+0

在get_text你做'TextInput。 text'而不是'textinput.text'。 – Tshirtman

回答

0

当您在kv代码,你直接绑定,就好像你最好通话功能例如

on_release: do_this() 

但是你做到了没有括号,就好像它是一个休闲的Python绑定:

self.bind(on_release=do_this) 

添加括号,它应该打印:

on_release: root.get_text()