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

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

问题描述

我想知道些什么?


  1. 如果有ID按钮:button_b(Get_Boys类)被释放,然后用ID标签:label_g(Get_Girls类)必须改变

  2. 如果按键采用ID:button_b(Get_Boys类)是pressed,然后用ID标签:root_lbl(Get_People类)必须改变

  3. 如果按键采用ID:root_btn(Get_People类)被释放,然后用ID标签:label_b(Get_Boys类)必须改变

这是在<一个解释(略) href=\"http://stackoverflow.com/questions/23770703/kivy-accessing-a-method-on-a-different-class\">this链接,而不是从来看初学者的角度。

我有2个文件


  1. test.py

  2. dates_test.kv

test.py

 类Get_People(BoxLayout的):
    高清__init __(自我,** kwargs):
        超(Get_People,个体经营).__的init __(** kwargs)
类Get_Boys(BoxLayout的):
    高清__init __(自我,** kwargs):
        超(Get_Boys,个体经营).__的init __(** kwargs)类Get_Girls(BoxLayout的):
    高清__init __(自我,** kwargs):
        超(Get_Girls,个体经营).__的init __(** kwargs)类TestApp(APP):
    高清版本(个体经营):
        self.load_k​​v('dates_test.kv')
        返回Get_People()

dates_test.kv文件

 &LT; Get_People计算值:
    方向:垂直
    按钮:
        名称:root_btn
        ID:root_btn
        文字:我的根按钮
        on_release:change_label_b
    标签:
        ID:root_lbl
        文字:我的根标签
    Get_Boys:
    Get_Girls:&所述; Get_Boys计算值:
    按钮:
        ID:button_b
        文字:按钮男孩
        ON_ preSS:change_label_root
        on_release:change_label_g
    标签:
        ID:label_b
        文字:标签男孩&所述; Get_Girls计算值:
    按钮:
        ID:button_g
        文字:巴顿女孩
    标签:
        ID:label_g
        文字:标签的女孩


解决方案

嘛!看起来像我自己找到了答案,我想与大家分享吧。

首先让我们给ID,在dates_test.kv文件。这样就可以在python code或.kv文件访问它们。

 &LT; Get_People计算值:
    stuff_p:root_lbl
    ...
    Get_Boys:
        编号:GB
    Get_Girls:
        ID:GG&所述; Get_Boys计算值:
    stuff_b:label_b&所述; Get_Girls计算值:
    stuff_c:label_g

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

他们OBJECTPROPERTY在自己的类中定义。对于容易的份上,你可以把它们作为C语言的指针,您在stuff_b在你的Python code的变化使得label_b变化你kivy文件已经链接。

 类Get_People(BoxLayout的):
    stuff_p = OBJECTPROPERTY(无)
    ...类Get_Boys(BoxLayout的):
    stuff_b = OBJECTPROPERTY(无)
    ...类Get_Girls(BoxLayout的):
    stuff_c = OBJECTPROPERTY(无)
    ...

对于1部分和第二部分


  

      
  1. 如果与ID按钮:button_b(Get_Boys类)被释放,然后标签
      id为:label_g(Get_Girls类)必须改变


  2.   
  3. 如果按键采用ID:button_b(Get_Boys类)是pressed,那么标签
      id为:root_lbl(Get_People类)必须改变


  4.   

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

 高清change_girl(个体经营):    self.parent.ids.gg.stuff_c.text =男孩女孩变!
    #self.stuff_b.text =我改变了我自己!高清change_people(个体经营):
    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文件

 &LT; Get_Boys计算值:
    stuff_b:label_b
    按钮:
        ID:button_b
        文本:按钮1
        on_release:root.change_girl()
        ON_ preSS:根。 change_people()

对于第3部分


  <醇开始=3>
  
  • 如果有ID按钮:root_btn(Get_People类)被释放,然后标签
      id为:label_b(Get_Boys类)必须改变

  •   

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

     高清植根(个体经营):
        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):
        def __init__(self,**kwargs):
            super(Get_People,self).__init__(**kwargs)
    
    
    class Get_Boys(BoxLayout):
        def __init__(self,**kwargs):
            super(Get_Boys,self).__init__(**kwargs)
    
    class Get_Girls(BoxLayout):
        def __init__(self,**kwargs):
            super(Get_Girls,self).__init__(**kwargs)
    
    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. For sake of easiness you can think them as pointers in C language, 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 /部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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