我在编译我使用的应用程序的可执行文件时遇到了问题:- Python 3.3——PyQT5——Matplotlib
我尝试用这个设置使用Cx_Freeze .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时,我得到这个错误:
https://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_())
经过更深入的研究,我做了以下几点:
- 安装PyWin32
- 已安装cx_Freeze测试版(可能没有必要)https://bitbucket.org/anthony_tuininga/cx_freeze/downloads
- 编辑python33/Lib/site-packages/matplotlib/mpl-data/matplotlib,使第32行:
backend: tkAgg
是
backend: Agg
最后一个源是importterror: No module named backend_tkagg
此解决方案适用于win7 64位Python3.3单窗口PyQT5与Matplotlib后端。