style: polish dashboard app UI
This commit is contained in:
1 parent
cf2b71f831
commit
a7d6bc87b6
5 files changed
+134
-283
No files matched your search
@@ -126,6 +126,17 @@ class RoundedSurface(tk.Frame):
|
||||
width = max(2, self.winfo_width())
|
||||
height = max(2, self.winfo_height())
|
||||
self._canvas.delete("all")
|
||||
if self._outline != self._fill:
|
||||
rounded_rect(
|
||||
self._canvas,
|
||||
3,
|
||||
4,
|
||||
width - 2,
|
||||
height - 1,
|
||||
self._radius,
|
||||
fill=theme.SHADOW,
|
||||
outline=theme.SHADOW,
|
||||
)
|
||||
rounded_rect(
|
||||
self._canvas,
|
||||
1,
|
||||
@@ -165,15 +176,15 @@ class RoundedButton(tk.Canvas):
|
||||
self.kind = kind
|
||||
self._state = "normal"
|
||||
colors = {
|
||||
"primary": (theme.TEAL, "#ffffff", theme.TEAL_DARK),
|
||||
"danger": (theme.RED_SOFT, theme.RED, "#ffe5e2"),
|
||||
"amber": (theme.AMBER_SOFT, "#8a5a00", "#ffe8b8"),
|
||||
"soft": (theme.SURFACE, theme.TEXT, theme.SURFACE_HOVER),
|
||||
"ghost": (theme.SURFACE, theme.TEXT, theme.TEAL_SOFT),
|
||||
"primary": (theme.TEAL, "#ffffff", theme.TEAL_DARK, theme.TEAL),
|
||||
"danger": (theme.RED_SOFT, theme.RED, "#ffe5e2", "#f2ccd1"),
|
||||
"amber": (theme.AMBER_SOFT, "#8a5a00", "#ffe8b8", "#f0d6a0"),
|
||||
"soft": (theme.SURFACE, theme.TEXT, theme.SURFACE_HOVER, theme.BORDER),
|
||||
"ghost": (theme.SURFACE, theme.TEXT, theme.TEAL_SOFT, theme.SURFACE),
|
||||
}
|
||||
self.bg_color, self.fg_color, self.hover_color = colors.get(kind, colors["soft"])
|
||||
w = width or max(98, len(self.text) * 14 + 34)
|
||||
super().__init__(parent, width=w, height=38, bg=_parent_bg(parent), highlightthickness=0, bd=0, cursor="hand2")
|
||||
self.bg_color, self.fg_color, self.hover_color, self.outline_color = colors.get(kind, colors["soft"])
|
||||
w = width or max(104, len(self.text) * 14 + 38)
|
||||
super().__init__(parent, width=w, height=40, bg=_parent_bg(parent), highlightthickness=0, bd=0, cursor="hand2")
|
||||
self._draw(self.bg_color)
|
||||
self.bind("<Button-1>", self._click)
|
||||
self.bind("<Enter>", lambda _e: self._draw(self.hover_color))
|
||||
@@ -181,7 +192,7 @@ class RoundedButton(tk.Canvas):
|
||||
|
||||
def _draw(self, fill: str) -> None:
|
||||
self.delete("all")
|
||||
outline = theme.TEAL if self.kind == "primary" else theme.BORDER
|
||||
outline = self.outline_color
|
||||
fg = self.fg_color if self._state != "disabled" else theme.SUBTLE
|
||||
rounded_rect(self, 1, 1, int(self["width"]) - 2, int(self["height"]) - 2, theme.RADIUS_SM, fill=fill, outline=outline)
|
||||
self.create_text(int(self["width"]) // 2, int(self["height"]) // 2, text=self.text, fill=fg, font=("Microsoft YaHei UI", 12, "bold") if self.kind == "primary" else theme.FONT_UI)
|
||||
@@ -211,7 +222,7 @@ class RoundedNavButton(tk.Canvas):
|
||||
self.text = _with_icon(text, NAV_ICONS.get(text))
|
||||
self.command = command
|
||||
self.active = active
|
||||
super().__init__(parent, height=34, bg=theme.SURFACE, highlightthickness=0, bd=0, cursor="hand2")
|
||||
super().__init__(parent, height=38, bg=theme.SURFACE, highlightthickness=0, bd=0, cursor="hand2")
|
||||
self.bind("<Configure>", lambda _e: self._draw())
|
||||
self.bind("<Button-1>", lambda _e: self.command())
|
||||
self.bind("<Enter>", lambda _e: self._draw(hover=True))
|
||||
@@ -226,10 +237,10 @@ class RoundedNavButton(tk.Canvas):
|
||||
width = max(1, self.winfo_width())
|
||||
bg = theme.TEAL_SOFT if self.active else (theme.SURFACE_HOVER if hover else theme.SURFACE)
|
||||
fg = theme.TEAL_DARK if self.active else theme.TEXT
|
||||
rounded_rect(self, 1, 2, width - 2, 32, theme.RADIUS_SM, fill=bg, outline=bg)
|
||||
rounded_rect(self, 1, 3, width - 2, 35, theme.RADIUS_SM, fill=bg, outline=bg)
|
||||
if self.active:
|
||||
rounded_rect(self, 3, 8, 8, 26, 4, fill=theme.TEAL, outline=theme.TEAL)
|
||||
self.create_text(18 if self.active else 13, 17, text=self.text, anchor="w", fill=fg, font=("Microsoft YaHei UI", 11, "bold") if self.active else theme.FONT_UI)
|
||||
rounded_rect(self, 4, 10, 9, 28, 4, fill=theme.TEAL, outline=theme.TEAL)
|
||||
self.create_text(20 if self.active else 14, 19, text=self.text, anchor="w", fill=fg, font=("Microsoft YaHei UI", 11, "bold") if self.active else theme.FONT_UI)
|
||||
|
||||
|
||||
def clear(frame: tk.Misc) -> None:
|
||||
@@ -246,19 +257,19 @@ def nav_button(parent, text: str, command: Callable[[], None], *, active: bool =
|
||||
|
||||
|
||||
def pill(parent, text: str, *, fg: str = theme.TEAL, bg: str = theme.TEAL_SOFT) -> tk.Label:
|
||||
return tk.Label(parent, text=text, bg=bg, fg=fg, font=("Microsoft YaHei UI", 10, "bold"), padx=10, pady=3)
|
||||
return tk.Label(parent, text=text, bg=bg, fg=fg, font=("Microsoft YaHei UI", 10, "bold"), padx=11, pady=4)
|
||||
|
||||
|
||||
def status_pill(parent, text: str, *, color: str = theme.GREEN, bg: str = theme.GREEN_SOFT) -> tk.Label:
|
||||
return tk.Label(parent, text=text, bg=bg, fg=color, font=("Microsoft YaHei UI", 11, "bold"), padx=14, pady=5)
|
||||
return tk.Label(parent, text=text, bg=bg, fg=color, font=("Microsoft YaHei UI", 11, "bold"), padx=15, pady=6)
|
||||
|
||||
|
||||
def card(parent, title: str | None = None, *, padding: int = 14) -> tk.Frame:
|
||||
frame = rounded_surface(parent, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD)
|
||||
if title:
|
||||
head = tk.Frame(frame, bg=theme.SURFACE)
|
||||
head.pack(fill="x", padx=padding + 2, pady=(padding, 7))
|
||||
tk.Frame(head, bg=theme.TEAL, width=4, height=18).pack(side="left", padx=(0, 10))
|
||||
head.pack(fill="x", padx=padding + 4, pady=(padding + 2, 8))
|
||||
tk.Frame(head, bg=theme.TEAL, width=4, height=18).pack(side="left", padx=(0, 11))
|
||||
ttk.Label(head, text=title, style="H2.TLabel").pack(side="left")
|
||||
return frame
|
||||
|
||||
@@ -277,26 +288,25 @@ def metric(parent, title: str, value: object, hint: str = "", color: str = theme
|
||||
def stat_tile(parent, title: str, value: object, hint: str, *, color: str, soft: str, compact: bool = False) -> tk.Frame:
|
||||
box = rounded_surface(parent, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD)
|
||||
if compact:
|
||||
box.configure(height=72)
|
||||
accent = tk.Frame(box, bg=color, width=4)
|
||||
accent.pack(side="left", fill="y")
|
||||
box.configure(height=86)
|
||||
box.pack_propagate(False)
|
||||
inner = tk.Frame(box, bg=theme.SURFACE)
|
||||
inner.pack(side="left", fill="both", expand=True)
|
||||
inner.pack(fill="both", expand=True, padx=16, pady=12)
|
||||
head = tk.Frame(inner, bg=theme.SURFACE)
|
||||
head.pack(fill="x", padx=16, pady=(10, 2))
|
||||
head.pack(fill="x")
|
||||
tk.Label(head, text=title, bg=theme.SURFACE, fg=theme.MUTED, font=theme.FONT_SMALL).pack(side="left")
|
||||
tk.Label(head, text="●", bg=theme.SURFACE, fg=color, font=("Segoe UI", 10, "bold")).pack(side="right")
|
||||
tk.Label(inner, text=str(value), bg=theme.SURFACE, fg=theme.TEXT, font=("Microsoft YaHei UI", 19, "bold")).pack(anchor="w", padx=16)
|
||||
tk.Label(inner, text=hint, bg=soft, fg=color, font=("Microsoft YaHei UI", 10, "bold"), padx=11, pady=3).pack(anchor="w", padx=16, pady=(6, 10))
|
||||
tk.Label(inner, text=str(value), bg=theme.SURFACE, fg=theme.TEXT, font=("Microsoft YaHei UI", 21 if compact else 22, "bold")).pack(anchor="w", pady=(4, 0))
|
||||
tk.Label(inner, text=hint, bg=soft, fg=color, font=("Microsoft YaHei UI", 10, "bold"), padx=11, pady=3).pack(anchor="w", pady=(6, 0))
|
||||
return box
|
||||
|
||||
|
||||
def section(parent, title: str, subtitle: str = "") -> tk.Frame:
|
||||
row = tk.Frame(parent, bg=theme.BG)
|
||||
row.pack(fill="x", pady=(2, 10))
|
||||
row.pack(fill="x", pady=(2, 12))
|
||||
ttk.Label(row, text=title, style="Title.TLabel").pack(side="left")
|
||||
if subtitle:
|
||||
tk.Label(row, text=subtitle, bg=theme.TEAL_SOFT, fg=theme.TEAL_DARK, font=theme.FONT_SMALL, padx=12, pady=4).pack(side="left", padx=(12, 0))
|
||||
tk.Label(row, text=subtitle, bg=theme.TEAL_SOFT, fg=theme.TEAL_DARK, font=theme.FONT_SMALL, padx=12, pady=5).pack(side="left", padx=(12, 0))
|
||||
return row
|
||||
|
||||
|
||||
@@ -333,8 +343,8 @@ def text_panel(parent, title: str, lines: Iterable[str], height: int = 10) -> tk
|
||||
|
||||
def log_panel(parent, title: str, lines: Iterable[str], height: int = 24) -> tuple[tk.Frame, tk.Text]:
|
||||
box = card(parent, title)
|
||||
wrap = tk.Frame(box, bg=theme.SURFACE_ALT)
|
||||
wrap.pack(fill="both", expand=True, padx=11, pady=(4, 11))
|
||||
wrap = tk.Frame(box, bg=theme.SURFACE_ALT, highlightbackground=theme.BORDER, highlightthickness=1)
|
||||
wrap.pack(fill="both", expand=True, padx=13, pady=(4, 13))
|
||||
wrap.grid_rowconfigure(0, weight=1)
|
||||
wrap.grid_columnconfigure(0, weight=1)
|
||||
text = tk.Text(
|
||||
@@ -377,18 +387,20 @@ def log_panel(parent, title: str, lines: Iterable[str], height: int = 24) -> tup
|
||||
def table_panel(parent, title: str, columns: Sequence[tuple[str, str, int]], rows: Sequence[dict], height: int = 8) -> tk.Frame:
|
||||
box = card(parent, title)
|
||||
table_wrap = tk.Frame(box, bg=theme.SURFACE)
|
||||
table_wrap.pack(fill="both", expand=True, padx=14, pady=(4, 14))
|
||||
table_wrap.pack(fill="both", expand=True, padx=16, pady=(4, 16))
|
||||
table_wrap.grid_rowconfigure(0, weight=1)
|
||||
table_wrap.grid_columnconfigure(0, weight=1)
|
||||
tree = ttk.Treeview(table_wrap, columns=[c[0] for c in columns], show="headings", height=height)
|
||||
tree.tag_configure("odd", background=theme.SURFACE)
|
||||
tree.tag_configure("even", background=theme.SURFACE_ALT)
|
||||
ybar = ttk.Scrollbar(table_wrap, orient="vertical", command=tree.yview)
|
||||
xbar = ttk.Scrollbar(table_wrap, orient="horizontal", command=tree.xview)
|
||||
tree.configure(yscrollcommand=ybar.set, xscrollcommand=xbar.set)
|
||||
for key, label, width in columns:
|
||||
tree.heading(key, text=label)
|
||||
tree.column(key, width=width, minwidth=min(width, 90), anchor="w", stretch=False)
|
||||
for row in rows:
|
||||
tree.insert("", "end", values=[str(row.get(key, "-")) for key, _, _ in columns])
|
||||
for idx, row in enumerate(rows):
|
||||
tree.insert("", "end", values=[str(row.get(key, "-")) for key, _, _ in columns], tags=("even" if idx % 2 else "odd",))
|
||||
if not rows:
|
||||
tree.insert("", "end", values=["暂无数据" if idx == 0 else "" for idx, _c in enumerate(columns)])
|
||||
tree.grid(row=0, column=0, sticky="nsew")
|
||||
@@ -417,7 +429,7 @@ def mini_table(parent, title: str, rows: Sequence[tuple[str, str, str]], *, colo
|
||||
|
||||
def field_shell(parent, label: str) -> tk.Frame:
|
||||
row = rounded_surface(parent, fill=theme.SURFACE_ALT, outline=theme.BORDER, radius=theme.RADIUS_SM)
|
||||
tk.Label(row, text=label, bg=theme.SURFACE_ALT, fg=theme.MUTED, font=theme.FONT_SMALL).pack(anchor="w", padx=9, pady=(7, 1))
|
||||
tk.Label(row, text=label, bg=theme.SURFACE_ALT, fg=theme.MUTED, font=theme.FONT_SMALL).pack(anchor="w", padx=11, pady=(8, 2))
|
||||
return row
|
||||
|
||||
|
||||
@@ -493,8 +505,8 @@ def switch_row(parent, label: str, variable: tk.Variable | None = None, value: b
|
||||
bd=0,
|
||||
anchor="w",
|
||||
justify="left",
|
||||
font=theme.FONT_SMALL,
|
||||
).pack(fill="x", padx=9, pady=6)
|
||||
font=theme.FONT_UI,
|
||||
).pack(fill="x", padx=10, pady=8)
|
||||
return row
|
||||
|
||||
|
||||
@@ -513,7 +525,7 @@ def toolbar(parent, actions: Sequence[tuple[str, Callable[[], None] | None, str]
|
||||
def preview_panel(parent, image_path: Path | None, title: str = "截图预览", max_size: tuple[int, int] = (360, 560)) -> tk.Frame:
|
||||
box = card(parent, title)
|
||||
body = tk.Frame(box, bg=theme.SURFACE_ALT, highlightbackground=theme.BORDER, highlightthickness=1)
|
||||
body.pack(fill="both", expand=True, padx=14, pady=(4, 14))
|
||||
body.pack(fill="both", expand=True, padx=16, pady=(4, 16))
|
||||
if image_path and image_path.exists():
|
||||
try:
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
Reference in new issue
Block a user