【问题标题】:cx_freeze error building PyQT5+Matplotlib Python 3 applicationcx_freeze 错误构建 PyQT5+Matplotlib Python 3 应用程序
【发布时间】:2014-06-22 09:12:05
【问题描述】:

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

我尝试在 setup.py 中使用 Cx_Freeze:

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

【问题讨论】:

  • 您也可以尝试将 matplotlib 放入“packages”而不是“includes”——这将使其包含所有子模块。

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


【解决方案1】:

经过更深入的研究,我做了以下工作:

backend: tkAgg

变成

backend: Agg

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

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

【讨论】:

  • 我在搜索这个问题时偶然发现了你的问题,只使用第 3 步解决了这个问题:修复 matplotlibrc 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多