Skip to content

Commit 30707d7

Browse files
committed
Add subprocess-dialog
初步实现子进程输出至对话框; 优化`Packaging`类中创建`QSubProcessTool`的方式;
1 parent b7bf56d commit 30707d7

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

src/py2exe_gui/Core/packaging.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, parent: Optional[QObject] = None):
2323
]
2424
self.args_dict: dict = dict.fromkeys(pyinstaller_args, "")
2525
self._args: List[str] = []
26-
self.subprocess: Optional[QSubProcessTool] = None
26+
self.subprocess: QSubProcessTool = QSubProcessTool()
2727

2828
def get_pyinstaller_args(self, arg: tuple[str, str]) -> None:
2929
"""
@@ -67,7 +67,5 @@ def run_packaging_process(self) -> None:
6767
:return: None
6868
"""
6969

70-
if self.subprocess is None: # 确保只在首次调用时实例化一个QSubProcess对象
71-
self.subprocess = QSubProcessTool()
72-
self.subprocess.output.connect(lambda val: print(val)) # 测试用
70+
# self.subprocess.output.connect(lambda val: print(val)) # 测试用
7371
self.subprocess.start_process("pyinstaller", self._args)

src/py2exe_gui/Widgets/dialog_widgets.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
from PySide6.QtCore import Qt
2-
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget
2+
from PySide6.QtWidgets import (
3+
QDialog,
4+
QFileDialog,
5+
QMessageBox,
6+
QTextBrowser,
7+
QVBoxLayout,
8+
QWidget,
9+
)
310

411
"""
512
由于各种对话框的设置代码较繁琐且独立,故单独在本模块中配置
@@ -86,6 +93,36 @@ def about_text(self) -> str:
8693
return self._about_text
8794

8895

96+
class SubProcessDlg(QDialog):
97+
"""用于显示子进程信息的对话框"""
98+
99+
def __init__(self, parent: QWidget = None) -> None:
100+
super(SubProcessDlg, self).__init__(parent=parent)
101+
self.browser = QTextBrowser()
102+
self._setup()
103+
104+
def _setup(self):
105+
"""
106+
配置子进程信息对话框 \n
107+
"""
108+
109+
layout = QVBoxLayout()
110+
layout.addWidget(self.browser)
111+
self.setLayout(layout)
112+
113+
def handle_output(self, subprocess_output: tuple[int, str]):
114+
"""
115+
处理子进程的输出 \n
116+
:param subprocess_output: 子进程输出
117+
:return: None
118+
"""
119+
output_type, output_text = subprocess_output
120+
if output_type == 2:
121+
self.browser.append(output_text)
122+
elif output_type == 3:
123+
self.browser.append(output_text)
124+
125+
89126
if __name__ == "__main__":
90127
import sys
91128

@@ -95,6 +132,7 @@ def about_text(self) -> str:
95132
# window = ScriptFileDlg()
96133
# window = IconFileDlg()
97134
# window.fileSelected.connect(lambda f: print(f)) # type: ignore
98-
window = AboutMessage()
135+
# window = AboutMessage()
136+
window = SubProcessDlg()
99137
window.open()
100138
sys.exit(app.exec())

src/py2exe_gui/__main__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from PySide6.QtWidgets import QApplication
44

55
from .Core.packaging import Packaging
6+
from .Widgets.dialog_widgets import SubProcessDlg
67
from .Widgets.main_window import MainWindow
78

89

@@ -21,9 +22,16 @@ def __init__(self, *args, **kwargs) -> None:
2122
)
2223

2324
self.center_widget.option_selected.connect(self.packager.get_pyinstaller_args)
24-
self.center_widget.run_packaging_btn.clicked.connect(
25-
self.packager.run_packaging_process
26-
)
25+
26+
self.subprocess_dlg = SubProcessDlg()
27+
28+
def run_packaging():
29+
self.packager.run_packaging_process()
30+
self.subprocess_dlg.show()
31+
32+
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
33+
34+
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
2735

2836
self.status_bar.showMessage("就绪")
2937

0 commit comments

Comments
 (0)