Lev Barenboim
Lev Barenboim

Reputation: 109

How can I access an element of a screen that is inside of a widget in kivy using self.ids method?

I have a kivy screen. It contains some elements and a widget, which, in its turn, contains its own elements. When I try something like

    self.ids.element_directly_on_a_screen.text = 'something' 

it works, yet when I try

    self.ids.element_defined_inside_of_a_widget.text = 'something_else'

it returns

     AttributeError: 'super' object has no attribute '__getattr__'

What should I do?

Edit: an example

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen

class SomeWidget(FloatLayout):
    pass

class SomeScreen(Screen):
    def ButtonOne(self):
        self.ids.label_one.text = 'Something'
    def ButtonTwo(self):
        self.ids.label_two.text = 'Something else'

class TestApp(App):
    pass

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

and test.kv

<SomeWidget>:
    Label:
        text: 'This is label inside of a widget'
        id: label_two
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.5}

SomeScreen:
    Label:
        text: 'This is just a label'
        id: label_one
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.5}
    SomeWidget:
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.2}
    Button:
        text: 'Click me to test the first Label!'
        size_hint: (0.35, 0.2)
        pos_hint: {"x": 0.2, "y": 0}
        on_release:
            root.ButtonOne()
    Button:
        text: 'Click me to test the second Label!'
        size_hint: (0.35, 0.2)
        pos_hint: {"x": 0.6, "y": 0}
        on_release:
            root.ButtonTwo()

Upvotes: 1

Views: 282

Answers (1)

eyllanesc
eyllanesc

Reputation: 243993

The id is accessible through the root, in your case "label_two" is accessible only through SomeWidget so you must first access that element so you must add an "id" to the SomeWidget created under SomeScreen.

def ButtonTwo(self):
    self.ids.some_widget.ids.label_two.text = 'Something else'
SomeScreen:
    # ...
    SomeWidget:
        id: some_widget
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.2}
    # ...

Upvotes: 1

Related Questions