随机抽奖程序

使用tkinter实现

import tkinter
import tkinter.messagebox
import random
import threading
import itertools
import time 

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('WM_DELETE_WINDOW', closeWindow)

list = ['1', '2', '3', '4', '5', '6', '7', '8']
#可编辑内容
root.flag = False

def switch():
    root.flag = True
    t = list[:]
    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
btnStop['state'] = 'disabled'
btnStop.place(x=150, y=10, width=80, height=20)

#定义
lbFirst = tkinter.Label(root, text='')
lbFirst.place(x=80, y=60, width=100, height=20)

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标准库itertools的cycle()函数来创建现有列表的无限循环,让列表一直滚动显示。

运行界面如图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值