TkinterDnD2 is a python wrapper for George Petasis'' tkDnD Tk extension version 2
Actively maintained. Last release 5 weeks ago, commits in 7 of the last 26 weeks. A safe choice.
pip install tkinterdnd2TkinterDnD2
Eliav2/tkinterdnd2 is a fork of the (unmaintained) pmgagne/tkinterdnd2 which is a python wrapper for tkdnd .
This repo was originally forked and edited for the purpose of publishing to pypi so one could simply install this package with pip install tkinterdnd2.
This repository is being maintained to ensure availability of tkinterdnd2 into the future, providing Tkinter native drag and drop support for windows, unix and Mac OSX.
What is TkDnD
tkDnD2 is a tcl/Tk extension adding native drag and drop support.
This repository contains the compiled binaries from https://github.com/petasis/tkdnd/releases/tag/tkdnd-release-test-v2.9.5 and my own compiled binaries for full Tcl 9 support: https://github.com/Squiblydoo/tkdnd. Tcl 9 binaries are not available for Intel Macs (osx-x64).
Install
python -m pip install tkinterdnd2
Usage
import tkinter as tk
from tkinterdnd2 import DND_FILES, TkinterDnD
root = TkinterDnD.Tk() # notice - use this instead of tk.Tk()
lb = tk.Listbox(root)
lb.insert(1, "drag files to here")
def on_drop(event):
# event.data is a raw Tcl list string, not a Python list - see note below
for path in root.tk.splitlist(event.data):
lb.insert(tk.END, path)
# register the listbox as a drop target
lb.drop_target_register(DND_FILES)
lb.dnd_bind('<<Drop>>', on_drop)
lb.pack()
root.mainloop()

[!NOTE]
event.datais a raw Tcl list, not a Python list or plain string. When multiple files are dropped, their paths come back space-separated in a single string, and any path containing a space is wrapped in{braces}so it can still be told apart from the others. Useroot.tk.splitlist(event.data)(as above) to get a clean Python list of paths — don't split the string yourself, since that will break on braced/spaced paths.
see any of the demos for usage examples.