import logging import sys import traceback import os import atexit import shutil import tempfile from pathlib import Path from PyQt5.QtWidgets import QApplication, QMessageBox 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) window = MainWindow() 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()