0

在 kivy 中,制作每行列数可变的屏幕的首选方法是什么?有没有办法在不明确指定布局中小部件的位置和大小的情况下完成此操作(即,有没有办法做到这一点,就好像您在屏幕内堆叠一堆具有不同行数和列数的 GridLayouts)?仅使用 python 代码执行此操作的方法是什么?

例如,假设您有一个包含某种类型布局的屏幕,称为“layout_scr1”。您将如何安排事情,例如 layout_scr1 的第一行包含 1 列,第二行包含 2 列,第三行包含 4 列?谢谢你。

4

1 回答 1

4

有很多选择,但我认为最简单的方法是使用BoxLayout代替GridLayout或什至StackLayout. StackLayout可以转到第二行,宽度不够,而BoxLayout保持GridLayout在同一行。您可以在此处BoxLayout找到和解释和之间的区别。GridLayout

这是输出:

在此处输入图像描述

这是代码:

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,您可以替换:

BoxLayout: 
    orientation: 'vertical'

为了这:

GridLayout: 
    cols: 1

和这个:

BoxLayout: 
    orientation: 'vertical'

为了这:

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()
于 2013-09-23T16:24:18.190 回答