重构-抽纯函数
This commit is contained in:
55
app/resources.py
Normal file
55
app/resources.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""资源与全局样式工具。
|
||||
|
||||
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():
|
||||
style = ttk.Style()
|
||||
# 移除背景色设置,使用默认背景色
|
||||
style.configure(
|
||||
"SidebarSelected.TButton",
|
||||
# anchor="w",
|
||||
padding=10,
|
||||
background="#005470",
|
||||
)
|
||||
Reference in New Issue
Block a user