diff --git a/app/view/main/window.py b/app/view/main/window.py index f5d04ecf..8298c23a 100644 --- a/app/view/main/window.py +++ b/app/view/main/window.py @@ -1,10 +1,6 @@ # ================================================== # 导入库 # ================================================== -import os -import subprocess -import sys -import loguru from loguru import logger from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtGui import QIcon @@ -19,6 +15,7 @@ PRE_CLASS_RESET_INTERVAL_MS, RESIZE_TIMER_DELAY_MS, MAXIMIZE_RESTORE_DELAY_MS, + EXIT_CODE_RESTART, ) from app.tools.path_utils import get_data_path from app.tools.personalised import get_theme_icon @@ -1019,29 +1016,10 @@ def restart_app(self): self.cleanup_shortcuts() - try: - working_dir = os.getcwd() - filtered_args = [arg for arg in sys.argv if not arg.startswith("--")] - startup_info = subprocess.STARTUPINFO() - startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW - subprocess.Popen( - [sys.executable] + filtered_args, - cwd=working_dir, - creationflags=subprocess.CREATE_NEW_PROCESS_GROUP - | subprocess.DETACHED_PROCESS, - startupinfo=startup_info, - ) - except Exception as e: - logger.error(f"启动新进程失败: {e}") - return - - try: - loguru.logger.remove() - except Exception as e: - logger.error(f"日志系统关闭出错: {e}") - - QApplication.quit() - sys.exit(0) + # 使用 EXIT_CODE_RESTART 退出码来触发重启 + # main.py 中的 handle_exit() 函数会检测此退出码并执行重启逻辑 + logger.info("正在退出以触发重启流程...") + QApplication.exit(EXIT_CODE_RESTART) def close_window_secrandom(self): """关闭窗口 diff --git a/main.py b/main.py index eef4bed9..aea2f5d4 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ import sys import time import gc +import subprocess +import platform import sentry_sdk from sentry_sdk.integrations.loguru import LoguruIntegration, LoggingLevels @@ -345,10 +347,7 @@ def restart_application(program_dir): logger.info("检测到重启信号,正在重启应用程序...") filtered_args = [arg for arg in sys.argv if not arg.startswith("--")] - if getattr(sys, "frozen", False): - executable = sys.executable - else: - executable = sys.executable + executable = sys.executable if not os.path.exists(executable): logger.critical(f"重启失败:无法找到可执行文件: {executable}") @@ -356,7 +355,23 @@ def restart_application(program_dir): try: os.chdir(program_dir) - os.execl(executable, executable, *filtered_args) + + # Windows 平台使用 subprocess.Popen 启动新进程 + if platform.system() == "Windows": + startup_info = subprocess.STARTUPINFO() + startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW + subprocess.Popen( + [executable] + filtered_args, + cwd=program_dir, + creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS, + startupinfo=startup_info, + ) + logger.info("Windows 平台:新进程已启动") + os._exit(0) + else: + # Linux/Unix/macOS 平台使用 os.execl 替换当前进程 + logger.info("Linux/Unix/macOS 平台:使用 execl 重启应用程序") + os.execl(executable, executable, *filtered_args) except Exception as e: logger.exception(f"重启应用程序失败: {e}") os._exit(1)