**Требованиям, которым должно соответствовать решение
- Необходимо использовать подпроцесс. Popen
- Старайтесь не изменять StreamReader**
Код: Выделить всё
#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()
введите здесь описание изображения
package команда выглядит следующим образом:
Код: Выделить всё
pyinstaller -D -w .\gui_print_log.py
введите здесь описание изображения.
1、I попробуйте использовать "subprocess.run", это все равно произошло
2、 Я пытаюсь использовать многопроцессорность, она не может выводить журналы
Подробнее здесь: https://stackoverflow.com/questions/792 ... utable-whe