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

11,608

Solution 1

I spent a few days to figured out how to install TkDnD lib, although it sounds like an easy question but did confused me a little while.

Two ways to install TkDnD on OSX: A. Copy to /System/Library/Tcl/8.x: OSX preinstalled Python already, and this is the path where Tcl library is installed. TkDnd lib will be loaded automatically while using Tk/Tcl lib.

B. Set os.environ['TKDND_LIBRARY'] to the location of TkDnd2.x.dylib: Sample code:

if sys.platform == 'win32':
   if getattr(sys, 'frozen', False):
      os.environ['TKDND_LIBRARY'] = os.path.join(os.path.dirname(sys.executable), 'tkdnd2.7')

Solution 2

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.

Solution 3

This is an update from 2017, with a bit more detail, since Mac decided to make it impossible to write files to /System/Library. The following solution is also cross-platform, since I am currently writing an app for both Windows/Mac that uses TkDnD.

The following solution works with pyInstaller, and also with Mac OS 10.12 and Windows 7.

First, we need to get a path to where tkDnD is. By default, I place tkdnd2.8 folder next to main.py.

import sys, os
if getattr(sys, 'frozen', False):
    # If the application is run as a bundle, the pyInstaller bootloader
    # extends the sys module by a flag frozen=True and sets the app 
    # path into variable _MEIPASS'.
    application_path = sys._MEIPASS
else:
    application_path = os.path.dirname(os.path.abspath(__file__))

TK_DND_PATH = os.path.join(application_path,'tkdnd2.8')

Note that Ellis's solution works Tcl directly, modifying the path. Make sure that SOMEWHERE, you have something along this gist:

import tkinter as tk
root = tk.Tk()
root.eval('lappend auto_path {' + TK_DND_PATH + '}')

After this, whenever you happen to actually import tkDnD, it will find it. I used DnD.py. Without the 'lappend auto_path' command, my program could never find tkDnD.

https://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html

Solution 4

2021 update

it annoyed me enough,so after the author did not responded I've forked the repo and built wheel for the latest compiled release(2.9.2) and upload it to pypi

you can now just do pip install tkinterdnd2 and it should work.

import is tkinterdnd2 and not Tkinterdnd2. see demos

Solution 5

This is an update from 2020 for MacOS Catalina users.

Firstly, the tkdnd library project has been moved to github. The latest version is 2.9.3, and if you do not want to compile the library yourself the latest release is 2.9.2.

Secondly, placing the tkdnd lib on /Library/Tcl doesn't work anymore (you will get "error: tkdnd library not found"). With Catalina, python looks for the tkdnd lib in its own folder, that is /Library/Frameworks/Python.framework/Versions/.../lib. Placing the tkdnd2.9.3 folder in this path works just fine.

By the way, placing the TkinterDnD2 Python wrapper in /Library/Frameworks/Python.framework/Versions/.../lib/python/site-packages still works on Catalina.

Just to clarify, I only tested it for Python 3 (3.8.5), I do not know if for Python 2 the solution is the same but I suppose so.

Share:
11,608
Ellis Shen
Author by

Ellis Shen

"Build great software and eager to face challenges!" Software developer working in Manhattan.

Updated on June 18, 2022

Comments

  • Ellis Shen
    Ellis Shen almost 2 years

    I've spent some time to search for workable solution to do Drag and Drop behavior with Python Tkinter on OSX platform, and the most possible solution found is TkDnD library. http://sourceforge.net/projects/tkdnd/files/

    However I cannot find any manual or guide about the installation and basically no sample on OSX. Can anyone share their experience with me?

    Furthermore, is it not a good choice to use Tkinter as a GUI solution? My users are all OSX platform and Python is preinstalled on all these machines. Any good suggestion to find a native GUI support without additional installation? PyQT seems to be another choice, but not sure if it requires the additional installation on Client machine.

  • Gary02127
    Gary02127 over 6 years
    I'm on OSX 10.11. Although I can't write to /System/Library, I can write to /Library. Putting the tkdnd2.8 directory there works, requiring no path or other Python reconfig for the tkdnd files to be found.
  • arthropod
    arthropod over 5 years
    Wow the method worked flawlessly with Python 3.6.4 on Windows 7. Almost too good to be true. Thank you so much!
  • Eliav Louski
    Eliav Louski about 3 years
    this actually worked! and actually, only the 2 step was needed for me (python 3.9 win10/64bit)
  • Iain Stanford
    Iain Stanford over 2 years
    Superb! Worked great for me and thanks for making it 10x easier for Mac users!
  • Steven H
    Steven H about 2 years
    Hey Eliav- appreciate your work on this (though I haven't succeeded in getting it working yet!). I've tried every which way to get the DnD working from external files (windows explorer) into the tkinter app, and I'm not having luck. Dragging text works, but not files. When dragging files, there is no event firing at all and the icon does not change to the "+" icon (it stays the circle/slash icon like when dragging to anything invalid). I'm using Win 10 64-bit, and I've tried manually installing, v2.8, etc. Any ideas? Thanks!
  • Steven H
    Steven H about 2 years
    ^ to update the prior, I found I am able to drag/drop files IF I drag them from the file browser the opens when I do File --> Open (from my Spyder IDE). However, I cannot drag files from Windows Explorer (which is the goal).
  • Steven H
    Steven H about 2 years
    And last update because I figured it out! Spyder (and thus the app) was running in Admin mode, vs the explorers being in standard user mode. That was preventing the drag and drop, and now all works as expected.