#!/usr/bin/env python # -*- coding: utf-8 -*- """主题管理模块""" import tkinter as tk from tkinter import scrolledtext, ttk # 私有主题模式变量 _theme_mode = "light" # 浅色和深色主题颜色 THEMES = { "light": { "bg": "#f8f9fa", "fg": "#212529", "button_bg": "#ffffff", "button_fg": "#495057", "button_hover": "#e9ecef", "primary_bg": "#007bff", "primary_fg": "#ffffff", "secondary_bg": "#6c757d", "secondary_fg": "#ffffff", "log_bg": "#ffffff", "log_fg": "#212529", "highlight_bg": "#007bff", "highlight_fg": "#ffffff", "border": "#dee2e6", "success": "#28a745", "error": "#dc3545", "warning": "#ffc107", "info": "#17a2b8", "card_bg": "#ffffff", "shadow": "#00000010" }, "dark": { "bg": "#1a1a1a", "fg": "#e9ecef", "button_bg": "#343a40", "button_fg": "#e9ecef", "button_hover": "#495057", "primary_bg": "#0d6efd", "primary_fg": "#ffffff", "secondary_bg": "#6c757d", "secondary_fg": "#ffffff", "log_bg": "#212529", "log_fg": "#e9ecef", "highlight_bg": "#0d6efd", "highlight_fg": "#ffffff", "border": "#495057", "success": "#198754", "error": "#dc3545", "warning": "#ffc107", "info": "#0dcaf0", "card_bg": "#2d3748", "shadow": "#00000030" } } def get_theme_mode() -> str: return _theme_mode def set_theme_mode(mode: str): global _theme_mode _theme_mode = mode def create_modern_button(parent, text, command, style="primary", width=None, height=None, px_width=None, px_height=None): """创建现代化样式的按钮""" theme = THEMES[_theme_mode] if style == "primary": bg_color = "white" fg_color = theme["primary_bg"] hover_color = "#f0f8ff" border_color = theme["primary_bg"] elif style == "secondary": bg_color = theme["secondary_bg"] fg_color = theme["secondary_fg"] hover_color = theme["button_hover"] border_color = theme["secondary_bg"] else: bg_color = "white" fg_color = theme["primary_bg"] hover_color = "#f0f8ff" border_color = theme["primary_bg"] button_frame = tk.Frame(parent, bg=border_color, highlightthickness=0) button_frame.configure(relief="flat", bd=0) if px_width or px_height: try: w = px_width if px_width else button_frame.winfo_reqwidth() h = px_height if px_height else 32 button_frame.configure(width=w, height=h) button_frame.pack_propagate(False) except Exception: pass button = tk.Button( button_frame, text=text, command=command, bg=bg_color, fg=fg_color, font=("Microsoft YaHei UI", 8), relief="flat", bd=0, padx=14, pady=4, anchor="center", cursor="hand2", activebackground=hover_color, activeforeground=fg_color ) if width: button.configure(width=width) else: button.configure(width=12) if height is not None: button.configure(height=height) else: button.configure(height=1) if height: button.configure(height=height) # 悬停效果 def on_enter(e): button.configure(bg=hover_color) def on_leave(e): button.configure(bg=bg_color) button.bind("", on_enter) button.bind("", on_leave) button_frame.bind("", on_enter) button_frame.bind("", on_leave) button.pack(fill=tk.BOTH, expand=True, padx=1, pady=1) return button_frame def create_card_frame(parent, title=None): """创建卡片样式的框架""" theme = THEMES[_theme_mode] card = tk.Frame( parent, bg=theme["card_bg"], relief="flat", borderwidth=1, highlightbackground=theme["border"], highlightthickness=1 ) if title: title_label = tk.Label( card, text=title, bg=theme["card_bg"], fg=theme["fg"], font=("Microsoft YaHei UI", 10, "bold") ) title_label.pack(pady=(6, 3)) return card def apply_theme(widget, theme_mode=None): """应用主题到小部件""" if theme_mode is None: theme_mode = _theme_mode theme = THEMES[theme_mode] try: widget.configure(bg=theme["bg"], fg=theme["fg"]) except Exception: pass for child in widget.winfo_children(): if isinstance(child, tk.Button) and not isinstance(child, ttk.Button): child.configure(bg=theme["button_bg"], fg=theme["button_fg"]) elif isinstance(child, scrolledtext.ScrolledText): child.configure(bg=theme["log_bg"], fg=theme["log_fg"]) else: try: child.configure(bg=theme["bg"], fg=theme["fg"]) except Exception: pass apply_theme(child, theme_mode)