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
相关产品推荐
相关产品推荐

