简体   繁体   English

如何在 KivyMD (Python) 中结合导航抽屉和多个屏幕?

[英]How to combine Navigation Drawer and multiple Screens in KivyMD (Python)?

I'm a coding beginner and I want to make a mobile app which contains a Toolbar with a Navigation Drawer and that one should bring the user to different screens in the app.我是一名编码初学者,我想制作一个包含带有导航抽屉的工具栏的移动应用程序,并且应该将用户带到应用程序中的不同屏幕。 I searched a lot around for a solution, but didn't really found one, because sometimes the whole code is in the.kv file, so it is hard to handle variables and print them for example such as in this code:我搜索了很多解决方案,但并没有真正找到,因为有时整个代码都在 .kv 文件中,因此很难处理变量并打印它们,例如在此代码中:

How to switch between screens within the NavigationDrawer using KivyMD 如何使用 KivyMD 在 NavigationDrawer 中的屏幕之间切换

I tried to combine it with this code which uses only Kivy instead of KivyMD but I failed bacause it isn't really for mobile Apps:我试图将它与仅使用 Kivy 而不是 KivyMD 的代码结合起来,但我失败了,因为它不是真正的移动应用程序:

https://www.techwithtim.net/tutorials/kivy-tutorial/multiple-screens/ https://www.techwithtim.net/tutorials/kivy-tutorial/multiple-screens/

I hope someone can help me.我希望有一个人可以帮助我。 Thx!谢谢!

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

from kivymd.app import MDApp

KV = '''
<ContentNavigationDrawer>:

    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"


Screen:

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

    MDNavigationLayout:
        x: toolbar.height

        ScreenManager:
            id: screen_manager

            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
'''


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


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


TestNavigationDrawer().run()

https://kivymd.readthedocs.io/en/latest/components/navigation-drawer/#switching-screens-in-the-screenmanager-and-using-the-common-mdtoolbar https://kivymd.readthedocs.io/en/latest/components/navigation-drawer/#switching-screens-in-the-screenmanager-and-using-the-common-mdtoolbar

I have a problem, when I try to add a command to a button inside the screen it says: " AttributeError: 'Screen' object has no attribute 'cl'"(cl is the name of the command)我有一个问题,当我尝试向屏幕内的按钮添加命令时,它显示:“AttributeError: 'Screen' object has no attribute 'cl'”(cl 是命令的名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM