PyQt5 GUI - 用 PyInstaller 制作的 exe 无法打开 [英] PyQt5 GUI - exe made with PyInstaller doesn't open

查看:105
本文介绍了PyQt5 GUI - 用 PyInstaller 制作的 exe 无法打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a GUI which runs perfectly fine when I execute it from the Anaconda Prompt. I get the following window as output:

我已经使用 pip 安装了 pyinstaller,然后运行了该行

I have installed pyinstaller using pip, and have then run the line

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

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

with my actual file path instead of [my file path]. This creates a file called 'mytest.exe'.

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

However, when I double-click on it, all that happens is that a black window is shown for about 5 seconds, then I get this message for a split second:

Python 脚本生成的窗口永远不会显示(与我直接执行 Python 脚本时不同).

The window that the Python script makes is never shown (unlike when I directly execute the Python script).

代码如下:

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.

What should I change so that the .exe file actually opens the window? Is this even possible? The end goal is to create a GUI that runs without the end user needing to install Anaconda or anything related to Python.

推荐答案

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

To view the error messages associated with running your executable, run the .exe file from the command prompt: /path/to/app/dist/MyApp.exe. This will allow you to more easily observe any errors that may exist after the app was bundled (rather than trying to grab a screenshot).

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

Your application is failing to launch because it cannot import the PyQt5 modules. You may add PyQt5 (or each of the PyQt5 modules that you are using) to the hiddenimports list in the .spec file that was generated after you first bundled this application with PyInstaller and regenerating the executable. Alternatively, you could explicitly add PyQt5 to your .py file by adding import PyQt5 before from PyQt5.QtWidgets import *

这篇关于PyQt5 GUI - 用 PyInstaller 制作的 exe 无法打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆