| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- """
- 主程序入口
- """
- import sys
- import os
- import time
- import logging
- # 修复 macOS 上 Qt 库冲突问题
- # 注意:由于 macOS SIP 限制,DYLD_LIBRARY_PATH 必须在启动前设置
- # 建议使用 run_mac.sh 脚本启动程序
- if sys.platform == 'darwin':
- # 设置 Qt 插件路径(如果环境变量未设置)
- if 'QT_PLUGIN_PATH' not in os.environ:
- try:
- import PyQt5
- pyqt5_dir = os.path.dirname(PyQt5.__file__)
- plugin_path = os.path.join(pyqt5_dir, 'Qt5', 'plugins')
- if os.path.exists(plugin_path):
- os.environ['QT_PLUGIN_PATH'] = plugin_path
- except ImportError:
- pass
-
- # 禁用 Anaconda 的 Qt 库加载警告
- os.environ.setdefault('QT_MAC_DISABLE_OSX_KEYS', '1')
- from PyQt5.QtWidgets import QApplication, QDialog
- from PyQt5.QtGui import QIcon
- from PyQt5.QtCore import QCoreApplication
- from ui.main_window import MainWindow
- from ui.login_dialog import LoginDialog
- from database import Database, get_base_path
- # 配置启动日志
- logging.basicConfig(
- level=logging.INFO,
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
- )
- startup_logger = logging.getLogger('startup')
- def get_icon_path():
- """获取图标文件路径"""
- base_path = get_base_path()
- icon_path = os.path.join(base_path, "logo.ico")
- # 如果图标文件不存在,尝试查找其他位置
- if not os.path.exists(icon_path):
- # 开发环境可能在项目根目录
- dev_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logo.ico")
- if os.path.exists(dev_path):
- icon_path = dev_path
- # 尝试从exe文件路径获取
- if not os.path.exists(icon_path):
- exe_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else None
- if exe_dir:
- exe_icon_path = os.path.join(exe_dir, "logo.ico")
- if os.path.exists(exe_icon_path):
- icon_path = exe_icon_path
- return icon_path if os.path.exists(icon_path) else None
- def main():
- """主函数"""
- start_time = time.time()
- startup_logger.info("=" * 60)
- startup_logger.info("🚀 程序启动开始")
- startup_logger.info("=" * 60)
-
- step_start = time.time()
- startup_logger.info("📱 步骤1: 创建 QApplication...")
-
- # macOS 上确保使用正确的 Qt 插件路径
- if sys.platform == 'darwin':
- plugin_path = os.environ.get('QT_PLUGIN_PATH')
- if plugin_path and os.path.exists(plugin_path):
- QCoreApplication.setLibraryPaths([plugin_path])
- startup_logger.info(f" 📍 使用 Qt 插件路径: {plugin_path}")
-
- app = QApplication(sys.argv)
- startup_logger.info(f" ✅ QApplication 创建完成 (耗时: {time.time() - step_start:.3f}秒)")
-
- # 优先设置应用程序图标(必须在创建任何窗口之前设置)
- # 这对Windows任务栏图标至关重要
- step_start = time.time()
- startup_logger.info("🎨 步骤2: 设置应用程序图标...")
- icon_path = get_icon_path()
- if icon_path:
- icon = QIcon(icon_path)
- app.setWindowIcon(icon)
- # 设置应用程序ID(Windows 7+任务栏图标识别)
- if sys.platform == 'win32':
- try:
- import ctypes
- # 设置应用程序用户模型ID,帮助Windows正确识别程序图标
- app_id = '地磅称重打印软件.1.0'
- ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
- except Exception:
- pass
- startup_logger.info(f" ✅ 图标设置完成 (耗时: {time.time() - step_start:.3f}秒)")
- else:
- startup_logger.info(f" ⚠️ 未找到图标文件 (耗时: {time.time() - step_start:.3f}秒)")
-
- # 设置应用程序样式
- step_start = time.time()
- startup_logger.info("🎨 步骤3: 设置应用程序样式...")
- app.setStyle('Fusion')
- startup_logger.info(f" ✅ 样式设置完成 (耗时: {time.time() - step_start:.3f}秒)")
-
- step_start = time.time()
- startup_logger.info("💾 步骤4: 初始化数据库连接...")
- db = Database()
- startup_logger.info(f" ✅ 数据库初始化完成 (耗时: {time.time() - step_start:.3f}秒)")
-
- step_start = time.time()
- startup_logger.info("🔐 步骤5: 创建登录对话框...")
- login_dialog = LoginDialog(db)
- startup_logger.info(f" ✅ 登录对话框创建完成 (耗时: {time.time() - step_start:.3f}秒)")
-
- step_start = time.time()
- startup_logger.info("👤 步骤6: 显示登录对话框,等待用户登录...")
- login_result = login_dialog.exec_()
- startup_logger.info(f" ✅ 登录对话框关闭 (耗时: {time.time() - step_start:.3f}秒)")
-
- if login_result == QDialog.Accepted and login_dialog.logged_in_user:
- step_start = time.time()
- startup_logger.info("🖥️ 步骤7: 创建主窗口...")
- window = MainWindow(db=db, current_user=login_dialog.logged_in_user)
- startup_logger.info(f" ✅ 主窗口创建完成 (耗时: {time.time() - step_start:.3f}秒)")
-
- step_start = time.time()
- startup_logger.info("📺 步骤8: 显示主窗口...")
- window.showMaximized() # 登录后自动最大化主界面
- startup_logger.info(f" ✅ 主窗口显示完成 (耗时: {time.time() - step_start:.3f}秒)")
-
- total_time = time.time() - start_time
- startup_logger.info("=" * 60)
- startup_logger.info(f"🎉 程序启动完成!总耗时: {total_time:.3f}秒")
- startup_logger.info("=" * 60)
- sys.exit(app.exec_())
- else:
- total_time = time.time() - start_time
- startup_logger.info("=" * 60)
- startup_logger.info(f"❌ 用户取消登录,程序退出。总耗时: {total_time:.3f}秒")
- startup_logger.info("=" * 60)
- sys.exit(0)
- if __name__ == '__main__':
- main()
|