运行bat文件的程序,其中包含使用Qthread运行可执行文件(longTask)的指令,但是当我使用Pyinstaller通过以下命令创建可执行文件时,该程序无法按预期工作。我给“ --windowed”提供了标准I / O的控制台窗口
pyinstaller --onefile --windowed main.py
但是有趣的是,当我删除--windowed参数时,它可以按预期工作
pyinstaller --onefile main.py
这是代码:
from PyQt4.Qt import * import subprocess def callSubprocess(): page = QWizardPage() page.setTitle("Run myLongTask") runButton = QPushButton("Run") progressBar = QProgressBar() procLabel = QLabel() procLabel1 = QLabel() progressBar.setRange(0, 1) layout = QGridLayout() layout.addWidget(runButton, 0, 0) layout.addWidget(progressBar, 0, 1) layout.addWidget(procLabel) layout.addWidget(procLabel1) # Calls thread class myLongTask = TaskThread() runButton.clicked.connect(lambda: OnStart(myLongTask, progressBar, procLabel1)) myLongTask.taskFinished.connect(lambda: onFinished(progressBar, procLabel)) page.setLayout(layout) return page def OnStart(myLongTask, progressBar, procLabel1): progressBar.setRange(0, 0) myLongTask.start() # I am waiting until my subprocess completes while not myLongTask.isFinished(): QCoreApplication.processEvents() procLabel1.setText("Hello This is main") def onFinished(progressBar, procLabel): # Stop the pulsation progressBar.setRange(0, 1) procLabel.setText("longTask finished") class TaskThread(QThread): taskFinished = pyqtSignal() def __init__(self): QThread.__init__(self) def run(self): proc = subprocess.Popen(r'C:\Users\Desktop\runInf.bat', bufsize=0, shell=True, stdout=subprocess.PIPE) proc.wait() self.taskFinished.emit() def __del__(self): self.wait() if __name__ == '__main__': import sys app = QApplication(sys.argv) wizard = QWizard() wizard.addPage(callSubprocess()) wizard.setWindowTitle("Example Application") wizard.show() sys.exit(wizard.exec_())
当在Pycharm中执行上述代码时,它将按预期工作。但是,当使用PyInstaller构建时,主线程不会等到子进程完成。
任何想法如何创建线程可以按预期工作的可执行文件。提前致谢