How to set an item with two left icons in a MDList from a .kv file?

159 views
Skip to first unread message

Abel Carlos Gutiérrez Camacho

unread,
Apr 21, 2023, 11:01:55 AM4/21/23
to Kivy users support

I'm learning kivymd and I would like to create a MDList with an OneLineAvatarIconListItem that has two left icons. The docs only show how to do that with two right icons. This is my code (I changed "right" to "left" everywhere):

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.list import ILeftBodyTouch

KV = '''
OneLineAvatarIconListItem:
    text: "One-line item with avatar"
    on_size:
        self.ids._left_container.width = container.width
        self.ids._left_container.x = container.width

    YourContainer:
        id: container

        MDIconButton:
            icon: "minus"

        MDIconButton:
            icon: "plus"

    IconRightWidget:
        icon: "cog"
'''


class YourContainer(ILeftBodyTouch, MDBoxLayout):
    adaptive_width = True


class Example(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        return Builder.load_string(KV)


Example().run()

And this is the result:

afJAx.png

I guess I have to do something more...

Also I would appreciate if you could write YourContainer into KV, I tried, but it didn't work.

Elliot Garbus

unread,
Apr 22, 2023, 1:12:07 PM4/22/23
to kivy-...@googlegroups.com

Taking a look at the source code I put together the ‘hack’ below.  If I was going to put this into production, I think I would create a custom list item derived from BaseListItem.

 

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.list import ILeftBodyTouch

KV =

'''


OneLineAvatarIconListItem:
    text: "One-line item with avatar"
    on_size:
        self.ids._left_container.width = container.width
        self.ids._left_container.x = container.width

        self._txt_left_pad = dp(100)  # increase pad to make room
       

    IconRightWidget:
        icon: "cog"

    YourContainer:
        id: container
        size_hint_x: None
        width: self.minimum_width  # size the BoxLayout



        MDIconButton:
            icon: "minus"

        MDIconButton:
            icon: "plus"

'''


class YourContainer(ILeftBodyTouch, MDBoxLayout):
    adaptive_width =
True


class
Example(MDApp):
   
def build(self):
       
self.theme_cls.theme_style = "Dark"
       
return Builder.load_string(KV)


Example().run()

 

 

The result:

 

I guess I have to do something more...

 

Also I would appreciate if you could write YourContainer into KV, I tried, but it didn't work.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/94463a3c-26d0-4102-9f2f-42f6f25f95a5n%40googlegroups.com.

 

afJAx.png
Reply all
Reply to author
Forward
0 new messages