Python Kivy 库添加小部件

Python Kivy library adding widgets

例如,我们用kivy语言创建了简单的BoxLayout1,其中包含button1和另一个BoxLayout2。当您单击 button1 时,它会在 BoxLayout2 中添加一个新按钮,但它是用 python.

编写的

是否可以在 python 代码中使用 kv 语言访问现有布局?或者唯一的解决方案是在 python?

中写入整个 window

我在 kivy 文档中找不到任何信息,也许我只是错过了一些东西。

编辑:

我有这样的东西

Kv:

<CreateWindow>:
    BoxLayout:
        Button:
              text: "test"
              on_release: root.press()
    BoxLayout:
        Label:
              text: "Label"

Python:

class CreateWindow(Screen):
      def press(self):

我想通过激活 press 功能

在 Label 附近添加一个新按钮

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