抽奖小程序

抽奖小程序

导入模块

用random进行随机抽奖,threading进行滚动,tkinter实现窗口

import time
import threading
import tkinter as tk  
import random   

创建窗口

root = tk.Tk()      #初始化Tk() 建立一个窗口
root.title('抽奖啦!') # 设置标题
root.minsize(700, 700)

label0 = tk.Label(root, bg='black')
label0.place(x=0, y=0, width=620, height=620)


label1 = tk.Label(root, text='纸巾', bg='yellow', font=('Arial', 20))
label1.place(x=5, y=5, width=200, height=200)

label2 = tk.Label(root, text='谢谢惠顾', bg='yellow', font=('Arial', 20))
label2.place(x=210, y=5, width=200, height=200)

label3 = tk.Label(root, text='自行车', bg='yellow', font=('Arial', 20))
label3.place(x=415, y=5, width=200, height=200)

label4 = tk.Label(root, text='再来一次', bg='yellow', font=('Arial', 20))
label4.place(x=415, y=210, width=200, height=200)

label5 = tk.Label(root, text='电视', bg='yellow', font=('Arial', 20))
label5.place(x=415, y=415, width=200, height=200)

label6 = tk.Label(root, text='IPhone X ', bg='yellow', font=('Arial', 20))
label6.place(x=210, y=415, width=200, height=200)

label7 = tk.Label(root, text='谢谢惠顾', bg='yellow', font=('Arial', 20))
label7.place(x=5, y=415, width=200, height=200)

label8 = tk.Label(root, text='美的空调', bg='yellow', font=('Arial', 20))
label8.place(x=5, y=210, width=200, height=200)

添加抽奖项

# 将所有抽奖选项添加到列表
things = [label1, label2, label3, label4, label5, label6, label7, label8]
cont =  (len(things) - 1)

starts = random.randint(0, cont)
# 是否停止标志
notround = False

滚动函数

通过添加中奖列表的中奖选项来改变中奖的概率。

# 定义滚动函数
def round():
    t = threading.Thread(target=startup) #启动start
    t.start()

# 定义开始函数
def startup():
    global starts
    global notround
    while True:
        # 检测停止按钮是否被按下
        if notround == True:
            notround = False
            return starts

        # 程序延时(跳动时间间隔)
        time.sleep(0.01)

        # 在所有抽奖选项中循环滚动
        for i in things:
            i['bg'] = 'lightSkyBlue' #开始时 底色变成天蓝
        things[starts]['bg'] = 'red' #滚动框为 红色
        starts += 1
        if starts > cont:
            starts = 0
            
# 定义停止函数
def stops():
    global notround # notround 为全局变量
    global starts

    notround = True  #停止标志位

    # 设置用户不能中奖
    # if starts in [0,2,5,7]:
    #     tmp = [1,3,4,6]
    #     starts = random.choice(tmp)

    # 设置用户一定中奖 略略略
    # tmp = [0,2,5,7]
    # starts = random.choice(tmp) 

    # 设置用户一定某个奖项
    #starts = 5

运行

# 设置启动按键 
btn1 = tk.Button(root, text='开始', bg='lightSkyBlue', font=('Arial', 50), command=round)
btn1.place(x=130, y=640, width=180, height=60)

# 设置停止按键 
btn2 = tk.Button(root, text='结束', bg='red', font=('Arial', 50), command=stops)
btn2.place(x=330, y=640, width=180, height=60)

root.mainloop()

结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值