程序笔记   发布时间:2022-07-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Python 实现点名系统大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

安装扩展库pywin32和speech,然后修改一下speech.py文件使得适用于Python 3.x。

 

步骤1:安装pywin32

在命令行模式运行:

pip install pywin32

安装出现超时错误,如下:

Python 实现点名系统

 

 pip --default-timeout=1000 install -U pywin32 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

 

步骤2:安装扩展库speech

安装扩展库speech

pip3 install speech

 

然后修改speech.py 文件使得适用于Python 3.x,重点修改之处如图,

Python 实现点名系统

 

 

Python 实现点名系统

 改为:

Python 实现点名系统

 

 

 

Python 实现点名系统

改为:

Python 实现点名系统

 

 

 

Python 实现点名系统

改为:

Python 实现点名系统

 

步骤3:

 

准备一个文本文件,保存学生信息,如图

学生名单.txt

20210223,张三

20210224,李四

20210225,王五

20210226,赵六

20210227,周七

20210228,钱八

 

 

源代码如下:

 

import tkinter
from tkinter.messagebox import showinfo
from time import sleep
from random import shuffle
from itertools import cycle
from threading import Thread
try :
    from speech import say
    has_speech = True
except:
    has_speech = false
root = tkinter.Tk() #窗口标题
root.title('随机提问')#窗口初始大小和位置
root.geometry( '260x180+400+300')#不允许改变窗口大小
root.resizable(false,falsE)
#关闭程序时执行的函数代码,停止滚动显示学生名单
def closewindow( ):
    if rolling.get():
        showinfo('不能关闭','请先停止名单滚动')
        return
    root.destroy()
root.protocol('WM_deletE_WINDOw' , closewindow)
#读取学生名单,如果不存在文件就使用模拟数据try :
try:
    with open( '学生名单.txt' , encoding='utf8 ' ) as fp:
        students = fp.read( ).splitlines()
except:
    showinfo('学生名单不存在',
        '当前目录中没有文件:学生名单.txtn临时使用模拟数据')
    students =['张三','李四','王五','赵六','周七','钱八']
#变量,用来控制是否滚动显示学生名单
rolling = tkinter.BooleanVar(root, value=falsE)


def switch():
    rolling.set(true)
    #随机打乱学生名单
    t = students[ : ]
    shuffle(t)
    t = cycle(t)

    while rolling.get():
        # 滚动显示
        lbFirst[ 'text'] = lbSecond[ 'text' ]
        lbSecond[ 'text'] = lbThird[ 'text']
        lbThird[ 'text'] = next(t)
        #数字可以修改,控制滚动速度
        sleep(0.1)

def btnStartClick():
    # 每次单击“开始”按钮启动新线程
    Thread(target=switch).start()
    btnStart[ 'state' ] = 'disabled'
    btnStop[ 'state' ] = 'normal'
btnStart = tkinter.button( root,
    text='开始',
    command=btnStartClick)
btnStart.place(x=30,y=10,width=80,height=20)
saying = tkinter.BooleanVar(root, value=falsE)
def say_name():
    while has_speech and saying.get():
        say(f"{lbSecond[ 'text ' ].replace( ' ,','')}回答问题")


def btnStopClick():
    #单击“停”按钮结束滚动显示rolling.set(false)
    sleep(0.3)
    saying.set(true)
    Thread(target=say_Name).start()
    showinfo('恭喜','本次中奖: '+lbSecond[ 'text' ])
    saying.set(false)
    btnStart[ 'state' ] = 'normal'
    btnStop[ 'state' ] = 'disabled'
btnStop = tkinter.button(root,text='', command=btnStopClick)
btnStop[ 'state'] = 'disabled'
btnStop.place(x=150,y=10, width=80,height=20)
#用来滚动显示学生名单的3个Label组件
#可以根据需要进行添加,但要修改上面的线程函数代码
lbFirst = tkinter.Label(root, text='')
lbFirst.place(x=80, y=60, width=100,height=20)
#红色Label组件,表示中奖名单
lbSecond = tkinter.Label(root,text='')
lbSecond[ 'fg' ] = 'red'
lbSecond.place(x=80,y=90,width=100,height=20)
lbThird = tkinter.Label(root,text='')
lbThird.place(x=80,y=120,width=100,height=20)
#启动tkinter主程序
root.mainloop()

 

 

 

来自:

Python 实现点名系统

 

大佬总结

以上是大佬教程为你收集整理的Python 实现点名系统全部内容,希望文章能够帮你解决Python 实现点名系统所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。