翼度科技»论坛 编程开发 python 查看内容

Python实现抽奖程序

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
抽奖程序


 
 
  1. '''
  2. 抽奖程序
  3. 使用时可以修改嘉宾名单,然后单机‘开始’和‘停止’按钮
  4. 来控制界面上名单的滚动实现抽奖功能,涉及的模块主要
  5. 有多线程
  6. '''
  7. import itertools
  8. import random
  9. import threading
  10. import time
  11. import tkinter
  12. import tkinter.messagebox
  13. root = tkinter.Tk()
  14. #窗口标题
  15. root.title('随机提问')
  16. root.geometry('260x180+400+300')
  17. #不允许改变窗口大小
  18. root.resizable(False,False)
  19. #关闭程序时,执行的函数代码,停止滚动显示学生名单
  20. def closeWindow():
  21.     root.flag = False
  22.     time.sleep(0.1)
  23.     root.destroy()
  24. root.protocol('VM_DELETE_WINDOW',closeWindow)
  25. #模拟学生名单,可以加上数据库访问接口,从数据库中读取学生名单
  26. student = ['张三','李四','王五','赵六','周七','钱八']
  27. #变量,用来控制是否滚动显示学生名单
  28. root.flag = False
  29. def switch():
  30.     root.flag = True
  31.     #随机打乱学生名单
  32.     t = student[:]
  33.     random.shuffle(t)
  34.     t = itertools.cycle(t)
  35.     while root.flag:
  36.         #滚动显示
  37.         lbFirst['text'] = lbSecond['text']
  38.         lbSecond['text'] = lbThird['text']
  39.         lbThird['text'] = next(t)
  40.         #数字可以修改,控制滚动速度
  41.         time.sleep(0.1)
  42. def btnStartClick():
  43.     #每次单机‘开始’按钮启动新线程,并禁用开始按钮,启动停止按钮
  44.     t = threading.Thread(target=switch)
  45.     t.start()
  46.     btnStart['state'] = 'disabled'
  47.     btnStop['state'] = 'normal'
  48. btnStart = tkinter.Button(root,text='开始',command=btnStartClick)
  49. btnStart.place(x=30,y=10,width=80,height=20)
  50. def btnStopClick():
  51.     #单机停按钮结束滚动显示,弹窗提示中将名单,修改按钮状态
  52.     root.flag = False
  53.     time.sleep(0.3)
  54.     tkinter.messagebox.showinfo('恭喜','本次中奖:'+lbSecond['text'])
  55.     btnStart['state'] = 'normal'
  56.     btnStop['state'] = 'disabled'
  57. btnStop = tkinter.Button(root,text='停',command=btnStopClick)
  58. btnStop['state'] = 'disabled'
  59. btnStop.place(x=150,y=10,width=80,height=20)
  60. #用来滚动显示学生名单的3个Label组件
  61. #可以根据需求添加Label组件的数量,但是要修改上面的代码函数代码
  62. lbFirst = tkinter.Label(root,text='')
  63. lbFirst.place(x=80,y=60,width=100,height=20)
  64. #红色Label组件,表示中奖名单
  65. lbSecond = tkinter.Label(root,text='')
  66. lbSecond['fg'] = 'red'
  67. lbSecond.place(x=80,y=90,width=100,height=20)
  68. lbThird = tkinter.Label(root,text='')
  69. lbThird.place(x=80,y=120,width=100,height=20)
  70. #启动tkinter主程序
  71. root.mainloop()
复制代码
 

来源:https://www.cnblogs.com/buchijiuminvhai/p/17058049.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具