python - Pyinstaller 不能很好地与 ImageTk 和 Tkinter 配合使用

标签 python tkinter python-imaging-library pyinstaller

我正在尝试使用 pyinstaller 来构建我一直在开发的程序,但之后我遇到了二进制文件的问题。这是我的程序。

首先我运行 pyinstall test.py 然后我使用 ./dist/main/test 运行二进制文件但是我得到了这个错误,我在运行时没有得到脚本正常(例如python3 test.py)。

Traceback (most recent call last):
  File "PIL/ImageTk.py", line 181, in paste
_tkinter.TclError: invalid command name "PyImagingPhoto"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "magic-collection-tracker/test.py", line 22, in <module>
  File "magic-collection-tracker/test.py", line 11, in main
  File "PIL/ImageTk.py", line 120, in __init__
  File "PIL/ImageTk.py", line 185, in paste
ModuleNotFoundError: No module named 'PIL._tkinter_finder'

这是重现该问题的最小示例。

from PIL import ImageTk
import PIL.Image
from tkinter import *



window = Tk()
pil_img = PIL.Image.open('./scr_images/blank_card.png')
tkimage = ImageTk.PhotoImage(pil_img)
canvas = Canvas(window)
canvas.create_image(0,0,image=tkimage, anchor=NW)
canvas.pack()

window.mainloop()

我是不是用错了 pyinstaller 还是有其他问题?

最佳答案

我现在晚了 2 个月,但为了添加一个更简单的解决方案,我设法让它工作,添加了几个隐藏的导入:

hiddenimports=['PIL', 'PIL._imagingtk', 'PIL._tkinter_finder']

关于python - Pyinstaller 不能很好地与 ImageTk 和 Tkinter 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675162/

相关文章:

Python Tk(Tkinter)在 ubuntu unity 上不透明

python - 结合 Tkinter (Python) 和 Swing (Jython)

python - Tkinter在按下按钮时不会改变图像

python - 如何加快枕头(python)中的图像加载?

python - 在 PIL 中使用多处理

python-3.x - pytesseract image_to_string 不拉字符串,但没有错误

python - matplotlib 返回的最大 PSD 频率箱是否错误?

python - word2vec tensorflow 中的执行流程

python - 如何在 Odoo 9.0 中使用自定义模型将自定义字段添加到数据透视 View

python - 涉及 Python 内置数字类型和 Numpy 类型 float64 和 complex128 的二元算术运算结果的混淆类型