首页 > 解决方案 > 向内容导航抽屉添加功能

问题描述

我正在尝试将功能添加到我的 kivymd 导航抽屉,但我找不到方法来做到这一点。我希望这些项目可以书写不同的页面。一个例子是设置项应该在点击时打开设置页面。我使用新更新的 kivymd 版本 0.103.0

这是一个示例代码

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty



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

    def on_start(self):
        icons_item = {
            "folder": "My files",
            "account-multiple": "Shared with me",
            "star": "Starred",
            "history": "Recent",
            "checkbox-marked": "Shared with me",
            "upload": "Upload",
        }
        for icon_name in icons_item.keys():
            self.root.ids.content_drawer.ids.md_list.add_widget(
                ItemDrawer(icon=icon_name, text=icons_item[icon_name])
            )

标签: pythonkivykivy-language

解决方案


另一种解决方案

我是一个绝对的初学者,我的解决方案可能不是最好的,甚至不是正确的。但它对我有用。

我的 kivy、kivymd 和 python 版本:

名称:基维版本:2.0.0

名称:kivymd 版本:0.104.1

蟒蛇 3.8.5

主文件

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty

from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem, MDList


class NavigationDrawer(BoxLayout):
    pass


class ContentNavigationDrawer(BoxLayout):
    pass


class ItemDrawer(OneLineIconListItem):
    icon = StringProperty()
    text_color = ListProperty((0, 0, 0, 1))


class DrawerList(ThemableBehavior, MDList):
    def set_color_item(self, instance_item):
        """Called when tap on a menu item."""

        # Set the color of the icon and text for the menu item.
        for item in self.children:
            if item.text_color == self.theme_cls.primary_color:
                item.text_color = self.theme_cls.text_color
                break
        instance_item.text_color = self.theme_cls.primary_color


class TestNavigationDrawer(MDApp):
    def build(self):
        return Builder.load_file('main.kv')

    def on_start(self):
        icons_item = {
            "home": "Home",
            "folder": "My files",
            "account-multiple": "Shared with me",
            "star": "Starred",
            "history": "Recent",
            "checkbox-marked": "Shared with others",
            "upload": "Upload",
        }
        for icon_name in icons_item.keys():
            self.root.ids.content_drawer.ids.md_list.add_widget(
                ItemDrawer(icon=icon_name, text=icons_item[icon_name])
            )


TestNavigationDrawer().run()

主文件

# Menu item in the DrawerList list.
<ItemDrawer>:
    theme_text_color: "Custom"
    # on_press: self.parent.set_color_item(self)
    on_press:
        app.root.ids.nav_drawer.set_state("close")
        app.root.ids.screen_manager.current = self.text.lower()
    

    IconLeftWidget:
        id: icon
        icon: root.icon
        theme_text_color: "Custom"
        text_color: root.text_color


<ContentNavigationDrawer>:
    orientation: "vertical"
    padding: "8dp"
    spacing: "8dp"

    AnchorLayout:
        anchor_x: "left"
        size_hint_y: None
        height: avatar.height

        Image:
            id: avatar
            size_hint: None, None
            size: "56dp", "56dp"
            source: "data/logo/kivy-icon-256.png"

    MDLabel:
        text: "KivyMD library"
        font_style: "Button"
        size_hint_y: None
        height: self.texture_size[1]

    MDLabel:
        text: "kivydevelopment@gmail.com"
        font_style: "Caption"
        size_hint_y: None
        height: self.texture_size[1]

    ScrollView:

        DrawerList:
            id: md_list


<NavigationDrawer>:
    orientation: 'vertical'

    MDToolbar:
        title: "Navigation Drawer"
        elevation: 10
        left_action_items: [["menu", lambda x: app.root.ids.nav_drawer.set_state("open")]]

    Widget:


Screen:

    NavigationLayout:

        ScreenManager:
            id: screen_manager

            Screen:
                name: "home"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "Home"
                        halign: "center"

            Screen:
                name: "my files"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "My Files"
                        halign: "center"

            Screen:
                name: "shared with me"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "Shared with me"
                        halign: "center"

            Screen:
                name: "starred"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "Starred"
                        halign: "center"

            Screen:
                name: "recent"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "Recent"
                        halign: "center"

            Screen:
                name: "shared with others"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "Shared with others"
                        halign: "center"

            Screen:
                name: "upload"
                NavigationDrawer:

                BoxLayout:
                    MDLabel:
                        text: "Upload"
                        halign: "center"


        MDNavigationDrawer:
            id: nav_drawer

            ContentNavigationDrawer:
                id: content_drawer

推荐阅读