cx_Freeze 转换后的 GUI 应用程序 (tkinter) 在按下绘图按钮后崩溃

cx_Freeze converted GUI-app (tkinter) crashes after pressing plot button

我已经处理这个问题好几天了,希望能得到一些帮助。我用导入的模块 tkinter、numpy、scipy、matplotlib 开发了一个 GUI 应用程序,它在 python 本身运行良好。转换为 exe 后,一切都按预期工作,但不是 matplotlib 部分。当我按下我定义的绘图按钮时,exe 只是关闭并且不显示任何绘图。 所以我想做一个最小的例子,我简单地绘制了一个 sin 函数并且我面临着同样的问题: 在 python 中完美运行,当将其转换为 exe 时,按下绘图按钮时它会崩溃。这是最小的例子:

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 机器上工作,我正在使用带有 Python 3.4.3.

的 WinPython 发行版

我在使用相同的 test.py 测试 PyInstaller 时找到了此问题的潜在解决方案(或至少是解释)。我收到有关缺少 dll 文件的错误消息,该文件为 mkl_intel_thread.dll

我搜索了那个文件,在 numpy 文件夹中找到了它。 我将匹配 mkl_*.dlllibiomp5md.dll 的文件复制到 test.exepython setup.py build 创建。在此之后,最小 test.exe 在按下 plot 按钮时显示了 matplotlib window。

文件位于文件夹 lib\site-packages\numpy\core.

我真的很想 post 这作为评论,但我没有声望。这主要是 J.J 的跟进。哈卡拉关于如何查找原因的回答

如果将基数更改为 "Console",即使用

base = "Console"

而不是

base = "Win32GUI"

程序启动时也会弹出一个控制台,打印出这个错误

Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll.

这有助于更快地找到问题的原因。

我认为这值得一提,因为这个技巧也有助于诊断其他问题。在最终版本中,可以恢复到 Win32GUI 以避免额外的控制台。我应该把功劳归功于另一个

我遵循了@J.J. Hakala的回答,但我发现没有必要复制所有的mkl_*.dll和libiomp5md.dll文件。对我来说,它适用于 libiomp5md.dll mkl_core.dll mkl_def.dll mkl_intel_thread.dll。这有助于将最终的包大小减少 ~500MB。

此外,您可以在 include_files 选项中包含要复制的文件。如果 sys.platformwin32.

,您也可能只想包含它们

我正在使用 Anaconda 以及 @Matt Williams,因此,稍微更改一下 OP 的代码:

import cx_Freeze
import matplotlib
import sys
import numpy
import tkinter
import os

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))

build_exe_options = {"includes":   ["matplotlib.backends.backend_tkagg","matplotlib.pyplot",
                             "tkinter.filedialog","numpy"],
                     "include_files":[(matplotlib.get_data_path(), "mpl-data")],
                     "excludes":[],
                    }

base = None

if sys.platform == "win32":
    base = "Win32GUI"
    DLLS_FOLDER = os.path.join(PYTHON_INSTALL_DIR, 'Library', 'bin')

    dependencies = ['libiomp5md.dll', 'mkl_core.dll', 'mkl_def.dll', 'mkl_intel_thread.dll']

    for dependency in dependencies:
        build_exe_options['include_files'].append(os.path.join(DLLS_FOLDER, dependency))

executables = [cx_Freeze.Executable("test.py", base=base)]

cx_Freeze.setup(
    name = "test it",
    options = {"build_exe": build_exe_options},
    version = "1.0",
    description = "I test it",
    executables = executables)

检查您是否安装了 numpy+mkl 软件包。卸载 numpy 并安装 numpy+mkl 包解决了我收到与 mkl_intel_thread.dll

相关的错误的问题