Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy - change FileChooser defaul location

Right now the file chooser opens the very root as the default location but i want it to skip that and open as default the internal storage (sdcard) and the user can go down from there.

this is a snipped of my code so far The class:

class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)

The definition in the kv file

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

The actual load code:

def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

def load(self, path, filename):
        wimg = os.path.join(path, filename[0])
        self.image_source = wimg
        self.dismiss_popup()

So basically the user should not have to go up 1 directory to get to the sdcard, should already be there. Worst case scenario is filtering all the other folders there except the ones that contain the word sdcard.

like image 654
Nick Avatar asked Feb 28 '17 09:02

Nick


1 Answers

Set the path attribute.

FileChooserListView:
    id: filechooser
    path: "/your/path"

To find a directory on your system with python, you can do something like this:

import os

for root, dirs, files in os.walk("/"):
    for name in dirs:
        if name == "DCIM":
            print(root, name)

Just be aware that it might find two or more directories named DCIM, on your sdcard and internal storage.

like image 183
el3ien Avatar answered Nov 03 '22 18:11

el3ien