python - Python Kivy 库添加小部件
问题描述
例如,我们使用 kivy 语言创建了简单的 BoxLayout1,其中包含 button1 和另一个 BoxLayout2。当您单击 button1 时,它会在 BoxLayout2 中添加一个新按钮,但它是用 python 编写的。
是否可以在 python 代码中以 kv 语言访问现有布局?还是唯一的解决方案是在 python 中编写整个窗口?
我在 kivy 文档中找不到任何信息,也许我只是错过了一些东西。
编辑:
我有这样的东西
千伏:
<CreateWindow>:
BoxLayout:
Button:
text: "test"
on_release: root.press()
BoxLayout:
Label:
text: "Label"
Python:
class CreateWindow(Screen):
def press(self):
我想通过激活按下功能在标签附近添加一个新按钮
解决方案
在python中会是这样的
class CreateWindow(Screen):
def press(self):
# the first box is the first child in the children list of the CreateWindow widget so
box1=self.children[0]
box2=self.children[1]
# and now you can decide which box you want to use add_widget method with
# in your case it should be
box2.add_widget(Button())
推荐阅读
- python - Issues on Height Map to Normal Map with Python OpenCV
- jquery-mobile - pagecontainer 触发事件(多页站点)
- java - 无法恢复活动运行时异常
- sql - 在 sql server 中修改列数据类型的最佳方法是什么?
- r - R - 使用 syuzhet 包在每个 NRC 情绪和情感中查找热门词
- javascript - 如何使用 VueI18n 进行基于组件实例的国际化?
- android - Android显示浮动小部件覆盖不起作用
- javafx-8 - 如何将 JavaFX 形状与不同颜色结合起来?
- c# - 一种轮询用户桌面以获取未处理异常弹出窗口的方法
- c# - NHibernate mysql 更新错误,GenericADOException,服务器错误代码 1064