我是一个编码初学者,我想制作一个包含导航抽屉工具栏的移动应用程序,并且应该将用户带到应用程序中的不同屏幕。我搜索了很多解决方案,但没有真正找到一个,因为有时整个代码都在。kv文件中,所以很难处理变量并打印它们,例如在以下代码中:
如何使用KivyMD在NavigationDrawer内的屏幕之间切换
我试图将它与这段代码相结合,这段代码只使用Kivy而不是KivyMD,但我失败了,因为它不适合移动应用程序:
https://www.techwithtim.net/tutorials/kivy-tutorial/multiple-screens/
我希望有人能帮助我。谢谢!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