import logging import sys import traceback import os import atexit import shutil import tempfile from pathlib import Path from PyQt5.QtWidgets import QApplication, QMessageBox, QSystemTrayIcon, QMenu from PyQt5.QtGui import QIcon from PyQt5.QtCore import Qt from gui.main_window import MainWindow def cleanup_temp(): """清理临时文件""" try: temp_dir = Path(tempfile._get_default_tempdir()) for item in temp_dir.glob('_MEI*'): try: if item.is_dir(): shutil.rmtree(str(item), ignore_errors=True) elif item.is_file(): item.unlink() except: pass except: pass def setup_logging(): """设置日志""" try: log_dir = Path.home() / ".cursor_switcher" / "logs" log_dir.mkdir(parents=True, exist_ok=True) log_file = log_dir / "switcher.log" # 只输出到文件,不输出到控制台 logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", handlers=[ logging.FileHandler(log_file, encoding="utf-8"), ] ) except Exception as e: # 不打印错误信息,只记录到日志 pass def main(): """主函数""" try: # 注册退出时的清理函数 atexit.register(cleanup_temp) setup_logging() # 检查Python版本 logging.info(f"Python版本: {sys.version}") # 检查工作目录 logging.info(f"当前工作目录: {Path.cwd()}") # 检查模块路径 logging.info("Python路径:") for p in sys.path: logging.info(f" - {p}") logging.info("正在初始化主窗口...") app = QApplication(sys.argv) # 设置应用程序ID (在设置图标之前) if sys.platform == "win32": import ctypes myappid = u'nezha.cursor.helper.v3' ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) logging.info(f"设置应用程序ID: {myappid}") # 设置应用程序图标 try: icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icon", "two.ico") if os.path.exists(icon_path): app_icon = QIcon(icon_path) if not app_icon.isNull(): app.setWindowIcon(app_icon) logging.info(f"成功设置应用程序图标: {icon_path}") else: logging.error("图标文件加载失败") else: logging.error(f"图标文件不存在: {icon_path}") except Exception as e: logging.error(f"设置应用程序图标失败: {str(e)}") window = MainWindow() window.setWindowIcon(app.windowIcon()) # 确保窗口使用相同的图标 logging.info("正在启动主窗口...") window.show() sys.exit(app.exec_()) except Exception as e: error_msg = f"程序运行出错: {str(e)}\n{traceback.format_exc()}" logging.error(error_msg) # 使用 QMessageBox 显示错误 app = QApplication(sys.argv) QMessageBox.critical(None, "错误", error_msg) sys.exit(1) if __name__ == "__main__": main()