Хорошо, в вашем случае это будет выглядеть так:
.py файл:
# import this
from kivy.properties import ObjectProperty
from kivy.clock import Clock
...
class MainWindow(Screen):
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
# we have to delay running that function, it will run when kv file will be ready to provide widgets by id
Clock.schedule_once(self.getids)
self.counter = 1
self.textlist = [TextInput()]
self.grid.add_widget(Label(text='Input value ' + counter))
self.counter += 1
self.grid.add_widget(self.textlist[0])
# don't forget this
grid = ObjectProperty(None)
# function to connect layout with the variable
def getids(self):
self.grid = self.ids.grid
# function to create new inputs, that button 'new line' calls:
def addnewtextinput(self):
self.grid.add_widget(Label(text='Input value ' + self.counter))
self.counter += 1
self.textlist.append(TextInput())
self.grid.add_widget(self.textlist[-1])
# function to get a result:
def getresult(self):
result = 0
for i in self.textlist:
# you may convert it to float if you need, like float(i.text)
result += int(i.text)
self.ids.label_id.text = str(result)
class MyMainApp(App):
def build(self):
self.b1 = WindowManager()
self.b1.add_widget(MainWindow())
return self.b1
.kv файл:
<MainWindow>:
name: "main"
# don't forget to add this
grid: grid.__self__
GridLayout:
cols:1
# you will control that GridLayout from .py so here it's empty
GridLayout:
# set the id to be able to control it from .py file
id: grid
cols: 2
CustButton:
text: "new line"
on_press: root.addnewtextinput()
CustButton:
text: "result"
font_size: "30sp"
on_press: root.getresult()
TextInput:
id:label_id
font_size: 40
multiline: True