Skip to content

Commit c8cffc3

Browse files
committed
Refactor the sub-widget button slot-funcs into MainApp
将主界面打包按钮槽函数、子进程对话框多功能按钮槽函数移动至 `MainApp` 中,避免了子控件中调用父控件属性的问题;
1 parent a7aaba1 commit c8cffc3

3 files changed

Lines changed: 65 additions & 55 deletions

File tree

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,11 @@ def clean_selected(selected: bool) -> None:
192192
self.option_selected.emit((PyinstallerArgs.clean, ""))
193193
self.parent_widget.statusBar().showMessage("不会删除缓存与临时文件")
194194

195-
@QtCore.Slot()
196-
def run_packaging() -> None:
197-
"""
198-
“运行打包”按钮的槽函数 \n
199-
"""
200-
201-
# 先显示对话框窗口,后运行子进程,确保调试信息/错误信息能被直观显示
202-
self.parent().subprocess_dlg.show()
203-
self.parent().packager.run_packaging_process()
204-
205195
# 连接信号与槽
206196
self.script_browse_btn.clicked.connect(self.script_file_dlg.open)
207197
self.script_file_dlg.fileSelected.connect(script_file_selected)
208198
self.project_name_le.editingFinished.connect(project_name_selected)
209199
self.fd_group.idClicked.connect(one_fd_selected)
210-
self.run_packaging_btn.clicked.connect(run_packaging)
211200

212201
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
213202
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open)

src/py2exe_gui/Widgets/subprocess_widget.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Licensed under the GPLv3 License: https://www.gnu.org/licenses/gpl-3.0.html
22
# For details: https://github.com/muziing/Py2exe-GUI/blob/main/README.md#license
33

4-
import subprocess
5-
64
from PySide6.QtCore import Slot
75
from PySide6.QtGui import QCloseEvent
86
from PySide6.QtWidgets import (
@@ -14,8 +12,6 @@
1412
QWidget,
1513
)
1614

17-
from ..Constants import PLATFORM
18-
from ..Core import RUNTIME_INFO
1915
from ..Core.subprocess_tool import SubProcessTool
2016

2117

@@ -45,9 +41,6 @@ def _setup(self) -> None:
4541
self.setMinimumWidth(400)
4642
self.setModal(True) # 设置为模态对话框
4743

48-
# 连接信号与槽
49-
self.multifunction_btn.clicked.connect(self.handle_multifunction)
50-
5144
# 布局管理器
5245
main_layout = QVBoxLayout()
5346
main_layout.addWidget(self.info_label)
@@ -85,34 +78,12 @@ def handle_output(self, subprocess_output: tuple[int, str]) -> None:
8578
self.browser.append("请检查是否已经安装正确版本的 PyInstaller")
8679
self.multifunction_btn.setText("关闭")
8780

88-
@Slot()
89-
def handle_multifunction(self) -> None:
90-
"""
91-
处理多功能按钮点击信号的槽 \n
92-
"""
93-
94-
btn_text = self.multifunction_btn.text()
95-
if btn_text == "取消":
96-
self.parent().packager.subprocess.abort_process()
97-
self.close()
98-
elif btn_text == "打开输出位置":
99-
dist_path = self.parent().packaging_task.script_path.parent / "dist"
100-
if PLATFORM.windows == RUNTIME_INFO.platform:
101-
from os import startfile as os_startfile # fmt: skip
102-
os_startfile(dist_path) # noqa
103-
elif PLATFORM.linux == RUNTIME_INFO.platform:
104-
subprocess.call(["xdg-open", dist_path])
105-
elif PLATFORM.macos == RUNTIME_INFO.platform:
106-
subprocess.call(["open", dist_path])
107-
elif btn_text == "关闭":
108-
self.close()
109-
11081
def closeEvent(self, event: QCloseEvent) -> None:
11182
"""
11283
重写关闭事件,进行收尾清理 \n
11384
:param event: 关闭事件
11485
"""
11586

116-
self.parent().packager.subprocess.abort_process()
87+
self.finished.emit(-1)
11788
self.browser.clear()
11889
super().closeEvent(event)

src/py2exe_gui/__main__.py

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
# For details: https://github.com/muziing/Py2exe-GUI/blob/main/README.md#license
33

44
import sys
5+
from subprocess import call as subprocess_call
56

7+
from PySide6.QtCore import Slot
68
from PySide6.QtGui import QCloseEvent
79
from PySide6.QtWidgets import QApplication
810

9-
from .Core import Packaging, PackagingTask # noqa
11+
from .Constants import PLATFORM # noqa
12+
from .Core import RUNTIME_INFO, Packaging, PackagingTask # noqa
1013
from .Resources import compiled_resources # noqa
1114
from .Widgets import MainWindow, SubProcessDlg # noqa
1215

@@ -32,27 +35,74 @@ def _connect_slots(self) -> None:
3235
连接各种信号与槽 \n
3336
"""
3437

35-
center_widget = self.center_widget
36-
packaging_task = self.packaging_task
37-
packager = self.packager
38-
39-
center_widget.option_selected.connect(packaging_task.handle_option)
40-
packaging_task.option_set.connect(packager.set_pyinstaller_args)
41-
packaging_task.option_set.connect(center_widget.handle_option_set)
42-
packaging_task.option_error.connect(center_widget.handle_option_error)
43-
packaging_task.ready_to_pack.connect(center_widget.handle_ready_to_pack)
44-
packager.args_settled.connect(
45-
lambda val: center_widget.pyinstaller_args_browser.enrich_args_text(val)
38+
self._connect_run_pkg_btn_slot()
39+
self._connect_mul_btn_slot(self.subprocess_dlg)
40+
41+
self.center_widget.option_selected.connect(self.packaging_task.handle_option)
42+
self.packaging_task.option_set.connect(self.packager.set_pyinstaller_args)
43+
self.packaging_task.option_set.connect(self.center_widget.handle_option_set)
44+
self.packaging_task.option_error.connect(self.center_widget.handle_option_error)
45+
self.packaging_task.ready_to_pack.connect(
46+
self.center_widget.handle_ready_to_pack
47+
)
48+
self.packager.args_settled.connect(
49+
lambda val: self.center_widget.pyinstaller_args_browser.enrich_args_text(
50+
val
51+
)
4652
)
47-
packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
53+
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
54+
55+
# 用户关闭子进程对话框时中止打包进程
56+
self.subprocess_dlg.finished.connect(
57+
lambda: self.packager.subprocess.abort_process(2000)
58+
)
59+
60+
def _connect_mul_btn_slot(self, subprocess_dlg):
61+
@Slot()
62+
def handle_multifunction() -> None:
63+
"""
64+
处理子进程窗口多功能按钮点击信号的槽 \n
65+
"""
66+
67+
btn_text = self.subprocess_dlg.multifunction_btn.text()
68+
if btn_text == "取消":
69+
self.packager.subprocess.abort_process()
70+
self.subprocess_dlg.close()
71+
elif btn_text == "打开输出位置":
72+
dist_path = self.packaging_task.script_path.parent / "dist"
73+
if PLATFORM.windows == RUNTIME_INFO.platform:
74+
from os import startfile as os_startfile # fmt: skip
75+
os_startfile(dist_path) # noqa
76+
elif PLATFORM.linux == RUNTIME_INFO.platform:
77+
subprocess_call(["xdg-open", dist_path])
78+
elif PLATFORM.macos == RUNTIME_INFO.platform:
79+
subprocess_call(["open", dist_path])
80+
elif btn_text == "关闭":
81+
self.subprocess_dlg.close()
82+
83+
# 连接信号与槽
84+
subprocess_dlg.multifunction_btn.clicked.connect(handle_multifunction)
85+
86+
def _connect_run_pkg_btn_slot(self):
87+
@Slot()
88+
def run_packaging() -> None:
89+
"""
90+
“运行打包”按钮的槽函数 \n
91+
"""
92+
93+
# 先显示对话框窗口,后运行子进程,确保调试信息/错误信息能被直观显示
94+
self.subprocess_dlg.show()
95+
self.packager.run_packaging_process()
96+
97+
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
4898

4999
def closeEvent(self, event: QCloseEvent) -> None:
50100
"""
51101
重写关闭事件,进行收尾清理 \n
52102
:param event: 关闭事件
53103
"""
54104

55-
self.packager.subprocess.abort_process()
105+
# self.packager.subprocess.abort_process(3000) # 不会访问到此行
56106
super().closeEvent(event)
57107

58108

0 commit comments

Comments
 (0)