如何从 kivy 文件 (.kv) 访问不同类的 id/widget?

Posted

技术标签:

【中文标题】如何从 kivy 文件 (.kv) 访问不同类的 id/widget?【英文标题】:How to access id/widget of different class from a kivy file (.kv)? 【发布时间】:2015-07-24 00:44:53 【问题描述】:

我想知道什么?

    如果释放 id 为 button_b(Get_Boys 类)的按钮,则必须更改 id 为 label_g(Get_Girls 类)的标签。 如果按下 id 为 button_b(Get_Boys 类)的按钮,则必须更改 id 为 root_lbl(Get_People 类)的标签。 如果释放 id 为 root_btn(Get_People 类)的 Button,则 id 为 label_b(Get_Boys 类)的标签必须更改。

在this链接中有解释(很少),但不是从初学者的角度。

我有 2 个文件

    test.py dates_test.kv

test.py

class Get_People(BoxLayout):
    pass

class Get_Boys(BoxLayout):
    pass

class Get_Girls(BoxLayout):
    pass

class TestApp(App):
    def build(self):
        self.load_kv('dates_test.kv')
        return Get_People()

dates_test.kv 文件

<Get_People>:
    orientation: 'vertical'
    Button:
        name: root_btn
        id: root_btn
        text: "I am Root Button"
        on_release: change_label_b
    Label:
        id: root_lbl
        text: "I am Root Label"
    Get_Boys:
    Get_Girls:

<Get_Boys>:
    Button:
        id: button_b
        text: "Button for boys"
        on_press: change_label_root
        on_release: change_label_g
    Label:
        id: label_b
        text: "Label for boys"

<Get_Girls>:
    Button:
        id: button_g
        text: "Button for girls"
    Label:
        id: label_g
        text:"Label for girls"

【问题讨论】:

【参考方案1】:

好吧!看来我自己找到了答案,我想分享一下。

首先让我们在 dates_test.kv 文件中给出“id”。这样您就可以在 python 代码或 .kv 文件中访问它们。

<Get_People>:
    stuff_p: root_lbl
    ...
    Get_Boys:
        id: gb
    Get_Girls:
        id: gg

<Get_Boys>:
    stuff_b: label_b

<Get_Girls>:
    stuff_c: label_g

你可能想知道 stuff_p、stuff_b 和 stuff_c 是什么???

它们是在自己的类中定义的 ObjectProperty。您在 python 代码中对 stuff_b 所做的更改会更改 label_b 中的内容,就像您在 kivy 文件中链接的那样。

class Get_People(BoxLayout):
    stuff_p = ObjectProperty(None)
    ...

class Get_Boys(BoxLayout):
    stuff_b = ObjectProperty(None)
    ...

class Get_Girls(BoxLayout):
    stuff_c = ObjectProperty(None)
    ...

第 1 部分和第 2 部分

    如果具有 id: button_b (Get_Boys 类) 的按钮被释放,则 Label id 为:label_g(Get_Girls 类)必须更改。

    如果按下 id 为 button_b(Get_Boys 类)的 Button,则 Label id 为:root_lbl(Get_People 类)必须更改。

在 Get_Boys 类 (test.py) 中定义这些方法。

def change_girl(self):

    self.parent.ids.gg.stuff_c.text = "Boys changed Girls!"
    #self.stuff_b.text = "i changed myself!"

def change_people(self):
    self.parent.ids.root_lbl.text = "Boys changed people!"

让我们看看这里发生了什么......

self.parent.ids.gg.stuff_c.text = "男孩换了女孩!"

    self.parent 指的是 Get_Parent 类。 .ids.gg 指的是我们在上面为 Get_Girls 创建的 id。 .stuff_c 用于引用 Get_Girls 类中的 label_g(上)。 .text 用于更改标签中的文字。

在 .kv 文件中

<Get_Boys>:
    stuff_b: label_b
    Button:
        id: button_b
        text: "button 1"
        on_release: root.change_girl()
        on_press: root. change_people()

第 3 部分

    如果 id 为 root_btn(Get_People 类)的 Button 被释放,则 Label id 为:label_b(Get_Boys 类)必须更改。

在 Get_People 类 (test.py) 中定义一个方法。

def rooted(self):
    self.ids.gb.stuff_b.text = "people changed boys!"

在.kv文件中

Button:
    id: root_btn
    text: "I am Root"
    on_release: root.rooted()

【讨论】:

非常感谢。我一直在努力理解 KV 语言,这个例子让很多东西都点击了。

以上是关于如何从 kivy 文件 (.kv) 访问不同类的 id/widget?的主要内容,如果未能解决你的问题,请参考以下文章

如何从已在其中创建的不同类对象中访问类对象的成员函数?

Kivy - 如何访问由 .kv 文件创建的类的实例?

如何正确使用.kv文件和.py? - 从 .kv 调用函数如何构建 .py

从 Swift 中的不同类访问 ViewController 中的对象

当屏幕在主 .kv 文件中管理时,如何从 .py 文件切换屏幕?

如何使用 Python 代码从 kv 文件中的 TextField 获取数据?