Skip to content Skip to sidebar Skip to footer

Accessing Value Of Child Widget Property In Kivy Lang And Python

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 of MyRootWidget? I ca

Solution 1:

First of all, before implementing any code, you must design your classes.

First we will do it with MyWidget, in your requirements you indicate that you want the text to be modifiable so it must be a root property.

MyWidget
┌--------------------------┐
|                          |
| ┌-------------┐   text---┼--->
| | Label       |     |    |
| |    *text ---┼-----┘    |
| └-------------┘          |--------------------------┘

The same could be done with MyRootWidget:

MyRootWidget
┌-----------------------------┐
|                             |
| ┌-------------┐ obj_widget--┼--->
| | MyWidget  --┼-----┘       |
| |             |             |
| └-------------┘             |-----------------------------┘

The depth of the property depends on your requirements, but if we observe exposing a property it implies creating a property in the root and making a binding so that when the root property is modified the internal property is modified as well.

Implementing the above you get the following:

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>:
    text: "DEFAULT"
    obj_label: label
    orientation: 'vertical'
    Label:
        id: label
        text: root.text

<MyRootWidget@BoxLayout>:
    obj_widget: w1
    MyWidget:
        id: w1
        text: "purple turtle"
''')

classMyRootWidget(BoxLayout):
    def__init__(self, **kwargs):
        super().__init__(**kwargs)
        print(self.obj_widget.text) 

classMainApp(App):
    defbuild(self):
        return MyRootWidget()

if __name__ == '__main__':
    MainApp().run()

So to avoid the ids you can create an alias to the children widget like I did with obj_widget which is an alias of w1.

By design you should not access the complete tree directly but you must modify the property of a layer and this layer if it is modified then you must update the necessary data in its internal part so we avoid the coupling between classes.

Post a Comment for "Accessing Value Of Child Widget Property In Kivy Lang And Python"