Hi all,
I've tried to simplify this example as much as possible:
In my app I have Container widgets which holds a list of InnerWidgets, which should be drawn inside it.
I have to add all of them (the containers and their inner widgets) programmatically, so I though __init__ would be a good place to do that:
main.py:from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty
class InnerWidget(BoxLayout):
pass
class Container(BoxLayout):
widgets = ListProperty([InnerWidget()])
def __init__(self, **kwargs):
super(Container, self).__init__(**kwargs)
# Add all of the inner widgets in the list
for var in self.widgets:
self.add_widget(var)
class Test(BoxLayout):
def __init__(self, **kwargs):
super(Test, self).__init__(**kwargs)
self.add_widget(InnerWidget())
self.add_widget(Container())
class TestApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TestApp().run()
test.kv:
<Test>:
Button:
text: 'test'
<InnerWidget>:
orientation: 'horizontal'
Button:
text: 'inner widget'
<Container>:
orientation: 'vertical'
Button:
text: 'container'
If you look at the result, The first 'test' button shows up fine, then the inner widget that was added directly to the Test object shows fine as well.
But the Container object that was added last, only shows its own content (a 'container' button) but not its children's content (an InnerWidget).
I debugged it and saw that when the InnerWidget is added to the Container, the children list of InnerWidget is empty.
I also tried moving the add_widget calls in Container to a function to be called later (when the user presses a button), but this doesn't help.
The only way I found so far is delete the kv layout for InnerWidget and programmatically add its content:
class InnerWidget(BoxLayout):
def __init__(self, **kwargs):
super(InnerWidget, self).__init__(**kwargs)
self.add_widget(Button(text='inner widget'))
But I don't want to do that, it doesn't seem like the Kivy way.
There must be a way for me to use the kv layout.
Am I missing something?
What is the proper way for adding sub widgets programmatically, while using their kv layout?
Thanks in advance,
Joel