fabocode
fabocode

Reputation: 62

How to show files list with ListView? Python - Kivy

I have a path with a file list that I want to show in kivy like this:

excel_file.xlsx

D.xlsx

U.xlsx

another_route.xlsx

test.txt

G.xlsx

When I'm implementing the same algorithm to show them in kivy as a list of items, I only got a list exactly like this:

G

.

x

l

s

x

I want to know what exactly what I'm doing wrong. Let me show you my code:

class Routes_List_Screen(Screen):
    path = "/home/pi/Documents/myRoutes"
    dirs = os.listdir(path)
    for file in dirs:
        my_data = ListProperty(file)
        print(file) # this is just to know what exactly is printing file variable

the kv file:

<Routes_List_Screen>:
    canvas:
        Color:
            rgb: [.30, .30, .30]
        Rectangle:
            pos: self.pos
            size: self.size
    BoxLayout:
        orientation: 'vertical'
        ListView:
            size_hint_y: .8
            adapter:
                ListAdapter(data=root.my_data,
                selection_mode='single',
                allow_empty_selection=False,
                cls=ListItemButton)
        Button:
            text: 'Load'

My output: What I've got from kivy with the current code

Upvotes: 0

Views: 1598

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

You must pass the list directly to ListProperty:

class Routes_List_Screen(Screen):
    path = "/home/pi/Documents/myRoutes"
    dirs = os.listdir(path)
    my_data = ListProperty(dirs)

Output:

enter image description here

Explanation:

Code:

for file in dirs:
    my_data = ListProperty(file)
    print(file) # this is just to know what exactly is printing file variable

In the previous code you are updating the variable my_data at every moment:

another_route.xlsx
excel_file.xlsx
G.xlsx
U.xlsx
test.xlsx
D.xlsx

The last stored value was the string D.xlsx, the string is iterable, so it separated it into letters.

Upvotes: 1

Related Questions