Skip to content

Commit 4f43837

Browse files
committed
Add platform_constants
添加运行平台相关的常量,使相关接口更统一;
1 parent fc89196 commit 4f43837

4 files changed

Lines changed: 37 additions & 23 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .app_constants import *
22
from .packaging_constants import *
3-
# from .type_constants import *
3+
from .platform_constants import *
4+
from .type_constants import *
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
4+
class PLATFORM:
5+
"""
6+
运行平台相关的常量 \n
7+
"""
8+
9+
windows = "Windows"
10+
linux = "Linux"
11+
macos = "macOS"
12+
others = "others"
13+
14+
15+
def get_platform() -> str:
16+
"""
17+
辅助函数,用于获取当前运行的平台 \n
18+
:return: platform
19+
"""
20+
21+
if sys.platform.startswith("win32"):
22+
return PLATFORM.windows
23+
elif sys.platform.startswith("linux"):
24+
return PLATFORM.linux
25+
elif sys.platform.startswith("darwin"):
26+
return PLATFORM.macos
27+
else:
28+
return PLATFORM.others

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717

1818
from ..Constants.packaging_constants import PyinstallerArgs
19+
from ..Constants.platform_constants import PLATFORM
1920
from .arguments_browser import ArgumentsBrowser
2021
from .dialog_widgets import IconFileDlg, ScriptFileDlg
2122

@@ -52,14 +53,14 @@ def __init__(self, parent: QMainWindow) -> None:
5253
self.fd_group = QButtonGroup()
5354

5455
# 应用图标(仅 Windows 与 macOS)
55-
if self.parent().running_platform in ("Windows", "macOS"):
56+
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
5657
self.icon_path_label = QLabel()
5758
self.icon_file_dlg = IconFileDlg()
5859
self.icon_browse_btn = QPushButton()
5960
self.icon_path_le = QLineEdit()
6061

6162
# 是否为stdio启用终端(仅 Windows 与 macOS)
62-
if self.parent().running_platform in ("Windows", "macOS"):
63+
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
6364
self.console_checkbox = QCheckBox()
6465

6566
# 预览生成的PyInstaller打包指令
@@ -92,7 +93,7 @@ def setup_ui(self) -> None:
9293
self.fd_group.addButton(self.one_dir_btn, 0)
9394
self.fd_group.addButton(self.one_file_btn, 1)
9495

95-
if self.parent().running_platform in ("Windows", "macOS"):
96+
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
9697
self.icon_path_label.setText("应用图标:")
9798
self.icon_path_le.setReadOnly(True)
9899
self.icon_path_le.setPlaceholderText("图标文件路径")
@@ -182,7 +183,7 @@ def run_packaging() -> None:
182183
self.fd_group.idClicked.connect(one_fd_selected) # type: ignore
183184
self.run_packaging_btn.clicked.connect(run_packaging) # type: ignore
184185

185-
if self.parent().running_platform in ("Windows", "macOS"):
186+
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
186187
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open) # type: ignore
187188
self.icon_file_dlg.fileSelected.connect(icon_file_selected) # type: ignore
188189
self.console_checkbox.toggled.connect(console_selected) # type: ignore
@@ -289,7 +290,7 @@ def _set_layout(self) -> None:
289290
main_layout.addLayout(fd_layout)
290291
main_layout.addSpacing(10)
291292

292-
if self.parent().running_platform in ("Windows", "macOS"):
293+
if self.parent().running_platform in (PLATFORM.windows, PLATFORM.macos):
293294
main_layout.addWidget(self.console_checkbox)
294295
main_layout.addSpacing(10)
295296

src/py2exe_gui/__main__.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,12 @@
33
from PySide6 import QtGui
44
from PySide6.QtWidgets import QApplication
55

6+
from .Constants.platform_constants import get_platform
67
from .Core import Packaging, PackagingTask
78
from .Resources.compiled_resources import *
89
from .Widgets import MainWindow, SubProcessDlg
910

1011

11-
# TODO 将此辅助函数移至他处、将返回值保存到常量中而非普通字符串
12-
def get_platform() -> str:
13-
"""
14-
辅助函数,用于获取当前运行的平台 \n
15-
:return: platform
16-
"""
17-
18-
if sys.platform.startswith("win32"):
19-
return "Windows"
20-
elif sys.platform.startswith("linux"):
21-
return "Linux"
22-
elif sys.platform.startswith("darwin"):
23-
return "macOS"
24-
else:
25-
return "others"
26-
27-
2812
class MainApp(MainWindow):
2913
"""
3014
应用主程序 \n

0 commit comments

Comments
 (0)