How to Install and Use TkDnD with Python Tkinter on OSX?

I got this working on both Windows (10) and OSX (10.11) by downloading:
A) Tk extensions tkdnd2.8 from https://sourceforge.net/projects/tkdnd/
B) Python wrapper TkinterDnD2 from https://sourceforge.net/projects/tkinterdnd/

On OSX:
1) Copy the tkdnd2.8 directory to /Library/Tcl
2) Copy the TkinterDnD2 directory to /Library/Frameworks/Python.framework/Versions/…/lib/python/site-packages
(Use the sudo command for copying files on OSX due to permissions.)

On Windows:
1) Copy the tkdnd2.8 directory to …\Python\tcl
2) Copy the TkinterDnD2 directory to …\Python\Lib\site-packages

And here’s a simple test case based on python drag and drop explorer files to tkinter entry widget. The TkinterDnD2 download comes with much more robust examples.

    import sys
    if sys.version_info[0] == 2:
        from Tkinter import *
    else:
        from tkinter import *
    from TkinterDnD2 import *

    def drop(event):
        entry_sv.set(event.data)

    root = TkinterDnD.Tk()
    entry_sv = StringVar()
    entry_sv.set('Drop Here...')
    entry = Entry(root, textvar=entry_sv, width=80)
    entry.pack(fill=X, padx=10, pady=10)
    entry.drop_target_register(DND_FILES)
    entry.dnd_bind('<<Drop>>', drop)
    root.mainloop()

Update: the above procedure works for Python 2 or 3.

Leave a Comment