kivymd / KivyMD

KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphical applications. https://youtube.com/c/KivyMD https://twitter.com/KivyMD https://habr.com/ru/users/kivymd https://stackoverflow.com/tags/kivymd
https://kivymd.readthedocs.io
MIT License
2.14k stars 655 forks source link

Removing MDListItemTrailingIcon raise an exception #1644

Closed ikus060 closed 3 months ago

ikus060 commented 3 months ago

Description of the Bug

When trying to remove the MDListItemTrailingIcon from a MDListItem an exception is raised.

Code and Logs

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp

KV = '''
<MyView>:

    MDBoxLayout:
        orientation: 'vertical'

        Button:
            text: "Toggle icon"
            on_release: root.toggle()

        MDListItem:
            id: item

            MDListItemHeadlineText:
                text: "Headline"

            MDListItemTrailingIcon:
                id: trailing_icon
                icon: "trash-can-outline"

MDScreen:

    MDBoxLayout:
        id: content

        MyView:
'''

class MyView(BoxLayout):
    _trailing_icon = None

    def toggle(self):
        if self._trailing_icon:
            self.ids.item.add_widget(self._trailing_icon)
        else:
            self._trailing_icon = self.ids.trailing_icon
            self._trailing_icon.parent.remove_widget(self._trailing_icon)

class Example(MDApp):

    def build(self):
        return Builder.load_string(KV)

Example().run()

"""

class MainApp(App):
    def build(self):
        self.root = Builder.load_string(kv)

if __name__ == '__main__':
    MainApp().run()

Here the exception getting raised.

...
   File "/home/ikus060/Downloads/test_kivy.py", line 41, in toggle
     self._trailing_icon.parent.remove_widget(self._trailing_icon)
   File "/home/ikus060/workspace/PDSL/minarca.git/.venv/lib/python3.10/site-packages/kivy/uix/boxlayout.py", line 331, in remove_widget
     return super(BoxLayout, self).remove_widget(widget, *args, **kwargs)
   File "/home/ikus060/workspace/PDSL/minarca.git/.venv/lib/python3.10/site-packages/kivy/uix/layout.py", line 105, in remove_widget
     super(Layout, self).remove_widget(widget, *args, **kwargs)
   File "/home/ikus060/workspace/PDSL/minarca.git/.venv/lib/python3.10/site-packages/kivy/uix/widget.py", line 714, in remove_widget
     self.children.remove(widget)
   File "kivy/properties.pyx", line 855, in kivy.properties.ObservableList.remove
   File "kivy/properties.pyx", line 806, in kivy.properties.observable_list_dispatch
   File "kivy/properties.pyx", line 625, in kivy.properties.Property.dispatch
   File "kivy/properties.pyx", line 606, in kivy.properties.Property._dispatch
   File "kivy/_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1189, in kivy._event.EventObservers._dispatch
   File "/home/ikus060/workspace/PDSL/minarca.git/.venv/lib/python3.10/site-packages/kivy/lang/builder.py", line 60, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "<string>", line 80, in <module>
 IndexError: list index out of range

This seams to be cause by a bad condition in list.kv not handling removal of children from trailing_container

        on_children:
            if text_container.children: \
            self.children[0].pos_hint = {"top": 1} \
            if len(text_container.children) == 3 else {"center_y": .5}

Screenshots

n/A

Versions

HeaTTheatR commented 3 months ago

The bug is here:

https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/list/list.kv#L78

Fix:

        on_children:
            if text_container.children and self.children: \
            ...
ikus060 commented 3 months ago

@HeaTTheatR I think so.

Any time line to get this fixed in master ?

HeaTTheatR commented 3 months ago

I can't tell you the timing of the update. There are too many changes in my local repository that need to be tested.

ikus060 commented 3 months ago

@HeaTTheatR I understand. May I submit a pull request ?

HeaTTheatR commented 3 months ago

Try:

https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/list/list.kv#L69 https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/list/list.kv#L78

ikus060 commented 3 months ago

Fixed by #1646 Thanks @HeaTTheatR