python - 在RelativeLayout中嵌入GridLayout

标签 python python-3.x kivy

我正在与 Kivy 作斗争,以使其正确显示按钮。我想创建一个在 for 循环中创建的按钮网格,并将它们显示在relativelayout 内部的网格中。这会导致按钮一层一层地堆叠。请看下面:

主.py:

import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang.builder import Builder
from kivy.uix.button import Button

Builder.load_file('TopMenu.kv')
Builder.load_file('BottomMenu.kv')
Builder.load_file('Center.kv')
Builder.load_file('Left.kv')

class mainHolder(AnchorLayout):
    pass

class MainApp(App):
    def build(self):
        self.title = "NAZWA APLIKACJI"
        #return Label(text = "Hello, world")
        return mainHolder()


if __name__ == '__main__':
    MainApp().run()

Main.kv

<MainHolder>:
    anchor_x: 'left'
    anchor_y: 'top'
    BoxLayout:
        orientation: 'vertical'
        #anchor_x: 'left'
        #anchor_y: 'top'

        TopMenu:
            id: _top_menu
            #size_hint: 1, 0.1
            #height: 0, 1
            canvas.before:
                Color:
                    rgba:0.5, 0.5, 0.5, 0.5
                Rectangle:
                    pos: self.pos
                    size: self.size
        Center:
        BottomMenu:

中心.kv:

<Center@GridLayout>:
    cols: 2
    size_hint: 1, 1
    Left:
    Label:
        text: 'prawa'

左.py:

from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button

class Left(RelativeLayout):
    def __init__(self, **kwargs):
        super(RelativeLayout, self).__init__(**kwargs)
        print("Left is alive!")

        for i in range(0, 11):
            print("Adding button: " + str(i))
            but = Button(text="X" + str(i))
            self.add_widget(but)

最后,left.kv:

#:import Left Left
<Left>:
    size_hint: 1.3, 1
    pos_hint: 1, 1

此方法创建以下内容: StackBtns 我尝试将RelativeLayout 更改为GridLayout,但按钮看起来大致相同,不同之处在于它们位于左下角,所以我认为RelativeLayout 是开始将它们放置在正确位置的好方法。

所以我在 Left.kv 中手动添加了一些 GridLayout,如下所示:

<Left>:
    size_hint: 1.3, 1
    pos_hint: 1, 1

    GridLayout:
        cols: 4
        Button:
            text: "t2"
        Button:
            text: "t2"
        Button:
            text: "t2"
        Button:
            text: "test"

效果很好。因此,接下来我创建了另一个类,这次是 GridLayout,但它根本不起作用 - 抱歉,我不再有代码,删除它,因为它没有结果。另外,如上所示添加 GridLayout 不会导致 add_widget() 将其添加到正确的位置。你们能帮我一下吗?我对此感到非常沮丧,谷歌没有提供任何合理的答案。我想从这个应用程序设计的一开始这可能就是错误的方法,但老实说,我不知道“好的”点是什么。

<小时/>

约翰·安德森谢谢你!这就像一个魅力。我是如此接近,但到目前为止,我永远不会找到这个。干杯! :-)

最佳答案

我认为使用GridLayout是一种合理的方法。我稍微修改了您的 Left 类来执行此操作:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class Left(GridLayout):
    def __init__(self, **kwargs):
        # corrected the call to super
        super(Left, self).__init__(**kwargs)

        print("Left is alive!")

        # set the number of columns in this grid
        self.cols = 5

        for i in range(0, 11):
            print("Adding button: " + str(i))
            but = Button(text="X" + str(i))
            self.add_widget(but)

关于python - 在RelativeLayout中嵌入GridLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59975778/

相关文章:

Python:修改 eml 文件中的值(电子邮件 header )

python - 在 PyQt 中将 Label 字体大小与布局同步

python - 如何在kivy应用程序中使用Windows默认文件浏览器

python - 如何通过部分标签过滤pandas数据框列

python - 使用 pip3 安装软件包时出现 "ssl module in Python is not available"

python - 通过列表理解或映射加速 numpy 数组分配?

python - Tensorflow ctc_loss_calculator : No valid path found

python - Pandas DataFrame 中的多步聚合

python - Kivy - python - 回收 View 行中的多个小部件

python - kivy iOS部署错误