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

Support remove_widget() on MDSwitch #1647

Closed ikus060 closed 3 months ago

ikus060 commented 3 months ago

Description of the problem

Similar to #1646, when trying to clear MDSwitch widget an exception is raised.

Describe the algorithm of actions that leads to the problem

Calling clear_widgets() on MDSwitch raises an exception.

Reproducing the problem

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

KV = '''
<MyView>:

    MDBoxLayout:
        orientation: 'vertical'

        Button:
            text: "Remove icon"
            on_release: root.remove()

        MDSwitch:
            id: switch
            active: True
            icon_active: "check"

MDScreen:

    MDBoxLayout:
        id: content

        MyView:
'''

class MyView(BoxLayout):
    _trailing_icon = None

    def remove(self):
        self.ids.switch.clear_widgets()
        # OR
        #thumb = self.ids.switch.ids.thumb
        #thumb.parent.remove_widget(thumb)

class Example(MDApp):

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

Example().run()

Screenshots of the problem

N/A

Description of Changes

New condition added to avoid AttributeError: 'NoneType' object has no attribute 'disabled'

Screenshots of the solution to the problem

N/A

Code for testing new changes

Same code below.

ikus060 commented 3 months ago

Hello @HeaTTheatR

I've made the change to the PR. Anything else need my attention to get this PR merged ?