How to access id/widget of different class from a kivy file (.kv)?

时间:2015-05-12 22:54:47

标签: python events button widget kivy

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

#PersSchemaTable td:nth-child(1),
#PersSchemaTable td:nth-child(3), 
#PersSchemaTable td:nth-child(5), 
#PersSchemaTable td:nth-child(7), 
#PersSchemaTable td:nth-child(9), 
#PersSchemaTable td:nth-child(11),
#PersSchemaTable td:nth-child(13) {
    border-right: 2px solid #781351;
}

dates_test.kv file

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

1 个答案:

答案 0 :(得分:14)

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

首先让我们在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.   
  3. 如果按下具有id:button_b(Get_Boys类)的Button,则为Label   id:root_lbl(Get_People类)必须更改。

  4.   

在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用于更改标签中的文字。
  5. 并在.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类)必须更改。
    2.   

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