【发布时间】:2016-10-01 14:52:14
【问题描述】:
好吧,假设我希望某个小部件中的标签使用另一个小部件内标签中的文本:
<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
这可行,但似乎不是很好的解决方案。如果我将 first 放在另一个小部件中,我需要在代码中的任何地方更改对它的引用(这可能会导致错误)。
我的第一个想法是至少在根级别存储对first 的引用并对其进行引用:
<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
但这会导致异常:
AttributeError: 'NoneType' object has no attribute 'text'
这很令人困惑,因为如果我写像 text: str(root.parent.l) 这样的东西,我会看到 Label object 而不是 NoneType。
所以我有两个问题:
- 为什么第二个解决方案不起作用?如何解决?
- 一般来说,从另一个小部件访问某些小部件属性的最佳方式是什么?我可以让它独立于小部件层次结构吗?
【问题讨论】:
标签: python kivy kivy-language