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

标签 python events button widget kivy

我想知道什么?

  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 类)的 Label 必须更改。

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

我有2个文件

  1. 测试.py
  2. dates_test.kv

测试.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"

最佳答案

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

首先让我们在 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. 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.

在 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. If Button with id: root_btn (Get_People class) is released, then Label with id: label_b (Get_Boys class) must change.

在 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()

关于python - 如何从 kivy 文件 (.kv) 访问不同类的 id/widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202801/

相关文章:

python - 将数据框中同一标题下的多个列分组

javascript - PWA 中 Android 返回按钮的自定义事件

events - 如何在道场日历中显示装饰来代替事件名称?

HTML 输入类型 = 按钮在 Chrome 中不可点击?

html - 如何向 CSS 表单中的按钮添加两个单独的文本样式?

将联接与 CSV writerow 相结合的 Pythonic 方式

python - 如何迭代地将 lxml.objectify.ObjectifiedElement 从authorize.net 转换为 python 字典

python - 我可以将 SIGINT 发送到 Windows 上的 Python 子进程吗?

c# - 哪种调用事件委托(delegate)的方法更好?

html - 如何在响应式 css 按钮中对齐文本中心?