【问题标题】:Python Kivy can't access id from another classPython Kivy 无法从另一个类访问 id
【发布时间】:2021-09-30 16:34:06
【问题描述】:

我无法通过 id 从另一个类获取小部件。我试过app.root.ids.first_lbl.text,但没用。有没有办法做到这一点?

这是我的代码:

ma​​in.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

【问题讨论】:

    标签: python class kivy kivy-language ids


    【解决方案1】:

    这是一个可行的技巧:

    <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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2021-05-20
      • 1970-01-01
      • 2013-07-01
      • 2018-03-14
      • 1970-01-01
      相关资源
      最近更新 更多