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!
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.
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With