我目前正在开发一个程序,该程序接受特定文件作为输入,处理数据并通过Matplotlib(数据解析文件中唯一导入的模块)提供两个图形。
为此,我制作了一个小GUI,供用户选择要制成图形的文件。 (带有tkinter和PIL导入)。
我必须用此制作一个应用程序,并且正在使用PyInstaller。不幸的是,我无法使最终文件正常工作(或无法正常运行)。
我已经对代码本身以及PyInstaller的.spec文件进行了一些修改。
我添加了修复PyInstaller路径的功能。 我已经修改了.spec文件以将路径添加到图像,而不是显示控制台。 我尝试了以下两种设置:UPX,一个文件。 我检查了日志文件中是否缺少模块,因为一切似乎都没问题。
一些代码:
在GUI文件上导入:
import sys
import os
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
from PIL import ImageTk, Image
import rectifierDataParser
导入数据分析文件:
import sys
import os
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
GUI文件:
class Application(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.fileButton = tk.Button(self, text(...))
self.quitButton = tk.Button(self, text(...))
self.fileButton.pack(side = 'top')
self.quitButton.pack(side = 'bottom')
def load_file(self):
filename = askopenfilename(title = "Select file", filetypes = [("Files", ("*.001", (...)))]
rectifierDataParser.main(filename)
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
app = Application()
app.master.title('Graphing Data')
app.master.geometry('300x200')
img = ImageTk.PhotoImage(Image.open("logo.jpg"))
panel = tk.Label(app, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
app.mainloop()
由于我目前无法在屏幕上看到GUI,因此代码本身或与PyInstaller的兼容性是否存在任何问题(假定PyInstaller具有Tkinter的全部功能)。
我希望你们中的一个能帮助我取得突破,因为我在这方面停留了很长时间!提前非常感谢!
答案 0 :(得分:2)
python discussed here随附的tcl版本存在一些问题。我编写了一个脚本,该脚本会自动将init.tcl文件更改为正确的版本。
您不应该使用--onefile
标志,因为文件目录不存在,脚本将无法工作。
cd /path/of/your/app
git clone https://github.com/jacob-brown/TCLChanger.git
pyinstaller --windowed app.py
python TCLChanger/TCLChanger.py
您现在应该可以在终端上通过双击打开应用程序。
答案 1 :(得分:0)
我遇到了完全相同的问题,并通过编辑.spec文件修复了该问题。
我必须设置
console=True
您将在.spec文件的底部找到该选项。
类似这样的东西:
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='YOU/EXECUTABLE/NAME',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True)
如果找不到您的.spec文件,则可以通过运行来生成一个文件。
pyi-makespec yourprogram.py