Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 888
kiok46 Avatar asked May 12 '15 22:05

kiok46


People also ask

What is .KV file in Kivy?

The file test.kv is selected because the name of the subclass of App is TestApp, which implies that kivy should try to load "test.kv". That file contains a root Widget. ''' import kivy kivy. require('1.0.7') from kivy.app import App class TestApp(App): pass if __name__ == '__main__': TestApp().

How many ways can the .KV file be loaded into code or application?

There are two ways to load the . kv file into code or an Application.

What is App class in Kivy?

The App class is the base for creating Kivy applications. Think of it as your main entry point into the Kivy run loop. In most cases, you subclass this class and make your own app.


1 Answers

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()
like image 143
kiok46 Avatar answered Oct 01 '22 14:10

kiok46