如何在kivy app中使用windows默认文件浏览器
How to use windows default file browser in kivy app
我正在开发一个 kivy 应用程序,对于文件上传,我想使用 windows 默认文件浏览器,而不是 kivy (https://kivy.org/doc/stable/api-kivy.uix.filechooser.html) 提供的浏览器。你知道这是否可能吗?我周围什么都没有发现。谢谢
实际使用kivy默认文件浏览器的功能如下:
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
您可以使用基于 tkinter 的解决方法,请参阅下面我的示例应用程序。
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()
您可以使用 Kivy Plyer 中的文件选择器。确保您已经使用 pip install plyer
安装了 Kivy Plyer。您可以在 Python 控制台中尝试以下代码。
from plyer import filechooser
path = filechooser.open_file(title="Pick a CSV file..",
filters=[("Comma-separated Values", "*.csv")])
print(path)
我正在开发一个 kivy 应用程序,对于文件上传,我想使用 windows 默认文件浏览器,而不是 kivy (https://kivy.org/doc/stable/api-kivy.uix.filechooser.html) 提供的浏览器。你知道这是否可能吗?我周围什么都没有发现。谢谢
实际使用kivy默认文件浏览器的功能如下:
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
您可以使用基于 tkinter 的解决方法,请参阅下面我的示例应用程序。
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()
您可以使用 Kivy Plyer 中的文件选择器。确保您已经使用 pip install plyer
安装了 Kivy Plyer。您可以在 Python 控制台中尝试以下代码。
from plyer import filechooser
path = filechooser.open_file(title="Pick a CSV file..",
filters=[("Comma-separated Values", "*.csv")])
print(path)