0

I'm just learning, and I tried to run this simple code : ihave a problem , can we help me : this is my code for my apps:


from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivymd.app import MDApp

KV = """
MDFloatLayout:

    Carousel:



        MDFloatLayout:
            id : slide     


            MDTextField:
                hint_text: "First Name"

                size_hint_x: 0.5
                icon_right: "account"
                pos_hint:{"center_x":0.5,"center_y":0.5}
                font_style: "H6"
                text_color: "Custom"
            MDTextField:
                hint_text: "Last Name"
                size_hint_x: 0.5
                icon_right: "account"
                pos_hint:{"center_x":0.5,"center_y":0.4}

            MDRaisedButton:
                text:"NEXT"
                size_hint_x:0.2
                pos_hint: {"center_x":0.5,"center_y":0.2}
                on_release: app.next()

"""


class Jeu(FloatLayout):
    def debut(self):
        self.size = Window.size
        self.add_widget(Image(source='w2.jpg', allow_stretch=True, keep_ratio=False))


class ViTVApp(MDApp):
    def build(self):
        kv1 = Builder.load_string(KV)
        kv = Jeu()
        img = Image(source="1611647504798.png",
                    size_hint_y=0.15, size_hint_x=0.15, pos_hint={'center_x': 0.5, 'center_y': 0.85}, opacity=0.8)

        kv.debut()
        kv.add_widget(img)
        kv.add_widget(kv1)
        return kv

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



if __name__ == "__main__":
    ViTVApp().run()

The console repeatedly gives this error, but the window pops up just fine:

File "", line 30, in File "E:\Developpement\ViTV\P1\P3.py", line 64, in next self.root.ids.slide.ids.carousel.load_next(mode="next") File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.getattr AttributeError: 'super' object has no attribute 'getattr'

1 Answers1

1

The problem is that you are trying to access an id in a widget that has no ids. The root widget of your App is the Jeu object, but only widgets built using kv (like kv1) will have ids, so self.root.ids will fail. Plus, the only id that you have defined in your kv is slide, so any attempt to access an id of carousel will also fail.

One way to fix this is to actually define a carousel id in your kv like this:

KV = """
MDFloatLayout:

    Carousel:
        id: carousel  # new id

        MDFloatLayout:
        ...
        ...

Then, save a reference to the widget built using that kv, and use that reference to access the Carousel widget:

class ViTVApp(MDApp):
    def build(self):
        self.kv1 = Builder.load_string(KV)  #save reference
        kv = Jeu()
        img = Image(source="1611647504798.png",
                    size_hint_y=0.15, size_hint_x=0.15, pos_hint={'center_x': 0.5, 'center_y': 0.85}, opacity=0.8)

        kv.debut()
        kv.add_widget(img)
        kv.add_widget(self.kv1)
        return kv

    def next(self):
        self.kv1.ids.carousel.load_next(mode="next")  # use saved reference
John Anderson
  • 35,991
  • 4
  • 13
  • 36