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

我想知道什么?

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

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

我有 2 个文件

  1. test.py
  2. 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"

【问题讨论】:

    标签: python events button widget kivy


    【解决方案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 部分

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

    2. 如果按下 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 = "男孩换了女孩!"

    1. self.parent 指的是 Get_Parent 类。
    2. .ids.gg 指的是我们在上面为 Get_Girls 创建的 id。
    3. .stuff_c 用于引用 Get_Girls 类中的 label_g(上)。
    4. .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 部分

    1. 如果 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 语言,这个例子让很多东西都点击了。
    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多