Search code examples
pythonpython-3.xkivykivy-languagekivymd

How do I add TwoLineIconListItem in a builder using a method in python kivymd?



I want to make an android application using kivy, kivymd in python. In
that app I want to add a music player which will automatically find
all the music files and then it will add all that files as a
OneLineIconListItem, and when we click on it the music will be played
as per the title of the OneLineIconListItem text I mean that
as a normal offline android music player which shows all the file(music name) and when we
click on that it will play the song my code is -:

   from kivy.lang import Builder
import os
from kivymd.app import MDApp

helper_string = """
Screen:
    name: "Music_Player"
    BoxLayout:
        orientation: "vertical"
        MDToolbar:
        title: "Toolbar"
        elevation: 10

        MDLabel:
        text: "Music_Player" 
"""


class MusicApp(MDApp):
    def build(self):
        screen = Builder.load_string(helper_string)
        return screen

    def getting_all_music_files(self):
        for root, dirs, files in os.walk('C:/'):
            for file in files:
                if file.endswith('.mp3'):
                    required_file = file
                    the_location = os.path.abspath(required_file)


If you want to get the file location then just print the_location and set the directory in my case it is in C:/
Now, what I want is to add a for loop that takes all the locations and then add it as a list in my screen: BoxLayout:
But I am not able to figure out that how to use a for loop for this that add the location OneLineIconListItem to my app and when we will click on it it will play the song..
I am new to programming so please help me out


Solution

  • from kivy.lang import Builder
    
    from kivymd.app import MDApp
    from kivymd.uix.list import OneLineListItem
    
    KV = '''
    ScrollView:
    
        MDList:
            id: container
    '''
    
    
    class Test(MDApp):
        def build(self):
            return Builder.load_string(KV)
    
        def on_start(self):
            for i in range(20):
                self.root.ids.container.add_widget(
                    OneLineListItem(text=f"Single-line item {i}")
                )
    
    Test().run()