import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageDraw, ImageFont
import os

# 主窗口
root = tk.Tk()
root.title("学生证件生成器")
root.geometry("420x380")

# 变量
name_var = tk.StringVar()
id_var = tk.StringVar()
class_var = tk.StringVar()
gender_var = tk.StringVar()
college_var = tk.StringVar()
img_path = tk.StringVar(value="")

def select_image():
    path = filedialog.askopenfilename(filetypes=[("图片文件", "*.jpg;*.png;*.jpeg")])
    if path:
        img_path.set(path)

def generate_card():
    name = name_var.get().strip()
    sid = id_var.get().strip()
    cls = class_var.get().strip()
    gender = gender_var.get().strip()
    college = college_var.get().strip()
    avatar_path = img_path.get()

    if not all([name, sid, cls, gender, college]):
        messagebox.showerror("错误", "请填写全部信息！")
        return

    # 证件尺寸
    w, h = 500, 320
    card = Image.new("RGB", (w, h), color="#f0f7ff")
    draw = ImageDraw.Draw(card)

    # 尝试加载字体，失败用默认
    try:
        font_title = ImageFont.truetype("simhei.ttf", 28)
        font_text = ImageFont.truetype("simhei.ttf", 20)
    except:
        font_title = ImageFont.load_default()
        font_text = ImageFont.load_default()

    # 标题
    draw.text((w//2, 20), "学生证", font=font_title, fill="#003388", anchor="mm")

    # 绘制头像区域
    avatar_x, avatar_y = 40, 60
    avatar_size = 120
    draw.rectangle([avatar_x, avatar_y, avatar_x+avatar_size, avatar_y+avatar_size], outline="#222222", width=2)

    if avatar_path and os.path.exists(avatar_path):
        try:
            avatar = Image.open(avatar_path).convert("RGBA")
            avatar = avatar.resize((avatar_size, avatar_size), Image.Resampling.LANCZOS)
            card.paste(avatar, (avatar_x, avatar_y), mask=avatar if avatar.mode=="RGBA" else None)
        except Exception as e:
            messagebox.showwarning("头像警告", "头像加载失败，将空白显示")

    # 文字信息
    text_x = 190
    line_y = 70
    line_gap = 38
    draw.text((text_x, line_y), f"姓名：{name}", font=font_text, fill="#222222")
    draw.text((text_x, line_y+line_gap), f"学号：{sid}", font=font_text, fill="#222222")
    draw.text((text_x, line_y+line_gap*2), f"性别：{gender}", font=font_text, fill="#222222")
    draw.text((text_x, line_y+line_gap*3), f"班级：{cls}", font=font_text, fill="#222222")
    draw.text((text_x, line_y+line_gap*4), f"学院：{college}", font=font_text, fill="#222222")

    # 底部文字
    draw.text((w//2, h-30), "———— 学生证件 ————", font=font_text, fill="#444444", anchor="mm")

    # 保存
    save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG图片","*.png"),("JPG图片","*.jpg")])
    if save_path:
        card.save(save_path)
        messagebox.showinfo("成功", f"证件已保存到：{save_path}")
        card.show()

# 界面组件
tk.Label(root, text="姓名").place(x=20, y=20)
tk.Entry(root, textvariable=name_var, width=25).place(x=90, y=20)

tk.Label(root, text="学号").place(x=20, y=60)
tk.Entry(root, textvariable=id_var, width=25).place(x=90, y=60)

tk.Label(root, text="班级").place(x=20, y=100)
tk.Entry(root, textvariable=class_var, width=25).place(x=90, y=100)

tk.Label(root, text="性别").place(x=20, y=140)
tk.Entry(root, textvariable=gender_var, width=25).place(x=90, y=140)

tk.Label(root, text="学院").place(x=20, y=180)
tk.Entry(root, textvariable=college_var, width=25).place(x=90, y=180)

tk.Button(root, text="选择头像图片", command=select_image).place(x=20, y=220)
tk.Label(root, textvariable=img_path, fg="#555555").place(x=140, y=222)

tk.Button(root, text="生成并保存学生证", command=generate_card, bg="#2b7cd3", fg="white", width=20, height=2).place(x=110, y=270)

root.mainloop()
