【问题标题】:pyinstaller package GUI applicationpyinstaller 包 GUI 应用程序
【发布时间】:2017-12-11 17:31:08
【问题描述】:

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

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

这完成并给了我一个可执行文件,但它不会打开并返回附加的错误消息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)

【问题讨论】:

  • 解释时是否产生任何错误?图片和可执行文件在同一个目录吗?
  • 检查您是否有任何隐藏的导入。在您的 Python 脚本文件中,您可以搜索 import。示例:模块 = __import__(name)。如果是这种情况,请尝试以下链接:pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html 并查看列出隐藏的导入。
  • 另外,如果您使用 --onefile 标志,则可以在临时文件夹中创建该图像。你是否修改了你的 pyinstaller .spec 文件?
  • 当我注释掉对图像的所有引用时,我已经成功地运行了更多的痛苦。仅当我尝试包含此内容时,它似乎才失败。我通过使用 python 脚本和同一目录中的图像运行 pyinstaller --onefile TimeDomainApp.py 来做到这一点。

标签: tkinter exe pyinstaller


【解决方案1】:

已解决:

添加

    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)

从 exe 运行时返回 MEIPASS 目录中的绝对文件路径,或者在 Python 中运行时返回绝对文件路径。然后通过编辑 .spec 文件将文件添加到包中以添加图像文件。

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2014-08-11
    • 2015-09-24
    • 1970-01-01
    • 2012-12-20
    • 2021-05-06
    相关资源
    最近更新 更多