import tkinter as tk from random import shuffle from time import sleep def load_students(): with open('students.txt', 'r', encoding='utf-8') as file: return [line.strip() for line in file] def select_student(students): shuffle(students) return students[0] def update_name(): global running, selected_name if running: selected_name = select_student(students) name_label.config(text=selected_name) root.after(5, update_name) def start_or_continue(): global running running = True update_name() def stop_update(): global running running = False root = tk.Tk() root.geometry("450x300") root.title("课堂随机点名") root.config(bg="#F7F7F7") students = load_students() selected_name = select_student(students) name_label = tk.Label(root, text=selected_name, font=("宋体", 36), bg="#F7F7F7",fg="red",pady=60) name_label.pack(ipadx=150) running = False title = tk.Label(root,text="猜猜是哪个倒霉蛋让我点到了", font=("宋体", 20),bg="gray",fg="yellow").place(x=0,y=0,width=450,height=50) start_button = tk.Button(root, text="开始", font=("宋体", 20), command=start_or_continue) stop_button = tk.Button(root, text="停止", font=("宋体",20), command=stop_update) start_button.place(x=20, y=50) stop_button.place(x=20, y=100) root.mainloop()
txt文件
效果图