在kivy中,每行的列数可变吗?

2024-06-17 15:37:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在kivy中,制作每行列数可变的屏幕的首选方法是什么?有没有一种方法可以在不显式地指定布局中控件的位置和大小的情况下实现这一点(例如,有没有一种方法可以像在一个屏幕中堆叠一堆具有不同行数和列数的网格布局一样做到这一点)?只使用python代码的方法是什么?在

例如,假设您有一个包含某种类型布局的屏幕,称为“Layout_scr1”。例如,布局的第一行包含1列,第二行包含2列,第三行包含4列,您将如何安排这些内容?谢谢您。在


Tags: 方法代码网格类型内容屏幕情况布局
1条回答
网友
1楼 · 发布于 2024-06-17 15:37:14

有很多选择,但我认为最简单的方法是使用BoxLayout而不是GridLayout甚至{}。StackLayout可以转到第二行,宽度不够,而BoxLayout和{}在同一行。您可以找到并解释BoxLayout和{}here之间的区别。在

输出如下:

enter image description here

代码如下:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Boxes>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                BoxLayout: 
                    orientation: 'vertical'
                    padding: 50
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "1"
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "2"
                        Button:
                            text: "3"
                        Button:
                            text: "4"
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "5"
                        Button:
                            text: "6"
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "7"
                        Button:
                            text: "8"
                        Button:
                            text: "9"
                        Button:
                            text: "10"
            Screen:
                name: 'screen2'
                Label: 
                    text: 'Another Screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Boxes(FloatLayout):
    pass

class TestApp(App):
    def build(self):
        return Boxes()

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

如果您仍然想使用GridLayouts,您可以替换:

^{pr2}$

为此:

GridLayout: 
    cols: 1

还有这个:

^{pr2}$

为此:

GridLayout: 
    cols: 1

如果你想找一个更具活力的方法:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

Builder.load_string("""
<Boxes>:
    boxes: _boxes 
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                BoxLayout: 
                    orientation: 'vertical'
                    padding: 50
                    id: _boxes
            Screen:
                name: 'screen2'
                Label: 
                    text: 'Another Screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Boxes(FloatLayout):
    def __init__(self, **kwargs):
        super(Boxes, self).__init__(**kwargs)
        bx1 = BoxLayout(orientation='horizontal')
        bx2 = BoxLayout(orientation='horizontal')
        bx3 = BoxLayout(orientation='horizontal')
        bx4 = BoxLayout(orientation='horizontal')

        for i in range(1,2):
            bx1.add_widget(Button(text=str(i)))
        for i in range(2,5):
            bx2.add_widget(Button(text=str(i)))
        for i in range(5,7):
            bx3.add_widget(Button(text=str(i)))
        for i in range(7,11):
            bx4.add_widget(Button(text=str(i)))

        self.boxes.add_widget(bx1)
        self.boxes.add_widget(bx2)
        self.boxes.add_widget(bx3)
        self.boxes.add_widget(bx4)


class TestApp(App):
    def build(self):
        return Boxes()

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

相关问题 更多 >