'remove an element from a list and remove a widget "TwoLineListItem" at the same time
Well, I have created a list "list_buy" and inside of this I'm putting some elements every time the function "imprimir" is executed. The problem is when I created another function called remove_widget_instance.. I want when the widget "TwoLineListItem" is removed, I can remove the same element in this same list "list_buy" at the same time. "list_buy" just store the price of some items every is pressed this button. the problem also is that in my function remove_widget_instance, I don't know How to assign the specific element of the list for being removed, If I use list_buy.pop() It's works but It just remove the last element I want to be the same element with the same widget
main.py
class SecondWindow(Screen):
list_general= []
list_price= []
list_buy= []
md= ObjectProperty(None) #I need the MDList el id of container USar ObjectProperty()
def imprimir(self, list_general, list_price, list_buy):
print(list_general)
for i in range(len(list_general)):
#print(i)
items= TwoLineListItem(text= list_general[i],secondary_text= "$"+list_price[i] + " Dollars" )
items.bind(on_release = lambda x: self.remove_widget_instance(items, self.md,list_buy,list_price))
self.md.add_widget(items)
list_buy.append(list_price[i])
list_general.pop(i)
list_price.pop(i)
print(list_buy)
def remove_widget_instance(self, instance, parent_widget, list_buy, list_price):
parent_widget.remove_widget(instance)# When this widget is removed I want to remove
list_buy.remove(?????)# this element of this list too but I don't know what can I put inside in .remove() to remove the specific element from the list_buy
print(list_buy)
main.kv
<SecondWindow>:
name: "Buy"
md: container1
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Productos Añadidos"
font_size: 19
size_hint: 1,0.3
ScrollView:
MDList:
id: container1
GridLayout:
cols:2
size_hint: 1,0.3
Label:
text:"Total"
#on_press: root.imprimir(root.list_torn)
Label:
text: "0.00"
Solution 1:[1]
I realised you passing the list buy before you add the list item to it. which means the version of the list which holds what you wanna delete does not have what you wanna delete
def imprimir(self, list_general, list_price, list_buy):
print(list_general)
for i in range(len(list_general)):
#print(i)
item_price = list_price[i]
list_buy.append(item_price)
items= TwoLineListItem(text= list_general[i],secondary_text= f"${item_price} Dollars" )
items.bind(on_release = lambda x: self.remove_widget_instance(items, self.md,list_buy,item_price))
self.md.add_widget(items)
list_general.pop(i)
list_price.pop(i)
print(list_buy)
def remove_widget_instance(self, instance, parent_widget, list_buy, item_price):
parent_widget.remove_widget(instance)# When this widget is removed I want to remove
list_buy.remove(item_price)# this element of this list too but I don't know what can I put inside in .remove() to remove the specific element from the list_buy
print(list_buy)
Your question isn't very clear but from what i can gather this should solve the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | darkmatter08 |
