0

I am currently working on MacOs Catalina 10.15.6 with Python 3.8 and Tcl/Tk 8.6. As suggested in here, I donwloaded both the Tk extension tkdnd2.8 from https://sourceforge.net/projects/tkdnd/ and the Python wrapper TkinterDnD2 from https://sourceforge.net/projects/tkinterdnd/, and then I copied the tkdnd2.8 directory to /Library/Tcl and the TkinterDnD2 directory to /Library/Frameworks/Python.framework/Versions/.../lib/python/site-packages.

After that, when trying to execute the following simple code:

from TkinterDnD2 import *
import tkinter as tk
root = TkinterDnD.Tk()

It gives me the error:

Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/TkinterDnD2/TkinterDnD.py", line 39, in _require
        TkdndVersion = tkroot.tk.call('package', 'require', 'tkdnd')
_tkinter.TclError: can't find package tkdnd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    File "/Users/Administrador/Desktop/tkdnd_test.py", line 3, in <module>
        root = TkinterDnD.Tk()
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/TkinterDnD2/TkinterDnD.py", line 271, in __init__
        self.TkdndVersion = _require(self)
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/TkinterDnD2/TkinterDnD.py", line 41, in _require
        raise RuntimeError('Unable to load tkdnd library.')
RuntimeError: Unable to load tkdnd library.

Since tkdnd was not automatically being found/loaded (renaming the tkdnd2.8 folder to tkdnd didn't work either), I tried to manually specify the library path as suggested by @Ellis Shen and @aong152. I copied the tkdnd2.8 folder next to my tkdnd_test.py file. Unfortunately, when running the following code:

from TkinterDnD2 import *
import tkinter as tk
import sys, os

print(os.environ.get('TKDND_LIBRARY'))
application_path = os.path.dirname(os.path.abspath(__file__))
TK_DND_PATH = os.path.join(application_path,'tkdnd2.8')
os.environ['TKDND_LIBRARY'] = TK_DND_PATH
print(os.environ.get('TKDND_LIBRARY'))

root = TkinterDnD.Tk()

The same error is displayed (before that, the tkdnd path is printed):

None
/Users/Administrador/Desktop/tkdnd2.8
Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/TkinterDnD2/TkinterDnD.py", line 39, in _require
        TkdndVersion = tkroot.tk.call('package', 'require', 'tkdnd')
_tkinter.TclError: can't find package tkdnd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    File "/Users/Administrador/Desktop/tkdnd_test.py", line 11, in <module>
        root = TkinterDnD.Tk()
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/TkinterDnD2/TkinterDnD.py", line 271, in __init__
        self.TkdndVersion = _require(self)
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/TkinterDnD2/TkinterDnD.py", line 41, in _require
        raise RuntimeError('Unable to load tkdnd library.')
RuntimeError: Unable to load tkdnd library.

I feel like I am missing something here, but I've spent a lot of time searching similar experiences and haven't been able to find a solution. Any help will be much appreciated.

waldner92
  • 13
  • 1
  • 3
  • You might have a 32 bit library attempting to be loaded into a 64 bit python. Check the architecture for both. – patthoyts Aug 31 '20 at 14:24
  • @patthoyts I also thought that, but when I run `file libtkdnd2.8.dylib `, the output is: `libtkdnd2.8.dylib: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [i386:Mach-O dynamically linked shared library i386]`. And when I run `python -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'` the output is `('7fffffffffffffff', True)` so I think they are both 64-bit. I also noticed that when i run the code in OS Mojave it just works fine, I only get the error in OS Catalina. – waldner92 Sep 05 '20 at 10:01
  • See my answer to https://stackoverflow.com/questions/25427347/how-to-install-and-use-tkdnd-with-python-tkinter-on-osx/46856247#46856247 – GaryMBloom Dec 07 '21 at 20:28
  • @Gary02127 yes, I used your answer bat had to modify some paths to make it work for macOS Catalina and above. You can see it at https://stackoverflow.com/a/64621610/14195896 – waldner92 Jan 07 '22 at 10:23
  • @Gary02127, however, Eliav Louski has built it and uploaded it to pypi, so there is no need to install it manually anymore, pip install just works fine. Thanks Eliav! – waldner92 Jan 07 '22 at 10:26

2 Answers2

2

I've built it and upload it to pypi

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

import is tkinterdnd2 and not Tkinterdnd2

Eliav Louski
  • 3,593
  • 2
  • 28
  • 52
  • 1
    I tried a fresh install on a new MacBook (with Intel and MacOs 11.2.3, not M1) and it worked perfectly with pip install! Thank you so much, you saved many struggling to a lot of people! – waldner92 Jan 07 '22 at 10:28
0

Solved! The folder where you should place the tkdnd library is /Library/Frameworks/Python.framework/Versions/.../lib/. Placing the lib on /Library/Tcl doesn't work for Catalina. I recommend you to install the newest tkdnd version (currently 2.9.3) available here.

waldner92
  • 13
  • 1
  • 3