kamel berkani
kamel berkani

Reputation: 57

how to use kivy widgets ids with add_widget

I want to remove a widget I added dynamically with add_widget() in function1 not in the kv file, my problem is that I can't give the widget an ID to remove it in function2, when I tried to add an ID, the ID didn't show up in the ids property of the root and I don't find it with the children of the root widget with the debug tool, it's like it doesn't exist even though it's displaying in my screen. I want to know how I can create and add a widget and access it with an ID.

self.root.get_screen('Third').add_widget(MDLabel(
                                                # id='titlesolution',
                                                text=f"Solutions",
                                                theme_text_color="Hint",
                                                halign='center',
                                                pos_hint={'center_x': 0.5,'center_y': 0.42}))

Upvotes: 1

Views: 641

Answers (1)

inclement
inclement

Reputation: 29460

ids are a kv language thing, there isn't an equivalent id property when instantiated from Python. The reason for this is that ids would be redundant on the Python side, as you automatically have a reference to any Widget you instantiate.

If you want to give a widget an identifier, you can, just use any property name you like and add it to the class.

If you want to access the children of a widget, try widget.children to look through the list directly.

the ID didn't show up in the ids property of the root

There isn't any clear root in the Python side.

Upvotes: 1

Related Questions