Kivy: How to check for the existence of a child widget

Viewed 3974

In my .py file I have a screen that looks something like:

class ExampleScreen(Screen):
    def create_layout(self):
        box = BoxLayout(orientation='vertical')
        # other layout stuff in here
        self.add_widget(box)

In my .kv file, I have a button which, when pressed, calls this function and displays this layout on <ExampleScreen>. However, I would like to be able to press this button and first check if this layout already exists, and if so, remove it before adding a new one. I anticipate modifying create_layout() to something like:

def create_layout(self):
    if (box layout child has already been added):
        self.remove_widget(box layout child)
    box = BoxLayout(orientation='vertical')
    # other layout stuff in here
    self.add_widget(box)

Does anyone know how to do this? Using id somehow?

2 Answers
Related