【发布时间】:2020-12-19 12:50:49
【问题描述】:
我目前正在使用 Python 3.8 和 Tcl/Tk 8.6 开发 MacOs Catalina 10.15.6。正如here 中所建议的那样,我从https://sourceforge.net/projects/tkdnd/ 下载了Tk 扩展tkdnd2.8 和https://sourceforge.net/projects/tkinterdnd/ 的Python 包装器TkinterDnD2,然后我复制了 tkdnd2.8 目录到 /Library/Tcl 和 TkinterDnD2 目录到 /Library/Frameworks/Python.framework/Versions/.. ./lib/python/site-packages.
之后,当尝试执行以下简单代码时:
from TkinterDnD2 import *
import tkinter as tk
root = TkinterDnD.Tk()
它给了我错误:
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.
由于没有自动找到/加载 tkdnd(将 tkdnd2.8 文件夹重命名为 tkdnd 也不起作用),我尝试按照 @Ellis Shen 和 @aong152 的建议手动指定库路径。我复制了我的 tkdnd_test.py 文件旁边的 tkdnd2.8 文件夹。不幸的是,在运行以下代码时:
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()
显示同样的错误(之前打印的是tkdnd路径):
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.
我觉得我在这里遗漏了一些东西,但我花了很多时间搜索类似的体验,但一直未能找到解决方案。任何帮助将不胜感激。
【问题讨论】:
-
您可能有一个 32 位库试图加载到 64 位 python 中。检查两者的架构。
-
@patthoyts 我也这么认为,但是当我运行
file libtkdnd2.8.dylib时,输出为: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]。当我运行python -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'时,输出为('7fffffffffffffff', True),所以我认为它们都是64 位的。我还注意到,当我在 OS Mojave 中运行代码时它运行良好,我只在 OS Catalina 中得到错误。 -
@Gary02127 是的,我使用了您的答案,但必须修改一些路径才能使其适用于 macOS Catalina 及更高版本。你可以在stackoverflow.com/a/64621610/14195896看到它
-
@Gary02127 但是,Eliav Louski 已经构建并上传到 pypi,因此无需再手动安装,pip install 就可以了。谢谢埃利亚夫!
标签: python python-3.x macos tkinter