设为首页 加入收藏

TOP

Python实现抽奖程序
2023-07-25 21:27:13 】 浏览:40
Tags:Python 程序

抽奖程序

来源:《python程序设计》第四版

作者:董付国

来源:《python程序设计》第四版

作者:董付国

 

 

'''
抽奖程序
使用时可以修改嘉宾名单,然后单机‘开始’和‘停止’按钮
来控制界面上名单的滚动实现抽奖功能,涉及的模块主要
有多线程
'''
import itertools
import random
import threading
import time
import tkinter
import tkinter.messagebox

root = tkinter.Tk()
#窗口标题
root.title('随机提问')
root.geometry('260x180+400+300')
#不允许改变窗口大小
root.resizable(False,False)
#关闭程序时,执行的函数代码,停止滚动显示学生名单
def closeWindow():
    root.flag = False
    time.sleep(0.1)
    root.destroy()
root.protocol('VM_DELETE_WINDOW',closeWindow)
#模拟学生名单,可以加上数据库访问接口,从数据库中读取学生名单
student = ['张三','李四','王五','赵六','周七','钱八']
#变量,用来控制是否滚动显示学生名单
root.flag = False
def switch():
    root.flag = True
    #随机打乱学生名单
    t = student[:]
    random.shuffle(t)
    t = itertools.cycle(t)

    while root.flag:
        #滚动显示
        lbFirst['text'] = lbSecond['text']
        lbSecond['text'] = lbThird['text']
        lbThird['text'] = next(t)
        #数字可以修改,控制滚动速度
        time.sleep(0.1)
def btnStartClick():
    #每次单机‘开始’按钮启动新线程,并禁用开始按钮,启动停止按钮
    t = threading.Thread(target=switch)
    t.start()
    btnStart['state'] = 'disabled'
    btnStop['state'] = 'normal'
btnStart = tkinter.Button(root,text='开始',command=btnStartClick)
btnStart.place(x=30,y=10,width=80,height=20)
def btnStopClick():
    #单机停按钮结束滚动显示,弹窗提示中将名单,修改按钮状态
    root.flag = False
    time.sleep(0.3)
    tkinter.messagebox.showinfo('恭喜','本次中奖:'+lbSecond['text'])
    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组件
#可以根据需求添加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()

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇TheFuck—Python写的超实用命令纠.. 下一篇Frida-trace常用命令

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目