如何从 kivy 文件 (.kv) 访问不同类的 id/widget? [英] How to access id/widget of different class from a kivy file (.kv)?

查看:41
本文介绍了如何从 kivy 文件 (.kv) 访问不同类的 id/widget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么?

  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 类)的标签必须改变.

这个链接中有解释(很少),但不是从初学者的角度来看.

我有 2 个文件

  1. test.py
  2. dates_test.kv

test.py

class Get_People(BoxLayout):经过类 Get_Boys(BoxLayout):经过类 Get_Girls(BoxLayout):经过类 TestApp(App):定义构建(自我):self.load_kv('dates_test.kv')返回 Get_People()

dates_test.kv 文件

:方向:'垂直'按钮:名称:root_btnid:root_btn文字:我是根按钮"on_release:change_label_b标签:id:root_lbl文字:我是根标签"Get_Boys:Get_Girls:<Get_Boys>:按钮:编号:button_b文字:男孩的按钮"on_press:change_label_rooton_release:change_label_g标签:编号:label_b文字:男孩的标签"<Get_Girls>:按钮:编号:button_g文字:女孩按钮"标签:编号:label_g文字:女孩的标签"

解决方案

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

首先让我们在dates_test.kv 文件中给出id".以便您可以在 python 代码或 .kv 文件中访问它们.

:东西_p:root_lbl...Get_Boys:编号:GBGet_Girls:编号:gg<Get_Boys>:东西_b:标签_b<Get_Girls>:东西_c:标签_g

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

它们是在自己的类中定义的 ObjectProperty.您在 python 代码中的 stuff_b 中所做的更改会在 label_b 中进行更改,因为您已在 kivy 文件中进行了链接.

class Get_People(BoxLayout):stuff_p = ObjectProperty(无)...类 Get_Boys(BoxLayout):stuff_b = ObjectProperty(无)...类 Get_Girls(BoxLayout):stuff_c = ObjectProperty(无)...

对于第 1 部分和第 2 部分

<块引用>

  1. 如果按钮id:button_b(Get_Boys类)被释放,那么Labelid:label_g(Get_Girls 类)必须更改.

  2. 如果 id 为 button_b(Get_Boys 类)的 Button 被按下,则 Labelid:root_lbl(Get_People 类)必须更改.

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

def change_girl(self):self.parent.ids.gg.stuff_c.text = "男孩变女孩了!"#self.stuff_b.text = "我改变了自己!"def change_people(self):self.parent.ids.root_lbl.text = "男孩改变了人!"

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

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 文件中

:东西_b:标签_b按钮:编号:button_b文字:按钮 1"on_release: root.change_girl()on_press:根.change_people()

对于第 3 部分

<块引用>

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

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

def rooted(self):self.ids.gb.stuff_b.text = "人变了男孩!"

并在 .kv 文件中

按钮:id:root_btn文字:我是根"on_release: root.rooted()

What I want to know?

  1. If button with id: button_b (Get_Boys class) is released, then Label with id: label_g (Get_Girls class) must change.
  2. If Button with id: button_b (Get_Boys class) is pressed, then Label with id: root_lbl (Get_People class) must change.
  3. If Button with id: root_btn (Get_People class) is released, then Label with id: label_b (Get_Boys class) must change.

It is explained (little) in this link, but not from the beginner's point of view.

I have 2 files

  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 file

<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"

解决方案

Well!, looks like I myself found the answer and I would like to share it.

First of all let us give "id" in dates_test.kv file. So that you can access them in python code or in .kv file.

<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

you might wonder what is stuff_p,stuff_b and stuff_c???

They are ObjectProperty defined in their own classes. The changes you make in stuff_b in your python code makes changes in label_b as you have linked in kivy file.

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

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

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

For Part 1 and Part 2

  1. If button with id: button_b (Get_Boys class) is released, then Label with id: label_g (Get_Girls class) must change.

  2. If Button with id: button_b (Get_Boys class) is pressed, then Label with id: root_lbl (Get_People class) must change.

In the Get_Boys class (test.py) define these methods.

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!"

let's see what happened here...

self.parent.ids.gg.stuff_c.text = "Boys changed Girls!"

  1. self.parent refers to Get_Parent class.
  2. .ids.gg refers to the id that we created above for Get_Girls.
  3. .stuff_c is used to refer label_g (above) in Get_Girls class.
  4. .text is used to change the text in the label.

and in the .kv file

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

For Part 3

  1. If Button with id: root_btn (Get_People class) is released, then Label with id: label_b (Get_Boys class) must change.

in the Get_People class (test.py) define a method.

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

and in .kv file

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

这篇关于如何从 kivy 文件 (.kv) 访问不同类的 id/widget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆