【问题标题】:Program packaged by pyinstaller crashes when runningpyinstaller打包的程序运行时崩溃
【发布时间】:2021-08-04 08:38:27
【问题描述】:

我尝试用pyqt5创建一个命令行程序,并使用pyinstaller对其进行打包。我的打包命令是:pyinstaller --windowed --onefile main.py

但是,输入命令运行后,我的程序崩溃了

如果我使用pyinstaller --windowed main.pypyinstaller --onefile main.py,程序仍然有效

代码如下:

ma​​in.py

import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QPlainTextEdit, QHBoxLayout, QVBoxLayout

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.window_width, self.window_height = 1200, 1500
        self.setMinimumSize(self.window_width, self.window_height)

        self.setWindowTitle('Command Line App')
        layout = QVBoxLayout()
        self.setLayout(layout)

        self.editorCommand = QPlainTextEdit()
        layout.addWidget(self.editorCommand, 3)

        self.editorOutput = QPlainTextEdit()
        layout.addWidget(self.editorOutput, 7)

        buttonLayout = QHBoxLayout()
        layout.addLayout(buttonLayout)

        self.button_run = QPushButton('&Run Command', clicked=self.runCommand)
        buttonLayout.addWidget(self.button_run)

        self.button_clear = QPushButton('&Clear', clicked=lambda: self.editorOutput.clear())
        buttonLayout.addWidget(self.button_clear)

        self.editorCommand.insertPlainText('dir')

    def runCommand(self):
        command_line = self.editorCommand.toPlainText().strip()
        p = os.popen(command_line)
        if p:
            self.editorOutput.clear()
            output = p.read()
            self.editorOutput.insertPlainText(output)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    app.setStyleSheet('''
        QWidget {
            font-size: 30px;
        }
    ''')
    
    myApp = MyApp()
    myApp.show()

    try:
        sys.exit(app.exec_())
    except SystemExit:
        print('Closing Window...')

【问题讨论】:

    标签: pyqt5 pyinstaller


    【解决方案1】:

    尝试以这种方式运行 PyInstaller:

    pyinstaller -F -w my_file.py
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2020-11-11
      • 2015-08-10
      • 1970-01-01
      • 2010-10-28
      • 2015-10-29
      • 1970-01-01
      相关资源
      最近更新 更多