HeoN
HeoN

Reputation: 249

cx_freeze error building PyQT5+Matplotlib Python 3 application

I'm having problems compiling an executable for an application I made using: - Python 3.3 - PyQT5 - Matplotlib

I tried using Cx_Freeze with this setup.py:

import sys
from cx_Freeze import setup, Executable
includes = ['sys','PyQt5.QtCore','PyQt5.QtGui', 'PyQt5.QtWidgets','matplotlib']
excludes = []
packages = []
path = []
base = None
if sys.platform == 'win32':
    base = 'Win32GUI'
options = {
    'build_exe': {
        "includes": includes,
        "excludes": excludes,
        "packages": packages,
        "path": path
        #'excludes': ['Tkinter']  # Sometimes a little finetuning is needed
    }
}
executables = [Executable('pyqt5_matplotlib.py', base=base)]
setup(name='pyqt5_matplotlib',
      version='0.1',
      description='Sample PyQT5-matplotlib script',
      executables=executables,
      options=options
      )

When running setup.py build a folder containing various dlls and the exe is created, no error at this time. When running the exe thus created I get this error:

https://i.sstatic.net/D0nsq.jpg

Can anyone please help me?

For the purpose of this question I will include a sample main script that when built reproduces the error:

# @author: Sukhbinder Singh
#  
# Simple QTpy and MatplotLib example with Zoom/Pan
#  
# Built on the example provided at
# How to embed matplotib in pyqt - for Dummies
#  
# http://stackoverflow.com/questions/12459811/how-to-embed-matplotib-in-pyqt-for-dummies
#  
# """
import sys
from PyQt5.QtWidgets import (QApplication, QCheckBox, QColorDialog, QDialog,
        QErrorMessage, QFileDialog, QFontDialog, QFrame, QGridLayout,
        QInputDialog, QLabel, QLineEdit, QMessageBox, QPushButton, QWidget, QVBoxLayout )
from PyQt5.QtCore import pyqtSlot, QDir, Qt
from PyQt5 import QtGui
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
import numpy
import random

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)

        self.toolbar = NavigationToolbar(self.canvas, self)
        #self.toolbar.hide()

        # Just some button 
        self.button = QPushButton('Plot')
        self.button.clicked.connect(self.plot)

        self.button1 = QPushButton('Zoom')
        self.button1.clicked.connect(self.zoom)

        self.button2 = QPushButton('Pan')
        self.button2.clicked.connect(self.pan)

        self.button3 = QPushButton('Home')
        self.button3.clicked.connect(self.home)

        # set the layout
        layout = QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        layout.addWidget(self.button3)
        self.setLayout(layout)

    def home(self):
        self.toolbar.home()
    def zoom(self):
        self.toolbar.zoom()

    def pan(self):
        self.toolbar.pan()

    def plot(self):
        #''' plot some random stuff '''

        #data = [random.random() for i in range(25)]
        data_matrix = numpy.random.random((256,256))
        ax = self.figure.add_subplot(111)
        ax.hold(False)

        #ax.plot(data, '*-')
        ax.imshow(data_matrix)
        self.canvas.draw()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Window()
    main.setWindowTitle('Simple QTpy and MatplotLib example with Zoom/Pan')
    main.show()
    sys.exit(app.exec_())

Upvotes: 1

Views: 2191

Answers (1)

HeoN
HeoN

Reputation: 249

After a more in depth research I did the following:

backend: tkAgg

becomes

backend: Agg

last source was ImportError: No module named backend_tkagg

This solution works on Win 7 64bit with Python3.3 for a single window PyQT5 with Matplotlib backends.

Upvotes: 3

Related Questions