我有一个GUI程序,当我从Anaconda提示符中运行时,它可以完美运行。以下是输出窗口的截图:
我使用pip安装了pyinstaller,并运行了下面这一行代码:
pyinstaller.exe --onefile [my file path]\mytest.py
使用我的实际文件路径代替[我的文件路径]
。这将创建一个名为'mytest.exe'的文件。
然而,当我双击它时,只会显示一个黑色窗口约5秒钟,然后出现一条短暂的消息:
Python脚本生成的窗口从未显示(与直接执行Python脚本不同)。
以下是代码:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
class LineBuilder:
def __init__(self, ax):
self.ax = ax
self.on = 1
self.lastline, = self.ax.plot([0],[0])
self.cid = ax.figure.canvas.mpl_connect('pick_event', self)
def __call__(self, event):
self.on *=-1
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
print(xdata[ind])
print('modified',xdata[ind][0])
self.lastline.remove()
self.lastline=self.ax.axvline(x=xdata[ind][0])
self.ax.figure.canvas.draw()
class View(QGraphicsView):
def __init__(self):
super(View, self).__init__()
self.initScene(5)
def initScene(self,h):
self.scene = QGraphicsScene()
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.figure.subplots_adjust(left=0.03,right=1,bottom=.1,top=1,wspace=0, hspace=0)
ax = self.figure.add_subplot(111)
ax.set_xlim([0,1000])
data = np.random.rand(1000)
ax.plot(data, '-')
self.canvas.draw()
self.setScene(self.scene)
self.scene.addWidget(self.canvas)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
self.setGeometry(150, 150, 700, 550)
self.view = View()
self.view.setGeometry(0,0,self.width()*2,500)
self.view.canvas.setGeometry(0,0,self.width()*2,500)
self.setCentralWidget(self.view)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
我应该更改什么,以便.exe文件实际打开窗口?这是否可能?最终目标是创建一个图形用户界面,而无需最终用户安装Anaconda或任何与Python相关的内容。