从列表中删除元素并同时删除小部件"TwoLineListItem"



我已经创建了一个列表"list_buy"在这里面,我每次都放一些元素函数&;primer &;是执行。问题是,当我创建另一个函数称为remove_widget_instance..我想当小部件"TwoLineListItem",我可以在这个列表中删除相同的元素"list_buy"同时。"list_buy"只要按下这个按钮就可以存储一些商品的价格。问题还在于,在我的函数remove_widget_instance,我不知道如何分配列表的特定元素被删除,如果我使用list_buy.pop()它的工作,但它只是删除最后一个元素,我想是相同的元素与相同的小部件

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"

我意识到您在添加列表项之前传递了列表购买。这意味着包含你想删除的内容的列表版本没有你想删除的内容

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)

你的问题不是很清楚,但据我所知,这应该能解决问题。