如何在不获取 ValueError 的情况下访问 class 的根 属性
How can I access the root property of a class without getting ValueError
我是一个 kivy/kivymd 新手,正在学习如何按照 kivymd 网站 ( https://kivymd.readthedocs.io/en/0.104.0/components/navigation-drawer/) 制作导航抽屉。我的代码在底部。
任何时候,我 运行 它给出这个错误的代码:
kivy.lang.builder.BuilderException: Parser: File "<inline>", line 10:
...
8: icon: root.icon
9: theme_text_color: "Custom"
>> 10: text_color: root.text_color
11:
12:<ContentNavigationDrawer>:
...
ValueError: None is not allowed for IconLeftWidget.text_color
我知道 root 是指尖括号中的父项 class,在本例中是 ItemDrawer。所以我相信它应该做 ItemDrawer.text_color。我是 kivy 和 kivymd 的忠实粉丝,我需要帮助来弄清楚如何解决这个问题!
这是我的代码。首先是包含我的字符串的 python 文件,Builder 加载该文件:
proof_helper = """
<ItemDrawer>:
theme_text_color: "Custom"
on_release: self.parent.set_color_item(self)
#invokes DrawerList set_color_item method
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: "Custom"
text_color: root.text_color
<ContentNavigationDrawer>:
orientation: 'vertical'
padding: '8dp'
spacing: '8dp'
ScrollView:
DrawerList:
id: md_list
Screen:
MDNavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Navigation Drawer"
elevation: 8
left_action_items : [["menu", lambda x: nav_drawer.set_state()]]
Widget:
Screen:
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
"""
这是我的 main.py 文件:
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivy.lang import Builder
from kivymd.uix.list import MDList, OneLineListItem, OneLineIconListItem
from kivy.core.window import Window
from proof_nav import proof_helper
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
Window.size = (300, 500)
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_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
class ProofApp(MDApp):
def build(self):
screen = Builder.load_string(proof_helper)
return screen
def on_start(self):
icons_item = {
"folder": "My files",
"account-multiple": "Shared with me",
"star": "Starred",
"history": "Recent",
"checkbox-marked": "Shared with me",
"upload": "Upload",
}
for item in icons_item:
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon=item, text=icons_item[item])
)
ProofApp().run()
OneLineIconListItem
的默认 text_color
是 None
。如果你想让我们像你一样,你必须将它的值设置为 None
.
以外的值
你也可以这样做:
text_color: root.text_color if root.text_color else (0,0,0)
我是一个 kivy/kivymd 新手,正在学习如何按照 kivymd 网站 ( https://kivymd.readthedocs.io/en/0.104.0/components/navigation-drawer/) 制作导航抽屉。我的代码在底部。
任何时候,我 运行 它给出这个错误的代码:
kivy.lang.builder.BuilderException: Parser: File "<inline>", line 10:
...
8: icon: root.icon
9: theme_text_color: "Custom"
>> 10: text_color: root.text_color
11:
12:<ContentNavigationDrawer>:
...
ValueError: None is not allowed for IconLeftWidget.text_color
我知道 root 是指尖括号中的父项 class,在本例中是 ItemDrawer。所以我相信它应该做 ItemDrawer.text_color。我是 kivy 和 kivymd 的忠实粉丝,我需要帮助来弄清楚如何解决这个问题!
这是我的代码。首先是包含我的字符串的 python 文件,Builder 加载该文件:
proof_helper = """
<ItemDrawer>:
theme_text_color: "Custom"
on_release: self.parent.set_color_item(self)
#invokes DrawerList set_color_item method
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: "Custom"
text_color: root.text_color
<ContentNavigationDrawer>:
orientation: 'vertical'
padding: '8dp'
spacing: '8dp'
ScrollView:
DrawerList:
id: md_list
Screen:
MDNavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Navigation Drawer"
elevation: 8
left_action_items : [["menu", lambda x: nav_drawer.set_state()]]
Widget:
Screen:
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
"""
这是我的 main.py 文件:
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivy.lang import Builder
from kivymd.uix.list import MDList, OneLineListItem, OneLineIconListItem
from kivy.core.window import Window
from proof_nav import proof_helper
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
Window.size = (300, 500)
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_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
class ProofApp(MDApp):
def build(self):
screen = Builder.load_string(proof_helper)
return screen
def on_start(self):
icons_item = {
"folder": "My files",
"account-multiple": "Shared with me",
"star": "Starred",
"history": "Recent",
"checkbox-marked": "Shared with me",
"upload": "Upload",
}
for item in icons_item:
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon=item, text=icons_item[item])
)
ProofApp().run()
OneLineIconListItem
的默认 text_color
是 None
。如果你想让我们像你一样,你必须将它的值设置为 None
.
你也可以这样做:
text_color: root.text_color if root.text_color else (0,0,0)