我有这个python文件
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.theming import ThemableBehavior, ThemeManager
from kivymd.uix.list import OneLineIconListItem, MDList
class ContentNavigationDrawer(BoxLayout):
pass
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
class MainApp(MDApp):
def on_start(self):
icons_item = {
'bug': 'Files'
}
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])
)
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_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
MainApp().run()
这个.kv文件 看到第3行,该错误来自未定义screen_manager
<ItemDrawer>:
theme_text_color: "Custom"
on_release: screen_manager.current = 'screen2'
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: "Custom"
text_color: 0,0,0,1
<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: "250", "250"
source: "logo.jpg"
ScrollView:
DrawerList:
id: md_list
NavigationLayout:
ScreenManager:
id: screen_manager
Screen:
name: 'screen1'
BoxLayout:
orientation: 'vertical'
Widget:
canvas:
Color:
rgba: 0,1,0.2117647058823529,0.3
Rectangle:
size: self.size
pos: self.pos
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
Screen:
name: 'screen2'
当我单击导航抽屉中的项目时,它表示未定义 screen_manager
我在做什么错?我在互联网上搜索了很多,但找不到有用的东西
请帮助我,如何更改屏幕?
答案 0 :(得分:0)
MDNavigation Drawer 应该与 ScreenManager 处于相同的缩进级别。此外,
MDNavigationLayout:
screen_manager:screen_manager
ScreenManager:
id: screen_manager
Screen:
name: 'screen1'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Navigation Drawer"
elevation: 10
left_action_items: [['menu', lambda x:nav_drawer.set_state("open")]]
Widget:
Screen:
name: 'screen2'
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
添加
screen_manager:screen_manager
在 NavigationLayout 之下,以便它可以作为 ObjectProperty访问。最后将 ItemDrawer 回调从
on_release: screen_manager.current = 'screen2'
到
on_release: app.root.screen_manager.current = 'screen2'
最后你也可以添加
nav_drawer: nav_drawer
在您的根窗口下方和顶部/下方行screen_manager: screen_manager
这可以在新屏幕弹出时关闭导航抽屉。
然后将您的回调更新为:
on_release:
app.root.screen_manager.current = 'screen2'
app.root.nav_drawer.set_state('close')