I have a Kivy form with one Recycleview. I would dinamically add a button to my Recycleview, by clicking a simple Button. My Python3 script:
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.recycleview import RecycleView
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data=[]
class app(App):
rv=RV()
def build(self):
return Builder.load_file('lab.kv')
def add_to_rv(self):
self.rv.data.append('ok')
# which command i should use to show new button in recycleview with new data?
app().run()
My kv file:
BoxLayout:
RV:
id:rv
viewclass:'Button'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Button:
text:'ADD TO RV'
on_press:app.add_to_rv()
Your add_to_rv()
method is adding to the data of self.rv
. But self.rv
is defined in the statement:
rv=RV()
which creates a new instance of RV
. However, that instance of RV
is not used in your GUI. The instance of RV
that is displayed in your GUI is built by the line of code:
return Builder.load_file('lab.kv')
And those two instances are not related. The fix is to access the correct instance of RV
in your add_to_rv()
method like this:
def add_to_rv(self):
self.root.ids.rv.data.append({'text': 'ok'})
Also, note that the data
is a list of dictionaries. And you can eliminate the rv=RV()
line.