2026-04-20 09:41:24 +08:00
|
|
|
|
"""资源与全局样式工具。
|
|
|
|
|
|
|
|
|
|
|
|
Step 0 重构:将原先散落在 pqAutomationApp.py 顶部的
|
|
|
|
|
|
get_resource_path / load_icon / backgroud_style_set 三个辅助函数
|
|
|
|
|
|
迁移到独立模块,保持行为完全一致。
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
import ttkbootstrap as ttk
|
|
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 项目根目录(app/ 的上一级)。开发环境下用它作为资源查找基准,
|
|
|
|
|
|
# 从而兼容从项目根目录启动的 pqAutomationApp.py。
|
|
|
|
|
|
_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_resource_path(relative_path):
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取资源文件的绝对路径(兼容开发环境和打包后)
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
relative_path: 相对路径,如 "assets/cie.png"
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
str: 资源文件的绝对路径
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# PyInstaller 打包后的临时文件夹路径
|
|
|
|
|
|
base_path = sys._MEIPASS
|
|
|
|
|
|
except AttributeError:
|
|
|
|
|
|
# 开发环境:使用项目根目录
|
|
|
|
|
|
base_path = _PROJECT_ROOT
|
|
|
|
|
|
|
|
|
|
|
|
return os.path.join(base_path, relative_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_icon(png_path):
|
|
|
|
|
|
"""加载并调整图标大小为 24x24(原注释写 64x64,实际 resize 为 24x24,保持原行为)"""
|
|
|
|
|
|
img = Image.open(get_resource_path(png_path))
|
|
|
|
|
|
img = img.resize((24, 24), Image.LANCZOS)
|
|
|
|
|
|
return ImageTk.PhotoImage(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def backgroud_style_set():
|
2026-05-28 10:20:17 +08:00
|
|
|
|
style = ttk.Style() # noqa: F841 - 保持原副作用:确保 Style 实例化
|
|
|
|
|
|
# 现代化样式集中注册(Card / ConfigHeader / Toolbar / StatusBar / Sidebar 等)
|
|
|
|
|
|
from app.views.modern_styles import apply_modern_styles
|
|
|
|
|
|
apply_modern_styles()
|