【问题标题】:Create an expansion panel in / on a new screen using kivymd and python使用 kivymd 和 python 在新屏幕中/上创建扩展面板
【发布时间】:2022-10-06 18:57:35
【问题描述】:

我创建了带有 2 个屏幕的代码,并且需要其中一个具有扩展面板。 不幸的是,我无法让面板显示其中的内容。取而代之的是,我陷入了混乱和偏头痛的一面,所以这是我的代码,一个我希望它看起来像的示例以及我设法创建的内容减去我的完整代码。

视频示例:https://www.kapwing.com/videos/62f4074bafd00100c829b84c

问题视频示例:https://www.kapwing.com/videos/62f41c828f6acd00521caae1

如视频示例所示:

第一个代码:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.expansionpanel import MDExpansionPanelOneLine
from kivymd.uix.boxlayout import MDBoxLayout

KV = \'\'\'
MDScreen:

    MDNavigationLayout:

        ScreenManager:
            id: manager

            MDScreen:
                name: \'Home\'

                AnchorLayout:
                    anchor_x: \"center\"
                    anchor_y: \"top\"
                    MDToolbar:
                        md_bg_color: 0, 0, 0, 0.5
                        title: \"Example\"
                        elevation: 10
                        left_action_items: [[\"menu\", lambda x: mud_list.set_state(\"open\")]]
                        right_action_items: [[\"dots-vertical\", lambda x:app.dropdown(x)]]

                MDNavigationDrawer:
                    id: mud_list

                    BoxLayout:
                        orientation: \'vertical\'
                        spacing: \'5dp\'
                        padding: \'5dp\'

                        ScrollView:
                            MDList:
                                OneLineIconListItem:
                                    text: \'[Settings]\'
                                    on_release:
                                        manager.current = \'Settings\'
                                        root.ids.mud_list.set_state(new_state=\'toggle\', animation=True)
                                    divider: None
                                    IconLeftWidget:
                                        icon: \'cog\'
                                        on_release:
                                            manager.current = \'Settings\'
                                            root.ids.mud_list.set_state(new_state=\'toggle\', animation=True)       

                        MDLabel:
                            text:\' By Author\'
                            size_hint_y: None
                            font_style: \'Button\'
                            height: self.texture_size[1]

            MDScreen:
                name: \'Settings\'

                AnchorLayout:
                    anchor_x: \"center\"
                    anchor_y: \"top\"

                    MDToolbar:
                        id: mdt_color
                        md_bg_color: 1, 1, 1, 1
                        elevation: 10

                MDIconButton: 
                    icon: \"keyboard-backspace\"
                    pos_hint: {\"center_x\": 0.09, \"center_y\": 0.945}
                    on_release: manager.current = \'Home\'

                MDBoxLayout:
                    size_hint: 1, 0.89
                    orientation : \'vertical\'    
                    ScrollView:
                        MDBoxLayout:
                            orientation:\'vertical\'
                            adaptive_height: True
                            padding:[dp(15),dp(15),dp(15),dp(35)]
                            spacing:dp(15)

                            Content
                                adaptive_height: True
                                orientation: \'vertical\'

                                OneLineIconListItem:
                                    text: \"Dark\"
                                    on_release:app.theme_changer2()
                                    divider: None
                                    IconLeftWidget:
                                        icon: \'weather-night\'
                                        on_release:app.theme_changer2()

                                OneLineIconListItem:
                                    text: \"Light\"
                                    on_release:app.theme_changer()
                                    divider: None
                                    IconLeftWidget:
                                        icon: \'white-balance-sunny\'
                                        on_release:app.theme_changer()

                            ScrollView:
                                MDGridLayout:
                                    id: box
                                    cols: 1
                                    adaptive_height: True
\'\'\'


class Content(MDBoxLayout):
    \"\"\"Custom content.\"\"\"

    def __draw_shadow__(self, origin, end, context=None):
        pass


class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.menu = None
        self.menu_list = None
        self.kvs = Builder.load_string(KV)
        self.screen = Builder.load_string(KV)

    def on_start(self):
        self.root.ids.box.add_widget(
            MDExpansionPanel(
                icon=\"theme-light-dark\",
                content=Content(),
                panel_cls=MDExpansionPanelOneLine(
                    text=\"Theme\",
                )
            )
        )

    def theme_changer(self):
        self.theme_cls.theme_style = \"Light\"
        self.root.ids.mdt_color.md_bg_color = [1, 1, 1, 1]

    def theme_changer2(self):
        self.theme_cls.theme_style = \"Dark\"
        self.root.ids.mdt_color.md_bg_color = [0, 0, 0, 1]

    def build(self):
        self.theme_cls.theme_style = \"Light\"
        screen = Screen()
        screen.add_widget(self.kvs)
        return self.screen


ma = MainApp()
ma.run()

第二个代码:我从这里的 kivymd 文档中得到 https://github.com/kivymd/KivyMD/wiki/Components-Expansion-Panel

第三个代码与第二个代码大致相同,但我自己制作了:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine

KV = \'\'\'
<Content>
    adaptive_height: True
    orientation: \'vertical\'

    OneLineIconListItem:
        text: \"Dark\"
        divider: None
        IconLeftWidget:
            icon: \'weather-night\'
            
    OneLineIconListItem:
        text: \"Light\"
        divider: None
        IconLeftWidget:
            icon: \'white-balance-sunny\'


ScrollView:

    MDGridLayout:
        id: box
        cols: 1
        adaptive_height: True
\'\'\'


class Content(MDBoxLayout):
    \"\"\"Custom content.\"\"\"

    def __draw_shadow__(self, origin, end, context=None):
        pass


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        self.root.ids.box.add_widget(
            MDExpansionPanel(
                icon=\"theme-light-dark\",
                content=Content(),
                panel_cls=MDExpansionPanelOneLine(
                    text=\"Theme\",
                )
            )
        )


Test().run()

我的问题是,如问题视频示例所示,扩展面板本身没有显示。

我一直在弄清楚这一点,所以在我尝试过的所有混乱中,我注意到“内容”的位置以及它下面的所有内容都与屏幕设置的锚布局有关\',导致面板显示,但内容不在里面。

与 \"content\" 或 \"MDGridlayout\" 是否具有 id: 框的效果相同。

总之,我希望能够在第二个代码中创建类似的东西,但在我的主应用程序的设置屏幕中,或者基本上将第三个代码复制并粘贴到我的主应用程序中。

哦,我以后可能会自己提出这个问题,但如果它足够简单,我怎样才能让它在主题改变时成为默认值?

    标签: python screen panel kivymd expansion


    【解决方案1】:

    花了一段时间,但在帮助下我终于明白了。这是我认为代码#Comments# 中的一些关键注释的示例。

    希望这可以帮助某人。

    from kivymd.app import MDApp
    from kivy.lang import Builder
    from kivymd.uix.boxlayout import MDBoxLayout
    from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
    
    KV = '''
    
    #THE CONTENT GOES ABOVE EVERYTHING ELSE EVEN THE 1ST SCREEN.#
    #IT IS REFERENCED LATER IN WHICH EVER SCREEN IT IS TO APPEAR USING MDGridLayout AND A def LATER ON#
    
    #It should also always have these angle brackets <>#
    
    <Content>
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
    
        OneLineIconListItem:
            text: 'Dark theme'
            on_release:app.theme_changer2()
            divider: None
            IconLeftWidget:
                icon: 'weather-night'
                on_release:app.theme_changer2()
    
        OneLineIconListItem:
            text: 'Light theme'
            on_release:app.theme_changer()
            divider: None
            IconLeftWidget:
                icon: 'white-balance-sunny'
                on_release:app.theme_changer()
    
    <Content2>
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
    
        OneLineIconListItem:
            text: 'I try to explain what i think are key points to note'
            on_release:app.theme_changer2()
            divider: None
            IconLeftWidget:
                icon: 'weather-night'
                on_release:app.theme_changer2()
    
        OneLineIconListItem:
            text: 'Hope this helps someone else'
            on_release:app.theme_changer()
            divider: None
            IconLeftWidget:
                icon: 'white-balance-sunny'
                on_release:app.theme_changer()
    
    MDScreen:
    
        MDNavigationLayout:
    
            ScreenManager:
                id: manager
    
                MDScreen:
                    name: 'Home'
    
                    #The location of this MDGridLayout shows it appears in the 1st screen.#
    
                    MDGridLayout:
                        cols: 1
                        adaptive_height: True
    
                        #This id is used to reference what content goes to which expansion panel#
    
                        id: box2
    
                    MDRaisedButton:
                        pos_hint: {"center_x": 0.5, "center_y": 0.5}
                        text: "PRESS ME"
                        on_release: manager.current = 'Home2'
    
                MDScreen:
                    name: 'Home2'
    
                    MDBoxLayout:
                        orientation: "vertical"
    
                        MDToolbar:
                            id: mdt_color
                            elevation: 10
    
                        ScrollView:
                            divider: 'None'
    
                            #The location of this MDGridLayout shows it appears in the 2nd screen.#
    
                            MDGridLayout:
                                cols: 1
                                adaptive_height: True
                                id: box
    
                    MDIconButton:
                        pos_hint: {"center_x": 0.05, "center_y": 0.945}
                        icon: "keyboard-backspace"
                        on_release: manager.current = 'Home'
    
    '''
    
    
    # Every expansion panel should have a class corresponding to it's name e.g.#
    
    
    class Content(MDBoxLayout):
        pass
    
    
    # The class name is referenced in the contents= section below#
    
    
    class Content2(MDBoxLayout):
        pass
    
    
    class MainApp(MDApp):
        def theme_changer(self):
            self.theme_cls.theme_style = "Light"
            self.root.ids.mdt_color.md_bg_color = [1, 1, 1, 1]
    
        def theme_changer2(self):
            self.theme_cls.theme_style = "Dark"
            self.root.ids.mdt_color.md_bg_color = [0, 0, 0, 1]
    
        def build(self):
            return Builder.load_string(KV)
    
        def on_start(self):
    
            # Here you see the id box used#
    
            self.root.ids.box.add_widget(
                MDExpansionPanel(
                    icon="theme-light-dark",
    
                    # Here the content name Content is referenced in content=#
    
                    content=Content(),
                    panel_cls=MDExpansionPanelOneLine(
                        text="Theme",
                    )
                )
            )
    
            # Here you see the id box2 used#
    
            self.root.ids.box2.add_widget(
                MDExpansionPanel(
                    icon="theme-light-dark",
    
                    # Here the content name Content2 is referenced in content=#
    
                    content=Content2(),
                    panel_cls=MDExpansionPanelOneLine(
                        text="Read the code #comments# for details",
                    )
                )
            )
    
    
    ma = MainApp()
    ma.run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      相关资源
      最近更新 更多