本文介绍了如何使用KivyMD在NavigationDrawer中的屏幕之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用kivymd的初学者,并尝试使用KivyMD在NavigationDrawer中的屏幕之间切换,我正在阅读一些文档,并最终获得了这段代码,但它对我不起作用:

I'm beginner in using kivymd and trying to switch between screens within the NavigationDrawer using KivyMD, I was reading some of the documents and ended up with this code but it's not working with me:

from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import MDList
from kivymd.app import MDApp



Window.size = (300, 500)
navigation_helper = """
Screen:
    MDToolbar:
        id: toolbar
        pos_hint: {"top": 1}
        elevation: 10
        title: "MDNavigationDrawer"
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

    NavigationLayout:
        x: toolbar.height

        ScreenManager:
            id: screen_manager

            Screen:
                name: "scr 1"

                MDLabel:
                    text: "Go to Hell"
                    halign: "center"

            Screen:
                name: "scr 2"

                MDLabel:
                    text: "Hell"
                    halign: "center"

        MDNavigationDrawer:
            id: nav_drawer

            ContentNavigationDrawer:
                screen_manager: screen_manager
                nav_drawer: nav_drawer

                ScrollView:
                    MDList:

                        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"


"""



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

    class DrawerList(ThemableBehavior, MDList):
        pass

    def build(self):
        screen = Builder.load_string(navigation_helper)
        return screen

    def on_start(self):
        pass


DemoApp().run()

但这给了我这个错误"AttributeError:屏幕"对象没有属性"nav_drawer"",我不确定自己做错了什么.

But it's giving me this error " AttributeError: 'Screen' object has no attribute 'nav_drawer'", I'm not sure what I've done incorrect.

推荐答案

在定义 id 的同一规则中使用 id 引用对象时,您无需在其前面加上 root :

When you are referencing an object using an id within the same rule where the id is defined, you do not need to prepend it with root:

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

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

这篇关于如何使用KivyMD在NavigationDrawer中的屏幕之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 02:52