您当前的位置: 首页 >  Python

轻松学Python

暂无认证

  • 0浏览

    0关注

    317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

教师节我用Python做了个学生点名系统送给老师当礼物,这回毕业稳了

轻松学Python 发布时间:2022-09-12 16:43:02 ,浏览量:0

今年教师节前夕,我特意用Python做了个学生点名系统,非常好用,送给各科老师、辅导员当节日礼物,老师们都喜滋滋,说平常逃课就原谅我了,我心想,这次毕业应该不是问题了~

本文背景

根据我的调查,现在的学生大部分都很积极,会主动举手回答问题。但是,也会遇到一些不好的情况,比如年级越高主动举手的人越少,有些班级举手的通常都是少部分积极的学生,有部分学生从来不举手。

所以我做了一个一个随机的学生点名系统可以帮老师解决这些问题。

  • 随机点名会从全班学生中随机点一个学生,这样所有人都有机会回答问题,促进教育公平。

  • 点名系统有几秒钟滚动的时间,会增加学生的紧张感,让开小差的学生也赶紧集中精神,起到一点督促学习的作用。

  • 如果真的没有学生举手,老师也不用为难,点名系统可以作为老师的“杀手锏”。

实际情况中可以一部分时间靠学生主动,一部分时间用点名系统,灵活使用。

效果展示

本文用Python实现了一个非常好用的学生点名系统,文末名片提供打包好的系统下载方式。先看一下效果:

实现方式

1、读取excel表格

openpyxl是Python中用于读写excel文件非常方便的库,pip install openpyxl安装即可使用。

本文用openpyxl来读取excel中的所有学生姓名。

def get_students_name():
    # 学生名单中需要有"姓名"列
    workbook = openpyxl.load_workbook('学生名单.xlsx')
    table = workbook.active
    rows, cols = table.max_row, table.max_column
    name_col = 0
    for col in range(cols):
        if table.cell(1, col + 1).value == '姓名':
            name_col = col
            break
    students_name = [table.cell(row+1, name_col+1).value for row in range(1, rows)
                     if table.cell(row+1, name_col+1).value is not None]
    return students_name

2、搭建系统界面

tkinter是Python中GUI编程非常好用的库,而且是标准库,不需要安装,导入即可使用。

本文用tkinter搭建学生点名系统的界面,并在界面上实现点名按钮和显示点名结果。

if __name__ == '__main__':
    window = tk.Tk()
    window.geometry('600x400+400+180')
    window.title('\t 第一届LOL点名系统')
    # 添加背景图片
    bg_png = tk.PhotoImage(file="背景图片.png")
    bg_label = Label(window, image=bg_png)
    bg_label.pack()
    # 添加显示框
    var = StringVar(value='公平 公正 公开')
    show_label1 = Label(window, textvariable=var, justify='left', anchor=CENTER, width=16,
                        height=2, font='楷体 -40 bold', foreground='white', bg='#1C86EE')
    show_label1.place(anchor=tk.NW, x=130, y=90)
    # 添加点名按钮
    button_png = tk.PhotoImage(file='button.png')
    button = Button(window, text='点 名', compound='center', font='楷体 -30 bold',
                    foreground='#9400D3', image=button_png,
                    command=lambda: call_lucky_student(var))
    button.place(anchor=NW, x=235, y=200)
    # 显示窗口
    window.mainloop()

3、随机选择学生

random库是Python中用于实现随机功能的库,也是Python的标准库,不需要安装,导入即可使用。

本文用random从学生名单中随机选择一个姓名,结合time模块设置延时,实现点名按钮的业务逻辑函数。

def call_lucky_student(var):
    """点名"""
    global is_run
    if is_run:
        return
    is_run = True
    start = time.time()
    choice_student(var, start)


def choice_student(var, start):
    global is_run
    show_member = random.choice(get_students_name())
    name = show_member[0]
    for zi in show_member[1:]:
        name += ' ' + zi
    var.set(name)
    end = time.time()
    if is_run and end-start             
关注
打赏
1663832040
查看更多评论
0.1916s