60 lines
2.4 KiB
Python
60 lines
2.4 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import tkinter as tk
|
||
|
|
from tkinter import ttk
|
||
|
|
from resources.code_res.game_constants import (
|
||
|
|
ACCENT_GOTH, BORDER_COLOR, CELL_BG, FG_COLOR,
|
||
|
|
MUTED_FG, PANEL_BG, STAT_FIELD_KEYS, SUBTLE_BORDER
|
||
|
|
)
|
||
|
|
from resources.code_res.i18n import t
|
||
|
|
|
||
|
|
|
||
|
|
class StatsBoardWidget(tk.Frame):
|
||
|
|
def __init__(self, parent, title: str, filter_map: dict[str, int | None], fetch_callback):
|
||
|
|
super().__init__(
|
||
|
|
parent, bg=PANEL_BG, padx=16, pady=12,
|
||
|
|
highlightthickness=1, highlightbackground=BORDER_COLOR
|
||
|
|
)
|
||
|
|
self.filter_map = filter_map
|
||
|
|
self.fetch_callback = fetch_callback
|
||
|
|
|
||
|
|
header = tk.Frame(self, bg=PANEL_BG)
|
||
|
|
header.pack(fill="x", pady=(0, 8))
|
||
|
|
|
||
|
|
self.title_lbl = tk.Label(header, text=title, font=("Georgia", 12, "bold"), fg=ACCENT_GOTH, bg=PANEL_BG)
|
||
|
|
self.title_lbl.pack(side="left")
|
||
|
|
|
||
|
|
first_key = next(iter(filter_map.keys()))
|
||
|
|
self.filter_var = tk.StringVar(value=first_key)
|
||
|
|
self.filter_cb = ttk.Combobox(
|
||
|
|
header, textvariable=self.filter_var,
|
||
|
|
values=list(filter_map.keys()), state="readonly",
|
||
|
|
width=28, style="Goth.TCombobox"
|
||
|
|
)
|
||
|
|
self.filter_cb.pack(side="right")
|
||
|
|
|
||
|
|
grid = tk.Frame(self, bg=PANEL_BG)
|
||
|
|
grid.pack(fill="x")
|
||
|
|
for col in range(4):
|
||
|
|
grid.columnconfigure(col, weight=1)
|
||
|
|
|
||
|
|
self.value_labels = {}
|
||
|
|
for i, (stat_key, i18n_key) in enumerate(STAT_FIELD_KEYS):
|
||
|
|
r, c = divmod(i, 4)
|
||
|
|
cell = tk.Frame(grid, bg=CELL_BG, padx=10, pady=6, highlightthickness=1, highlightbackground=SUBTLE_BORDER)
|
||
|
|
cell.grid(row=r, column=c, padx=4, pady=3, sticky="nsew")
|
||
|
|
tk.Label(cell, text=t(i18n_key), font=("Helvetica", 9), fg=MUTED_FG, bg=CELL_BG).pack(anchor="w")
|
||
|
|
val_lbl = tk.Label(cell, text="0", font=("Helvetica", 12, "bold"), fg=FG_COLOR, bg=CELL_BG)
|
||
|
|
val_lbl.pack(anchor="w", pady=(1, 0))
|
||
|
|
self.value_labels[stat_key] = val_lbl
|
||
|
|
|
||
|
|
self.filter_cb.bind("<<ComboboxSelected>>", lambda e: self.refresh())
|
||
|
|
self.refresh()
|
||
|
|
|
||
|
|
def set_title(self, new_title: str):
|
||
|
|
self.title_lbl.configure(text=new_title)
|
||
|
|
|
||
|
|
def refresh(self):
|
||
|
|
vid = self.filter_map.get(self.filter_var.get())
|
||
|
|
data = self.fetch_callback(vid)
|
||
|
|
for k, lbl in self.value_labels.items():
|
||
|
|
lbl.configure(text=str(data.get(k, "0")))
|