Adding RelativeLayout to existing app in kivy file

Viewed 18

So what I am trying to achieve is:

class FirstWindow(Screen):
    layout=self.ids['second']
    lab1=Label(text="one",size_hint_x=.35)
    lab2=Label(text="two",size_hint_x=.35)
    layout.add_widget(lab1)
    layout.add_widget(lab2)
<FirstWindow>:
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height    
        Image:
            source: "gui-background.jpg"
            size_hint: None, None
            size: root.size
            allow_stretch: True
            keep_ratio: False
            RelativeLayout:
                id: second
                cols:2   
                size_hint:(0.5, 0.4)
                pos_hint:{'x':0, 'y': 0.6}

I want to add both Labels to the RelativeLayout with id: second. The issue that code shows is: enter image description here

How do I fix it?

1 Answers

I resolved my problem with adding this entire stuff into a simple function

class FirstWindow(Screen):
    def abc(self):
        layout=self.ids['second']
        lab1=Label(text="one",size_hint_x=.35)
        lab2=Label(text="two",size_hint_x=.35)
        layout.add_widget(lab1)
        layout.add_widget(lab2)

Then it's just calling it with a button.

Button:
    text: "click"
    on_press: root.abc()

I read that it's problematic because self is an argument only available at function call time.

Related