Search code examples
python-3.xclassnestedkivykivy-language

Kivy: inherit another dynamic classes values


I have a dynamic classes like this:

<BLMother@BoxLayout>:
    orientation:"horizontal"
    padding: 10, 0
    spacing: 10

For some of my CustomBoxLayout, I would like to add a canvas:before. I could create a new dynamic classes which combine the values of both like this:

<BLChildren@BoxLayout>:
    orientation:"horizontal"
    padding: 10, 0
    spacing: 10
    canvas.before:
        Color:
            rgba: 1, 1, 1, 0.8
       Rectangle:
            size: self.size
            pos: self.x + self.width*0.025, self.y    

Is there a way BLChildren can inherit all the value from BLMother?

I use Kivy (1.10.1.dev0)


Solution

  • Yes, the BLChildren can inherit all the value from BLMother. Please refer to the example below for details.

    Example - Inheritance from BLMother

    main.py

    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    
    
    class RootWidget(FloatLayout):
        pass
    
    
    class TestApp(App):
        title = "With Inheritance of BLMother"
    
        def build(self):
            return RootWidget()
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    test.kv

    #:kivy 1.10.0
    
    <BLMother@BoxLayout>:
        orientation:"horizontal"
        padding: 10, 0
        spacing: 10
    
    <BLChildren@BoxLayout>:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 0.8
            Rectangle:
                size: self.size
                pos: self.x + self.width*0.025, self.y
        BLMother:
    
    <RootWidget>:
        BLChildren:
    

    Output - Inheritance from BLMother

    enter image description here