Skip to content

Instantly share code, notes, and snippets.

@el3
Last active July 27, 2022 09:57
Show Gist options
  • Save el3/ad6a4d05cf79d645607b45f5e0a50dfe to your computer and use it in GitHub Desktop.
Save el3/ad6a4d05cf79d645607b45f5e0a50dfe to your computer and use it in GitHub Desktop.
Kivy CheckBoxes in RecycleView
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.properties import BooleanProperty
KV = '''
<Row>:
active: cb.active
text: ""
Label:
text: root.text
CheckBox:
id: cb
active: root.active
on_release: root.store_checkbox_state()
<RV>:
viewclass: 'Row'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
BoxLayout:
RV:
id: rv
Button:
text: "Clear"
on_release:
rv.data = [d.update({'active': False}) or d for d in rv.data]
rv.refresh_from_data()
print(rv.data)
'''
class Row(RecycleDataViewBehavior, BoxLayout):
active = BooleanProperty()
index = 0
def refresh_view_attrs(self, rv, index, data):
self.index = index
super().refresh_view_attrs(rv, index, data)
def store_checkbox_state(self):
rv = App.get_running_app().rv
rv.data[self.index]['active'] = self.active
print(rv.data)
class RV(RecycleView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.data = [{'text': str(x), "active":False} for x in range(100)]
App.get_running_app().rv = self
class TestApp(App):
def build(self):
return Builder.load_string(KV)
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment