Skip to content

Commit b2f7fff

Browse files
committed
Optimize PLATFORM class with enum
将 `PLATFORM` 改为枚举值类; 优化 type hint;
1 parent b049835 commit b2f7fff

3 files changed

Lines changed: 55 additions & 45 deletions

File tree

src/py2exe_gui/Constants/platform_constants.py

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

44
import sys
5+
from enum import Enum, unique
56

67

7-
class PLATFORM:
8+
@unique
9+
class PLATFORM(Enum):
810
"""
911
运行平台相关的常量 \n
1012
"""
@@ -15,7 +17,7 @@ class PLATFORM:
1517
others = "others"
1618

1719

18-
def get_platform() -> str:
20+
def get_platform() -> PLATFORM:
1921
"""
2022
辅助函数,用于获取当前运行的平台 \n
2123
:return: platform

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def __init__(self, parent: QMainWindow) -> None:
3939

4040
super(CenterWidget, self).__init__(parent)
4141

42+
self.parent_widget = parent
43+
44+
# 根据运行平台不同,提供的控件也有所区别
45+
self.running_platform: PLATFORM = parent.running_platform # type: ignore
46+
4247
# 待打包的入口脚本
4348
self.script_path_label = QLabel()
4449
self.script_file_dlg = ScriptFileDlg()
@@ -56,14 +61,14 @@ def __init__(self, parent: QMainWindow) -> None:
5661
self.fd_group = QButtonGroup()
5762

5863
# 应用图标(仅 Windows 与 macOS)
59-
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
64+
if self.running_platform in (PLATFORM.windows, PLATFORM.macos):
6065
self.icon_path_label = QLabel()
6166
self.icon_file_dlg = IconFileDlg()
6267
self.icon_browse_btn = QPushButton()
6368
self.icon_path_le = QLineEdit()
6469

6570
# 是否为stdio启用终端(仅 Windows 与 macOS)
66-
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
71+
if self.running_platform in (PLATFORM.windows, PLATFORM.macos):
6772
self.console_checkbox = QCheckBox()
6873

6974
# 预览生成的PyInstaller打包指令
@@ -96,7 +101,7 @@ def setup_ui(self) -> None:
96101
self.fd_group.addButton(self.one_dir_btn, 0)
97102
self.fd_group.addButton(self.one_file_btn, 1)
98103

99-
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
104+
if self.running_platform in (PLATFORM.windows, PLATFORM.macos):
100105
self.icon_path_label.setText("应用图标:")
101106
self.icon_path_le.setReadOnly(True)
102107
self.icon_path_le.setPlaceholderText("图标文件路径")
@@ -142,10 +147,10 @@ def one_fd_selected(btn_id: int) -> None:
142147

143148
if btn_id == 0:
144149
self.option_selected.emit((PyinstallerArgs.FD, "One Dir"))
145-
self.parent().statusBar().showMessage("将打包至单个目录中")
150+
self.parent_widget.statusBar().showMessage("将打包至单个目录中")
146151
elif btn_id == 1:
147152
self.option_selected.emit((PyinstallerArgs.FD, "One File"))
148-
self.parent().statusBar().showMessage("将打包至单个文件中")
153+
self.parent_widget.statusBar().showMessage("将打包至单个文件中")
149154

150155
@QtCore.Slot(bool)
151156
def console_selected(console: bool) -> None:
@@ -156,10 +161,10 @@ def console_selected(console: bool) -> None:
156161

157162
if console:
158163
self.option_selected.emit((PyinstallerArgs.console, "console"))
159-
self.parent().statusBar().showMessage("将为打包程序的 stdio 启用终端")
164+
self.parent_widget.statusBar().showMessage("将为打包程序的 stdio 启用终端")
160165
else:
161166
self.option_selected.emit((PyinstallerArgs.console, "windowed"))
162-
self.parent().statusBar().showMessage("不会为打包程序的 stdio 启用终端")
167+
self.parent_widget.statusBar().showMessage("不会为打包程序的 stdio 启用终端")
163168

164169
@QtCore.Slot(str)
165170
def icon_file_selected(file_path: str) -> None:
@@ -186,7 +191,7 @@ def run_packaging() -> None:
186191
self.fd_group.idClicked.connect(one_fd_selected) # type: ignore
187192
self.run_packaging_btn.clicked.connect(run_packaging) # type: ignore
188193

189-
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
194+
if self.running_platform in (PLATFORM.windows, PLATFORM.macos):
190195
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open) # type: ignore
191196
self.icon_file_dlg.fileSelected.connect(icon_file_selected) # type: ignore
192197
self.console_checkbox.toggled.connect(console_selected) # type: ignore
@@ -203,18 +208,20 @@ def handle_option_set(self, option: tuple[str, str]) -> None:
203208
if option_key == PyinstallerArgs.script_path:
204209
script_path = Path(option_value)
205210
self.script_path_le.setText(script_path.name)
206-
self.parent().statusBar().showMessage(
211+
self.parent_widget.statusBar().showMessage(
207212
f"打开脚本路径:{str(script_path.resolve())}"
208213
)
209214

210215
elif option_key == PyinstallerArgs.icon_path:
211216
icon_path = Path(option_value)
212217
self.icon_path_le.setText(icon_path.name)
213-
self.parent().statusBar().showMessage(f"打开图标路径:{str(icon_path.resolve())}")
218+
self.parent_widget.statusBar().showMessage(
219+
f"打开图标路径:{str(icon_path.resolve())}"
220+
)
214221

215222
elif option_key == PyinstallerArgs.out_name:
216223
self.project_name_le.setText(option_value)
217-
self.parent().statusBar().showMessage(f"已将项目名设置为:{option_value}")
224+
self.parent_widget.statusBar().showMessage(f"已将项目名设置为:{option_value}")
218225

219226
@QtCore.Slot(str)
220227
def handle_option_error(self, option: str) -> None:
@@ -229,32 +236,32 @@ def handle_option_error(self, option: str) -> None:
229236

230237
# 警告对话框
231238
result = QMessageBox.critical(
232-
self.parent(),
239+
self.parent_widget,
233240
"错误",
234241
"选择的不是有效的Python脚本文件,请重新选择!",
235-
QMessageBox.Cancel,
236-
QMessageBox.Ok,
242+
QMessageBox.StandardButton.Cancel,
243+
QMessageBox.StandardButton.Ok,
237244
)
238-
if result == QMessageBox.Cancel:
245+
if result == QMessageBox.StandardButton.Cancel:
239246
self.script_path_le.clear()
240247
self.project_name_le.clear()
241-
elif result == QMessageBox.Ok:
248+
elif result == QMessageBox.StandardButton.Ok:
242249
self.script_file_dlg.exec()
243250

244251
elif option == PyinstallerArgs.icon_path:
245252
self.icon_file_dlg.close()
246253

247254
# 警告对话框
248255
result = QMessageBox.critical(
249-
self.parent(),
256+
self.parent_widget,
250257
"错误",
251258
"选择的不是有效的图标文件,请重新选择!",
252-
QMessageBox.Cancel,
253-
QMessageBox.Ok,
259+
QMessageBox.StandardButton.Cancel,
260+
QMessageBox.StandardButton.Ok,
254261
)
255-
if result == QMessageBox.Cancel:
262+
if result == QMessageBox.StandardButton.Cancel:
256263
self.icon_path_le.clear()
257-
elif result == QMessageBox.Ok:
264+
elif result == QMessageBox.StandardButton.Ok:
258265
self.icon_file_dlg.exec()
259266

260267
@QtCore.Slot(bool)
@@ -293,7 +300,7 @@ def _set_layout(self) -> None:
293300
main_layout.addLayout(fd_layout)
294301
main_layout.addSpacing(10)
295302

296-
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
303+
if self.running_platform in (PLATFORM.windows, PLATFORM.macos):
297304
main_layout.addWidget(self.console_checkbox)
298305
main_layout.addSpacing(10)
299306

src/py2exe_gui/Widgets/dialog_widgets.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
QWidget,
1818
)
1919

20+
from ..Constants import PLATFORM
2021
from ..Core.subprocess_tool import SubProcessTool
2122

2223

@@ -39,14 +40,14 @@ def _setup(self) -> None:
3940
配置脚本路径对话框 \n
4041
"""
4142

42-
self.setAcceptMode(QFileDialog.AcceptOpen)
43-
self.setViewMode(QFileDialog.Detail)
43+
self.setAcceptMode(QFileDialog.AcceptMode.AcceptOpen)
44+
self.setViewMode(QFileDialog.ViewMode.Detail)
4445
self.setNameFilters(("Python脚本文件 (*.py *.pyw)", "所有文件 (*)"))
45-
self.setFileMode(QFileDialog.ExistingFiles)
46-
self.setLabelText(QFileDialog.FileName, "Python入口文件")
47-
self.setLabelText(QFileDialog.FileType, "Python文件")
48-
self.setLabelText(QFileDialog.Accept, "打开")
49-
self.setLabelText(QFileDialog.Reject, "取消")
46+
self.setFileMode(QFileDialog.FileMode.ExistingFiles)
47+
self.setLabelText(QFileDialog.DialogLabel.FileName, "Python入口文件")
48+
self.setLabelText(QFileDialog.DialogLabel.FileType, "Python文件")
49+
self.setLabelText(QFileDialog.DialogLabel.Accept, "打开")
50+
self.setLabelText(QFileDialog.DialogLabel.Reject, "取消")
5051

5152

5253
class IconFileDlg(QFileDialog):
@@ -68,14 +69,14 @@ def _setup(self) -> None:
6869
配置应用图标对话框 \n
6970
"""
7071

71-
self.setAcceptMode(QFileDialog.AcceptOpen)
72-
self.setViewMode(QFileDialog.Detail)
72+
self.setAcceptMode(QFileDialog.AcceptMode.AcceptOpen)
73+
self.setViewMode(QFileDialog.ViewMode.Detail)
7374
self.setNameFilters(("图标文件 (*.ico *.icns)", "所有文件 (*)"))
74-
self.setFileMode(QFileDialog.ExistingFile)
75-
self.setLabelText(QFileDialog.FileName, "图标")
76-
self.setLabelText(QFileDialog.FileType, "图标文件")
77-
self.setLabelText(QFileDialog.Accept, "打开")
78-
self.setLabelText(QFileDialog.Reject, "取消")
75+
self.setFileMode(QFileDialog.FileMode.ExistingFile)
76+
self.setLabelText(QFileDialog.DialogLabel.FileName, "图标")
77+
self.setLabelText(QFileDialog.DialogLabel.FileType, "图标文件")
78+
self.setLabelText(QFileDialog.DialogLabel.Accept, "打开")
79+
self.setLabelText(QFileDialog.DialogLabel.Reject, "取消")
7980

8081

8182
class AddDataDlg(QFileDialog):
@@ -121,8 +122,8 @@ def _setup(self) -> None:
121122
"""
122123

123124
self.setWindowTitle("关于")
124-
self.setStandardButtons(QMessageBox.Ok)
125-
self.setTextFormat(Qt.MarkdownText)
125+
self.setStandardButtons(QMessageBox.StandardButton.Ok)
126+
self.setTextFormat(Qt.TextFormat.MarkdownText)
126127
self.setText(self.about_text)
127128
self.setIconPixmap(QPixmap(":/Icons/Py2exe-GUI_icon_72px"))
128129

@@ -133,10 +134,10 @@ def about_text(self) -> str:
133134
:return: 关于信息
134135
"""
135136

136-
# 因使用qrc系统,所以使用Qt风格读取文本文件
137+
# 因使用qrc/rcc系统,所以使用Qt风格读取文本文件
137138
about_file = QFile(":/Texts/About_Text")
138139
about_file.open(QIODevice.ReadOnly | QIODevice.Text) # type: ignore
139-
about_text = bytes(about_file.readAll()).decode("utf-8")
140+
about_text = str(about_file.readAll(), encoding="utf-8") # type: ignore
140141
about_file.close()
141142

142143
if about_text:
@@ -217,10 +218,10 @@ def handle_multifunction(self) -> None:
217218
self.close()
218219
elif btn_text == "打开输出位置":
219220
dist_path = self.parent().packaging_task.script_path.parent / "dist"
220-
if self.parent().running_platform == "Windows":
221+
if self.parent().running_platform == PLATFORM.windows:
221222
import os # fmt: skip
222223
os.startfile(dist_path) # type: ignore
223-
elif self.parent().running_platform == "Linux":
224+
elif self.parent().running_platform == PLATFORM.linux:
224225
subprocess.call(["xdg-open", dist_path])
225-
elif self.parent().running_platform == "macOS":
226+
elif self.parent().running_platform == PLATFORM.macos:
226227
subprocess.call(["open", dist_path])

0 commit comments

Comments
 (0)