99 lines
3.1 KiB
Python
99 lines
3.1 KiB
Python
# 统计数据页
|
|
|
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
class Page2:
|
|
def __init__(self, root):
|
|
self.root = root
|
|
|
|
# 创建主框架
|
|
main_frame = ttk.Frame(root, padding="20")
|
|
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
|
main_frame.columnconfigure(0, weight=1)
|
|
main_frame.rowconfigure(1, weight=1)
|
|
|
|
# 标题
|
|
title_label = ttk.Label(main_frame, text="当前数据统计", font=("Arial", 16, "bold"))
|
|
title_label.grid(row=0, column=0, columnspan=4, pady=(0, 20))
|
|
|
|
# === 关键指标区域 ===
|
|
self.kpi_data = []
|
|
|
|
# KPI 容器
|
|
self.kpi_container = ttk.Frame(main_frame)
|
|
self.kpi_container.grid(row=1, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
|
for i in range(4):
|
|
self.kpi_container.columnconfigure(i, weight=1)
|
|
|
|
self.value_labels = []
|
|
self.create_dashboard_units_4col()
|
|
|
|
def create_kpi_unit(self, parent, name, value, row, col):
|
|
"""创建单个单元"""
|
|
unit_frame = ttk.Frame(parent, relief="solid", borderwidth=1, padding=10)
|
|
unit_frame.grid(row=row, column=col, padx=8, pady=8, sticky="nsew")
|
|
|
|
# 设置行权重,使内容垂直居中
|
|
parent.rowconfigure(row, weight=1)
|
|
|
|
if name and value:
|
|
name_label = ttk.Label(unit_frame, text=f"{name}:", font=("Arial", 11))
|
|
name_label.pack(anchor="w")
|
|
value_label = ttk.Label(unit_frame, text=value, font=("Arial", 11, "bold"))
|
|
value_label.pack(anchor="e")
|
|
return value_label
|
|
else:
|
|
# 如果 name 或 value 为空,显示占位符或空白
|
|
ttk.Label(unit_frame, text="—", foreground="gray").pack()
|
|
return None
|
|
|
|
def create_dashboard_units_4col(self):
|
|
"""创建4列布局的单元"""
|
|
rows = (len(self.kpi_data) + 3) // 4 # 向上取整
|
|
|
|
for r in range(rows):
|
|
self.kpi_container.rowconfigure(r, weight=1)
|
|
|
|
for idx, item in enumerate(self.kpi_data):
|
|
row_idx = idx // 4
|
|
col_idx = idx % 4
|
|
|
|
if item and len(item) >= 2:
|
|
name, value = item[0], item[1]
|
|
else:
|
|
name, value = None, None
|
|
value_label = self.create_kpi_unit(self.kpi_container, name, value, row_idx, col_idx)
|
|
self.value_labels.clear()
|
|
self.value_labels.append(value_label)
|
|
|
|
def update_data(self, new_values):
|
|
self.kpi_data = new_values
|
|
self.create_dashboard_units_4col()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import backend
|
|
import random
|
|
# 实例化后端
|
|
c_backend = backend.Backend()
|
|
|
|
# 创建 GUI
|
|
root = tk.Tk()
|
|
root.title("统计面板")
|
|
root.geometry("800x600")
|
|
app = Page2(root)
|
|
|
|
def on_refresh():
|
|
s = c_backend.get_statistics()
|
|
for v in s:
|
|
if v is not None and len(v) >= 2:
|
|
v[1] = str(random.randint(1,100))
|
|
app.update_data(s)
|
|
|
|
# 刷新按钮
|
|
refresh_btn = ttk.Button(root, text="刷新数据", command=on_refresh)
|
|
refresh_btn.grid(row=2, column=0, pady=10)
|
|
|
|
root.mainloop()
|