Py file
- Используйте цикл for, чтобы пройти через контейнер всех виджетов, например.
TextInput
.
Отрывки
for child in reversed(self.container.children):
if isinstance(child, TextInput):
self.data_list.append(child.text)
файл кв
- Используйте контейнер, например.
GridLayout
- Добавить
id
для контейнера
- Добавьте все эти
Label
и TextInput
виджеты как дочерние для GridLayout
Отрывки
GridLayout:
id: container
cols: 2
Label:
text: "Last Name:"
TextInput:
id: last_name
Пример
main.py
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, ListProperty
from kivy.lang import Builder
Builder.load_file('main.kv')
class MyScreen(Screen):
container = ObjectProperty(None)
data_list = ListProperty([])
def save_data(self):
for child in reversed(self.container.children):
if isinstance(child, TextInput):
self.data_list.append(child.text)
print(self.data_list)
class TestApp(App):
def build(self):
return MyScreen()
if __name__ == "__main__":
TestApp().run()
main.kv
#:kivy 1.11.0
<MyScreen>:
container: container
BoxLayout:
orientation: 'vertical'
GridLayout:
id: container
cols: 2
row_force_default: True
row_default_height: 30
col_force_default: True
col_default_width: dp(100)
Label:
text: "Last Name:"
TextInput:
id: last_name
Label:
text: "First Name:"
TextInput:
id: first_name
Label:
text: "Age:"
TextInput:
id: age
Label:
text: "City:"
TextInput:
id: city
Label:
text: "Country:"
TextInput:
id: country
Button:
text: "Save Data"
size_hint_y: None
height: '48dp'
on_release: root.save_data()
выход