简体   繁体   中英

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/ ). 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. So I believe it should do ItemDrawer.text_color. I am a complete nube with kivy and kivymd, and I need help figuring out how to solve this issue!

Here is my code. First is the python file that contains my string, which the Builder loads:

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:

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 . If you want to us that as you have, you must set its value to something other than None .

You could also do something like:

text_color: root.text_color if root.text_color else (0,0,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM