chiffa
chiffa

Reputation: 2088

Python kivy: how to use filechooser access files outside C drive

I am a total beginner in Kivy.

When I tried to re-implement the tutorial on the kivy.uix.filechooser module, I could only access the files on my C: drive, but not the other drives. I tried to look for where the filechoser roots, but to no avail.

Is there any way to access the other drives?

Upvotes: 5

Views: 3527

Answers (1)

Nykakin
Nykakin

Reputation: 8747

You need to change filechooser.path value:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string('''
<MyWidget>:
    file_chooser: file_chooser
    FileChooserListView:
        id: file_chooser
''')

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.file_chooser.path = "D:\\"

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

More advanced example:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.utils import platform

Builder.load_string('''
#:import lv kivy.uix.listview
#:import la kivy.adapters.listadapter

<MyWidget>:
    drives_list: drives_list
    file_chooser: file_chooser
    ListView:
        id: drives_list
        adapter:
            la.ListAdapter(data=root.get_win_drives(), selection_mode='single', allow_empty_selection=False, cls=lv.ListItemButton)
    FileChooserListView:
        id: file_chooser
''')

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.drives_list.adapter.bind(on_selection_change=self.drive_selection_changed)

    def get_win_drives(self):
        if platform == 'win':
            import win32api

            drives = win32api.GetLogicalDriveStrings()
            drives = drives.split('\000')[:-1]

            return drives
        else:    
            return []

    def drive_selection_changed(self, *args):
        selected_item = args[0].selection[0].text
        self.file_chooser.path = selected_item

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

Recipe for getting avaiable drives letters taken from here.

Upvotes: 9

Related Questions