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

Fix Enabling/Disabling MDTextField #1648

Closed ikus060 closed 3 months ago

ikus060 commented 3 months ago

Description of the problem

An exception is raised when enabling or disabling MDTextField. See #1643

Describe the algorithm of actions that leads to the problem

See #1643

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: "Toggle state"
            on_release: root.toggle()

        MDTextField:
            id: field

            MDTextFieldHintText:
                text: "Hint text"

MDScreen:

    MDBoxLayout:
        id: content

        MyView:
'''

class MyView(BoxLayout):

    def toggle(self):
        self.ids['field'].disabled = not self.ids['field'].disabled 

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()

Screenshots of the problem

N/A

Description of Changes

The exception is raised because the function _set_enabled_colors and _set_disabled_colors tried to update the colors of every individual child widget of the MDTextField regardless if the the widget exists or not.

I've added new condition to check if the widget exists before updating the color of it.

Screenshots of the solution to the problem

N/A

Code for testing new changes

Same as above.