Я хочу добавить или удалить карту из списка карт в представлении переработки.
Итак, у меня есть экран с текстовым полем и кнопкой, которую я использую, чтобы добавить карточку в RV, и на каждой карточке в списке у меня есть кнопка корзины, чтобы удалить карточку из списка. .
Теперь я могу добавить карту в список, но не знаю, как удалить карту из списка.
вот код:
.Py
Код: Выделить всё
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivymd.uix.card import MDCard
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.screenmanager import Screen, ScreenManager
import Class
import pickle
try:
with open("list1.pickle","rb") as f:
list1 = pickle.load(f)
except:
list1 = []
class ScreenManager(ScreenManager):
pass
class Screen1(Screen):
def add_card(self):
text = self.ids.text_field.text
obj = Class.Reparto(text)
self.ids.text_field.text = ""
list1.append(obj)
with open('list1.pickle',"wb") as f:
pickle.dump(list1,f)
self.ids.RV_List.refreshview(list1)
return list1
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'text': str(reparto.nome),'id':str(reparto)} for reparto in list1]
def refreshview(self, lista):
self.data = [{'text': str(reparto.nome)} for reparto in lista]
class RVBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
pass
class CardView(MDCard, RecycleDataViewBehavior):
text= StringProperty()
def remove_card(self):
for item in list1:
if item.nome == self.ids.Label.text:
list1.remove(item)
MDApp.get_running_app().root.ids.RV.refreshview(list1)
class CreaIntervento(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
return Builder.load_file("KV.kv")
if __name__ == "__main__":
CreaIntervento().run()
Код: Выделить всё
MDBoxLayout:
orientation:'vertical'
md_bg_color: app.theme_cls.primaryColor
ScreenManager:
id:screen_manager
Screen1:
viewclass:'CardView'
RVBoxLayout:
orientation: 'vertical'
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
style:'elevated'
size_hint_y:None
height: dp(80)
MDRelativeLayout:
MDIconButton:
id:func_icon
icon: "trash-can"
pos_hint:{'center_x':0.9,'center_y':0.5}
on_release:root.remove_card()
MDLabel:
id:Label
text: root.text
adaptive_size: True
color: "grey"
pos_hint:{'center_x':0.1,'center_y':0.5}
bold: True
name:"Aggiungi_Reparto"
text_field:text_field
rvNome:RV_List
MDBoxLayout:
orientation: 'vertical'
MDFloatLayout:
size_hint_y: None
height:dp(100)
MDTextField:
id: text_field
mode: "outlined"
pos_hint: {"center_x": .4, "center_y":.5}
size_hint_x: None
width: dp(400)
MDTextFieldHintText:
text: "Nome nuovo reparto"
MDTextFieldHelperText:
MDTextFieldMaxLengthText:
max_text_length: 30
MDButton:
style: "filled"
on_release: root.add_card()
pos_hint: {"center_x": .8, "center_y":.5}
size_hint_x: None
width: dp(100)
MDButtonText:
text: "Aggiungi"
RV:
id:RV_List
Подробнее здесь: https://stackoverflow.com/questions/787 ... ecycleview