花费2分钟就开发了点名小工具,毫不夸张。
import tkinter as tkfrom tkinter import ttk, filedialog, messageboximport pandas as pdimport random# 主窗口配置root = tk.Tk()root.title("🎯 智能随机点名系统")root.geometry("600x450")root.resizable(False, False)# 全局变量name_list = [] # 存储名单is_running = False # 是否正在点名current_name = tk.StringVar(value="等待导入名单")blink_id = None # 动画定时器ID# ---------------------- 功能函数 ----------------------def import_excel(): """导入Excel名单""" global name_list file_path = filedialog.askopenfilename( title="选择Excel文件", filetypes=[("Excel文件", "*.xlsx;*.xls")] ) if not file_path: return try: # 读取Excel第一列作为姓名 df = pd.read_excel(file_path) name_list = df.iloc[:, 0].dropna().astype(str).tolist() if len(name_list) == 0: messagebox.showwarning("警告", "Excel中未找到有效姓名!") return current_name.set(f"已导入 {len(name_list)} 人") start_btn.config(state=tk.NORMAL) except Exception as e: messagebox.showerror("错误", f"导入失败:{str(e)}")def random_name(): """随机滚动姓名(闪烁效果)""" global blink_id if not is_running: return # 随机选一个名字 name = random.choice(name_list) current_name.set(name) # 控制闪烁速度(50ms切换一次) blink_id = root.after(50, random_name)def start_stop(): """开始/停止点名动画""" global is_running if not name_list: messagebox.showwarning("提示", "请先导入Excel名单!") return if not is_running: # 开始点名 is_running = True start_btn.config(text="停止点名") confirm_btn.config(state=tk.DISABLED) random_name() else: # 停止点名 is_running = False if blink_id: root.after_cancel(blink_id) start_btn.config(text="开始点名") confirm_btn.config(state=tk.NORMAL)def confirm_name(): """确定选中姓名""" confirm_btn.config(state=tk.DISABLED) start_btn.config(state=tk.NORMAL) messagebox.showinfo("结果", f"本次点名:\n\n【 {current_name.get()} 】")# ---------------------- 界面布局 ----------------------# 顶部导入区域frame_top = ttk.Frame(root, padding=20)frame_top.pack(fill=tk.X)import_btn = ttk.Button(frame_top, text="📂 导入Excel名单", command=import_excel)import_btn.pack(side=tk.LEFT, padx=10)# 中间姓名显示区域frame_center = ttk.Frame(root, padding=30)frame_center.pack(fill=tk.BOTH, expand=True)# 姓名标签(大号字体+闪烁效果)name_label = ttk.Label( frame_center, textvariable=current_name, font=("微软雅黑", 36, "bold"), foreground="#2c3e50")name_label.pack(expand=True)# 底部按钮区域frame_bottom = ttk.Frame(root, padding=20)frame_bottom.pack(fill=tk.X)start_btn = ttk.Button( frame_bottom, text="开始点名", command=start_stop, state=tk.DISABLED)start_btn.pack(side=tk.LEFT, expand=True, padx=10)confirm_btn = ttk.Button( frame_bottom, text="确定姓名", command=confirm_name, state=tk.DISABLED)confirm_btn.pack(side=tk.RIGHT, expand=True, padx=10)# 界面样式美化style = ttk.Style()style.configure('TButton', font=('微软雅黑', 12))style.configure('TFrame', background='#f5f5f5')frame_top.config(style='TFrame')frame_center.config(style='TFrame')frame_bottom.config(style='TFrame')# 启动主程序root.mainloop()
