无法使用PyInstaller /创建有效的PyQt5 Python可执行文件

时间:2019-11-02 19:12:44

标签: python-3.x pyqt5 pyinstaller

我试图从我使用PyQt5编写的Python代码创建可执行文件,但无法使其正常工作。我一直在关注:https://blog.aaronhktan.com/posts/2018/05/14/pyqt5-pyinstaller-executable 添加图像并将所有PyQt5内容添加到spec文件的隐藏导入中的示例,但似乎无济于事。

在我的桌面上创建此代码后,我在其中开发了代码,一切正常且正常。该图像显示出来,并直接从dist文件夹运行。但是,当我通过电子邮件将exe发送到笔记本电脑并将其打开时,我得到“无法执行脚本abra_teleport”。我只是不确定它不喜欢什么。

编辑:我将代码更改为该代码,而不是从PyQt5.QtWidgets导入*从PyQt5.QtWidgets导入QLabel,QPushButton,QApplication,QMainWindow更改了代码,但这没有任何改变。

代码:

import sys
import os
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Abra(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title='I choose you! Abra!'
        self.left=100
        self.top=100
        self.width=320
        self.height=200
        self.window()

    def window(self):
        def resource_path(relative_path):
            if hasattr(sys, '_MEIPASS'):
                return os.path.join(sys._MEIPASS, relative_path)
            return os.path.join(os.path.abspath('.'), relative_path)
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.label=QLabel('',self)
        self.label.move(100,20)
        self.abra=QLabel(self)
        self.abra.setPixmap(QPixmap(resource_path('resources/sprites/abra.png')))
        self.abra.move(130, 90)
        self.button=QPushButton('Go Abra!',self)
        self.button.move(110,150)
        self.button.clicked.connect(self.on_click)
        self.show()


    def part1(self):
        def resource_path(relative_path):
            if hasattr(sys, '_MEIPASS'):
                return os.path.join(sys._MEIPASS, relative_path)
            return os.path.join(os.path.abspath('.'), relative_path)
        try:
            self.label.setText('')
            self.abra.setPixmap(QPixmap(resource_path('resources/sprites/abra.png')))
            self.button.setText('Go Abra!')
            self.button.clicked.connect(self.on_click)
        except:
            print('mmm')

    def on_click(self):
        try:
            self.label.setText('Abra used Teleport!')
            self.label.adjustSize()
            self.abra.setPixmap(QPixmap(''))
            self.button.setText('Try again!')
            self.button.clicked.connect(self.part1)
        except:
            print('nope')

if __name__=='__main__':
    app=QApplication(sys.argv)
    ex=Abra()
    ex.show()
    sys.exit(app.exec_())

规范文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['abra_teleport.py'],
             pathex=['C:\\Users\\marina\\Documents\\PokemonGUIProject\\Tests'],
             binaries=[],
             datas=[],
             hiddenimports=['PyQt5','PyQt5.QtWidgets','PyQt5.QtGui','PyQt5.QtCore'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

a.datas += [('resources/sprites/abra.png','resources/sprites/abra.png','DATA')]

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='abra_teleport',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False )

0 个答案:

没有答案