cx_freeze错误构建PyQT5 + Matplotlib Python 3应用程序

时间:2014-05-05 22:41:58

标签: user-interface deployment matplotlib cx-freeze pyqt5

我在编译我使用的应用程序的可执行文件时遇到问题: - Python 3.3 - PyQT5 - Matplotlib

我尝试使用Cx_Freeze和这个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
      )

运行setup.py时,构建一个包含各种dll并创建了exe的文件夹,此时没有错误。 当运行由此创建的exe时,我收到此错误:

http://i.stack.imgur.com/D0nsq.jpg

有人可以帮助我吗?

出于这个问题的目的,我将包括一个示例主脚本,在构建时重现错误:

# @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_())

1 个答案:

答案 0 :(得分:3)

经过更深入的研究后,我做了以下几点:

backend: tkAgg

变为

backend: Agg

最后一个来源是ImportError: No module named backend_tkagg

此解决方案适用于带有Python3.3的Win 7 64位,适用于带有Matplotlib后端的单个窗口PyQT5。