我想将 kivy list-item 与 on_press:root 绑定。manager.current = 'screen1'
I want to bind kivy list-item with on_press:root.manager.current = 'screen1'
我是初学者(抱歉代码笨拙)。
我想编写代码,以便每当我单击 header 中的菜单图标时,导航栏都会切换。项目列表出现在导航栏中。 (这有效)
当我单击导航栏中的项目时,它会将我带到另一个屏幕(这不起作用)。
下面是代码。它显示 'NoneType' 没有 'current'
的属性
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import MDList
from kivy.uix.screenmanager import Screen, ScreenManager
navigation_helper = """
MDScreen:
MDNavigationLayout:
ScreenManager:
MDScreen:
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Title'
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
elevation:10
Widget:
Screen1:
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
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: "180dp", "100dp"
source: "logo.png"
ScrollView:
DrawerList:
id: md_list
MDList:
OneLineIconListItem:
text: "Screen 1"
on_press: root.manager.current = 'screen1'
IconLeftWidget:
icon: "account-multiple"
<Screen1>:
name: 'screen1'
MDLabel:
text: 'Profile'
halign: 'center'
"""
class Screen1(Screen):
pass
sm = ScreenManager()
sm.add_widget(Screen1(name='screen1'))
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
pass
class NavBar(MDApp):
def build(self):
screen = Builder.load_string(navigation_helper)
self.theme_cls.primary_palette = 'Indigo'
return screen
def on_start(self):
pass
NavBar().run()
默认情况下 root
是 None
,因此当您编写 on_press: root.manager.current = 'screen1'
时会引发预期的错误,AttributeError
。
但还要注意,即使您删除了它,您也会收到另一个错误 NameError
,因为您没有提供对 'manager'.
的任何引用
因此您需要进行更改,
MDNavigationLayout:
ScreenManager:
id: manager # Use it as id keeping the name as it is.
MDScreen:
还有,
MDList:
OneLineIconListItem:
text: "Screen 1"
on_press: manager.current = 'screen1'
IconLeftWidget:
icon: "account-multiple"
我是初学者(抱歉代码笨拙)。 我想编写代码,以便每当我单击 header 中的菜单图标时,导航栏都会切换。项目列表出现在导航栏中。 (这有效) 当我单击导航栏中的项目时,它会将我带到另一个屏幕(这不起作用)。 下面是代码。它显示 'NoneType' 没有 'current'
的属性from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import MDList
from kivy.uix.screenmanager import Screen, ScreenManager
navigation_helper = """
MDScreen:
MDNavigationLayout:
ScreenManager:
MDScreen:
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Title'
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
elevation:10
Widget:
Screen1:
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
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: "180dp", "100dp"
source: "logo.png"
ScrollView:
DrawerList:
id: md_list
MDList:
OneLineIconListItem:
text: "Screen 1"
on_press: root.manager.current = 'screen1'
IconLeftWidget:
icon: "account-multiple"
<Screen1>:
name: 'screen1'
MDLabel:
text: 'Profile'
halign: 'center'
"""
class Screen1(Screen):
pass
sm = ScreenManager()
sm.add_widget(Screen1(name='screen1'))
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
pass
class NavBar(MDApp):
def build(self):
screen = Builder.load_string(navigation_helper)
self.theme_cls.primary_palette = 'Indigo'
return screen
def on_start(self):
pass
NavBar().run()
默认情况下 root
是 None
,因此当您编写 on_press: root.manager.current = 'screen1'
时会引发预期的错误,AttributeError
。
但还要注意,即使您删除了它,您也会收到另一个错误 NameError
,因为您没有提供对 'manager'.
因此您需要进行更改,
MDNavigationLayout:
ScreenManager:
id: manager # Use it as id keeping the name as it is.
MDScreen:
还有,
MDList:
OneLineIconListItem:
text: "Screen 1"
on_press: manager.current = 'screen1'
IconLeftWidget:
icon: "account-multiple"