The children list of the boxlayout is a list. You can use the list index() method and pass in the widget and it will return the appropriate index.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
kv = """
BoxLayout:
ScrollView:
BoxLayout:
id: box
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
"""
class IndexButton(Button):
def on_release(self):
app = App.get_running_app()
children = app.root.ids.box.children
index = children.index(self)
print(f'This button is at index: {index}')
class IndexWidgetDemo(App):
def build(self):
return Builder.load_string(kv)
def on_start(self):
box = self.root.ids.box
for i in range(20):
box.add_widget(IndexButton(text=str(i), size_hint_y=None, height=48))
IndexWidgetDemo().run()
You can also see the children list is the opposite order of insertion.