import tkinter as tk, random, time, sys, math

hearts, all_wins = [], []
tips = ["学业有成", "好好爱自己", "好好吃饭", "保持好心情", "身体健康",
        "事事顺利", "别熬夜", "天冷了多穿衣服","你是特别的人","只对你好"]                         #爱心输出内容
colors = ["pink", "lightblue", "lightgreen", "lemonchiffon",
          "hotpink", "skyblue"]                                       #爱心颜色


def heart_points(n, screen_w, screen_h):
    points = []
    for i in range(n):
        t = i / n * 2 * math.pi
        x = 16 * math.sin(t) ** 3
        y = (13 * math.cos(t) - 5 * math.cos(2 * t)
             - 2 * math.cos(3 * t) - math.cos(4 * t))
        sx = int(screen_w / 2 + x * 20 - 50)
        sy = int(screen_h / 2 - y * 20 - 80)
        sx = max(0, min(sx, screen_w - 150))
        sy = max(0, min(sy, screen_h - 60))
        points.append((sx, sy))
    return points


def create_popup(x, y, tip=None):
    win = tk.Toplevel()
    win.geometry(f"150x60+{x}+{y}")
    win.title("提示")
    win.attributes('-topmost', 1)
    text = tip or random.choice(tips)
    bg = random.choice(colors)
    tk.Label(win, text=text, bg=bg,
             font=("微软雅黑", 14), width=20, height=3).pack()    #字体样式和字号          
    win.bind('<space>',
             lambda e: [w.destroy() for w in hearts + all_wins] or sys.exit())
    return win


def main():#主函数，从这里运行
    root = tk.Tk()
    root.withdraw()
    sw = root.winfo_screenwidth()
    sh = root.winfo_screenheight()

    points = heart_points(200, sw, sh)                  #200指形成爱心的方格数可以更改
    for i, (x, y) in enumerate(points):
        tip = "我喜欢你" if i == len(points) - 1 else None#最后一个爱心的内容
        win = create_popup(x, y, tip)
        hearts.append(win)
        root.update()
        time.sleep(0.05)                    #形成爱心时间间隔

    time.sleep(1)
    for w in hearts:
        if isinstance(w, tk.Toplevel) and w.winfo_exists():
            w.destroy()

    count = sw // 150 * sh // 40 + 50
    for _ in range(count+50):                          #形成最后全屏弹窗数量50是可以任意更改
        x = random.randint(0, sw - 150)
        y = random.randint(0, sh - 60)
        win = create_popup(x, y)
        all_wins.append(win)
        root.update()
        time.sleep(0.001)                            #形成弹窗间隔时间

    time.sleep(3)                                   #生成后停留3s

    interval = 1.0 / len(all_wins) if all_wins else 0
    for win in all_wins:
        if isinstance(win, tk.Toplevel) and win.winfo_exists():
            win.destroy()
        root.update()
        time.sleep(interval)
    root.mainloop()


if __name__ == "__main__":
    main()
