图形化界面的随机抽取人系统,把代码中注释解除掉后能指定人选。
import tkinter as tk
import random
class GUI:
def __init__(self) -> None:
self.root = tk.Tk()
self.root.title("课堂随机点名")
self.root.geometry("600x300")
self.st = True
self.interface()
def interface(self):
tk.Label(
self.root,
text="课堂随机点名",
font=["SimHei", 20, "bold"],
width=41,
height=3,
foreground="Yellow",
background="Grey",
).grid(row=0, column=0)
tk.Button(
self.root, text="开始", font=["SimHei", 20], command=self.show_time
).place(x=50, y=120, relwidth=0.2, relheight=0.2)
tk.Button(
self.root, text="结束", font=["SimHei", 20], command=self.luck_one
).place(x=50, y=200, relwidth=0.2, relheight=0.2)
tk.Label(
self.root, text="测试", font=["SimHei", 30, "bold"], foreground="red"
).place(x=280, y=140, relwidth=0.3, relheight=0.3)
def luck_one(self):
self.st = False
def show_time(self):
random.shuffle(name_list)
self.st = True
def update_name(index=0):
if self.st == True:
if index == len(name_list):
index = 0
name = name_list[index]
tk.Label(
self.root, text=name, font=["SimHei", 30, "bold"], foreground="red"
).place(x=280, y=140, relwidth=0.3, relheight=0.3)
self.root.after(100, update_name, index + 1)
# else:
# tk.Label(
# self.root, text="张三", font=["SimHei", 30, "bold"], foreground="red"
# ).place(x=280, y=140, relwidth=0.3, relheight=0.3)
update_name()
if __name__ == "__main__":
global name_list
name_list = []
with open("./Python/data/stu_name.txt", "r", encoding="utf-8") as fp:
for line in fp:
name_list.append(line)
a = GUI()
a.root.mainloop()