【问题标题】:cx_Freeze executable not displaying matplotlib figurescx_Freeze 可执行文件不显示 matplotlib 数字
【发布时间】:2019-04-03 04:06:15
【问题描述】:

我使用的是 Python 3.5,我能够使用 cx_Freeze 创建一个可执行文件,但是每当我尝试运行可执行文件时,它运行时没有错误,但它无法显示任何 matplotlib 图形。我已将 Tkinter 用于我的 GUI。我尝试将 matplotlib 后端作为 tkinter,但数字仍然没有显示。我无法共享整个代码,因为它很大。请帮忙。

【问题讨论】:

  • 我很难用 cx_freeze 构建一个包含 matplotlib 的应用程序。我最终不得不删除代码的绘图部分来构建我的应用程序。这只是一小部分,但由于某种原因,相同的 cx_freeze 不会编译 matplotlib 部分。我确实试图让它在几天内工作,但最后我认为 cx_freeze 只是不喜欢 matplotlib。
  • @Mike-SMT 请查看我的回答以获取反例。

标签: python user-interface matplotlib tkinter cx-freeze


【解决方案1】:

以下示例改编自matplotlib 示例Embedding In Tkcx_Freeze 示例Tkinter 适用于我的配置(python 3.6、matplotlib2.2.2、numpy1.14.3+mkl、 cx_Freeze 5.1.1 在 Windows 7 上)。

主脚本main.py:

import tkinter

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import math


root = tkinter.Tk()
root.wm_title("Embedding in Tk")

# Data for plotting
i = range(0, 300)
t = [_i / 100. for _i in i]
s = [2. * math.sin(2. * math.pi * _t) for _t in t]

fig = Figure(figsize=(5, 4), dpi=100)
fig.add_subplot(111).plot(t, s)

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)


canvas.mpl_connect("key_press_event", on_key_press)


def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)

tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is closed with the window manager.

设置脚本setup.py:

import sys
from cx_Freeze import setup, Executable
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

options = {
    'build_exe': {
        'include_files': [
            (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
            (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))
         ],
        'packages': ['numpy']
    },
}

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [
    Executable('main.py', base=base)
]

setup(name='matplotlib_embedding_in_Tkinter',
      version='0.1',
      description='Sample cx_Freeze matplotlib embedding in Tkinter script',
      options=options,
      executables=executables
      )

解释:

【讨论】:

  • 这个方法对我不起作用。我已经遵循了这些步骤,所以也许还有其他一些因素导致了我的编译问题。我希望它适用于 OP。
  • @Mike-SMT 您在这篇文章的代码中遇到了哪些错误,您的配置是什么?
  • @jpeg 我已经按照您的建议进行了更改,cx_freeze 能够无错误地创建可执行文件,但 exe 仍然无法显示 matplotlib 绘图。
猜你喜欢
  • 2019-01-27
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
相关资源
最近更新 更多