Search code examples
pythonkivytypeerrorattributeerrorkivymd

AttributeError and TypeError in KivyMD (Python)


I got this Error when running my KivyMD app. I'm new to this field and don't know to resolve it and I have no any idea about it. But I want to prevent from this Error. I have tried many ways from the Internet. But nothing was succed. Who can resolve this Issue? Please...

.kv

MDScreen:
    name: "welcome"
    MDFloatLayout:
        md_bg_color : 1, 1, 1, 1
        Carousel:
            id: carousel
            on_current_slide: app.current_slide(self.index)
            MDFloatLayout:
                Image:
                    source: "Assets/1.png"
                    pos_hint: {"center_x": .5, "center_y": .6}
                MDLabel:
                    text: "1"
                    pos_hint: {"center_y": .087}
            MDFloatLayout:
                Image:
                    source: "Assets/2.png"
                    pos_hint: {"center_x": .5, "center_y": .6}
                MDLabel:
                    text: "2"
                    pos_hint: {"center_y": .087}

.py

class DiaryApp (MDApp):
    def build(self):
        screen_manager = ScreenManager()
        screen_manager.add_widget(Builder.load_file('kv/Diary.kv'))
        return screen_manager

    def on_start(self):
        carousel = self.root.ids.carousel
        carousel.loop = True
        Clock.schedule_interval(carousel.load_next, 3.0)

    def current_slide(self, index):
        pass

    def next(self):
        self.root.ids.carousel.load_next(mode="next")

DiaryApp().run()

Error

 Traceback (most recent call last):
   File "kivy\properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'carousel'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "D:\Kusal\Python\Projects\venv\Projects\Diary\Diary.py", line 47, in <module>
     DiaryApp().run()
           .
           .
           .
   File "D:\Kusal\Python\Projects\venv\Projects\Diary\Diary.py", line 22, in next
     self.root.ids.carousel.load_next(mode="next")
   File "kivy\properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

Solution

  • The problem is that you are trying to access the carousel id through the root ids, but that id is defined under the MDScreen, which is not the root. The fix is to access it through the MDScreen where it is defined:

    def on_start(self):
        welcome_screen = self.root.get_screen('welcome')
        carousel = welcome_screen.ids.carousel
        carousel.loop = True
        Clock.schedule_interval(carousel.load_next, 3.0)