Riccardo Volpe
Riccardo Volpe

Reputation: 1623

Drag-and-Drop multiple files in Python (Windows)

I'm starting to learn Python, and I thought to create a converter from a file to another (for example, from png to avi or between other file extensions) under Windows OS for now.

I wrote a script which works fine and it completes the conversion process, but I want improve it in functionality (and then in graphics); I'm using Tkinter and I thought to load the files with the possibility to drag-and-drop them as input for the next conversion command, instead of open a folder in which to put files as "input source". I found this topic (python drag and drop explorer files to tkinter entry widget) and I used it in this way:

import sys
import os
import Tkinter
from tkdnd_wrapper import TkDND
import shlex, subprocess
from subprocess import Popen, PIPE
import glob
import shutil

root = Tkinter.Tk()
dnd = TkDND(root)
entry = Tkinter.Entry()
entry.grid()

def handle(event):
    inputfilespath = event.data
    event.widget.insert(0, inputfilespath)
    filesdir = os.path.dirname(os.path.realpath(inputfilespath))
    files = glob.iglob(os.path.join(filesdir, "*.myext"))
    for inputfilespath in files:
        if os.path.isfile(inputfilespath):
            subprocess1 = subprocess.Popen([...conversion command given as list, not string...], shell=True)
            print "\n\nConversione in corso..."
            subprocess1.wait()
            subprocess1.terminate()
            print "\n\nProcesso terminato!"

dnd.bindtarget(entry, handle, 'text/uri-list')
root.mainloop()

The problems:

  1. If filename has a space, there is no conversion, and process ends without even notify any error too. "inputfilespath" wants to be the generic name for all the input files which I selected, and (for what I read) I can't (?) use quotes for an environment variable hoping to include filename's whitespace...

  2. I tried to copy different files (with the same file extension and without whitespaces into the filename) in the same folder, and if I drag-and-drop only one of them on the Entry widget, the process starts fine (and this is great!), but it continues also for all the other no-selected files with the same extension in the same folder, whereas if I drag-and-drop multiple files on the Entry widget, no conversion occurs....

Upvotes: 4

Views: 6916

Answers (2)

Dan Alexander
Dan Alexander

Reputation: 2072

Just use the tkinter file dialog, then just have it insert the files into the entry box.

Example:

filedialog = tkFileDialog.askopenfilenames(*options*)
entry.insert(END, filedialog)

Example Using StringVar:

entryVar = StringVar()
entry = Entry(textvariable=entryVar)
filedialog = tkFileDialog.askopenfilenames(*options*)
entryVar.set(filedialog

Hope this helps!

Upvotes: 2

Oblivion
Oblivion

Reputation: 1729

It seems that filenames containing whitespace are being wrapped in braces (Tcl list style). To get a usable filelist, you should be able to do something like:

import Tkinter
from untested_tkdnd_wrapper import TkDND


def handle(event):
    files = root.tk.splitlist(event.data)

    for filename in files:
        event.widget.insert('end', filename)


root = Tkinter.Tk()    
lb   = Tkinter.Listbox(root, width=50)
lb.pack(fill='both', expand=1)

dnd = TkDND(root)
dnd.bindtarget(lb, handle, 'text/uri-list')

root.mainloop()

Upvotes: 3

Related Questions