pythonkivyfile-browser

How to use windows default file browser in kivy app


I am developing a kivy application and for file uploads i would like to use the windows default file browser, and not the ones offered by kivy (https://kivy.org/doc/stable/api-kivy.uix.filechooser.html). Do you know if that is even possible? I haven't found anything around. Thanks.

The functions that are actually working with kivy default file browser is the following:

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

def load_file(self, path, filename):
    self.filename = filename

    full_path = os.path.join(path, filename[0])

    filename = os.path.basename(full_path)

    app = App.get_running_app()
    self.check_input(full_path)

    self.dismiss_popup()

def check_input(self, filepath):
    '''Auxiliary method for checking user input.
    '''
    data_df = load_dataframe(filepath, self.ftypes, sep=';')

    if not isinstance(data_df, pd.DataFrame) and data_df == -999:
        self.warning_popup(message='File format not accepted')

    elif data_df.empty:
        self.warning_popup(message='Empty file')

    # If everything is OK
    else:
        self.data_df = data_df
        self.filepath = filepath

Solution

  • You could use a workaround based on tkinter, see my example app below.

    import tkinter as tk
    from tkinter import filedialog
    
    from kivy.app import App
    from kivy.base import Builder
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.button import Button
    
    Builder.load_string("""
    <rootwi>:
        orientation:'vertical'
        PathButton:
            on_press: label.text = self.get_path()
        Label:
            id: label
    
    """)
    class PathButton(Button):
        @staticmethod
        def get_path():
            root = tk.Tk()
            root.withdraw()
    
            return( filedialog.askopenfilename() )
    
    class rootwi(BoxLayout):
        pass
    
    
    class MyApp(App):
        def build(self):
            return rootwi()
    
    if __name__ == '__main__':
        MyApp().run()