Search code examples
pythonkivykivy-languagekivymd

KivyMD AttributeError: Content/TwoLineIconListItem object has no attribute '_disabled_count'


Starting from an example of the Kivy MD Expansion Panel, I would like to customize the content of the panel. The example shows the same content for each expansion panel. I would like to set values of the fields 'text' and 'secondary text'. So I modified the code as following:

from kivy.lang import Builder
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.app import MDApp
from kivymd import images_path
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelThreeLine
from kivymd.uix.list import TwoLineIconListItem

KV = '''
MDScreen:

    MDBoxLayout:
        orientation: "vertical"

        MDTopAppBar:
            title: "Expansion panel"
            elevation: 10

        ScrollView:

            MDGridLayout:
                cols: 1
                adaptive_height: True
                id: box
'''


class Content(MDBoxLayout, TwoLineIconListItem):
    def __init__(self, primaryText, secondaryText):
        self.size_hint_y = None
        self.height = self.minimum_height
        self.text = primaryText
        self.secondary_text = secondaryText


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        for i in range(10):
            
            myContent = Content('PRIMARY ' + str(i), 'SECONDARY ' + str(i))
            
            self.root.ids.box.add_widget(
                MDExpansionPanel(
                    icon="language-python",
                    content=myContent,
                    panel_cls=MDExpansionPanelThreeLine(
                        text="Text " + str(i),
                        secondary_text="Secondary text " + str(i),
                        tertiary_text="Tertiary text " + str(i),
                    )
                )
            )


Test().run()

Unexpectably, I get this error: AttributeError: 'Content' object has no attribute 'disabled_count'.

In this second version, I didn't get this error anymore, but no content is shown:

from kivy.lang import Builder

from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.app import MDApp
from kivymd import images_path
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelThreeLine
from kivymd.uix.list import TwoLineIconListItem

KV = '''
<Content>
    size_hint_y: None
    height: self.minimum_height

    TwoLineIconListItem:  
        

MDScreen:

    MDBoxLayout:
        orientation: "vertical"

        MDTopAppBar:
            title: "Expansion panel"
            elevation: 10

        ScrollView:

            MDGridLayout:
                cols: 1
                adaptive_height: True
                id: box
'''

class Content(MDBoxLayout):
    pass


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        for i in range(10):
            
            myContent = Content()
            myContent.text = 'PRIMARY ' + str(i)
            myContent.secondary_text = 'SECONDARY ' + str(i)
            
            self.root.ids.box.add_widget(
                MDExpansionPanel(
                    icon="language-python",
                    content=myContent,
                    panel_cls=MDExpansionPanelThreeLine(
                        text="Text " + str(i),
                        secondary_text="Secondary text " + str(i),
                        tertiary_text="Tertiary text " + str(i),
                    )
                )
            )


Test().run()

Solution

  • In your second version you just need to add the text and secondary_text properties to the Content class, and then reference those properties in the kv. Here is the updated Content class:

    class Content(MDBoxLayout):
        text = StringProperty('')
        secondary_text = StringProperty('')
    

    And the updated kv:

    <Content>
        size_hint_y: None
        height: self.minimum_height
    
        TwoLineIconListItem:  
            text: root.text
            secondary_text: root.secondary_text
            
    
    MDScreen:
    
        MDBoxLayout:
            orientation: "vertical"
    
            MDTopAppBar:
                title: "Expansion panel"
                elevation: 10
    
            ScrollView:
    
                MDGridLayout:
                    cols: 1
                    adaptive_height: True
                    id: box