Search code examples
pythonkivykivymd

How do I make a trailing button in MDListItem in kivymd 2.0.1


I have recently updated my kivymd to 2.0.1 and I have run into a problem. I cannot make a trailing button in MDListItem the same way I could in earlier versions. The only documentation I found talks about MDListItemTrailing Icon.

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.list import MDListItem

kv = """
Screen:
    CustomMDListItem:
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        MDListItemHeadlineText:
            text: "Упражнение 1"
        MDListItemTrailingIcon:
            icon: "information"
            MDButton:
                on_press: print("information")
"""


class CustomMDListItem(MDListItem):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


class MyApp(MDApp):
    def build(self):
        return Builder.load_string(kv)


if __name__ == "__main__":
    MyApp().run()

Here are the results of this code. The trailing icon cannot be pressed

I tried putting a button in there but it didn't work.

I tried putting a MDBoxLayout directly into the CustomMDListItem. It did work but I was unable to put the buttons to the end of the screen. pos_hint did not help (they didn't move at all). This is the code with the MDBoxLayout:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.list import MDListItem

kv = """
Screen:
    CustomMDListItem:
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        MDListItemHeadlineText:
            text: "Упражнение 1"
        MDBoxLayout:
            MDIconButton:
                icon: "information"
                on_press: print("information")
            MDIconButton:
                icon: "menu"
                on_press: print("menu")
            
"""


class CustomMDListItem(MDListItem):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


class MyApp(MDApp):
    def build(self):
        return Builder.load_string(kv)


if __name__ == "__main__":
    MyApp().run()

and here are the results Also, on a somewhat unrelated note, is it better to use older versions of kivymd (like 1.20 or something) that stood the test of time or the new 2.0.1. I'm not sure if I have become an accidental beta-tester or the 2.0.1 version is a finished product.


Solution

  • You code is trying to add the MDButton inside the MDListItemTrailingIcon, but the MDListItemTrailingIcon does not accept MDButton as a child. Try just adding the MDButton to the CustomMDListItem like this:

    Screen:
        CustomMDListItem:
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            MDListItemHeadlineText:
                text: "Упражнение 1"
            MDListItemTrailingIcon:
                icon: "information"
            MDButton:
                on_press: print("information")
    

    Note the change in indentation.

    Or perhaps you can replace the MDListItemTrailingIcon with a MDIconButton:

    Screen:
        CustomMDListItem:
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            MDListItemHeadlineText:
                text: "Упражнение 1"
            MDIconButton:
                icon: "information"
                on_press: print("information")