How to use windows default file browser in kivy app
Asked Answered
C

2

6

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
Cornejo answered 15/5, 2019 at 11:26 Comment(0)
G
7

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()
Gath answered 15/5, 2019 at 17:10 Comment(0)
R
8

You can use filechooser from Kivy Plyer. Make sure you already install Kivy Plyer using pip install plyer. You can try below code in Python console.

from plyer import filechooser
path = filechooser.open_file(title="Pick a CSV file..", 
                             filters=[("Comma-separated Values", "*.csv")])
print(path)
Raptorial answered 22/3, 2020 at 7:18 Comment(0)
G
7

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()
Gath answered 15/5, 2019 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.