Pyinstaller 与 ImageTk 和 Tkinter 不能很好地配合

Tox*_*DOS 3 python tkinter pyinstaller python-imaging-library

我正在尝试使用 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'
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

我是否错误地使用了 pyinstaller 或者还有其他问题?

fre*_*yer 8

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

hiddenimports=['PIL', 'PIL._imagingtk', 'PIL._tkinter_finder']
Run Code Online (Sandbox Code Playgroud)