3

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Dan
  • 511
  • 2
  • 9
  • 19

3 Answers3

2

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.

D K
  • 5,530
  • 7
  • 31
  • 45
  • Do you have this binding ? It is no longer accessible. Also, see this question http://stackoverflow.com/questions/14267900/python-drag-and-drop-explorer-files-to-tkinter-entry-widget for a new question regarding this same problem. – mmgp Jan 11 '13 at 22:22
  • @mmgp You can use WayBack to get the zip (http://web.archive.org/web/20120104000747/http://klappnase.bubble.org/download/TkinterDnD-0.4.zip) – D K Jan 12 '13 at 22:21
1

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.

arunkumar
  • 32,803
  • 4
  • 32
  • 47
  • You seriously need to AppleScript this up on Macs, in 2011? The Windows installation sets this up automatically - dropped files are fed to the `argv`. – Karl Knechtel Aug 06 '11 at 22:26
0

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.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151