简体   繁体   中英

TkDnD Error: Unable to load tkdnd library (_tkinter.TclError: can't find package tkdnd)

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.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM