我已经处理这个问题好几天了,希望能找到一些帮助。我开发了一个带有导入模块 tkinter、numpy、scipy、matplotlib 的 GUI 应用程序,它在 python 本身中运行良好。转换为 exe 后,一切都按预期工作,但不是 matplotlib 部分。当我按下我定义的绘图按钮时,exe 只是关闭并且不显示任何绘图。所以我想做一个最小的例子,我只是简单地绘制了一个 sin 函数并且我面临着同样的问题:在 python 中完美运行,当将它转换为 exe 时,它在按下 plot 按钮时崩溃。这是最小的例子:
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
class MainWindow(tk.Frame):
def __init__(self):
tk.Frame.__init__(self,bg='#9C9C9C',relief="flat", bd=10)
self.place(width=x,height=y)
self.create_widgets()
def function(self):
datax = np.arange(-50,50,0.1)
datay = np.sin(datax)
plt.plot(datax,datay)
plt.show()
def create_widgets(self):
plot = tk.Button(self, text='PLOT', command=self.function)
plot.pack()
x,y=120,300
root=tk.Tk()
root.geometry(str(x)+"x"+str(y))
app = MainWindow()
app.mainloop()
这是我对应的 setup.py
用于使用 cx_Freeze 进行转换:
import cx_Freeze
import matplotlib
import sys
import numpy
import tkinter
base = None
if sys.platform == "win32":
base = "Win32GUI"
executables = [cx_Freeze.Executable("test.py", base=base)]
build_exe_options = {"includes": ["matplotlib.backends.backend_tkagg","matplotlib.pyplot",
"tkinter.filedialog","numpy"],
"include_files":[(matplotlib.get_data_path(), "mpl-data")],
"excludes":[],
}
cx_Freeze.setup(
name = "test it",
options = {"build_exe": build_exe_options},
version = "1.0",
description = "I test it",
executables = executables)
任何可能解决问题的想法都将受到高度赞赏。我在 64 位 Windows10 机器上工作,我正在使用 WinPython Distribution 和 Python 3.4.3。
原文由 benellinger 发布,翻译遵循 CC BY-SA 4.0 许可协议
在使用相同的 test.py 测试 PyInstaller 时,我找到了针对此问题的潜在解决方案(或至少是解释)。我收到有关缺少 dll 文件的错误消息,该文件是 mkl_intel_thread.dll 。
我搜索了那个文件,它是在 numpy 文件夹中找到的。我将匹配 mkl_*.dll 和 libiomp5md.dll 的文件复制到由
python setup.py build
创建的 test.exe 所在的同一目录。在此之后,最小的 test.exe 在按下 绘图 按钮时显示了 matplotlib 窗口。这些文件位于文件夹 lib\site-packages\numpy\core 中。