I’m trying to make a screen with textinput a btn and a recycling view.
The btn add to the recycling view a card with the text of the textinput, each card has a trash-bin button to remove the card and the item from the list.
At the moment I can’t update the recycling view when i press the trash-bin button. I remove the item from the list but the card stay on the screen.
How can I fix this?
Here the code:
.KV
MDBoxLayout:
orientation:'vertical'
md_bg_color: app.theme_cls.primaryColor
WindowsManager:
AggiungiRepartoWindow:
<RepartiRV>
viewclass:'RepartoCard'
ListaRepartiBoxLayout:
orientation: 'vertical'
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
<RepartoCard>
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.cancella_reparto()
MDLabel:
id:Label
text: root.text
adaptive_size: True
color: "grey"
pos_hint:{'center_x':0.1,'center_y':0.5}
bold: True
<AggiungiRepartoWindow>
name:"Aggiungi_Reparto"
nome:nome
rvNome:rv_lista_reparti
MDBoxLayout:
orientation: 'vertical'
MDFloatLayout:
size_hint_y: None
height:dp(100)
MDTextField:
id: nome
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.crea_reparto()
pos_hint: {"center_x": .8, "center_y":.5}
size_hint_x: None
width: dp(100)
MDButtonText:
text: "Aggiungi"
RepartiRV:
id:rv_lista_reparti
.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.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
import Class
import pickle
try:
with open("listareparti.pickle","rb") as f:
lista_reparti = pickle.load(f)
except:
lista_reparti = []
try:
with open("listalinee.pickle","rb") as f:
lista_linee = pickle.load(f)
except:
lista_linee = []
try:
with open("listamacchine.pickle","rb") as f:
lista_macchine = pickle.load(f)
except:
lista_macchine = []
try:
with open("listainterventi.pickle","rb") as f:
lista_interventi = pickle.load(f)
except:
lista_interventi = []
class WindowsManager(ScreenManager):
pass
class AggiungiRepartoWindow(Screen):
def crea_reparto(self):
nome = self.ids.nome.text
rep = Class.Reparto(nome)
self.ids.nome.text = ""
lista_reparti.append(rep)
with open('listareparti.pickle',"wb") as f:
pickle.dump(lista_reparti,f)
self.ids.rv_lista_reparti.refreshview(lista_reparti)
return lista_reparti
class RepartiRV(RecycleView):
def __init__(self, **kwargs):
super(RepartiRV, self).__init__(**kwargs)
self.data = [{'text': str(reparto.nome),'id':str(reparto)} for reparto in lista_reparti]
def refreshview(self, lista):
for item in lista:
nome= item.nome
print(nome)
self.data = [{'text': str(reparto.nome)} for reparto in lista]
class ListaRepartiBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
pass
class RepartoCard(MDCard, RecycleDataViewBehavior):
index= None
text= StringProperty()
def refresh_view_attrs(self,rv, index, data):
return super(RepartoCard, self).refresh_view_attrs(
rv, index, data)
def cancella_reparto(self):
id= self.id
for item in lista_reparti:
if str(item) == id:
print(item)
lista_reparti.remove(item)
print(lista_reparti)
class CreaIntervento(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
return Builder.load_file("Aggiungi_intervento.kv")
if __name__ == "__main__":
CreaIntervento().run()
New contributor
TestFour is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.