Created
December 6, 2017 17:30
-
-
Save Tungsteno74/c524176aeb4f7f0f7815e52d6554cfc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import sample | |
from string import ascii_lowercase | |
from uuid import uuid4 | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.uix.checkbox import CheckBox | |
from kivy.uix.button import Button | |
kv = """ | |
<ColorizedBLayout@BoxLayout>: | |
canvas.before: | |
Color: | |
rgba: 0.5, 0.5, 0.5, 1 | |
Rectangle: | |
size: self.size | |
pos: self.pos | |
<Row>: | |
uuid: '' | |
value: '' | |
BoxLayout: | |
ColorizedBLayout: | |
orientation: 'vertical' | |
Label: | |
text: root.value | |
Label: | |
text: root.value | |
Label: | |
text: root.value | |
CheckBox: | |
active: False | |
allow_no_selection: True | |
on_active: root.select() | |
<Test>: | |
canvas: | |
Color: | |
rgba: 0.3, 0.3, 0.3, 1 | |
Rectangle: | |
size: self.size | |
pos: self.pos | |
rv: rv | |
orientation: 'vertical' | |
GridLayout: | |
cols: 4 | |
rows: 2 | |
size_hint_y: None | |
height: dp(108) | |
padding: dp(8) | |
spacing: dp(16) | |
Button: | |
text: 'Populate list' | |
on_press: root.populate() | |
Button: | |
text: 'Sort list' | |
on_press: root.sort() | |
Button: | |
text: 'Clear list' | |
on_press: root.clear() | |
BoxLayout: | |
spacing: dp(8) | |
Button: | |
text: 'Insert new item' | |
on_press: root.insert(new_item_input.text) | |
TextInput: | |
id: new_item_input | |
size_hint_x: 0.6 | |
hint_text: 'value' | |
padding: dp(10), dp(10), 0, 0 | |
BoxLayout: | |
spacing: dp(8) | |
Button: | |
text: 'Update first item' | |
on_press: root.update(update_item_input.text) | |
TextInput: | |
id: update_item_input | |
size_hint_x: 0.6 | |
hint_text: 'new value' | |
padding: dp(10), dp(10), 0, 0 | |
Button: | |
text: 'Remove first item' | |
on_press: root.remove() | |
Button: | |
text: 'Remove selected' | |
on_press: root.remove_selected() | |
RecycleView: | |
id: rv | |
scroll_type: ['bars', 'content'] | |
scroll_wheel_distance: dp(114) | |
bar_width: dp(10) | |
viewclass: 'Row' | |
RecycleBoxLayout: | |
default_size: None, dp(56) | |
default_size_hint: 1, None | |
size_hint_y: None | |
height: self.minimum_height | |
orientation: 'vertical' | |
spacing: dp(2) | |
""" | |
Builder.load_string(kv) | |
selected_rvg = [] | |
class Row(BoxLayout): | |
def select(self): | |
global selected_rvg | |
if self.value: | |
selected_rvg.append(self.uuid) | |
else: | |
selected_rvg.remove(self.uuid) | |
class Test(BoxLayout): | |
def populate(self): | |
global rvg | |
self.rv.data = [{'uuid':str(uuid4()),'value': ''.join(sample(ascii_lowercase, 6))} for x in range(100)] | |
rvg = self.rv | |
def sort(self): | |
self.rv.data = sorted(self.rv.data, key=lambda x: x['value']) | |
def clear(self): | |
self.rv.data = [] | |
def insert(self, value): | |
self.rv.data.insert(0, {'value': value or 'default value'}) | |
def update(self, value): | |
if self.rv.data: | |
self.rv.data[0]['value'] = value or 'default new value' | |
self.rv.refresh_from_data() | |
def remove(self): | |
if self.rv.data: | |
self.rv.data.pop(0) | |
def remove_selected(self): | |
global rvg | |
global selected_rvg | |
rv = rvg | |
for id in selected_rvg: | |
for da in rv.data: | |
if id is da['uuid']: | |
dad = rv.data.index(da) | |
rv.data.pop(dad)##??? | |
del selected_rvg[:] | |
class TestApp(App): | |
def build(self): | |
return Test() | |
if __name__ == '__main__': | |
TestApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment