Search code examples
pythonkivykivy-language

Kivy - Accessing id of widget outside of the root class


I wish to retrieve the id of a widget in order to access its text attribute, in a widget class outside of the root class. I understand in order to affect or retrieve items with an id from kv-lang, the following syntax may be used:

self.ids.some_id.some_attribute = new_value

This works perfectly fine when used in the root class, as access is available to all its children. However in a widget class, the class only refers to the widget declared, so any ids outside of it are out of scope.

<Root>:
    ...
        SomeButton:
           ...
        TextInput:
            id: some_id

What doesn't work:

class SomeButton(Button):
    def on_press(self):
        print(self.ids.some_id.text)

As I mentioned, this is understandable. But I don't know then what is used in this instance. Any assistance would be appreciated :)


Solution

  • I'm new in kivy but i think TextInput is not a child widget of a SomeButton but you are trying to access it from Button anyway. That's your problem.

    Try self.parent.ids.some_id.text