我正在尝试使用 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/