Dan
Dan

Reputation: 519

Drag drop file onto Python script

I'm looking for a cross-platform way of making my Python script process a file's path by implementing a drag n drop method. At the moment I manually go to the terminal and use the sys.argv method:

python myscript.py /Python/myfile.xls

However this is slow and "techy". Ideally I would a quick and interactive way of allowing a file be processed by my Python script. I primarily need this to work for Mac but cross-platform would be better.

Upvotes: 3

Views: 8544

Answers (3)

D K
D K

Reputation: 5760

If you want to use Tkinter, have a look at the Tkinter DnD binding from here http://klappnase.bubble.org/TkinterDnD/index.html

When run, the binding shows an example with a listbox that allows you to drag a file on to it.

Upvotes: 2

arunkumar
arunkumar

Reputation: 34053

Do you want to drag and drop the myfile.xls onto your python script within your file navigator ? Say Finder or whatever on Mac, Explorer on Win, Nautilus etc. ? In that case there will not be a simple cross-platform solution, given that you will have to hook into different software on different systems.

For a Mac specific solution try AppleScript - here is a sample

And for something Pythonic there is http://appscript.sourceforge.net/ , http://docs.python.org/library/macosa.html

Otherwise the solution is in the answer above. Use a custom GUI built in Tk, or wx or QT. You can look up their respective documentation for drag and drop, they do have cross-platform ways of doing it.

Upvotes: 1

Rafe Kettler
Rafe Kettler

Reputation: 76955

It'd be easiest to just write a small GUI with Tkinter or something similar and have the user select a file from within the GUI. Something along these lines:

import tkFileDialog
f = tkFileDialog.askopenfilename()
# Go on from there; f is a handle to the file that the user picked

I'm not aware of any cross platform methods to get a script to work with drag and drop, however. This is probably easier, though.

Upvotes: 0

Related Questions