Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy: drag n drop, get file path

Tags:

python

kivy

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?

Thanks!

like image 743
fire_water Avatar asked Jun 01 '16 16:06

fire_water


People also ask

Does kivy have drag and drop?

There are 2 primary and one internal component used to have drag and drop: The DraggableObjectBehavior . Each widget that can be dragged needs to inherit from this.

Is kivy slow?

Many people state that kivy is slow. While this may be true it is mostly due to that python is slow to run on android devices. Thus it is in the programmer's hands to properly optimize their code so as to create a performant application.

What is root in kivy?

Inside a kv file, root always refers to a parent with angle brackets. There can therefore be multiple roots which you can refer to in a kv file, depending on where you are in the file.

Is kivy object oriented?

The Kivy App object does an impressive amount of work on your behalf. That is the beauty of object-oriented programming. This object does something, and all you have to do is tell it to do its job by invoking the run method.


1 Answers

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()
like image 61
avi Avatar answered Oct 11 '22 19:10

avi