Python Kivy 无法从另一个 class 访问 id

Python Kivy can't access id from another class

我无法通过 ID 从另一个 class 获取小部件。我试过 app.root.ids.first_lbl.text,但没有用。有什么办法吗?

这是我的代码:

main.py:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout


class MainWidget(Widget):
    pass

class SecondWidget(BoxLayout):
    pass

class MyApp(App):
    def build(self):
        return MainWidget()


if __name__ == "__main__":
    MyApp().run()

my.kv:

<MainWidget>
    BoxLayout:
        size: root.width, root.height

        BoxLayout:
            Label:
                id: first_lbl
                text: "TEXT123"

        SecondWidget:

<SecondWidget>:
    Label:
        text: app.root.ids.first_lbl.text

这里有一个可行的技巧:

<MainWidget>
    BoxLayout:
        size: root.width, root.height

        BoxLayout:
            Label:
                id: first_lbl
                text: "TEXT123"

        SecondWidget:
            t1: first_lbl.text  # set the t1 property of SecondWidget to the text of first_lbl

<SecondWidget>:
    t1: ''
    Label:
        text: root.t1  # use the t1 property of SecondWidget