Skip to content

Commit 8f12b26

Browse files
committed
Enable cross-platform functionality
在主程序中启用平台识别,部分功能对不同的操作系统将有不同的表现; 重构 `center_widget`,改变 pyinstaller_args 参数使用方式(有待进一步完善)、实现不同平台不同界面、修复文件选择错误提示对话框无法再次打开文件选择对话框的问题等; 其他细小优化;
1 parent 89b642c commit 8f12b26

3 files changed

Lines changed: 74 additions & 62 deletions

File tree

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
QWidget,
1616
)
1717

18+
from ..Constants.packaging_constants import *
1819
from .arguments_browser import ArgumentsBrowser
1920
from .dialog_widgets import IconFileDlg, ScriptFileDlg
2021

21-
# TODO 使用字适当数据结构管理选项列表
22-
2322

2423
class CenterWidget(QWidget):
2524
"""
2625
主界面的中央控件
2726
"""
2827

2928
# 自定义信号
30-
option_selected = QtCore.Signal(tuple)
29+
option_selected = QtCore.Signal(tuple) # 用户通过界面控件选择选项后发射此信号
3130

3231
def __init__(self, parent: QMainWindow = None) -> None:
3332
"""
@@ -52,14 +51,16 @@ def __init__(self, parent: QMainWindow = None) -> None:
5251
self.one_file_btn = QRadioButton()
5352
self.fd_group = QButtonGroup()
5453

55-
# 应用图标
56-
self.icon_path_label = QLabel()
57-
self.icon_file_dlg = IconFileDlg()
58-
self.icon_browse_btn = QPushButton()
59-
self.icon_path_le = QLineEdit()
54+
# 应用图标(仅 Windows 与 macOS)
55+
if self.parent().running_platform in ("Windows", "macOS"):
56+
self.icon_path_label = QLabel()
57+
self.icon_file_dlg = IconFileDlg()
58+
self.icon_browse_btn = QPushButton()
59+
self.icon_path_le = QLineEdit()
6060

61-
# Windows/MacOS 独占,注意后期处理成在Linux下不显示
62-
self.console_checkbox = QCheckBox()
61+
# 是否为stdio启用终端(仅 Windows 与 macOS)
62+
if self.parent().running_platform in ("Windows", "macOS"):
63+
self.console_checkbox = QCheckBox()
6364

6465
# 预览生成的PyInstaller打包指令
6566
self.pyinstaller_args_browser = ArgumentsBrowser()
@@ -91,14 +92,14 @@ def setup_ui(self) -> None:
9192
self.fd_group.addButton(self.one_dir_btn, 0)
9293
self.fd_group.addButton(self.one_file_btn, 1)
9394

94-
self.icon_path_label.setText("应用图标:")
95-
self.icon_path_le.setReadOnly(True)
96-
self.icon_path_le.setPlaceholderText("图标文件路径")
97-
self.icon_browse_btn.setText("浏览")
95+
if self.parent().running_platform in ("Windows", "macOS"):
96+
self.icon_path_label.setText("应用图标:")
97+
self.icon_path_le.setReadOnly(True)
98+
self.icon_path_le.setPlaceholderText("图标文件路径")
99+
self.icon_browse_btn.setText("浏览")
98100

99-
# Windows/MacOS 独占,注意!!!
100-
self.console_checkbox.setText("为标准I/O启用终端")
101-
self.console_checkbox.setChecked(True) # 默认值
101+
self.console_checkbox.setText("为标准I/O启用终端")
102+
self.console_checkbox.setChecked(True) # 默认值
102103

103104
self.pyinstaller_args_browser.setMaximumHeight(80)
104105

@@ -117,17 +118,16 @@ def script_file_selected(file_path: str) -> None:
117118
:param file_path: 脚本文件路径
118119
"""
119120

120-
self.option_selected.emit(("script_path", file_path))
121+
self.option_selected.emit((PyinstallerArgs.script_path, file_path))
121122

122-
@QtCore.Slot(str)
123+
@QtCore.Slot()
123124
def project_name_selected() -> None:
124125
"""
125126
输出程序名称完成输入的槽 \n
126127
"""
127128

128129
project_name: str = self.project_name_le.text()
129-
self.option_selected.emit(("out_name", project_name))
130-
self.parent().statusBar().showMessage(f"已将项目名设置为:{project_name}")
130+
self.option_selected.emit((PyinstallerArgs.out_name, project_name))
131131

132132
@QtCore.Slot(int)
133133
def one_fd_selected(btn_id: int) -> None:
@@ -137,10 +137,10 @@ def one_fd_selected(btn_id: int) -> None:
137137
"""
138138

139139
if btn_id == 0:
140-
self.option_selected.emit(("FD", "One Dir"))
140+
self.option_selected.emit((PyinstallerArgs.FD, "One Dir"))
141141
self.parent().statusBar().showMessage("将打包至单个目录中")
142142
elif btn_id == 1:
143-
self.option_selected.emit(("FD", "One File"))
143+
self.option_selected.emit((PyinstallerArgs.FD, "One File"))
144144
self.parent().statusBar().showMessage("将打包至单个文件中")
145145

146146
@QtCore.Slot(bool)
@@ -151,10 +151,10 @@ def console_selected(console: bool) -> None:
151151
"""
152152

153153
if console:
154-
self.option_selected.emit(("console", "console"))
154+
self.option_selected.emit((PyinstallerArgs.console, "console"))
155155
self.parent().statusBar().showMessage("将为打包程序的 stdio 启用终端")
156156
else:
157-
self.option_selected.emit(("console", "windowed"))
157+
self.option_selected.emit((PyinstallerArgs.console, "windowed"))
158158
self.parent().statusBar().showMessage("不会为打包程序的 stdio 启用终端")
159159

160160
@QtCore.Slot(str)
@@ -164,15 +164,18 @@ def icon_file_selected(file_path: str) -> None:
164164
:param file_path: 图标路径
165165
"""
166166

167-
self.option_selected.emit(("icon_path", file_path))
167+
self.option_selected.emit((PyinstallerArgs.icon_path, file_path))
168168

169+
# 连接信号与槽
169170
self.script_browse_btn.clicked.connect(self.script_file_dlg.open) # type: ignore
170171
self.script_file_dlg.fileSelected.connect(script_file_selected) # type: ignore
171172
self.project_name_le.editingFinished.connect(project_name_selected) # type: ignore
172173
self.fd_group.idClicked.connect(one_fd_selected) # type: ignore
173-
self.console_checkbox.toggled.connect(console_selected) # type: ignore
174-
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open) # type: ignore
175-
self.icon_file_dlg.fileSelected.connect(icon_file_selected) # type: ignore
174+
175+
if self.parent().running_platform in ("Windows", "macOS"):
176+
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open) # type: ignore
177+
self.icon_file_dlg.fileSelected.connect(icon_file_selected) # type: ignore
178+
self.console_checkbox.toggled.connect(console_selected) # type: ignore
176179

177180
@QtCore.Slot(tuple)
178181
def handle_option_set(self, option: tuple[str, str]) -> None:
@@ -183,20 +186,21 @@ def handle_option_set(self, option: tuple[str, str]) -> None:
183186

184187
option_key, option_value = option
185188

186-
if option_key == "script_path":
189+
if option_key == PyinstallerArgs.script_path:
187190
script_path = Path(option_value)
188191
self.script_path_le.setText(script_path.name)
189192
self.parent().statusBar().showMessage(
190193
f"打开脚本路径:{str(script_path.resolve())}"
191194
)
192195

193-
elif option_key == "icon_path":
196+
elif option_key == PyinstallerArgs.icon_path:
194197
icon_path = Path(option_value)
195198
self.icon_path_le.setText(icon_path.name)
196199
self.parent().statusBar().showMessage(f"打开图标路径:{str(icon_path.resolve())}")
197200

198-
elif option_key == "out_name":
201+
elif option_key == PyinstallerArgs.out_name:
199202
self.project_name_le.setText(option_value)
203+
self.parent().statusBar().showMessage(f"已将项目名设置为:{option_value}")
200204

201205
@QtCore.Slot(str)
202206
def handle_option_error(self, option: str) -> None:
@@ -206,7 +210,7 @@ def handle_option_error(self, option: str) -> None:
206210
"""
207211

208212
# 清空重置该项的输入控件,并弹出警告窗口,等待用户重新输入
209-
if option == "script_path":
213+
if option == PyinstallerArgs.script_path:
210214
self.script_file_dlg.close()
211215

212216
# 警告对话框
@@ -221,9 +225,9 @@ def handle_option_error(self, option: str) -> None:
221225
self.script_path_le.clear()
222226
self.project_name_le.clear()
223227
elif result == QMessageBox.Ok:
224-
self.script_file_dlg.open() # FIXME 修复无法再次打开对话框的问题
228+
self.script_file_dlg.exec()
225229

226-
elif option == "icon_path":
230+
elif option == PyinstallerArgs.icon_path:
227231
self.icon_file_dlg.close()
228232

229233
# 警告对话框
@@ -237,7 +241,7 @@ def handle_option_error(self, option: str) -> None:
237241
if result == QMessageBox.Cancel:
238242
self.icon_path_le.clear()
239243
elif result == QMessageBox.Ok:
240-
self.icon_file_dlg.open() # FIXME 修复无法再次打开对话框的问题
244+
self.icon_file_dlg.exec()
241245

242246
@QtCore.Slot(bool)
243247
def handle_ready_to_pack(self, ready: bool) -> None:
@@ -267,17 +271,25 @@ def _set_layout(self) -> None:
267271
fd_layout.addWidget(self.one_dir_btn, 1, 0)
268272
fd_layout.addWidget(self.one_file_btn, 1, 1)
269273

270-
icon_layout = QGridLayout()
271-
icon_layout.addWidget(self.icon_path_label, 0, 0, 1, 2)
272-
icon_layout.addWidget(self.icon_path_le, 1, 0)
273-
icon_layout.addWidget(self.icon_browse_btn, 1, 1)
274-
275274
main_layout = QVBoxLayout()
276275
main_layout.addLayout(script_layout)
276+
main_layout.addSpacing(10)
277277
main_layout.addLayout(name_layout)
278+
main_layout.addSpacing(10)
278279
main_layout.addLayout(fd_layout)
279-
main_layout.addWidget(self.console_checkbox)
280-
main_layout.addLayout(icon_layout)
280+
main_layout.addSpacing(10)
281+
282+
if self.parent().running_platform in ("Windows", "macOS"):
283+
main_layout.addWidget(self.console_checkbox)
284+
main_layout.addSpacing(10)
285+
286+
icon_layout = QGridLayout()
287+
icon_layout.addWidget(self.icon_path_label, 0, 0, 1, 2)
288+
icon_layout.addWidget(self.icon_path_le, 1, 0)
289+
icon_layout.addWidget(self.icon_browse_btn, 1, 1)
290+
main_layout.addLayout(icon_layout)
291+
292+
main_layout.addSpacing(10)
281293
main_layout.addWidget(self.pyinstaller_args_browser)
282294
main_layout.addWidget(self.run_packaging_btn)
283295
self.setLayout(main_layout)

src/py2exe_gui/__main__.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@
22
import subprocess
33
import sys
44

5-
from PySide6 import QtGui
5+
from PySide6 import QtCore, QtGui
66
from PySide6.QtWidgets import QApplication
77

88
# from .Constants import *
99
from .Core import Packaging, PackagingTask
1010
from .Widgets import MainWindow, SubProcessDlg
1111

1212

13+
def get_platform() -> str:
14+
if sys.platform.startswith("win32"):
15+
return "Windows"
16+
elif sys.platform.startswith("linux"):
17+
return "Linux"
18+
elif sys.platform.startswith("darwin"):
19+
return "macOS"
20+
21+
1322
class MainApp(MainWindow):
1423
"""
1524
主程序
1625
"""
1726

1827
def __init__(self, *args, **kwargs) -> None:
28+
self.running_platform = get_platform() # 获取当前运行的平台信息
1929
super(MainApp, self).__init__(*args, **kwargs)
2030

2131
self.packaging_task = PackagingTask(self)
@@ -45,25 +55,27 @@ def _connect_slots(self) -> None:
4555
)
4656
)
4757

48-
def run_packaging():
58+
@QtCore.Slot()
59+
def run_packaging() -> None:
4960
self.packager.run_packaging_process()
5061
self.subprocess_dlg.show()
5162

5263
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
5364

54-
def handle_multifunction():
65+
@QtCore.Slot()
66+
def handle_multifunction() -> None:
5567
"""处理子进程对话框多功能按钮点击信号的槽 \n"""
5668
btn_text = self.subprocess_dlg.multifunction_btn.text()
5769
if btn_text == "取消":
5870
self.packager.subprocess.abort_process()
5971
self.subprocess_dlg.close()
6072
elif btn_text == "打开输出位置":
6173
dist_path = self.packaging_task.script_path.parent / "dist"
62-
if sys.platform.startswith("win32"):
63-
os.startfile(dist_path)
64-
elif sys.platform.startswith("linux"):
74+
if self.running_platform == "Windows":
75+
os.startfile(dist_path) # type: ignore
76+
elif self.running_platform == "Linux":
6577
subprocess.call(["xdg-open", dist_path])
66-
elif sys.platform.startswith("darwin"):
78+
elif self.running_platform == "macOS":
6779
subprocess.call(["open", dist_path])
6880

6981
self.subprocess_dlg.multifunction_btn.clicked.connect(handle_multifunction)

src/py2exe_gui/cross_platform.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)