Andy
Andy

Reputation: 3

Remove an entire boxLayout and its children in Kivy with Python dynamically

How do I remove a parent BoxLayout using a child button? Keep in mind I am going to have many of these similar box layouts.

Here is where I am creating each BoxLayout and adding it to a global layout itemsLayout

    def submitProductToWatch(self, productName, ebay, letGo, mercari):
         self.current = 'main'
         newItem = BoxLayout(id="boxID",size_hint_y=None, height=40)

        myCheck = CheckBox(id="checkID",size_hint_x=None, color=(0.467,.878,.259,1))
        prodLabel= Label(text=productName, size_hint_x=None)
        webCoverageString = self.displayWebsitesForProduct(ebay, letGo, mercari)
        covLabel= Label(id="coverageID",text=webCoverageString)
        remButton= Button(id="removeID",text="Remove", color=(1,0,0,.7),size_hint= (.6,.7),
                              font_size= 15)

        newItem.add_widget(myCheck)
        newItem.add_widget(prodLabel)


        newItem.add_widget(covLabel)
        newItem.add_widget(remButton)

        itemsLayout.add_widget(newItem)

Upvotes: 0

Views: 1597

Answers (2)

AlexAndDraw
AlexAndDraw

Reputation: 630

How do I remove a parent BoxLayout using a child button? Keep in mind I am >going to have many of these similar box layouts.

You could do something like:

on_press:self.parent.parent.remove_widget(self.parent). 

So the Button basically tells its Grandad to disown its dad.

Using the code you've presented, you could write it like:

remButton.bind(on_press=self.ids.removeID.parent.parent.remove_widget(self.ids.removeID.parent))

A bit cleaner:

Box = remButton.parent
remButton.bind(on_press=Box.parent.remove_widget(Box))

Upvotes: 1

Gábor Fekete
Gábor Fekete

Reputation: 1358

Here's a way to do this:

class Main(Widget):
    def __init__(self):
        super().__init__()
        self.layout = BoxLayout()
        button = Button()
        button.bind(on_press=self.remove_layout)
        self.layout.add_widget(button)
        self.add_widget(self.layout)

    def remove_layout(self, *ignore):
        self.remove_widget(self.layout)

Upvotes: 1

Related Questions