向内容导航抽屉添加功能

时间:2020-02-17 17:28:33

标签: python kivy kivy-language

我正在尝试向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])
            )

3 个答案:

答案 0 :(得分:0)

正如Lothric所说,您需要在ItemDrawer实例中添加一个'on_release'属性。该属性的值应该是您希望在按下按钮时调用的任何函数(如果使用方法回调,则通常为lambda)。

不是您问的,但是KivyMD在GitLab中有一个“ kitchensink”演示应用程序,用于演示其某些小部件的功能。 Here's the link

答案 1 :(得分:0)

另一种方法是从类TestNavigationDrawer()中简单地删除方法on_start() 并在.kv ScrollView文件中编写代码以从一页切换到另一页。 我刚刚开始学习,所以我的代码可能不是最佳的,但完全满足了所提到的要求。 即我要粘贴完整的代码

使用下面提供的示例代码:

main.py

from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty

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

class ContentNavigationDrawer(BoxLayout):
    screen_manager = ObjectProperty()
    nav_drawer = ObjectProperty()


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 ItemDrawer(OneLineIconListItem):
    icon = StringProperty()


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



#GUI = Builder.load_file(r"C:\Users\main.kv")
class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.theme_cls = ThemeManager()

    def build(self):
        self.theme_cls.theme_style = "Light"
        self.theme_cls.primary_palette = "Green"
        self.theme_cls.primary_hue = "200"
        #return GUI


MainApp().run()

main.kv

#: include navigationdrawer.kv

Screen:
    MDToolbar:
        id: toolbar
        pos_hint: {"top": 1}
        elevation: 10
        title: "Anything"
        left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]

    NavigationLayout:
        x: toolbar.height

        ScreenManager:
            id: screen_manager

            Screen:
                name: "scr 0"
                MDLabel:
                    text: "Screen 0"
                    halign: "center"

            Screen:
                name: "scr 1"
                MDLabel:
                    text: "Screen 1"
                    halign: "center"
            Screen:
                name: "scr 2"
                MDLabel:
                    text: "Screen 2"
                    halign: "center"


        MDNavigationDrawer:
            id: nav_drawer
            ContentNavigationDrawer:
                screen_manager: screen_manager
                nav_drawer: nav_drawer

navigationdrawer.kv

<ItemDrawer>:
    theme_text_color: "Custom"
    on_release:
        print("HI IM Pringint" )
        self.parent.set_color_item(self)

    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: "300dp", "300dp"
            source: "img/tiger.jpg"

    MDLabel:
        text: "All Starts Here!"
        font_style: "Button"
        size_hint_y: None
        height: self.texture_size[1]

    MDLabel:
        text: "Adding green energy to your life..."
        font_style: "Caption"
        size_hint_y: None
        height: self.texture_size[1]

    ScrollView:

#        DrawerList:
#            id: md_list

        MDList:
            id: md_list
            OneLineListItem:
                text: "Screen 1"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "scr 1"

            OneLineListItem:
                text: "Screen 2"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "scr 2"

谢谢,希望对您有所帮助!

答案 2 :(得分:0)

如何为内容添加功能的简短示例;

# .py file
class ItemDrawer(OneLineIconListItem):
    icon = StringProperty()

    def upload(self):
        if self.icon == "upload":
            print("Item uploaded")

# .kv file
<ItemDrawer>:
    on_release: root.upload()
相关问题