import tkinter as tk
import random
from tkinter import font

# ===================== 主窗口配置 =====================
root = tk.Tk()
root.title("东台市实验小学520班 - 随机点名系统")
root.geometry("800x550")    # 窗口大小
root.resizable(False, False) # 固定窗口
root.config(bg="#f8f9fa")   # 清新浅灰背景

# ===================== 字体配置（楷体 + 兼容） =====================
def create_font(font_size, is_bold=False):
    try:
        # 优先使用楷体（Windows系统）
        return font.Font(family="楷体", size=font_size, weight="bold" if is_bold else "normal")
    except:
        # 兜底默认字体，保证不报错
        return font.Font(size=font_size, weight="bold" if is_bold else "normal")

font_title = create_font(38, True)    # 主标题
font_name = create_font(90, True)      # 点名字体
font_btn = create_font(24)             # 按钮字体
font_corner = create_font(26)          # 左上角文字
font_tip = create_font(20)             # 提示文字

# ===================== 配色方案（校园清新风） =====================
COLOR_BG = "#f8f9fa"         # 背景色
COLOR_TITLE_BG = "#2c7cdd"   # 标题栏蓝色
COLOR_NAME = "#e63946"        # 名字红色
COLOR_BTN_START = "#28a745"   # 开始按钮绿色
COLOR_BTN_STOP = "#dc3545"    # 停止按钮红色
COLOR_BTN_RESET = "#17a2b8"   # 重置按钮蓝色
COLOR_WHITE = "#ffffff"      # 白色

# ===================== 班级名单（可自由修改） =====================
STUDENT_LIST = [
    "陈洢诺", "张子轩", "李雨桐", "王浩宇", "刘思雅",
    "周文博", "吴雨欣", "郑浩然", "冯子涵", "陈俊杰",
    "董雨菲", "徐文轩", "马一诺", "胡雨彤", "林宇泽",
    "高雨萱", "罗子豪", "宋雨桐", "谢文宇", "韩雨欣"
]

# ===================== 全局变量 =====================
is_rolling = False
roll_timer = None
show_name = "等待点名"

# ===================== 核心功能函数 =====================
def roll():
    """随机滚动名字"""
    global show_name, roll_timer
    if is_rolling:
        show_name = random.choice(STUDENT_LIST)
        name_label.config(text=show_name)
        roll_timer = root.after(80, roll)

def start_stop():
    """开始 / 停止 点名（一键切换）"""
    global is_rolling
    if not is_rolling:
        is_rolling = True
        roll()
        start_btn.config(text="停止点名", bg=COLOR_BTN_STOP)
    else:
        is_rolling = False
        if roll_timer:
            root.after_cancel(roll_timer)
        start_btn.config(text="开始点名", bg=COLOR_BTN_START)

def reset():
    """重置点名器"""
    global is_rolling, show_name, roll_timer
    is_rolling = False
    if roll_timer:
        root.after_cancel(roll_timer)
    show_name = "等待点名"
    name_label.config(text=show_name)
    start_btn.config(text="开始点名", bg=COLOR_BTN_START)

# ===================== 界面布局（美观规整） =====================
# 1. 左上角专属文字：520 陈洢诺
corner_label = tk.Label(
    root, text="520 陈洢诺", font=font_corner,
    fg="#ff2b85", bg=COLOR_BG
)
corner_label.place(x=25, y=15)

# 2. 顶部标题栏（学校+班级）
title_frame = tk.Frame(root, bg=COLOR_TITLE_BG, width=800, height=90)
title_frame.place(x=0, y=50)

title_label = tk.Label(
    title_frame, text="东台市实验小学520班",
    font=font_title, fg=COLOR_WHITE, bg=COLOR_TITLE_BG
)
title_label.place(relx=0.5, rely=0.5, anchor="center")

# 3. 中间点名显示区（核心）
name_frame = tk.Frame(root, bg=COLOR_WHITE, width=700, height=180, bd=3, relief="groove")
name_frame.place(x=50, y=180)

name_label = tk.Label(
    name_frame, text=show_name, font=font_name,
    fg=COLOR_NAME, bg=COLOR_WHITE
)
name_label.place(relx=0.5, rely=0.5, anchor="center")

# 4. 底部按钮区
start_btn = tk.Button(
    root, text="开始点名", font=font_btn,
    bg=COLOR_BTN_START, fg=COLOR_WHITE,
    width=12, command=start_stop
)
start_btn.place(x=200, y=420)

reset_btn = tk.Button(
    root, text="重置", font=font_btn,
    bg=COLOR_BTN_RESET, fg=COLOR_WHITE,
    width=12, command=reset
)
reset_btn.place(x=460, y=420)

# 底部提示文字
tip_label = tk.Label(
    root, text="🎈 随机点名 | 公平公正", font=font_tip,
    fg="#6c757d", bg=COLOR_BG
)
tip_label.place(relx=0.5, y=500, anchor="center")

# 启动主循环
root.mainloop()