[英]How can I access the root property of a class without getting ValueError
I am a kivy/kivymd novice learning how to make a Navigation Drawer following the kivymd website( https://kivymd.readthedocs.io/en/0.104.0/components/navigation-drawer/ ).我是 kivy/kivymd 新手,正在学习如何按照 kivymd 网站( https://kivymd.readthedocs.io/en/0.104.0/components/navigation-drawer/ )制作导航抽屉。 I have my code towards the bottom.我的代码在底部。
Whenever, I run the code it gives this error:每当我运行代码时,它都会出现此错误:
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
I know that root refers to the parent class that is in angle brackets, which in this case is the ItemDrawer.我知道 root 指的是尖括号中的父类,在这种情况下是 ItemDrawer。 So I believe it should do ItemDrawer.text_color.所以我相信它应该做 ItemDrawer.text_color。 I am a complete nube with kivy and kivymd, and I need help figuring out how to solve this issue!我是一个完整的 kivy 和 kivymd nube,我需要帮助弄清楚如何解决这个问题!
Here is my code.这是我的代码。 First is the python file that contains my string, which the Builder loads:首先是包含我的字符串的 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
"""
Here is my main.py file:这是我的 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()
The default text_color
for a OneLineIconListItem
is None
.默认text_color
的OneLineIconListItem
是None
。 If you want to us that as you have, you must set its value to something other than None
.如果你想像你一样给我们,你必须将它的值设置为None
以外的值。
You could also do something like:您还可以执行以下操作:
text_color: root.text_color if root.text_color else (0,0,0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.