0

Trying to add some buttons with a for-loop to a container that is already defined in the .kv file:

The code looks like this:

a_list = ["Spam", "Eggs"]

for i in range(len(a_list)):
    self.ids.container.add_widget(

        ThreeLineListItem(id = a_list[i], text= "Some info ",
                                secondary_text = "More info",
                                tertiary_text = "Final info", 
                          on_release=lambda x: print_me(self.id))
                          )
def print_me(the_id):
    print(the_id)

This just prints "None", how come? And how to fix it so the first button prints "Spam" on release and the second one "Eggs"?

Sixtenson
  • 87
  • 8

1 Answers1

1
from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem

KV = '''
ScrollView:

    MDList:
        id: box

'''


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        a_list = ["Spam", "Eggs"]
        for name in a_list:
            self.root.ids.box.add_widget(
                OneLineListItem(text=name, on_release=self.print_item)
            )

    def print_item(self, instance):
        print(instance, instance.text)


Test().run()
Xyanight
  • 1,315
  • 1
  • 7
  • 10