Search code examples
pythonkivykivymd

How can i click a three line list item in kivymd and capture the first line text


learner / noob alert :)

im working on my first project and ive hit another wall.

im building a three line list item on the python side ( in my project it returns search values from a DB hence the need for a 'for' loop)

ive stripped down the code to where im stuck -

#.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.list import ThreeLineListItem


class Testing(BoxLayout):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def pop_list(self):
        peeps = range(0, 10)
        for peep in peeps:
            self.ids.container.add_widget(
                ThreeLineListItem(id='item', text=f"Line {peep}",
                                  secondary_text=f"Line {peep}",
                                  tertiary_text=f"Line {peep}"))
            # on_release= example_function()

    def example_function(self):
        # stores first line of text from the three line list
        # moves to other screen in screen manager defined in kv
        

class TestingApp(MDApp):
    pass


TestingApp().run()

and

#.kv

Testing:
    orientation: "vertical"
    Button:
        size_hint:0.4,0.1
        pos_hint: {"x":0.3}
        text: "add"
        on_press: root.pop_list()
    ScrollView:
        pos_hint: {"top":1}
        size_hint:1,1
        MDList:
            id: container

as the example shows im trying to use an on_release to trigger a def function that captures the first line text of the specific list item clicked and moves to a different screen.

while i have had to use id's, object and string properties in other areas i admit im at the "partial under standing" stage with these things and would appreciate a helping hand!

and one other thing id like to ask while im here and its in my head,

where do i find the modules and examples for teh kivy and kivymd widgets? on my searches it keeps popping up as a good way to learn to read their code are they are in the site packages folder of my venv? ( using pycharm btw )

Thanks again Folks! :)


Solution

  • Add the on_release to each ThreeLineListItem as:

    def pop_list(self):
        peeps = range(0, 10)
        for peep in peeps:
            self.ids.container.add_widget(
                ThreeLineListItem(id='item', text=f"Line {peep}",
                                  secondary_text=f"Line {peep}",
                                  tertiary_text=f"Line3{peep}",
                                  on_release=self.example_function))
    

    Then, in the example_function():

    def example_function(self, item):
        print('example', item.text)