首页 > 数据库 > 数据库开发技术 > Python实现高校上课随机点名系统V1版
自动点名程序效果展示
加载库
设置文件路径并加载数据
定义一个随机筛选名字并语音播报的函数
定义窗口并展示点名系统

我们先来看看随机点名小程序最终的实现效果。

首先加载实现本文功能需要的库。
import tkinter as tkfrom pandas import read_excelfrom random import randintimport osfrom PIL import Imagefrom PIL import ImageTkfrom win32com.client import Dispatch

Python中有可以自动播报语音的库,这样就可以免去我们一个一个念名字的麻烦啦。
#控制播放语音speaker = Dispatch("SAPI.SpVoice")#设置文件存放路径os.chdir(r"F:\微信公众号\Python\46_随机点名系统")#读入数据data = read_excel(r'学生名单.xls')df1 = list(data['姓名'])df2 = list(data['性别'])

定义一个可以反复调用的随机筛选名字并语音播报的函数。
def roll_call():if len(df1)>0:index_ = randint(0, len(df1) - 1)name = df1.pop(index_)sex = df2.pop(index_)t.insert('insert', f'{name} {sex}\n') # 插入到tkinter界面speaker.Speak(name)else:speaker.Speak('点名结束')

最后我们定义一个展示窗口,在窗口中生成一些按钮和名字展示模块,当点击按钮时实现自动点名播报。
win = tk.Tk()win.title('欢迎进入点名程序')win.iconbitmap("pikaqiu2.ico")win.geometry('900x900')# Entry单行文本L = tk.Label(win, bg='lemonchiffon', text = "随机点名小程序", font=("KaiTi", 26), width=55, height=3) # 关键:设置为背景图片L.place(x=0, y=0)#设置随机点名按钮,退出系统按钮b1 = tk.Button(win, bg='lightblue', text="随机点名", font=("KaiTi", 16), width=20, height=2, command=roll_call)b1.place(x=250, y=200)b2 = tk.Button(win, bg='lightblue', text="退出系统", font=("KaiTi", 16), width=20, height=2, command=win.quit)b2.place(x=550, y=200)# Entry 单行文本L = tk.Label(win, text="点到的学生名单如下", font=("KaiTi", 18), width=60, height=1)L.place(x=90, y=315)# 设置多行文本框 宽 高 文本框中字体 选中文字时文字的颜色t = tk.Text(win, width=60, height=100, font=("KaiTi", 24), selectforeground='black') #显示文本t.place(x=90, y=350)win.mainloop()


扫一扫关注我
19967879837
投稿微信号
原文作者:阿黎逸阳的代码;如有侵权,请联系管理员删除。