import tkinter as tk
from tkinter import ttk, messagebox
import random
import time

class LotteryApp:
    def __init__(self, root):
        # 主窗口配置
        self.root = root
        self.root.title("简易抽签器")
        self.root.geometry("520x420")
        self.root.resizable(False, False)
        self.root.configure(bg="#f0f4f8")

        # 抽签数据
        self.name_list = []
        self.history = []

        # 搭建界面
        self.create_widgets()

    def create_widgets(self):
        # 输入区域框架
        frame_input = tk.Frame(self.root, bg="#f0f4f8")
        frame_input.pack(pady=10, padx=10, fill="x")

        tk.Label(frame_input, text="输入抽签名单（每行一个名字）：", bg="#f0f4f8", font=("微软雅黑", 11)).pack(anchor="w")
        self.text_names = tk.Text(frame_input, height=6, font=("微软雅黑", 10))
        self.text_names.pack(fill="x", pady=5)

        # 按钮区域
        frame_btn = tk.Frame(self.root, bg="#f0f4f8")
        frame_btn.pack(pady=5)

        self.btn_start = tk.Button(frame_btn, text="开始抽签", command=self.draw_lottery,
                                   width=10, bg="#228be6", fg="white", font=("微软雅黑", 11))
        self.btn_start.grid(row=0, column=0, padx=8)

        self.btn_clear_data = tk.Button(frame_btn, text="清空名单", command=self.clear_name,
                                        width=10, bg="#fa5252", fg="white", font=("微软雅黑", 11))
        self.btn_clear_data.grid(row=0, column=1, padx=8)

        self.btn_clear_history = tk.Button(frame_btn, text="清空记录", command=self.clear_history,
                                           width=10, bg="#40c057", fg="white", font=("微软雅黑", 11))
        self.btn_clear_history.grid(row=0, column=2, padx=8)

        # 抽签结果展示
        frame_result = tk.Frame(self.root, bg="#f0f4f8")
        frame_result.pack(pady=10)
        tk.Label(frame_result, text="本次中签结果：", bg="#f0f4f8", font=("微软雅黑", 12)).pack()
        self.label_result = tk.Label(frame_result, text="等待抽签", font=("微软雅黑", 20, "bold"), fg="#d9480f", bg="#f0f4f8")
        self.label_result.pack(pady=6)

        # 抽签历史记录
        frame_history = tk.Frame(self.root, bg="#f0f4f8")
        frame_history.pack(pady=5, fill="both", expand=True, padx=10)
        tk.Label(frame_history, text="抽签历史记录：", bg="#f0f4f8", font=("微软雅黑", 11)).pack(anchor="w")

        scroll = ttk.Scrollbar(frame_history)
        scroll.pack(side="right", fill="y")
        self.list_history = tk.Listbox(frame_history, yscrollcommand=scroll.set, font=("微软雅黑", 10))
        scroll.config(command=self.list_history.yview)
        self.list_history.pack(fill="both", expand=True)

    # 读取输入的名单
    def get_name_list(self):
        content = self.text_names.get("1.0", tk.END).strip()
        if not content:
            return []
        # 按行分割，去除空行
        data = [name.strip() for name in content.splitlines() if name.strip()]
        return data

    # 抽签核心逻辑
    def draw_lottery(self):
        self.name_list = self.get_name_list()
        if len(self.name_list) == 0:
            messagebox.showwarning("提示", "请先填写抽签名单！")
            return

        # 滚动动画效果
        for _ in range(20):
            temp_name = random.choice(self.name_list)
            self.label_result.config(text=temp_name)
            self.root.update()
            time.sleep(0.06)

        # 最终抽取结果
        pick_name = random.choice(self.name_list)
        self.label_result.config(text=pick_name)
        # 写入历史
        self.history.append(pick_name)
        self.list_history.insert(tk.END, pick_name)

    # 清空名单输入框
    def clear_name(self):
        self.text_names.delete("1.0", tk.END)

    # 清空历史记录
    def clear_history(self):
        self.list_history.delete(0, tk.END)
        self.history.clear()

if __name__ == "__main__":
    window = tk.Tk()
    app = LotteryApp(window)
    window.mainloop()