【问题标题】:*.py script with tkinterdnd2 module doesn`t compile to an executable file带有 tkinterdnd2 模块的 *.py 脚本不会编译为可执行文件
【发布时间】:2021-10-12 13:46:48
【问题描述】:

我制作了一个使用 tkinter 和 tkinterdnd moudles 的应用程序。当我将它作为脚本启动时它工作得很好,但是当我尝试从中制作可执行文件并启动它时,会出现以下错误:

Traceback (most recent call last):
  File "TkinterDnD2\TkinterDnD.py", line 53, in _require
_tkinter.TclError: can't find package tkdnd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "IxcomGUI.py", line 128, in <module>
  File "IxcomGUI.py", line 11, in __init__
  File "TkinterDnD2\TkinterDnD.py", line 285, in __init__
  File "TkinterDnD2\TkinterDnD.py", line 55, in _require
RuntimeError: Unable to load tkdnd library.
[14512] Failed to execute script 'IxcomGUI' due to unhandled exception!

我尝试了以下操作:

  1. 使用pip install 安装tkinterdnd2 并使用pyinstaller myscript.py 构建。
  2. 手动安装 tkinterdnd2 模块,如本视频所示 https://www.youtube.com/watch?v=JIy0QjwQBl0&t=605s&ab_channel=RamonWilliams 并使用 pyinstaller myscript.py 构建
  3. 重复上一步,但添加了这个东西https://github.com/pmgagne/tkinterdnd2/blob/master/hook-tkinterdnd2.py
  4. 试图通过使用 --paths 标志指定模块路径来隐式告诉 pyinstaller 路径到 tkdnd 模块。

所有这些尝试都导致了以下错误。有谁知道某种解决方案?

【问题讨论】:

  • 您是否在 pyinstaller 命令行中包含了 --additional-hooks-dir=. 部分,就像步骤 3 中所说的那样?
  • 是的,我做了,但我也偶然发现了一个解决方案,我已经在这里发布了。

标签: python tkinter pyinstaller


【解决方案1】:

这里的问题是 Drag n Drop 需要两个组件:TCL 库和它们的 Tkinter 接口。我手动安装两者来配置我的环境。 (请参阅我对How to Install and Use TkDnD with Python Tkinter on OSX? 的回答)。我知道有人在 PyPI 上为 TkinterDnD2 打包了一些东西,但我还没有调查过。

我有一个使用 TkinterDnD2 的项目。我使用 PyInstaller 构建它并看到您看到的相同错误(或多或少)。使用 --onedir 选项(而不是 --onefile 选项)运行 PyInstaller,我看到我的 dist 目录中缺少 tkdnd2.8

为了解决这个问题,我在 Windows 上添加了

--add-binary "C:/Python/Python38-32/tcl/tkdnd2.8;tkdnd2.8"

到 PyInstaller 命令行,就成功了。我现在可以构建一个 --onefile 可执行文件,它可以正常运行。

【讨论】:

    【解决方案2】:

    显然,解决这个问题出乎意料地容易。您只需进入&lt;YourPath&gt;\Python39\tcl 目录并找到tkdnd2.8 目录,然后将其移至tcl8.6 目录。我也把它重命名为tkdnd,但是我不知道是否有必要。

    这是拯救我的原始链接:http://pyinstaller.47505.x6.nabble.com/tkinter-TclError-can-t-find-package-tkdnd-td2330.html

    【讨论】:

    • 我也遇到了问题,但我可以通过 pyinstaller 成功构建并制作一个可执行文件,但是当我尝试启动时,出现以下错误:RuntimeError: Unable to load tkdnd library。我刚刚检查了我的 tcl 目录没有 tkdnd 文件夹,请在这个问题上给我建议。谢谢
    • 呃,我已经有一段时间没有从事这个项目了,但也许你的库安装在其他目录中?
    • 以上链接失效了。在这里我找到了存档:mail-archive.com/pyinstaller@googlegroups.com/msg07800.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多