Update
This commit is contained in:
91
ui/utilities/icon_utils.py
Normal file
91
ui/utilities/icon_utils.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
图标工具模块
|
||||
负责图标路径获取和窗口图标设置
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def get_icons_dir() -> str:
|
||||
"""获取 icons 目录的绝对路径
|
||||
|
||||
Returns:
|
||||
icons 目录的绝对路径
|
||||
"""
|
||||
# 获取当前文件所在目录(ui/utilities/)
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# 往上两级到项目根目录
|
||||
project_root = os.path.dirname(os.path.dirname(current_dir))
|
||||
|
||||
# 拼接 icons 文件夹
|
||||
icons_dir = os.path.join(project_root, "icons")
|
||||
|
||||
# 如果是打包后的应用,使用 sys._MEIPASS
|
||||
if getattr(sys, 'frozen', False):
|
||||
# 打包后的应用
|
||||
icons_dir = os.path.join(sys._MEIPASS, "icons")
|
||||
|
||||
return icons_dir
|
||||
|
||||
|
||||
def get_icon_path(icon_name: str = "NexusLauncher.ico") -> str:
|
||||
"""获取图标文件的绝对路径
|
||||
|
||||
Args:
|
||||
icon_name: 图标文件名,默认为 "NexusLauncher.ico"
|
||||
|
||||
Returns:
|
||||
图标文件的绝对路径
|
||||
"""
|
||||
icons_dir = get_icons_dir()
|
||||
icon_path = os.path.join(icons_dir, icon_name)
|
||||
return icon_path
|
||||
|
||||
|
||||
def set_window_icon(window, icon_name: str = "NexusLauncher.ico"):
|
||||
"""为窗口设置图标
|
||||
|
||||
Args:
|
||||
window: Tkinter 窗口对象
|
||||
icon_name: 图标文件名,默认为 "NexusLauncher.ico"
|
||||
"""
|
||||
icon_path = get_icon_path(icon_name)
|
||||
|
||||
if os.path.exists(icon_path):
|
||||
try:
|
||||
window.iconbitmap(icon_path)
|
||||
window.wm_iconbitmap(icon_path)
|
||||
except Exception as e:
|
||||
print(f"Failed to set window icon: {e}")
|
||||
else:
|
||||
print(f"Icon file not found: {icon_path}")
|
||||
|
||||
|
||||
def setup_dialog_icon(dialog, icon_name: str = "NexusLauncher.ico"):
|
||||
"""为对话框设置图标(包含延迟设置以确保生效)
|
||||
|
||||
Args:
|
||||
dialog: 对话框窗口对象
|
||||
icon_name: 图标文件名,默认为 "NexusLauncher.ico"
|
||||
"""
|
||||
icon_path = get_icon_path(icon_name)
|
||||
|
||||
if not os.path.exists(icon_path):
|
||||
return
|
||||
|
||||
def set_icon():
|
||||
try:
|
||||
dialog.iconbitmap(icon_path)
|
||||
dialog.wm_iconbitmap(icon_path)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 立即设置
|
||||
set_icon()
|
||||
|
||||
# 延迟设置(确保生效)
|
||||
dialog.after(50, set_icon)
|
||||
dialog.after(200, set_icon)
|
||||
Reference in New Issue
Block a user