Skip to content Skip to sidebar Skip to footer

How To Address Remove_widget What Widget To Remove Inside Another Layout In Kivy

I am a beginner just learning coding and Python. I am not able to remove a widget that I just have created. I can still create new button. This is actually just little side test. E

Solution 1:

AttributeError

     self.remove_widget(self.children[0])
 AttributeError: 'MyApp' object has no attribute 'remove_widget'

Root Cause

The App class inherited by MyApp, does not have the method, remove_widget(). Only a root widget, which usually has children that can have children of their own.

Question

remove widget inside another layout

Solution

  • Replace self.remove_widget(...) to self.root.ids.abc.remove_widget(...)
  • Replace self.children[0] with self.root.ids.abc.children[0]
  • Check that there are children inside the layout before we invoke remove_widget()

Snippets

def remove(self):
    print('hello')
    if len(self.root.ids.abc.children) > 0:   # check for children
        self.root.ids.abc.remove_widget(self.root.ids.abc.children[0])  # remove child FIFO

Kivy Widget » remove_widget()

Widgets in Kivy are organized in trees. Your application has a root widget, which usually has children that can have children of their own. Children of a widget are represented as the children attribute, a Kivy ListProperty.

The widget tree can be manipulated with the following methods:

add_widget(): add a widget as a child
remove_widget(): remove a widget from the children list
clear_widgets(): remove all children from a widget


Solution 2:

Since you load the layout as self.root = Builder.load_string(KV), you can remove the first child with self.root.remove_widget(self.root.children[0])


Post a Comment for "How To Address Remove_widget What Widget To Remove Inside Another Layout In Kivy"