Skip to content

Commit 7bb8306

Browse files
committed
Refactor SubProcessDlg class
重构子进程对话框类,将多功能按钮的槽函数移至类方法中; 修正各对话框类__init__方法的类型注解; 注释等其他微小优化;
1 parent 7755298 commit 7bb8306

2 files changed

Lines changed: 42 additions & 44 deletions

File tree

src/py2exe_gui/Widgets/dialog_widgets.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from PySide6.QtCore import Qt
1+
import subprocess
2+
from typing import Optional
3+
4+
from PySide6 import QtCore
25
from PySide6.QtGui import QPixmap
36
from PySide6.QtWidgets import (
47
QDialog,
@@ -19,7 +22,7 @@ class ScriptFileDlg(QFileDialog):
1922
用于获取入口脚本文件的对话框
2023
"""
2124

22-
def __init__(self, parent: QWidget = None) -> None:
25+
def __init__(self, parent: Optional[QWidget] = None) -> None:
2326
"""
2427
:param parent: 父控件对象
2528
"""
@@ -48,7 +51,7 @@ class IconFileDlg(QFileDialog):
4851
用于获取应用图标文件的对话框
4952
"""
5053

51-
def __init__(self, parent: QWidget = None) -> None:
54+
def __init__(self, parent: Optional[QWidget] = None) -> None:
5255
"""
5356
:param parent: 父控件对象
5457
"""
@@ -77,7 +80,7 @@ class AddDataDlg(QFileDialog):
7780
用于添加附加数据的对话框
7881
"""
7982

80-
def __init__(self, parent: QWidget = None) -> None:
83+
def __init__(self, parent: Optional[QWidget] = None) -> None:
8184
"""
8285
:param parent: 父控件对象
8386
"""
@@ -99,7 +102,7 @@ class AboutDlg(QMessageBox):
99102
用于显示关于信息的对话框
100103
"""
101104

102-
def __init__(self, parent: QWidget = None) -> None:
105+
def __init__(self, parent: Optional[QWidget] = None) -> None:
103106
"""
104107
:param parent: 父控件对象
105108
"""
@@ -116,7 +119,7 @@ def _setup(self) -> None:
116119

117120
self.setWindowTitle("关于")
118121
self.setStandardButtons(QMessageBox.Ok)
119-
self.setTextFormat(Qt.MarkdownText)
122+
self.setTextFormat(QtCore.Qt.MarkdownText)
120123
self.setText(self.about_text)
121124
self.setIconPixmap(
122125
QPixmap("py2exe_gui/Resources/Icons/Py2exe-GUI_icon_72px.png")
@@ -145,11 +148,9 @@ class SubProcessDlg(QDialog):
145148
用于显示子进程信息的对话框
146149
"""
147150

148-
# TODO 重构SubProcessDlg
149-
150-
def __init__(self, parent: QWidget = None) -> None:
151+
def __init__(self, parent: QWidget) -> None:
151152
"""
152-
:param parent: 父控件对象
153+
:param parent: 父控件对象,必须为 MainApp 类
153154
"""
154155

155156
super(SubProcessDlg, self).__init__(parent)
@@ -177,6 +178,10 @@ def _setup(self) -> None:
177178
main_layout.addWidget(self.multifunction_btn)
178179
self.setLayout(main_layout)
179180

181+
# 连接信号与槽
182+
self.multifunction_btn.clicked.connect(self.handle_multifunction) # type:ignore
183+
184+
@QtCore.Slot(tuple)
180185
def handle_output(self, subprocess_output: tuple[int, str]) -> None:
181186
"""
182187
处理子进程的输出 \n
@@ -196,17 +201,21 @@ def handle_output(self, subprocess_output: tuple[int, str]) -> None:
196201
if output_text == "正在运行中……":
197202
self.multifunction_btn.setText("取消")
198203

204+
@QtCore.Slot()
205+
def handle_multifunction(self) -> None:
206+
"""
207+
处理多功能按钮点击信号的槽 \n
208+
"""
199209

200-
if __name__ == "__main__":
201-
import sys
202-
203-
from PySide6.QtWidgets import QApplication
204-
205-
app = QApplication(sys.argv)
206-
# window = ScriptFileDlg()
207-
# window = IconFileDlg()
208-
# window.fileSelected.connect(lambda f: print(f)) # type: ignore
209-
# window = AboutDlg()
210-
window = SubProcessDlg()
211-
window.open()
212-
sys.exit(app.exec())
210+
btn_text = self.multifunction_btn.text()
211+
if btn_text == "取消":
212+
self.parent().packager.subprocess.abort_process()
213+
self.close()
214+
elif btn_text == "打开输出位置":
215+
dist_path = self.parent().packaging_task.script_path.parent / "dist"
216+
if self.parent().running_platform == "Windows":
217+
os.startfile(dist_path) # type: ignore
218+
elif self.parent().running_platform == "Linux":
219+
subprocess.call(["xdg-open", dist_path])
220+
elif self.parent().running_platform == "macOS":
221+
subprocess.call(["open", dist_path])

src/py2exe_gui/__main__.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import os
2-
import subprocess
31
import sys
42

53
from PySide6 import QtCore, QtGui
64
from PySide6.QtWidgets import QApplication
75

8-
# from .Constants import *
96
from .Core import Packaging, PackagingTask
107
from .Widgets import MainWindow, SubProcessDlg
118

129

1310
def get_platform() -> str:
11+
"""
12+
辅助函数,用于获取当前运行的平台 \n
13+
:return: platform
14+
"""
15+
1416
if sys.platform.startswith("win32"):
1517
return "Windows"
1618
elif sys.platform.startswith("linux"):
1719
return "Linux"
1820
elif sys.platform.startswith("darwin"):
1921
return "macOS"
22+
else:
23+
return "others"
2024

2125

2226
class MainApp(MainWindow):
@@ -57,28 +61,13 @@ def _connect_slots(self) -> None:
5761

5862
@QtCore.Slot()
5963
def run_packaging() -> None:
64+
"""
65+
“运行打包”按钮的槽函数 \n
66+
"""
6067
self.packager.run_packaging_process()
6168
self.subprocess_dlg.show()
6269

6370
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
64-
65-
@QtCore.Slot()
66-
def handle_multifunction() -> None:
67-
"""处理子进程对话框多功能按钮点击信号的槽 \n"""
68-
btn_text = self.subprocess_dlg.multifunction_btn.text()
69-
if btn_text == "取消":
70-
self.packager.subprocess.abort_process()
71-
self.subprocess_dlg.close()
72-
elif btn_text == "打开输出位置":
73-
dist_path = self.packaging_task.script_path.parent / "dist"
74-
if self.running_platform == "Windows":
75-
os.startfile(dist_path) # type: ignore
76-
elif self.running_platform == "Linux":
77-
subprocess.call(["xdg-open", dist_path])
78-
elif self.running_platform == "macOS":
79-
subprocess.call(["open", dist_path])
80-
81-
self.subprocess_dlg.multifunction_btn.clicked.connect(handle_multifunction)
8271
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
8372

8473
def closeEvent(self, event: QtGui.QCloseEvent) -> None:

0 commit comments

Comments
 (0)