Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt5 GUI - exe made with PyInstaller doesn't open

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

enter image description here

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

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

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

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:

enter image description here

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

Here's the code:

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

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.

like image 842
ignoring_gravity Avatar asked Mar 07 '23 00:03

ignoring_gravity


1 Answers

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).

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 *

like image 74
apogalacticon Avatar answered Mar 17 '23 01:03

apogalacticon