yoel
yoel

Reputation: 315

passing information from child class to parent, using dynamic classes in kvlang (Kivy)

How can I pass information from a child class to its parent in kvlang, using dynamic classes?

I have the following simple kv code and Python code:

kv:

BoxLayout:
Label:
    id: label_id
    text: 'label'   
Button1:
    mylabel: label_id
    text: self.mylabel.text

<Button1@Button>:

Python:

import kivy
from kivy.app import App

class Test2App(App):
    pass

if __name__ == '__main__':
    Test2App().run()

This works, making the Button's text the same as the label's text.

But what I want to do is set the Button's text in the parent class:

BoxLayout:
Label:
    id: label_id
    text: 'label'   
Button1:
    mylabel: label_id

<Button1@Button>:
    text: self.mylabel.text

This obviously leads to an error, because Button1 isn't aware of mylabel. But I want it to be. I know I can solve this by declaring a property in the Python file, but I'd rather have a solution inside the kv file alone.

Does anyone have an idea how to do that?

EDIT:

Interestingly, I'm able to pass information as a StringProperty. The following code works and I get the right text on my button:

BoxLayout:
Label:
    id: label_id
    text: 'label'   
Button1:
    my_label_text: label_id.text

<Button1@Button>:
    my_label_text: ''
    text: self.my_label_text

Does anyone know how I can pass the whole label (as an object) to the parent?

Upvotes: 1

Views: 684

Answers (1)

inclement
inclement

Reputation: 29468

Try this:

BoxLayout:
    Label:
        id: label_id
        text: 'label'   
    Button1:
        mylabel: label_id

    <Button1@Button>:
        mylabel: None
        text: self.mylabel.text if self.mylabel is not None else ''

The mylabel: None creates a property automatically.

Upvotes: 1

Related Questions