将 TextInput 绑定到 .kv 文件中的标签

我正在关注 Alexander Taylor 的教程视频“Kivy 速成课程 3:更有趣的小部件交互”,但他正在使用 python 而不是 .kv 文件编写代码。我试图使用 .kv 文件来学习教程,但我对如何将 TextInput 文本绑定到标签文本感到困惑。是否可以将其写入 .kv 文件中,或者是否必须将其写入 .py 文件中。你能给个例子吗?


.py


from kivy.app import App


from kivy.lang import Builder

from kivy.uix.scatter import Scatter

from kivy.uix.label import Label

from kivy.uix.floatlayout import FloatLayout

from kivy.uix.textinput import TextInput

from kivy.uix.boxlayout import BoxLayout



class MainApp(App):


    def build(self):

        return pres    


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


if __name__ == "__main__":

    MainApp().run()

.kv


BoxLayout:

    orientation: 'vertical'

    TextInput:

        size_hint_y: None

        height: 200

        font_size: 150

        hint_text: "Enter Text"

        text: ""

    FloatLayout:

        Scatter:

            Label:

                text: ""

                font_size: 150


qq_遁去的一_1
浏览 173回答 1
1回答

牧羊人nacy

.kv 中的绑定更简单,因为它是一种声明性语言,赋值就足够了,但具有信息的元素必须具有 id。*.kvBoxLayout:&nbsp; &nbsp; orientation: 'vertical'&nbsp; &nbsp; TextInput:&nbsp; &nbsp; &nbsp; &nbsp; id: ti # <---&nbsp; &nbsp; &nbsp; &nbsp; size_hint_y: None&nbsp; &nbsp; &nbsp; &nbsp; height: 200&nbsp; &nbsp; &nbsp; &nbsp; font_size: 150&nbsp; &nbsp; &nbsp; &nbsp; hint_text: "Enter Text"&nbsp; &nbsp; FloatLayout:&nbsp; &nbsp; &nbsp; &nbsp; Scatter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: ti.text # <---&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 150
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python