This is my first question, I’m new on kivy…
I want to add or remove a card from a list of card in a recyclingview.
So I have a screen with a text box and a button that I use to add the card to the RV, and on each card on the list, I have a trash button to remove the card from the list.
Now I can add the card to the list, but I don’t know how to remove the card from the list.
here the code:
.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()
.Kv
MDBoxLayout:
orientation:'vertical'
md_bg_color: app.theme_cls.primaryColor
ScreenManager:
id:screen_manager
Screen1:
<RV>
viewclass:'CardView'
RVBoxLayout:
orientation: 'vertical'
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
<CardView>
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
<Screen1>
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
I’ve tried to call a method from the RV, but without results.
TestFour is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
OK I’ve find a solution:
def remove_card(self):
for item in list1:
if item.nome == self.ids.Label.text:
list1.remove(item)
root= self.parent
root= root.parent
root.refreshview(list1)
with open('list1.pickle',"wb") as f:
pickle.dump(list1,f)
return list1
Using parent I can use the refreshview()
method of the RV class.
TestFour is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.