Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Last active November 26, 2019 14:39
Show Gist options
  • Save tshirtman/48aaed06d2af6bb2d7c78edf0e7efe35 to your computer and use it in GitHub Desktop.
Save tshirtman/48aaed06d2af6bb2d7c78edf0e7efe35 to your computer and use it in GitHub Desktop.
use CheckBox/RadioButton in kivy's RecycleView
from kivy.lang import Builder
from kivy.app import App
from kivy.properties import ListProperty, NumericProperty
KV = '''
#:import RGBA kivy.utils.rgba
<Row@BoxLayout>:
text: ''
rv_key: 0
CheckBox:
active: app.current_selection == root.rv_key
on_active: app.select_row(root.rv_key, self.active)
group: 1
Label:
text: root.text
FloatLayout:
RecycleView:
size_hint: .8, .8
pos_hint: {'center': (.5, .5)}
data: app.data
viewclass: 'Row'
canvas.before:
Color:
rgba: RGBA('#212121')
Rectangle:
pos: self.pos
size: self.size
RecycleBoxLayout:
orientation: 'vertical'
size: self.minimum_size
size_hint_y: None
default_size_hint: 1, None
default_size: 0, 50
'''
class TestApp(App):
data = ListProperty()
current_selection = NumericProperty(allownone=True)
def build(self):
self.data = [
dict(
text='key {}'.format(i),
rv_key=i,
)
for i in range(100)
]
return Builder.load_string(KV)
def select_row(self, rv_key, active):
if active:
self.current_selection = rv_key
elif rv_key == self.current_selection:
self.current_selection = None
if __name__ == '__main__':
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment