Search code examples
pythonfunctionkivykivy-languagekivymd

callback for loop values of List Item's (Kivy & KivyMD)


Here I have a loop that looks for keys & values from a JSON List

Currently, the view of the KivyMD List is correct but I want to pass onto the correct key onto print instead of having the last key from JSON printed out.

ScrollView:
    MDList:
        id: container
   def on_start(self):
        for (self.k, v) in self.data.items():  

            item = TwoLineAvatarListItem(text=str(self.k),secondary_text=str(v["NAME"]),on_release=self.show_data)
            image = ImageLeftWidget(source=v["Image"])
            item.add_widget(image)
            self.root.ids.container.add_widget(item)

    def show_data(self,obj):
        print(self.k)

enter image description here

The value: E101a is always printed

To summarize i want to look at the "container values" separate


Solution

  • Jonas! I have solved it with this:

    on_release=lambda x, value_for_pass=value: self.passValue(value_for_pass)
    

    In a loop like this:

    .py file:

    # (...)
    class AllClientsScreen(Screen):
        
        def on_enter(self):
            self.show_clients()
    
        def show_clients(self):
            for i in range(10):
                client_id = i
                name = "Roger" + str(client_id) + " id: " + str(client_id)
                last_name = "Rabbit" + str(client_id)
                phone = "555" + str(client_id)
                email = "roger" + str(client_id) + "@rabbit.com"
                item = ThreeLineListItem(
                text = name + " " + last_name,
                secondary_text = phone,
                tertiary_text = email,
                on_release=lambda x, value_for_pass=name: self.passValue(value_for_pass)
                )
                    
                self.ids.container.add_widget(item)
        
        def passValue(self, *args):
            args_str = ','.join(map(str,args))
            screen2 = self.manager.get_screen ('oneclient')
            screen2.ids.client_id.text = args_str
            self.manager.current = 'oneclient'
            print(args)
    

    .kv file:

    #:import NoTransition kivy.uix.screenmanager.NoTransition
    
    ScreenManager:
        transition: NoTransition()
        MenuScreen:
        AllClientsScreen:
        OneClientScreen:
    
    <MenuScreen>:
        name: 'menu'
        MDRectangleFlatButton:
            text: 'All Clients'
            pos_hint: {'center_x': 0.5, 'center_y':0.5}
            on_release: root.manager.current = 'allclients'
        
    <AllClientsScreen>:
        name: 'allclients'
        ScrollView:
            MDList:
                id: container
                padding: dp(20)
    
        MDLabel:
            id: clientsdata
            text: 'All Clients'
            halign: 'center'
        MDRectangleFlatButton:
            text: 'Menu'
            pos_hint: {'center_x': 0.5, 'center_y':0.4}
            on_release: root.manager.current = 'menu'
    
    <OneClientScreen>:
        name: 'oneclient'
        MDLabel:
            id: client_id
            text: 'Cliente'
            halign: 'center'
        ScrollView:
            MDList:
                id: container
                padding: dp(40)
        MDRectangleFlatButton:
            text: 'Menu'
            pos_hint: {'center_x': 0.5, 'center_y':0.4}
            on_release: root.manager.current = 'menu'
    

    I have just posted the essential parts of the codes, if you have any doubts or get succed, please let me know. Have a nice development!