PyQt5 GUI - exe made with PyInstaller doesn't open

时间:2018-02-03 10:03:07

标签: python windows user-interface pyqt pyinstaller

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.

1 个答案:

答案 0 :(得分:2)

要查看与运行可执行文件关联的错误消息,请从命令提示符处运行.exe文件:let jsonTickets = """ {"tickets":[{"name":"d5b5d618-8a74-4e5f","status":"VALID","department":"IT"},{"name":"a58f54b5-9420-49b6","status":"INVALID","department":"Travel"}]} """ public struct Ticket: Codable { public let name: String public let status: String public let department: String } do { let data = Data(jsonTickets.utf8) let tickets = try JSONDecoder().decode([String:[Ticket]].self, from: data) print(tickets) let jsonTicketsEncodeBack = try JSONEncoder().encode(tickets) jsonTickets == String(data: jsonTicketsEncodeBack, encoding: .utf8) // true } catch { print(error) } 。这样您就可以更轻松地观察捆绑应用后可能存在的任何错误(而不是尝试抓取屏幕截图)。

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

之前添加import PyQt5来明确地将PyQt5添加到.py文件中