pyinstaller 包 GUI 应用程序

pyinstaller package GUI application

我正在尝试打包我的应用程序以作为独立应用程序发送给人们。

我尝试通过键入以下内容来使用 pyinstaller: pyinstaller --onefile TimeDomainAnalysis.py

这完成并给我一个可执行文件,但它无法打开并且returns附加的错误消息error message on double clicking the .exe

我的部分GUI代码(引用图片的部分)如下:

from Tkinter import *
from PIL import Image, ImageTk
import tkMessageBox
from tkFileDialog import askopenfilename
import numpy as np
import matplotlib 
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,         NavigationToolbar2TkAgg
from numpy import arange, sin, pi

# Each class is a frame in the root main window
class HomeFrame(object):
"""docstring for HomeFrame"""
def __init__(self, root):   # constructor places the widgets in the home     frame.  Each instance (self) is a new window placed in the main window
    super(HomeFrame, self).__init__()
    self.root=root      #oull the root window from the input constructor
    self.root.attributes("-topmost", False)
    self.root.title("Wave Analyzer App")
    self.Frame1=Frame(self.root)    #Create the home frame
    self.Frame1.pack()  #pack the frame.  It must be on a new line to be a     referencable object
    lab=Label(self.Frame1,text='Time Domain Wave Analyzer', font=("Helvetica", 30)).pack()  #label object within the frame that is not dynamic or to be passed to other instances

    self.But1=Button(self.Frame1,text='Get Started',command=self.B1Click)   #place a button in the frame
    self.But1.pack()

    path = "waves-circles-285359_960_720.jpg"

    #Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
    img = ImageTk.PhotoImage(Image.open(path))

    # load = Image.open("waves-circles-285359_960_720.jpg")
    # render = ImageTk.PhotoImage(load)
    IMLab=Label(self.Frame1,text='here i am',image=img)
    IMLab.image = img # You must keep a ref to the image else it gets destroyed!!
    IMLab.pack(side = "bottom", fill = "both", expand = "yes")
    lab2=Label(self.Frame1,text='By Ben Howey', font=("Helvetica", 12))     #label object within the frame that is not dynamic or to be passed to other instances
    lab2.pack(side=RIGHT)

已解决:

添加

    def resource_path(self,relative_path):
        if hasattr(sys, '_MEIPASS'):
            return os.path.join(sys._MEIPASS, relative_path)
        return os.path.join(os.path.abspath("."), relative_path)

到 return 一个绝对文件路径,当 运行 来自 exe 时,在 MEIPASS 目录中,或者当 运行 在 Python 中时,绝对文件路径。然后通过编辑 .spec 文件添加图像文件来将文件添加到包中。