简体   繁体   English

如何在不获取 ValueError 的情况下访问类的根属性

[英]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_colorOneLineIconListItemNone 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.

相关问题 如何使用 plotly 库 plot 而不会出现 ValueError? - How can I plot by using plotly library without getting ValueError? 如何在没有root访问权限的情况下完全安装numpy? - How can I install numpy exactly without root access on server? 如何访问另一个基础 class 的基础 class 的属性? - How can I access a property of the base class of another base class? 我如何在不获取AttributeError的情况下访问kivy中另一个类的对象属性:&#39;super&#39;对象没有属性&#39;__getattr__&#39; - How can i access object properties of another class in kivy without getting AttributeError: 'super' object has no attribute '__getattr__' 为什么我在 Python 中收到 ValueError 以及如何修复它? - Why am I getting ValueError in Python and how can I fix it? 如何在不按名称获取根节点的情况下读取它们的属性? - How can I read the attributes of a root node without getting them by name? 如何在没有 root 访问权限的服务器上杀死其他人的 Python 进程? - How can I kill someone else's Python processes on a server without root access? 如何在没有ROOT ACCESS的情况下安装python模块YAML(&#39;easy_install&#39;和&#39;pip&#39;不可用)? - How can I install the python module YAML without ROOT ACCESS ( 'easy_install' and 'pip' are not available)? 如何在不使用异常的情况下解决 ValueError dtype - How Can I Solve ValueError dtype Without Using An Exception 我可以在不扩展该类的情况下使用另一个类的属性吗? - Can I use another class's property without extending that class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM