refactor(gui): 抽 log_text 颜色配置到 components/log_text_colors (1878 → 1813)

This commit is contained in:
Your Name committed 2026-05-30 16:29:27 +08:00
1 parent 29cef2c578
commit b686a4e3d5
2 files changed
+102 -70

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
"""运行日志 widget 的主题颜色配置。
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的内联段,根据当前 GUI 主题
为 log_text 配置背景/前景色 + 多种 log_xxx 标签 + 操作类型颜色标签。
依赖通过显式参数注入:
- ``text_widget`` - 待配置的 Text/ScrolledText
- ``theme_getter`` - ``() -> str`` 返回当前主题名
"""
from __future__ import annotations
import tkinter as tk
from typing import Callable
try:
from gui.components.log_color_config import get_color_config
except ImportError:
def get_color_config(theme: str = "litera"): # type: ignore[no-redef]
return {}
def configure_log_text_colors(
text_widget: tk.Text,
*,
theme_getter: Callable[[], str],
) -> None:
# 配置日志颜色标签(如果支持颜色)
try:
# 根据主题调整日志颜色(明亮主题使用深色文字/蓝色系)
light_themes = {"litera", "flatly", "journal", "minty", "cosmo"}
try:
current_theme = theme_getter()
except Exception:
current_theme = "litera"
if current_theme in light_themes:
# 明亮主题 - 使用更适合人眼观看的颜色组合
text_widget.configure(
background="#FFFFFF", foreground="#2C2C2C", insertbackground="#2C2C2C"
)
text_widget.tag_configure("log_timestamp", foreground="#8B8B8B") # 柔和的灰色
text_widget.tag_configure(
"log_info", foreground="#1E88E5"
) # 明亮的蓝色,易于阅读
text_widget.tag_configure(
"log_warning", foreground="#F57C00"
) # 橙色,警告色但不过于刺眼
text_widget.tag_configure(
"log_error", foreground="#D32F2F"
) # 深红色,醒目但不刺眼
text_widget.tag_configure(
"log_critical", foreground="#B71C1C", font=("Consolas", 10, "bold")
) # 加粗深红色
text_widget.tag_configure(
"log_performance", foreground="#388E3C"
) # 深绿色,性能指标
text_widget.tag_configure("log_module", foreground="#7B1FA2") # 紫色,模块标识
else:
# 暗色主题 - 优化对比度和可读性
text_widget.configure(
background="#1E1E1E", foreground="#E0E0E0", insertbackground="#FFFFFF"
)
text_widget.tag_configure(
"log_timestamp", foreground="#A0A0A0"
) # 浅灰色时间戳
text_widget.tag_configure("log_info", foreground="#64B5F6") # 亮蓝色,信息级
text_widget.tag_configure(
"log_warning", foreground="#FFB74D"
) # 金色警告,暗色背景下醒目
text_widget.tag_configure(
"log_error", foreground="#EF5350"
) # 亮红色,错误突出
text_widget.tag_configure(
"log_critical", foreground="#F44336", font=("Consolas", 10, "bold")
) # 加粗亮红色
text_widget.tag_configure(
"log_performance", foreground="#81C784"
) # 亮绿色,性能数据
text_widget.tag_configure(
"log_module", foreground="#BA68C8"
) # 亮紫色,模块标识
# 配置操作类型颜色标签(使用模块化配置)
try:
color_config = get_color_config(current_theme)
for op_type, colors in color_config.items():
tag_name = f"op_{op_type}"
text_widget.tag_configure(
tag_name,
foreground=colors["foreground"],
background=colors["background"],
)
except Exception:
pass # 忽略操作类型颜色配置失败
except Exception:
pass # 忽略标签配置失败
+5 -70
View File
@@ -94,6 +94,7 @@ from gui.components.reservation_tab import setup_reservation_tab
from gui.components.run_tab import setup_run_tab
from gui.components.filter_tab import setup_filter_tab
from gui.components.realtime_tab import setup_realtime_tab
from gui.components.log_text_colors import configure_log_text_colors
# 导入日志颜色配置模块
try:
@@ -1277,76 +1278,10 @@ def main():
pass
log_text.pack(fill=BOTH, expand=True, padx=10, pady=(0, 10))
# 配置日志颜色标签(如果支持颜色)
try:
# 根据主题调整日志颜色(明亮主题使用深色文字/蓝色系)
light_themes = {"litera", "flatly", "journal", "minty", "cosmo"}
try:
current_theme = str(gui_theme_var.get() or "litera")
except Exception:
current_theme = "litera"
if current_theme in light_themes:
# 明亮主题 - 使用更适合人眼观看的颜色组合
log_text.configure(
background="#FFFFFF", foreground="#2C2C2C", insertbackground="#2C2C2C"
)
log_text.tag_configure("log_timestamp", foreground="#8B8B8B") # 柔和的灰色
log_text.tag_configure(
"log_info", foreground="#1E88E5"
) # 明亮的蓝色,易于阅读
log_text.tag_configure(
"log_warning", foreground="#F57C00"
) # 橙色,警告色但不过于刺眼
log_text.tag_configure(
"log_error", foreground="#D32F2F"
) # 深红色,醒目但不刺眼
log_text.tag_configure(
"log_critical", foreground="#B71C1C", font=("Consolas", 10, "bold")
) # 加粗深红色
log_text.tag_configure(
"log_performance", foreground="#388E3C"
) # 深绿色,性能指标
log_text.tag_configure("log_module", foreground="#7B1FA2") # 紫色,模块标识
else:
# 暗色主题 - 优化对比度和可读性
log_text.configure(
background="#1E1E1E", foreground="#E0E0E0", insertbackground="#FFFFFF"
)
log_text.tag_configure(
"log_timestamp", foreground="#A0A0A0"
) # 浅灰色时间戳
log_text.tag_configure("log_info", foreground="#64B5F6") # 亮蓝色,信息级
log_text.tag_configure(
"log_warning", foreground="#FFB74D"
) # 金色警告,暗色背景下醒目
log_text.tag_configure(
"log_error", foreground="#EF5350"
) # 亮红色,错误突出
log_text.tag_configure(
"log_critical", foreground="#F44336", font=("Consolas", 10, "bold")
) # 加粗亮红色
log_text.tag_configure(
"log_performance", foreground="#81C784"
) # 亮绿色,性能数据
log_text.tag_configure(
"log_module", foreground="#BA68C8"
) # 亮紫色,模块标识
# 配置操作类型颜色标签(使用模块化配置)
try:
color_config = get_color_config(current_theme)
for op_type, colors in color_config.items():
tag_name = f"op_{op_type}"
log_text.tag_configure(
tag_name,
foreground=colors["foreground"],
background=colors["background"],
)
except Exception:
pass # 忽略操作类型颜色配置失败
except Exception:
pass # 忽略标签配置失败
configure_log_text_colors(
log_text,
theme_getter=lambda: str(gui_theme_var.get() or "litera"),
)
# 截图预览
preview_top = ttk.Frame(preview_tab)