@@ -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
0 commit comments