PyInstaller可执行文件不显示GUI [英] PyInstaller executable doesn't show GUI
问题描述
我目前正在开发一个程序,该程序接受特定文件作为输入,处理数据并通过Matplotlib(数据解析文件中唯一导入的模块)提供两个图形.
I'm currently working on a program that accepts a specific file as input, works on the data and provides two graphs through Matplotlib (the only module imported in the data parsing file).
为此,我制作了一个小的GUI,供用户选择要制成图形的文件. (带有tkinter和PIL导入).
For this I have made a small GUI for the user to choose the file to have a graph made of. (w/ tkinter and PIL imported).
我必须使用此应用程序制作一个应用程序,并且正在使用PyInstaller.不幸的是,我无法使最终文件正常工作(或无法正常运行).
I have to make an app out of this and I'm using PyInstaller to it. Unfortunately I haven't been able to make the final file work (or run properly).
我已经对代码本身以及PyInstaller的.spec文件进行了一些修改.
I have already made several modifications either to the code itself, but also to the PyInstaller's .spec file.
我添加了修复PyInstaller路径的功能. 我已经修改了.spec文件,以将路径添加到图像,而不是显示控制台. 我尝试过以下两种设置:UPX,一个文件. 我检查了日志文件中是否缺少模块,因为一切似乎都没问题.
I have added the function to fix the path for PyInstaller. I have modified the .spec file to add the path to an image, not to show the console. I have tried with these settings both on/off: UPX, One File. I have checked the log files for missing modules as everything seems to be ok.
一些代码:
在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的全部功能).
As I'm currently not able to see the GUI on the screen, is there any problem with the code itself or any compatibility with PyInstaller (supposedly PyInstaller is fully functional w/Tkinter).
我希望你们中的一个能帮助我取得突破,因为我在这方面停留了很长时间!提前非常感谢!
I hope one of you might help me have a breakthrough as I have been stuck on this for way to much time! Many thanks in advance!
推荐答案
There is a few issues with the tcl version that comes with python, discussed here. I've written an script that automatically changes the init.tcl file to the correct version.
您不应该使用--onefile
标志,因为文件目录不存在,并且脚本无法正常工作.
N.B. you shouldn't use the --onefile
flag as the file directories aren't present, and the script won't work.
-
cd /path/of/your/app
-
git clone https://github.com/jacob-brown/TCLChanger.git
-
pyinstaller --windowed app.py
-
python TCLChanger/TCLChanger.py
cd /path/of/your/app
git clone https://github.com/jacob-brown/TCLChanger.git
pyinstaller --windowed app.py
python TCLChanger/TCLChanger.py
您现在应该可以从终端并双击打开您的应用程序.
You should now be able to open your app, from the terminal and via double clicking.
这篇关于PyInstaller可执行文件不显示GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!