很多时候,人们都会遇到随机抽号或者随机点名的场景,利用Python可以方便地实现一个这样的系统。课堂上老师想随机抽取一位同学回答问题,界面设置有开始和停止按钮,学生名单存储在一个txt文本文件中,点击开始按钮,名字快速的随机跳动,点击停止按钮,出现一个随机选取的学生姓名。再次点击开始按钮可以进行新一轮的随机抽号的过程,系统实现界面如下图所示。
import tkinter as tk
import random
from PIL import Image, ImageTk
# 函数:从文件中加载学生名单
def load_students(filename):
with open(filename, 'r', encoding='utf-8') as file:
return [line.strip() for line in file.readlines()]
# 函数:更新显示的学生姓名
def update_display(name):
display_label.config(text=name)
# 函数:随机选择学生
def select_student():
return random.choice(students)
# 函数:开始点名
def start_pointing():
global selecting
if selecting:
return
selecting = True
update_display(select_student())
root.after(500, start_pointing) # 每500毫秒更新一次
# 函数:停止点名
def stop_pointing():
global selecting
selecting = False
# 创建主窗口
root = tk.Tk()
root.title("课堂随机点名")
# 加载学生名单
students = load_students('students.txt')
# 设置窗口的最小大小
root.minsize(400, 200)
# 创建标题标签
title_label = tk.Label(root, text="课堂随机点名", font=("Arial", 24, "bold"), fg="yellow")
title_label.pack(side=tk.TOP, fill=tk.X, padx=20, pady=10)
# 使用PIL加载图像并将其放置在标题旁边
try:
img = Image.open('background_image.jpg') # 替换为你的图像文件名
img = ImageTk.PhotoImage(img)
img_label = tk.Label(root, image=img)
img_label.pack(side=tk.TOP, padx=20, pady=(10, 0))
except:
print("图像文件无法加载,将使用纯色背景。")
# 创建显示学生姓名的标签,初始时显示一个学生姓名
display_label = tk.Label(root, text=select_student(), font=("Arial", 48, "bold"), fg="red")
display_label.pack(side=tk.RIGHT, padx=(0, 20), pady=20, fill=tk.Y)
# 创建一个Frame用于垂直放置开始和停止按钮
button_frame = tk.Frame(root, bg='lightgrey', bd=5) # 添加边框
button_frame.pack(side=tk.LEFT, fill=tk.Y, padx=20)
# 创建开始按钮
start_button = tk.Button(button_frame, text="开始", font=("Arial", 24), bg='green', fg='white', command=start_pointing)
start_button.pack(side=tk.TOP, fill=tk.Y)
# 创建停止按钮
stop_button = tk.Button(button_frame, text="停止", font=("Arial", 24), bg='red', fg='white', command=stop_pointing)
stop_button.pack(side=tk.TOP, fill=tk.Y)
selecting = False
# 主事件循环
root.mainloop()
以下是对提供的Python Tkinter代码的分析,以及主要代码块的功能介绍:
1. **导入库**:
import tkinter as tk
import random
from PIL import Image, ImageTk
- `tkinter`:用于创建GUI应用程序。
- `random`:用于随机选择学生姓名。
- `PIL`(Python Imaging Library,通过`Image`和`ImageTk`模块):用于处理图像。
2. **加载学生名单**:
def load_students(filename):
with open(filename, 'r', encoding='utf-8') as file:
return [line.strip() for line in file.readlines()]
- 从指定的文本文件中读取学生名单,并返回一个列表。
3. **更新显示的学生姓名**:
def update_display(name):
display_label.config(text=name)
- 更新界面上显示的学生姓名。
4. **随机选择学生**:
def select_student():
return random.choice(students)
- 从学生名单中随机选择一个学生并返回其姓名。
5. **开始点名**:
def start_pointing():
if selecting:
return
selecting = True
update_display(select_student())
root.after(500, start_pointing)
- 开始点名过程,随机选择学生并更新显示,然后每500毫秒重复此过程。
6. **停止点名**:
def stop_pointing():
selecting = False
- 停止点名过程,将`selecting`标志设置为`False`。
7. **创建主窗口**:
root = tk.Tk()
root.title("课堂随机点名")
- 创建Tkinter应用程序的主窗口,并设置窗口标题。
8. **加载学生名单**:
students = load_students('students.txt')
- 调用`load_students`函数加载学生名单。
9. **设置窗口的最小大小**:
root.minsize(400, 200)
- 设置窗口的最小宽度和高度,防止窗口过小。
10. **创建标题标签**:
title_label = tk.Label(root, text="课堂随机点名", font=("Arial", 24, "bold"), fg="yellow")
title_label.pack(side=tk.TOP, fill=tk.X, padx=20, pady=10)
- 创建并显示应用程序标题的标签,设置字体样式和颜色。
11. **加载和显示图像**:
try:
img = Image.open('background_image.jpg')
img = ImageTk.PhotoImage(img)
img_label = tk.Label(root, image=img)
img_label.pack(side=tk.TOP, padx=20, pady=(10, 0))
except:
print("图像文件无法加载,将使用纯色背景。")
- 尝试加载一个图像文件并在界面上显示。如果加载失败,将使用纯色背景。
12. **创建显示学生姓名的标签**:
display_label = tk.Label(root, text=select_student(), font=("Arial", 48, "bold"), fg="red")
display_label.pack(side=tk.RIGHT, padx=(0, 20), pady=20, fill=tk.Y)
- 创建一个标签用于显示选中的学生姓名,设置字体样式和颜色,并将其放置在界面右侧。
13. **创建按钮Frame**:
button_frame = tk.Frame(root, bg='lightgrey', bd=5)
button_frame.pack(side=tk.LEFT, fill=tk.Y, padx=20)
- 创建一个Frame作为容器,用于垂直排列开始和停止按钮,设置背景颜色和边框。
14. **创建开始和停止按钮**:
start_button = tk.Button(button_frame, text="开始", ..., command=start_pointing)
stop_button = tk.Button(button_frame, text="停止", ..., command=stop_pointing)
- 创建开始和停止按钮,设置样式和点击时调用的函数。
15. **全局变量**:
selecting = False
- 用于控制点名过程的全局标志变量。
16. **主事件循环**:
root.mainloop()
- 进入Tkinter的事件循环,等待用户操作。
这个脚本创建了一个简单的课堂随机点名系统,具有开始和停止点名的功能,以及一个美观的界面。通过使用Tkinter库,我们可以轻松地创建图形用户界面,并响应用户的输入。