如何在kivymd中进行gridlayout?

时间:2020-09-14 12:05:17

标签: python kivy kivymd

我正在尝试在kivymd中进行网格布局。创建了GridLayout,但宽度未填充屏幕。如何在kivymd中扩展列的宽度以适合屏幕?

我使用了kivymd文档中的示例,并使用它创建了网格布局。

app.py

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from main_screen_str import helper_string
from kivy.core.window import Window

Window.size = (300, 500)


class MainScreen(Screen):
    pass


class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.sm = ScreenManager()
        self.sm.add_widget(MainScreen(name="main_screen"))

        self.main_str = Builder.load_string(helper_string)

    def build(self):
        screen = Screen()
        screen.add_widget(self.main_str)
        return screen


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

这是构建器字符串。创建了网格布局,但是宽度不适合屏幕。 如何在gridlayout中扩展单个列的宽度?

构建字符串

helper_string = """
ScreenManager:
    MainScreen:

<MainScreen>:
    name: 'main_screen'
        
    MDGridLayout:
        cols: 3
        
        MDIconButton:
            icon: "android"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color
        MDIconButton:
            icon: "android"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color
            
        MDIconButton:
            icon: "android"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color
        MDIconButton:
            icon: "android"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color
            
        MDIconButton:
            icon: "android"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color
            
        MDIconButton:
            icon: "android"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color 
"""

1 个答案:

答案 0 :(得分:1)

根据MDIconBuuton Documentation

默认情况下,MDIconButton按钮的大小为(dp(48),dp(48))

然后GridLayout将使用这些大小值来调整列的大小。您可以通过调整MDIconButtons的大小来调整列的大小。因此,如果您仅添加:

size_hint_x: 0.33

对于每个MDIconButton,每一列将是MainScreen宽度的三分之一。