0
好讓說,我想這個標籤在一些小部件使用的文本來自標籤其他窗口內:如何從Kivy中的其他窗口部件訪問某些窗口小部件屬性?
<[email protected]>:
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
:
<[email protected]>:
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
。
所以我有兩個問題:
- 爲什麼沒有第二個解決方案的工作?如何修復?
- 一般來說,從另一個小部件訪問某些小部件屬性的最佳方式是什麼?我可以使它獨立於小部件層次結構嗎?