python - PyQt5 GUI - 使用 PyInstaller 制作的 exe 无法打开

标签 python windows user-interface pyqt pyinstaller

我有一个 GUI,当我从 Anaconda Prompt 执行它时,它运行得非常好。我得到以下窗口作为输出:

enter image description here

我已经使用 pip 安装了 pyinstaller,然后运行了这条线

pyinstaller.exe --onefile [my file path]\mytest.py

使用我的实际文件路径而不是 [我的文件路径]。这将创建一个名为“mytest.exe”的文件。

但是,当我双击它时,所发生的只是一个黑色窗口显示大约 5 秒钟,然后我在一瞬间收到此消息:

enter image description here

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 相关的东西即可运行的 GUI。

最佳答案

要查看与运行可执行文件相关的错误消息,请从命令提示符运行 .exe 文件:/path/to/app/dist/MyApp.exe。这将使您能够更轻松地观察应用程序被捆绑后可能存在的任何错误(而不是试图抓取屏幕截图)。

您的应用程序无法启动,因为它无法导入 PyQt5 模块。您可以将 PyQt5(或您正在使用的每个 PyQt5 模块)添加到 .spec 文件中的 hiddenimports 列表中,该文件是在您首次将此应用程序与 PyInstaller 捆绑并重新生成可执行文件后生成的。或者,您可以通过在 from PyQt5.QtWidgets import *

之前添加 import PyQt5 将 PyQt5 显式添加到您的 .py 文件中

关于python - PyQt5 GUI - 使用 PyInstaller 制作的 exe 无法打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48596114/

相关文章:

windows - 使用 nvprof 和 Visual Profiler 进行 GPU 功耗分析

windows - 如何为 Windows 创建自定义用户界面?

c++ - 窗口不活动警报

java - 动态调整 JPanels 的大小以适应窗口的宽度

python - 如何替换 pandas 数据框中列中的日期?

python - Django RSS-feed 转义 €

c++ - 使用 Gtkmm 在 GtkTextView 中加载完整文件

Java swing - ActionListener 应该去哪里?

python - 使用 SymPy 计算任意向量的散度

python - 如何从列表中收集范围?