【问题标题】:Python Kivy library adding widgetsPython Kivy 库添加小部件
【发布时间】:2021-08-29 18:05:23
【问题描述】:

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

是否可以在 python 代码中以 kv 语言访问现有布局?还是唯一的解决方案是在 python 中写入整个窗口?

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

编辑:

我有这样的事情

Kv:

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

Python:

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

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

【问题讨论】:

  • 一般来说,你可以随意混合和匹配 python 和 kv,并且你可以期望在任何一个中创建的小部件之间进行通信很容易。 This blog post 讨论各种方法。
  • 可以添加一些可运行的代码以便我们为您提供帮助
  • 我认为@inclement 发送的博客文章是解决方案,我一定会检查它,但如果您知道我的问题的解决方案,请继续添加!

标签: python kivy kivy-language


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多