Python应用程序在运行时找不到图像(.png)文件

Python应用程序在运行时找不到图像(.png)文件,python,image,runtime,exe,pyinstaller,Python,Image,Runtime,Exe,Pyinstaller,所以我用tkinter GUI构建了这个python应用程序。我使用pyinstaller将.py文件转换为可执行(.exe)文件,以便将其发送给非技术人员使用 当我用注释掉的这两行代码运行代码时 root = tk.Tk() root.title("DOJ Pricing Analyzer") root.resizable(False,False) #canvas1 = tk.Canvas(root, width = 350, height = 500, bg = 'white') <-

所以我用tkinter GUI构建了这个python应用程序。我使用pyinstaller将.py文件转换为可执行(.exe)文件,以便将其发送给非技术人员使用

当我用注释掉的这两行代码运行代码时

root = tk.Tk()
root.title("DOJ Pricing Analyzer")
root.resizable(False,False)
#canvas1 = tk.Canvas(root, width = 350, height = 500, bg = 'white') <-----------This line commented out
#image = ImageTk.PhotoImage( file = "matrix.png" )<--------------This line commented out
canvas1.create_image(0, 0, image = image, anchor = NW)
canvas1.create_text(185,75,fill="white",font="Impact 15 ",

但在运行了它生成的.exe之后,我遇到了以下错误:


Traceback (most recent call last):
  File "DOJPriceAnalyzer.py", line 16, in <module>
  File "site-packages\PIL\ImageTk.py", line 89, in __init__
  File "site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
  File "site-packages\PIL\Image.py", line 2809, in open
FileNotFoundError: [Errno 2] No such file or directory: 'matrix.png'
[3080] Failed to execute script DOJPriceAnalyzer
Exception ignored in: <function PhotoImage.__del__ at 0x0F9F07C8>
Traceback (most recent call last):
  File "site-packages\PIL\ImageTk.py", line 118, in __del__
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'


但这些都会产生相同的错误。有什么建议吗

我认为问题在于,您没有保留对图像的引用。 看一看

要添加永久引用,只需添加一行,如

canvas1.im = image
政府确实提到了这一点

“图像对象。这应该是PhotoImage或BitmapImage,或者是兼容的对象(如PIL PhotoImage)。应用程序必须保留对图像对象的引用,”,但很容易忽略这一点

我也不认为这是一个具体的问题。将图像添加到spec文件中的
数据
的方法是正确的,应该可以正常工作

datas = [('matrix.png','.')]
我试图用您提供的代码片段重现您的案例,我能够复制错误,然后用此方法修复它,希望这也能帮助您。

可能相关:
datas=[('C:\\Users\\CV7617\\Desktop\\program','data')],
canvas1.im = image
datas = [('matrix.png','.')]