Kivy:拖放,获取文件路径

2022-01-15 00:00:00 python kivy

问题描述

在 Kivy 中,我正在尝试构建一个界面,用户可以在其中将文件拖放到小部件(文本输入)中,然后我的代码将检索该文件的文件系统路径(/path/to/users.文件).这似乎比使用 FileChooser 小部件更简单,但我该怎么做呢?

In Kivy, I am trying to build an interface where the user can drag and drop a file into a widget (text input) and then my code would retrieve the file system path of that file (/path/to/users.file). That seems like a simpler approach than using the FileChooser widget, but how would I do it?

谢谢!


解决方案

使用on_dropfile 事件处理程序.这是一个工作示例:

Use on_dropfile event handler. Here is an working example:

from kivy.app import App
from kivy.core.window import Window


class WindowFileDropExampleApp(App):
    def build(self):
        Window.bind(on_dropfile=self._on_file_drop)
        return

    def _on_file_drop(self, window, file_path):
        print(file_path)
        return

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

相关文章