Reputation: 197
I am trying to delete all buttons in a grid Layout which are dynamically created when a button is pressed. ive tried doing this using the clear_widget() but that does not work. now i assign ids to those dynamically created buttons and try to delete them but its also not working
def drinksSelect(self,value): # creating a button by referring the id of the layout in which to create button
drinkImagePath = {'pepsi': 'drinksPictures/pepsi.png','7up': 'drinksPictures/7up.png'}
if self.root.a_s.l < self.root.a_s.limit: # You know what I mean
st = 'number'
img = myImage(source= drinkImagePath[value], size=(200,20), id=st)
self.root.a_s.ids['place_remaining'].add_widget(img)
self.root.a_s.l += 1
def checkout(self): #when this fucntion is called, it should clear the gridLayout with id drinksLayout
#self.root.a_s.ids.drinksLayout.clear_widget(self.root.a_s.ids.place_remaining)
st = 'number'
self.root.a_s.ids.place_remaining.remove_widget(self.root.a_s.ids.st)
GridLayout:
id: drinksLayout
size_hint_y: 0.3
orientation: 'horizontal'
rows: 1
GridLayout:
id: place_remaining
rows: 1
size_hint_x: 80
Button:
id: label1
width: 200
size_hint: None,0.4
background_normal:'1.jpg'
text: 'Checkout'
on_release: app.checkout()
Upvotes: 5
Views: 4162
Reputation: 16031
To clear GridLayout of all its children, please refer to the example for details.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
class RemoveWidgetDemo(BoxLayout):
place_remaining = ObjectProperty(None)
def __init__(self, **kwargs):
super(RemoveWidgetDemo, self).__init__(**kwargs)
self.drinksSelect()
def drinksSelect(self):
drinkImagePath = {'pepsi': 'drinksPictures/pepsi_logo.png', '7up': 'drinksPictures/7up-logo.png'}
for value in drinkImagePath.values():
self.place_remaining.add_widget(Image(source=value, size=(200, 20)))
def checkout(self):
for child in [child for child in self.place_remaining.children]:
self.place_remaining.remove_widget(child)
class TestApp(App):
title = "Kivy Clear GridLayout of all its children"
def build(self):
return RemoveWidgetDemo()
if __name__ == '__main__':
TestApp().run()
#:kivy 1.10.0
<RemoveWidgetDemo>:
place_remaining: place_remaining
GridLayout:
id: drinksLayout
size_hint_y: 0.3
orientation: 'horizontal'
rows: 1
GridLayout:
id: place_remaining
rows: 1
size_hint_x: 80
Button:
id: label1
width: 200
size_hint: None,0.4
background_normal:'1.png'
text: 'Checkout'
color: 0, 0, 0, 1
on_release: root.checkout()
Upvotes: 2
Reputation: 2645
You have just to edit your checkout
method a lil bit
def checkout(self): # when this fucntion is called, it should clear the gridLayout with id drinksLayout
self.root.a_s.ids.place_remaining.clear_widgets()
#but don't forget
self.root.a_s.l = 0
Upvotes: 6