Python KivyMD: How can I make the toggle_nav_drawer() function work?

前端 未结 1 1521
陌清茗
陌清茗 2021-01-25 04:07

I got a problem with my kv code here. I want to create a MDNavigationDrawer with a few NavigationDrawerIconButtons. The window shows up but when I clic

相关标签:
1条回答
  • 2021-01-25 04:21

    Error - AttributeError

         left_action_items: [['dots-vertical', lambda x: root.toggle_nav_drawer()]]
       File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
     AttributeError: 'LayoutPy' object has no attribute 'toggle_nav_drawer'
    

    Root Cause

    The object, LayoutPy does not has an attribute toggle_nav_drawer because the toggle_nav_drawer is defined in the instantiated child, NavigationLayout: of LayoutPy.

    Solution

    Replace root.toggle_nav_drawer() with root.ids.nav_layout.toggle_nav_drawer()

    Snippets - kv file

    BoxLayout:
        orientation: 'vertical'
    
        MDToolbar:
            id: toolbar
            title: 'Menu'
            md_bg_color: app.theme_cls.primary_color
            background_palette: 'Primary'
            background_hue: '500'
            left_action_items: [['dots-vertical', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]
    

    Output

    0 讨论(0)
提交回复
热议问题