Files
nezhacursor/main.py
huangzhenpc c58903846d refactor: migrate GUI from tkinter to PyQt5
主要改动:

1. GUI框架迁移 - 从 tkinter 完全迁移到 PyQt5 - 重写了所有界面组件和布局 - 优化了界面交互逻辑

2. 界面改进 - 使用 QMainWindow 作为主窗口 - 采用 QVBoxLayout 和 QHBoxLayout 进行布局 - 使用 QFrame 组织界面结构 - 添加了状态显示区域的滚动功能

3. 功能优化 - 改进了错误处理和消息显示 - 使用 QMessageBox 替代 tkinter messagebox - 添加了剪贴板操作功能 - 优化了状态更新逻辑

4. 性能提升 - 移除了不必要的窗口刷新 - 使用 QTimer 优化状态检查 - 简化了窗口管理逻辑

5. 代码质量 - 改进了代码组织结构 - 增强了错误处理 - 添加了详细的日志记录 - 优化了资源管理

6. 依赖管理 - 更新了 PyInstaller spec 文件 - 添加了必要的 PyQt5 依赖 - 确保打包后的程序正常运行
2025-02-12 13:33:11 +08:00

83 lines
2.2 KiB
Python

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()