Files
hk4e-gm-clone/ui/theme.py
T

125 lines
5.2 KiB
Python

"""深色主题:配色、字体、ttk 样式。"""
from __future__ import annotations
import tkinter as tk
from tkinter import font as tkfont
from tkinter import ttk
C = {
"bg": "#16181E",
"sidebar": "#1B1E25",
"sidebar_hi": "#262A34",
"card": "#20242C",
"card2": "#262B35",
"field": "#2B303B",
"border": "#333A47",
"border2": "#414A5A",
"text": "#E9ECF2",
"dim": "#98A2B3",
"faint": "#6B7484",
"accent": "#7C6BF0",
"accent_hi": "#8F80F5",
"accent_dim": "#3A3358",
"blue": "#3D7BF7",
"ok": "#43C08A",
"warn": "#E0B04A",
"danger": "#E05C6E",
"ink": "#FFFFFF",
}
FONT_CN = "Microsoft YaHei UI"
FONT_MONO = "Consolas"
F_TITLE = (FONT_CN, 15, "bold")
F_H2 = (FONT_CN, 11, "bold")
F_BODY = (FONT_CN, 9)
F_SMALL = (FONT_CN, 8)
F_MONO = (FONT_MONO, 9)
F_MONO_S = (FONT_MONO, 8)
F_NUM = (FONT_CN, 20, "bold")
def install(root: tk.Misc) -> ttk.Style:
style = ttk.Style(root)
try:
style.theme_use("clam")
except Exception:
pass
style.configure(".", background=C["bg"], foreground=C["text"], font=F_BODY,
bordercolor=C["border"], focuscolor=C["accent"])
style.configure("TFrame", background=C["bg"])
style.configure("Card.TFrame", background=C["card"])
style.configure("TLabel", background=C["bg"], foreground=C["text"], font=F_BODY)
style.configure("Card.TLabel", background=C["card"], foreground=C["text"], font=F_BODY)
style.configure("Dim.TLabel", background=C["card"], foreground=C["dim"], font=F_SMALL)
style.configure("H2.TLabel", background=C["card"], foreground=C["text"], font=F_H2)
style.configure("TEntry", fieldbackground=C["field"], background=C["field"],
foreground=C["text"], insertcolor=C["text"], bordercolor=C["border"],
lightcolor=C["border"], darkcolor=C["border"], padding=5)
style.map("TEntry", bordercolor=[("focus", C["accent"])])
style.configure("TCombobox", fieldbackground=C["field"], background=C["field"],
foreground=C["text"], arrowcolor=C["dim"], bordercolor=C["border"],
lightcolor=C["border"], darkcolor=C["border"], padding=4)
style.map("TCombobox",
fieldbackground=[("readonly", C["field"])],
foreground=[("readonly", C["text"])],
bordercolor=[("focus", C["accent"])])
root.option_add("*TCombobox*Listbox.background", C["card2"])
root.option_add("*TCombobox*Listbox.foreground", C["text"])
root.option_add("*TCombobox*Listbox.selectBackground", C["accent"])
root.option_add("*TCombobox*Listbox.font", F_BODY)
style.configure("Treeview", background=C["card"], fieldbackground=C["card"],
foreground=C["text"], rowheight=26, borderwidth=0, font=F_BODY)
style.configure("Treeview.Heading", background=C["card2"], foreground=C["dim"],
relief="flat", font=F_SMALL, padding=(6, 4))
style.map("Treeview.Heading", background=[("active", C["border"])])
style.map("Treeview", background=[("selected", C["accent_dim"])],
foreground=[("selected", C["text"])])
style.configure("TScrollbar", background=C["card2"], troughcolor=C["bg"],
bordercolor=C["bg"], arrowcolor=C["dim"], gripcount=0,
darkcolor=C["card2"], lightcolor=C["card"])
style.configure("Vertical.TScrollbar", background=C["card2"], troughcolor=C["bg"],
bordercolor=C["bg"], arrowcolor=C["dim"], gripcount=0,
darkcolor=C["card2"], lightcolor=C["card"], arrowsize=12)
style.map("Vertical.TScrollbar", background=[("active", C["border2"])])
style.configure("TCheckbutton", background=C["card"], foreground=C["text"], font=F_BODY)
style.map("TCheckbutton", background=[("active", C["card"])])
style.configure("TSeparator", background=C["border"])
return style
def mk_button(parent: tk.Misc, text: str, command=None, kind: str = "ghost",
width: int = 0, font=F_BODY) -> tk.Button:
colors = {
"primary": (C["accent"], C["ink"], C["accent_hi"]),
"danger": (C["danger"], "#FFFFFF", "#F0707F"),
"ok": (C["ok"], "#08120D", "#57D79F"),
"ghost": (C["field"], C["text"], C["card2"]),
"flat": (C["card"], C["dim"], C["field"]),
}
bg, fg, hover = colors.get(kind, colors["ghost"])
btn = tk.Button(parent, text=text, command=command, bg=bg, fg=fg,
activebackground=hover, activeforeground=fg,
relief="flat", bd=0, font=font, cursor="hand2",
padx=14, pady=6, highlightthickness=0)
if width:
btn.configure(width=width)
btn.bind("<Enter>", lambda e: btn.configure(bg=hover))
btn.bind("<Leave>", lambda e: btn.configure(bg=bg))
return btn
def mk_entry(parent: tk.Misc, textvariable=None, width: int = 18, show: str = "") -> tk.Entry:
e = tk.Entry(parent, textvariable=textvariable, width=width, bg=C["field"], fg=C["text"],
insertbackground=C["text"], relief="flat", bd=0, font=F_BODY,
highlightthickness=1, highlightbackground=C["border"],
highlightcolor=C["accent"], disabledbackground=C["card2"],
disabledforeground=C["faint"])
if show:
e.configure(show=show)
return e