-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_viewclass.py
More file actions
149 lines (124 loc) · 3.45 KB
/
Copy pathkey_viewclass.py
File metadata and controls
149 lines (124 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''
A form generator, using random data, but can be data driven (json or whatever)
Shows that you can use the key_viewclass attribute of RecycleView to select a
different Widget for each item.
'''
from random import choice, choices
from string import ascii_lowercase
from kivy.app import App
from kivy.lang import Builder
from kivy import properties as P
KV = r'''
<RVTextInput,RVCheckBox,RVSpinner>:
size_hint_y: None
height: self.minimum_height
index: None
title: ''
<RVTextInput@BoxLayout>:
value: ''
Label:
text: root.title
size_hint_y: None
height: self.texture_size[1]
TextInput:
text: root.value
on_text: app.handle_update(self.text, root.index)
size_hint_y: None
height: dp(40)
multiline: False
<RVCheckBox@BoxLayout>:
value: False
Label:
text: root.title
size_hint_y: None
height: self.texture_size[1]
CheckBox:
active: root.value
on_active: app.handle_update(self.active, root.index)
size_hint_y: None
height: dp(40)
<RVSpinner@BoxLayout>:
value: ''
values: []
Label:
text: root.title
size_hint_y: None
height: self.texture_size[1]
Spinner:
text: root.value
values: root.values
size_hint_y: None
height: dp(40)
on_text: app.handle_update(self.text, root.index)
FloatLayout:
RecycleView:
id: rv
data: app.data
key_viewclass: 'widget'
size_hint_x: 1
RecycleBoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
default_size_hint: 1, None
'''
class Application(App):
'''A form manager demonstrating the power of RecycleView's key_viewclass
property.
'''
data = P.ListProperty()
def build(self):
root = Builder.load_string(KV)
rv = root.ids.rv
self.data = [
self.create_random_input(rv, index)
for index in range(20)
]
return root
def handle_update(self, value, index):
if None not in (index, value):
self.data[index]['value'] = value
def create_random_input(self, rv, index):
return choice((
self.create_textinput,
self.create_checkbox,
self.create_spinner
))(rv, index)
def create_spinner(self, rv, index):
"""
create a dict of data for a spinner
"""
return {
'index': index,
'widget': 'RVSpinner',
'value': '',
'values': [
letter * 5
for letter in ascii_lowercase[:5]
],
'ready': True,
}
def create_checkbox(self, rv, index):
"""
create a dict of data for a checkbox
"""
return {
'index': index,
'widget': 'RVCheckBox',
'value': choice((True, False)),
'title': ''.join(choices(ascii_lowercase, k=10)),
'ready': True,
}
def create_textinput(self, rv, index):
"""
create a dict of data for a textinput
"""
return {
'index': index,
'widget': 'RVTextInput',
'value': ''.join(choices(ascii_lowercase, k=10)),
'title': ''.join(choices(ascii_lowercase, k=10)),
'ready': True,
}
if __name__ == "__main__":
Application().run()