屏模组添加colorinfo设置、修改配置项UI样式

This commit is contained in:
xinzhu.yin
2026-05-28 10:20:17 +08:00
parent c63b9ef615
commit f8f2d471e5
8 changed files with 752 additions and 155 deletions

View File

@@ -1,104 +1,149 @@
import ttkbootstrap as ttk
"""现代化的可折叠面板(取代 v1 的图标按钮版本)。
升级要点(保留 ``add(child, title=...)`` 旧签名兼容):
- header 整条可点击切换展开/收起;
- 使用 Unicode chevron (▾/▸),无需 PNG 资源;
- 新增 ``preview_textvariable``:折叠时在 header 显示当前配置摘要;
- 新增 ``header_actions``:在 header 右侧注入自定义按钮(如顶部工具条)。
"""
import tkinter
from tkinter import ttk
from pathlib import Path
from ttkbootstrap import Style
import sys
import os
def get_resource_path(relative_path):
"""
获取资源文件的绝对路径(兼容开发环境和打包后)
Args:
relative_path: 相对路径,如 "assets/icons8_double_up_24px.png"
Returns:
str: 资源文件的绝对路径
"""
try:
# PyInstaller 打包后的临时文件夹路径
base_path = sys._MEIPASS
except AttributeError:
# 开发环境:使用项目根目录
# 当前文件: app/views/collapsing_frame.py
# 项目根目录: app/views 的祖父目录
current_file = os.path.abspath(__file__)
views_dir = os.path.dirname(current_file)
app_dir = os.path.dirname(views_dir)
base_path = os.path.dirname(app_dir)
return os.path.join(base_path, relative_path)
class CollapsingFrame(ttk.Frame):
"""
A collapsible frame widget that opens and closes with a button click.
"""
"""A modern collapsible frame widget."""
CHEVRON_OPEN = "\u25be" # ▾
CHEVRON_CLOSED = "\u25b8" # ▸
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.columnconfigure(0, weight=1)
self.cumulative_rows = 0
p = Path(__file__).parent
self.images = [
tkinter.PhotoImage(
name="open", file=get_resource_path("assets/icons8_double_up_24px.png")
),
tkinter.PhotoImage(
name="closed",
file=get_resource_path("assets/icons8_double_right_24px.png"),
),
]
# 兼容旧代码可能引用 self.images
self.images: list = []
def add(self, child, title="", style="primary.TButton", **kwargs):
"""Add a child to the collapsible frame
# ------------------------------------------------------------------
# 公共 API
# ------------------------------------------------------------------
def add(
self,
child,
title: str = "",
style: str = "primary.TButton", # 兼容旧签名(不再使用)
preview_textvariable=None,
header_actions=None,
**kwargs,
):
"""添加一个子区段到折叠面板。
:param ttk.Frame child: the child frame to add to the widget
:param str title: the title appearing on the collapsible section header
:param str style: the ttk style to apply to the collapsible section header
:param child: 必须是一个 ttk.Frame
:param title: 标题文本;
:param preview_textvariable: 折叠时显示在 header 上的状态摘要 StringVar
:param header_actions: 回调 ``fn(actions_frame)``,可在 header 右侧添加按钮。
"""
if child.winfo_class() != "TFrame": # must be a frame
if child.winfo_class() != "TFrame":
return
style_color = style.split(".")[0]
frm = ttk.Frame(self, style=f"{style_color}.TFrame")
frm.grid(row=self.cumulative_rows, column=0, sticky="ew")
# header title
lbl = ttk.Label(frm, text=title, style=f"{style_color}.Inverse.TLabel")
if kwargs.get("textvariable"):
lbl.configure(textvariable=kwargs.get("textvariable"))
lbl.pack(side="left", fill="both", padx=10)
header = ttk.Frame(self, style="ConfigHeader.TFrame", padding=(12, 6))
header.grid(row=self.cumulative_rows, column=0, sticky="ew")
header.columnconfigure(1, weight=1)
# header toggle button
btn = ttk.Button(
frm,
image="open",
style=style,
command=lambda c=child: self._toggle_open_close(child),
# chevron + 标题
title_box = ttk.Frame(header, style="ConfigHeader.TFrame")
title_box.grid(row=0, column=0, sticky="w")
chevron = ttk.Label(
title_box, text=self.CHEVRON_OPEN, style="ConfigChevron.TLabel"
)
btn.pack(side="right")
chevron.pack(side="left", padx=(0, 8))
title_lbl = ttk.Label(title_box, text=title, style="ConfigHeader.TLabel")
if kwargs.get("textvariable"):
title_lbl.configure(textvariable=kwargs.get("textvariable"))
title_lbl.pack(side="left")
# 中:折叠状态预览
preview_lbl = None
if preview_textvariable is not None:
preview_lbl = ttk.Label(
header,
textvariable=preview_textvariable,
style="ConfigPreview.TLabel",
)
preview_lbl.grid(row=0, column=1, sticky="w", padx=(16, 8))
# 右actions如顶部工具条按钮
actions_frame = ttk.Frame(header, style="ConfigHeader.TFrame")
actions_frame.grid(row=0, column=2, sticky="e")
if callable(header_actions):
try:
header_actions(actions_frame)
except Exception:
# 注入失败不应影响整体折叠面板渲染
pass
# 整条 header 点击切换
clickable = [header, title_box, chevron, title_lbl]
if preview_lbl is not None:
clickable.append(preview_lbl)
for w in clickable:
w.bind(
"<Button-1>",
lambda _e, c=child: self._toggle_open_close(c),
)
try:
w.configure(cursor="hand2")
except tkinter.TclError:
pass
child._chevron = chevron
child._header = header
child._preview_lbl = preview_lbl
# 兼容旧代码 child.btn.invoke() / child.btn.configure(image=...)
child.btn = _HeaderToggleProxy(self, child, chevron)
# assign toggle button to child so that it's accesible when toggling (need to change image)
child.btn = btn
child.grid(row=self.cumulative_rows + 1, column=0, sticky="news")
# increment the row assignment
self.cumulative_rows += 2
# ------------------------------------------------------------------
# 内部实现
# ------------------------------------------------------------------
def _toggle_open_close(self, child):
"""
Open or close the section and change the toggle button image accordingly
:param ttk.Frame child: the child element to add or remove from grid manager
"""
if child.winfo_viewable():
child.grid_remove()
child.btn.configure(image="closed")
try:
child._chevron.configure(text=self.CHEVRON_CLOSED)
except (AttributeError, tkinter.TclError):
pass
else:
child.grid()
child.btn.configure(image="open")
try:
child._chevron.configure(text=self.CHEVRON_OPEN)
except (AttributeError, tkinter.TclError):
pass
class _HeaderToggleProxy:
"""兼容旧代码:``child.btn.invoke()`` / ``child.btn.configure(image=...)``。"""
def __init__(self, owner: "CollapsingFrame", child, chevron):
self._owner = owner
self._child = child
self._chevron = chevron
def invoke(self):
self._owner._toggle_open_close(self._child)
def configure(self, **kwargs):
image = kwargs.get("image")
if image == "closed":
self._chevron.configure(text=CollapsingFrame.CHEVRON_CLOSED)
elif image == "open":
self._chevron.configure(text=CollapsingFrame.CHEVRON_OPEN)
config = configure
# class Application(tkinter.Tk):