首页 > 解决方案 > 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):

我想通过激活按下功能在标签附近添加一个新按钮

标签: pythonkivykivy-language

解决方案


在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())

推荐阅读