Jubiluleu
Jubiluleu

Reputation: 61

I can't access the "Root" of my Kivy code

I made a code, where there is a root class "Screen", and on the side of the.kv file, there is a "ScrollView", with a "List". In the .py file, I made a loop that creates several buttons within the self.ids.scroll list, using add_widget and Builder to build the widget:

Class Main(Screen):
    def test(self):
        print("Ok")

    def updatelist(self):
        for i in range(10):
            self.ids.scroll.add_widget(Builder.load_string(
            f"""Button:
                    text: str({i})
             """
                )
            )

The problem is that when I try to create an on_release inside the button:

on_release: root.test()

It returns an error, saying that "Button" does not have the "test" function, that is, it is not finding the root, the "Main" class where the function is, it is only limited to itself. I tried to create the buttons without using the "Builder", just using:

for i in range(10):
    self.ids.scroll.add_widget(Button())

But I had problems, for example, capturing his self.text, because it is only possible to capture with the Kv language.

Any help, thank you!

Upvotes: 0

Views: 396

Answers (1)

Arthur Pereira
Arthur Pereira

Reputation: 1559

You have to reference the current running app on kv language, otherwise it will look inside the Button class.

To reference your main class use:
on_release: app.root.test()

Upvotes: 2

Related Questions