How can I overwrite the default value of a Kivy widget's child? i.e.
MyWidget.label
is 'default' but I want to change it to e.g. 'purple turtle' when a child ofMyRootWidget
?I can access the children of children as I've done in
MyRootWidget.__init__()
, but it seems cumbersome, especially for a deep tree ... is there a more elegant way of doing it?
I've been looking over the Kivy lang and Widget pages, but haven't been able to understand a solution if it's there. I haven't seen quite this issue in SO pages (though the did answer a different question while I was searching).
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
root = Builder.load_string('''
<MyWidget@BoxLayout>:
orientation: 'vertical'
Label:
id: label
text: 'DEFAULT'
<MyRootWidget@BoxLayout>:
id: root_widget
MyWidget:
id: w1
# (---1---)
''')
class MyRootWidget(BoxLayout):
w1 = ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
print(self.ids.w1.ids.label.text) # (---2---)
class MainApp(App):
def build(self):
return MyRootWidget()
if __name__ == '__main__':
MainApp().run()