0

I am using Python 3.5 and I was able to create an executable using cx_Freeze but whenever I try to run the executable it runs without error but it cannot display any matplotlib figure. I have used Tkinter for my GUI. I have tried putting matplotlib backend as tkinter but figures are still not displaying.I cannot share the whole code as it is huge. Kindly Help.

jpeg
  • 2,372
  • 4
  • 18
  • 31
Shubham_geo
  • 368
  • 1
  • 4
  • 16
  • I had a really hard time building an application that contained matplotlib in it with cx_freeze. I ended up having to just remove the plot portion of my code to build my application. It was only a minor part but just the same cx_freeze would not compile the matplotlib portion for some reason. I did try to get it to work over a few days but in the end I think cx_freeze just doesn't like matplotlib. – Mike - SMT Oct 30 '18 at 13:45
  • @Mike-SMT please see my answer for a counter-example. – jpeg Oct 31 '18 at 12:45

1 Answers1

0

The following example adapted from the matplotlib example Embedding In Tk and from the cx_Freeze sample Tkinter works for my configuration (python 3.6, matplotlib 2.2.2, numpy 1.14.3+mkl, cx_Freeze 5.1.1 on Windows 7).

Main script 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 script 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
      )

Explanations:

jpeg
  • 2,372
  • 4
  • 18
  • 31
  • This method did not work for me. I had already followed these steps so maybe there is some other factor that is causing my compile issue. I hope it works for the OP though. – Mike - SMT Oct 31 '18 at 13:12
  • @Mike-SMT Which error do you get with the code of this post and what is your configuration? – jpeg Oct 31 '18 at 13:21
  • @jpeg I have already made the changes that you suggested beforehand, cx_freeze is able to create an executable without error but the exe still can not display the matplotlib plots. – Shubham_geo Nov 01 '18 at 07:25