【问题标题】:getting input from kivy textinput从 kivy textinput 获取输入
【发布时间】:2016-11-13 14:11:57
【问题描述】:

我想在以后的项目中使用它,我只想通过单击MainScreen 上的按钮来打印来自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()   

【问题讨论】:

  • 在 get_text 中你使用 'TextInput.text' 而不是 'textinput.text'。

标签: python-2.7 kivy kivy-language


【解决方案1】:

当您在kv 中编码时,您直接绑定就好像您要调用函数,例如

on_release: do_this()

但是你在没有括号的情况下这样做,就好像它是一个随意的 python 绑定:

self.bind(on_release=do_this)

添加括号,它应该打印出来:

on_release: root.get_text()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 2016-07-11
    • 1970-01-01
    相关资源
    最近更新 更多