【问题标题】:Adding width to parent layout by button release using Kivy language使用 Kivy 语言通过按钮释放向父布局添加宽度
【发布时间】:2021-12-26 20:15:06
【问题描述】:

我有一个简单的问题,我似乎找不到直接的答案。我正在尝试通过单击子按钮将父布局宽度增加特定的增量。

这是我想要实现的一个示例:

GridLayout:
    width: 200
    size_hint_x: None
    cols:1
    Button:
        size_hint:1, 0.07
        text:"Add"
        on_release: root.parent.width + 100
    Button:
        size_hint:1, 0.07
        text:"Remove"
        on_release: root.parent.width - 100

在这个简单的示例中,按下Add 按钮应该将100 宽度添加到父网格布局,反之亦然,使用Remove 按钮。但是,不知道如何用on_release: root.parent.width 调用宽度的变化。

有什么想法吗?

【问题讨论】:

    标签: kivy kivy-language


    【解决方案1】:

    当您提及root.parent 时,您指的是包含出现在您的kv 中的GridLayout 的任何内容。如果要调整GridLayout 的大小,只需使用root.width。此外,使用 + 运算符不会更改值,它只是添加到值并返回结果。尝试将您的 kv 更改为:

    GridLayout:
        width: 200
        size_hint_x: None
        cols:1
        Button:
            size_hint:1, 0.07
            text:"Add"
            on_release: root.width += 100
        Button:
            size_hint:1, 0.07
            text:"Remove"
            on_release: root.width -= 100
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 2014-11-23
      • 2021-04-17
      • 2023-03-09
      • 1970-01-01
      • 2017-03-23
      • 2013-04-17
      相关资源
      最近更新 更多