Skip to content

Commit eb68927

Browse files
committed
Add info label to SubProcessDlg
为子进程对话框添加信息文本; 修复直接运行`__main__.py`会导致的相对导入问题
1 parent f09a024 commit eb68927

3 files changed

Lines changed: 32 additions & 45 deletions

File tree

src/py2exe_gui/Core/packaging.py

Lines changed: 3 additions & 25 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: SubProcessTool = SubProcessTool(self, program="pyinstaller")
26+
self.subprocess: SubProcessTool = SubProcessTool(self, program="pyinstaller")
2727

2828
def set_pyinstaller_args(self, arg: tuple[str, str]) -> None:
2929
"""
@@ -68,27 +68,5 @@ def run_packaging_process(self) -> None:
6868
"""
6969

7070
# self._subprocess.output.connect(lambda val: print(val)) # 测试用
71-
self._subprocess.set_arguments(self._args)
72-
self._subprocess.start_process()
73-
74-
def abort_process(self) -> int:
75-
"""
76-
紧急终止打包进程 \n
77-
:return: 子进程返回值
78-
"""
79-
80-
if self._subprocess._process:
81-
result = 0
82-
83-
def handel(output: tuple):
84-
"""处理子进程的输出,获取进程结束的返回值"""
85-
nonlocal result
86-
if output[0] == 1:
87-
result = int(output[1])
88-
89-
self._subprocess.output.connect(handel)
90-
self._subprocess.abort_process()
91-
92-
return result
93-
else:
94-
return 0
71+
self.subprocess.set_arguments(self._args)
72+
self.subprocess.start_process()

src/py2exe_gui/Widgets/dialog_widgets.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
from PySide6.QtWidgets import (
33
QDialog,
44
QFileDialog,
5+
QLabel,
56
QMessageBox,
67
QTextBrowser,
78
QVBoxLayout,
89
QWidget,
910
)
1011

11-
"""
12-
由于各种对话框的设置代码较繁琐且独立,故单独在本模块中配置
13-
"""
12+
from py2exe_gui.Core.subprocess_tool import SubProcessTool
1413

1514

1615
class ScriptFileDlg(QFileDialog):
1716
"""用于获取入口脚本文件的对话框"""
1817

1918
def __init__(self, parent: QWidget = None) -> None:
20-
super().__init__(parent)
19+
super(ScriptFileDlg, self).__init__(parent)
2120
self._setup()
2221

2322
def _setup(self) -> None:
@@ -63,7 +62,7 @@ class AboutMessage(QMessageBox):
6362
"""用于显示关于信息的对话框"""
6463

6564
def __init__(self, parent: QWidget = None) -> None:
66-
super(AboutMessage, self).__init__(parent=parent)
65+
super(AboutMessage, self).__init__(parent)
6766
self._about_text: str = ""
6867
self._setup()
6968

@@ -97,30 +96,40 @@ class SubProcessDlg(QDialog):
9796
"""用于显示子进程信息的对话框"""
9897

9998
def __init__(self, parent: QWidget = None) -> None:
100-
super(SubProcessDlg, self).__init__(parent=parent)
101-
self.browser = QTextBrowser()
99+
super(SubProcessDlg, self).__init__(parent)
100+
self.info_label = QLabel(self)
101+
self.browser = QTextBrowser(self)
102102
self._setup()
103103

104-
def _setup(self):
104+
def _setup(self) -> None:
105105
"""
106106
配置子进程信息对话框 \n
107107
"""
108108

109+
self.setWindowTitle("PyInstaller")
110+
self.setModal(True)
111+
109112
layout = QVBoxLayout()
113+
layout.addWidget(self.info_label)
110114
layout.addWidget(self.browser)
111115
self.setLayout(layout)
112116

113-
def handle_output(self, subprocess_output: tuple[int, str]):
117+
def handle_output(self, subprocess_output: tuple[int, str]) -> None:
114118
"""
115119
处理子进程的输出 \n
116120
:param subprocess_output: 子进程输出
117121
:return: None
118122
"""
123+
119124
output_type, output_text = subprocess_output
120-
if output_type == 2:
125+
if output_type == SubProcessTool.STDOUT:
121126
self.browser.append(output_text)
122-
elif output_type == 3:
127+
elif output_type == SubProcessTool.STDERR:
123128
self.browser.append(output_text)
129+
elif output_type == SubProcessTool.FINISHED:
130+
self.info_label.setText("打包完成!")
131+
elif output_type == SubProcessTool.STATE:
132+
self.info_label.setText(output_text)
124133

125134

126135
if __name__ == "__main__":

src/py2exe_gui/__main__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import sys
22

3+
from PySide6 import QtGui
34
from PySide6.QtWidgets import QApplication
45

5-
from .Core.packaging import Packaging
6-
from .Widgets.dialog_widgets import SubProcessDlg
7-
from .Widgets.main_window import MainWindow
6+
from py2exe_gui.Core.packaging import Packaging
7+
from py2exe_gui.Widgets.dialog_widgets import SubProcessDlg
8+
from py2exe_gui.Widgets.main_window import MainWindow
89

910

1011
class MainApp(MainWindow):
@@ -20,7 +21,6 @@ def __init__(self, *args, **kwargs) -> None:
2021
val
2122
)
2223
)
23-
2424
self.center_widget.option_selected.connect(self.packager.set_pyinstaller_args)
2525

2626
self.subprocess_dlg = SubProcessDlg(self)
@@ -30,18 +30,18 @@ def run_packaging():
3030
self.subprocess_dlg.show()
3131

3232
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
33-
34-
# TODO 在 Packaging 类中增加处理输出的方法,将所有 output 信号连接至该方法处理
35-
self.packager._subprocess.output.connect(self.subprocess_dlg.handle_output)
33+
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
3634

3735
self.status_bar.showMessage("就绪")
3836

39-
def closeEvent(self, event):
37+
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
4038
"""
4139
重写关闭事件,进行收尾清理 \n
40+
:param event: 关闭事件
41+
:return: None
4242
"""
4343

44-
self.packager.abort_process()
44+
self.packager.subprocess.abort_process()
4545
super(MainApp, self).closeEvent(event)
4646

4747

0 commit comments

Comments
 (0)