Ok let's say I want that label in some widget to use text from label inside another widget:
<SubWidget@RelativeLayout>:
Label:
text: str(root.parent.ids.first.text)
<RootWidget>:
Label:
id: first
center_x: 100
text: "text"
SubWidget:
id: second
center_x: 200
This works but doesn't seem to be nice solution. If I'll place first
inside another widget I'll need to change reference to that it everywhere in the code (that can lead to errors).
My first idea was at least to store reference to first
at root level and reference to it:
<SubWidget@RelativeLayout>:
Label:
text: str(root.parent.l.text)
<RootWidget>:
l: first
Label:
id: first
center_x: 100
text: "text"
SubWidget:
id: second
center_x: 200
But this leads to exception:
AttributeError: 'NoneType' object has no attribute 'text'
This is confusing since if I'll write something like text: str(root.parent.l)
I'll see Label object
rather than NoneType
.
So I have two questions:
- Why doesn't second solution works? How it can be fixed?
- In general, what's the best way to access some widget attribute from another widget? Can I make it independent of widgets hierarchy?