Bovaz
Bovaz

Reputation: 375

Kivy: set ListProperty from kv language

I am using Kivy to build a simple app that would load different images in different tabs of a tabbed panel. The different Panel items should all behave similarly, but with different images, so I created a widget class. I am trying to initialize my app using the kv language like in many examples.
Currently, I am unable to make it work, because I cannot find how to pass the file names in a list from the kv language part to the widget instance. I am able to work with other Properties, but the ListProperty has me stumped.

Here is a snippet from my code:

Builder.load_string("""
<MyMainClass>:
    #stuff
    TabbedPanelItem:
        MyClassLayout:
            filenames: ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'pic4.jpg']
    #other TabbedPanelItems like the one above, 
    #with different strings in the list
""")

def MyMainClass(TabbedPanel):
    pass

def MyClassLayout(FloatLayout):
    filenames = ListProperty([])
    #rest of my class

Things I already tried:

The result is always that the filenames list in my widget is always at the default value. That would be [] in the snippet above, or whatever I set in its declaration in my class.

Would someone please point out what it is I am doing wrong?

Thanks.

Upvotes: 0

Views: 907

Answers (1)

Bovaz
Bovaz

Reputation: 375

I managed to fix this.

The issue was that I was trying to read the lists in the constructor. However, they receive their value from the kv lang part after the widget object has finished its constructor.

As a fix, I call the method that reads the list like so:

Clock.schedule_once(self.late_init, 0.02)

I hope people find this and it helps them.

Upvotes: 1

Related Questions