- Create a ScrollView of GridLayout
- Use a loop to create each line of the form consisting of a Label and TextInput widgets
- Use a loop to access child widget in self.container.children.
Please refer to the example below for details.
Example
main.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.metrics import dp
from kivy.properties import ObjectProperty, DictProperty
Window.size = (500, 330)
class FloatInput(TextInput):
def on_text(self, instance, text):
if text != "":
print("id={0}, text={1}".format(instance.id, text))
class TestScreen(Screen):
container = ObjectProperty(None)
def __init__(self, **kwargs):
super(TestScreen, self).__init__(**kwargs)
self.container.bind(minimum_height=self.container.setter('height'))
self.create_form()
def create_form(self):
# create 50 labels and text inputs
for i in range(50):
label = Label(text="Label {}".format(i), size_hint_y=None, height=dp(40))
text_input = FloatInput(id=str(i), hint_text="TextInput {}".format(i), size_hint_y=None, height=dp(40))
self.container.add_widget(label)
self.container.add_widget(text_input)
def check_validation(self):
for child in reversed(self.container.children):
if isinstance(child, Label):
label_text = child.text
if isinstance(child, FloatInput):
if child.text.strip() == "":
print("{0} - TextInput is blank, obj={1}".format(label_text, child))
class Test(App):
def build(self):
return TestScreen()
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
<TestScreen>:
container: container
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
ScrollView:
GridLayout:
id: container
cols: 2
size_hint_y: None
padding : 30,30
spacing: 10, 10
BoxLayout:
size_hint_y: 0.2
Button:
text: 'Ok'
on_release: root.check_validation()
Button:
text: 'Cancel'
Output
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…