You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy点击按钮时将.py文件变量值显示到Label控件的实现方法

Kivy按钮点击将输入内容展示到Label控件实现方案

修改后simple.py代码

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty

class SimpleWidget(BoxLayout):
    result = ObjectProperty(None)
    # 新增绑定目标Label的属性
    your_input = ObjectProperty(None)

    def button(self, result):
        # 把打印逻辑替换为给Label的text属性赋值
        self.your_input.text = result.text

class SupersimpleApp(App):
    def build(self):
        return SimpleWidget()

if __name__ == "__main__":
    SupersimpleApp().run()

修改后supersimple.kv代码

<SimpleWidget>:
    result: result
    # 新增绑定kv中的your_input控件到Python类的属性
    your_input: your_input
    orientation: 'vertical'
    BoxLayout:
        id: box1
        Label:
            text: 'Type something'
        TextInput:
            id: result     
            height: 80            
    Button:
        id: button
        text: 'Press this'
        on_press: root.button(result)
    BoxLayout:
        id: box2
        Label:
            id: your_input

核心逻辑说明

  • 通过ObjectProperty建立Python逻辑层和KV布局层控件的对应关系
  • 按钮触发方法中直接修改目标Label的text属性,Kivy会自动刷新GUI界面完成内容更新

内容的提问来源于stack exchange,提问作者SSeb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 08:06:09