#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Windows任务栏图标修复模块 解决PyQt5应用在任务栏显示Python图标的问题 """ import os import sys from PyQt5.QtGui import QIcon, QPixmap from PyQt5.QtCore import QSize def set_windows_app_id(app_id="ShuidropAI.CustomerService.1.0"): """设置Windows应用程序用户模型ID""" try: import ctypes ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id) print(f"[INFO] Windows应用ID已设置: {app_id}") return True except Exception as e: print(f"[WARNING] 设置Windows应用ID失败: {e}") return False def create_multi_size_icon(base_path="static"): """创建包含多种尺寸的QIcon对象""" try: icon = QIcon() # 我们有的图标文件 available_files = [ ("ai_assistant_icon_16.png", 16), ("ai_assistant_icon_32.png", 32), ("ai_assistant_icon_64.png", 64), ("ai_assistant_icon_128.png", 128) ] icon_loaded = False for filename, size in available_files: icon_path = os.path.join(base_path, filename) if os.path.exists(icon_path): icon.addFile(icon_path, QSize(size, size)) print(f"[INFO] 添加图标 {size}x{size}: {filename}") icon_loaded = True # 如果没有加载任何图标,尝试加载单个文件 if not icon_loaded: fallback_path = os.path.join(base_path, "ai_assistant_icon_32.png") if os.path.exists(fallback_path): icon = QIcon(fallback_path) print(f"[INFO] 使用备用图标: {fallback_path}") else: print("[WARNING] 没有找到任何图标文件") return icon except Exception as e: print(f"[ERROR] 创建多尺寸图标失败: {e}") return QIcon() def setup_windows_taskbar_icon(app, window=None): """完整设置Windows任务栏图标""" print("[INFO] 开始设置Windows任务栏图标...") # 1. 设置应用程序唯一ID set_windows_app_id() # 2. 创建多尺寸图标 icon = create_multi_size_icon() # 3. 设置应用程序级别图标 app.setWindowIcon(icon) print("[INFO] 应用程序图标已设置") # 4. 如果提供了窗口,也设置窗口图标 if window: window.setWindowIcon(icon) print("[INFO] 窗口图标已设置") # 5. 强制刷新应用程序图标缓存 try: if window and hasattr(window, 'winId'): # 等待窗口完全初始化 from PyQt5.QtCore import QTimer def delayed_icon_refresh(): try: window.setWindowIcon(icon) print("[INFO] 已延迟刷新窗口图标") except Exception as e: print(f"[WARNING] 延迟图标刷新失败: {e}") # 延迟100ms再次设置图标 QTimer.singleShot(100, delayed_icon_refresh) except Exception as e: print(f"[WARNING] 图标刷新失败: {e}") print("[INFO] Windows任务栏图标设置完成") return icon if __name__ == "__main__": # 测试模块 from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel app = QApplication(sys.argv) window = QMainWindow() window.setWindowTitle("任务栏图标测试") window.setCentralWidget(QLabel("测试Windows任务栏图标")) # 设置任务栏图标 setup_windows_taskbar_icon(app, window) window.show() sys.exit(app.exec_())