1515 QWidget ,
1616)
1717
18+ from ..Constants .packaging_constants import *
1819from .arguments_browser import ArgumentsBrowser
1920from .dialog_widgets import IconFileDlg , ScriptFileDlg
2021
21- # TODO 使用字适当数据结构管理选项列表
22-
2322
2423class 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 )
0 commit comments