jyj789
jyj789

Reputation: 1

use subprocess. Popen module and use pyinstaller to package as an executable when multiple window interfaces appear

When you use subprocess.Popen to call a child process to execute a Python script, it is executed normally in PyCharm. However, when it is packaged as an executable (.exe), the main interface window is repeatedly opened. The interface window is written using PyQt5.

I tried using the tkinter module to create a GUI interface, but the issue still persists.

**The requirements that the solution needs to meet

  1. must be used Subprocess.Popen
  2. Try not to modify the StreamReader**

The code is as follows:

#main file gui_print_log.py

# -*- coding: utf-8 -*-
# @Time    : 2024/12/18 9:24
# @Author  : jinjie
# @File    : gui_print_log.py


import sys
from PyQt5.QtWidgets import QApplication, QTextEdit, QVBoxLayout, QPushButton, QWidget
from PyQt5.QtCore import QThread, pyqtSignal
import subprocess

class StreamReader(QThread):
    append_text = pyqtSignal(str)

    def __init__(self, process):
        super().__init__()
        self.process = process
        self._is_running = True

    def run(self):
        while self._is_running:
            output = self.process.stdout.readline()
            if output == '' and self.process.poll() is not None:
                self._is_running = False
                break
            if output:
                self.append_text.emit(output.strip())

    def stop(self):
        self._is_running = False

class LogWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 800, 600)
        self.setWindowTitle('log window')

        self.layout = QVBoxLayout()
        self.log_widget = QTextEdit()
        self.layout.addWidget(self.log_widget)

        self.start_button = QPushButton('start process')
        self.start_button.clicked.connect(self.start_process)
        self.layout.addWidget(self.start_button)

        self.setLayout(self.layout)

    def append_log(self, message):
        self.log_widget.append(message)

    # path = "F:\PyCharm_project\generate_testbench_local\jyj_suanfa_release_V1_4_4\FPGA_project_1"
    # enable_gui = "y"
    # vunit_args = "-p 2"
    def start_process(self):

        process = subprocess.Popen(
            [sys.executable, 'subprocess_script.py'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            encoding="utf-8"
        )

        self.stream_reader = StreamReader(process)
        self.stream_reader.append_text.connect(self.append_log)
        self.stream_reader.start()


    def stop_process(self):
        pass
    
    

if __name__ == "__main__":
    app = QApplication(sys.argv)
    log_window = LogWindow()
    log_window.show()
    sys.exit(app.exec_())


# -*- coding: utf-8 -*-
# @Time    : 2024/12/18 9:36
# @Author  : jinjie
# @File    : subprocess_script.py


import time

def main():
    for i in range(10):
        print(f'日志消息 {i + 1}')
        time.sleep(1)

if __name__ == "__main__":
    main()


When running in pycharm gui_print_log.py the opening interface is normal, as shown in the figure: enter image description here

package command is as follows:

pyinstaller -D -w  .\gui_print_log.py

After packaging as an exe, multiple windows will appear

enter image description here

1、I try to use "subprocess.run" ,it still happened 2、 I try to use multiprocessing , it can't output the logs

Upvotes: 0

Views: 31

Answers (0)

Related Questions