Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support remove_widget() on MDSwitch #1647

Merged

Conversation

ikus060
Copy link
Contributor

@ikus060 ikus060 commented Mar 12, 2024

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 ikus060 force-pushed the patrik-fix-remove-widget-selectioncontrol branch from 389a191 to 7b5a372 Compare March 12, 2024 19:30
@@ -56,6 +56,8 @@
pos_hint: {"center_x": .5, "center_y": .5}
theme_text_color: "Custom"
text_color:
self.theme_cls.surfaceContainerHighestColor if not root.parent else \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would solve this in the following way:

<Thumb>
    [...]

    ThumbIcon:
        [...]

        text_color:
            [...]

            if root.parent else self.theme_cls.transparentColor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Thanks for the recommendation. I pushed the modification.

@ikus060 ikus060 force-pushed the patrik-fix-remove-widget-selectioncontrol branch from 7b5a372 to 5a03c6d Compare March 12, 2024 20:02
@ikus060
Copy link
Contributor Author

ikus060 commented Mar 14, 2024

Hello @HeaTTheatR

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

@HeaTTheatR HeaTTheatR merged commit 4e9fdba into kivymd:master Mar 22, 2024
3 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants