python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python抽奖系统

基于Python实现商场抽奖小系统

作者:顾木子吖

这篇文章主要为大家详细介绍了如何利用Python语言实现一个简单的商场抽奖小系统,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下

导语

嘿!下午好,木子来上新啦~

期待今天的内容嘛?挠头.jpg 日常等更新的小可爱们我来了。看看给大家带来了什么好东西

💦💦💦💦💦💦💦💦💦💦💦💦我是华丽的分隔符💦💦💦💦💦💦💦💦💦💦💦💦💦

今天早上出门了一趟,话说长沙的天气用一个字形容就是:”热“、二个字形容:”真热“、三个字形容:”热死人“,据说这几天的温度快达到40°了。大家记得做好防晒哦~

一出门就感受到了太阳的拥抱,泪流满面的做完事情之后跑到商场喝了杯茶颜,然后逛着街吹着免费的空调,巴适的很啊!逛商场的时候看到了转盘抽奖活动,简直不要太适合我这种想买买买(白嫖)的人。嘿嘿,嘛~说了这么多的(废话)话,揭开谜底吧!我想你们等不及了

今天的主题就是给大家制作一款商场抽奖小系统,保证你喜欢,学了不后悔系列~

一、运行环境

小编使用的环境:Python3、Pycharm社区版、Tkinter、PIL模块部分自带就不一一 展示啦。

模块安装:pip install -i https://pypi.douban.com/simple/+模块名

二、素材(图片等)

界面图片的话可以自己准备,这里不展示,想换什么背景可以自己随便找一张图就可哈。

但是要注意背景的大小尺寸问题哈。

三、代码展示

基本上每行代码都有注释的,这款代码写的挺简单的,有点儿小基础的可以看的懂哈!

如没基础小白的话可以找我先拿一些简单的视频,教你从0开始学习吧!

import time
import threading
from PIL import Image
import tkinter as tk  # 导入 tk库 模块
import random         # 导入 随机库 模块
 
root = tk.Tk()      #初始化Tk() 建立一个窗口
root.title('简易商场抽奖系统-顾木子吖') # 设置标题
root.minsize(1000, 700)
 
photo = tk.PhotoImage(file="ETA.png")  # file:图片路径
imgLabel = tk.Label(root, image=photo)  # 把图片整合到标签类中
imgLabel.pack(side=tk.RIGHT)  # 右对齐
 
label1 = tk.Label(root, text='谢谢惠顾', bg='yellow', font=('Arial', 50))
label1.place(x=0, y=600, width=390, height=250)
 
label2 = tk.Label(root, text='1000优惠券', bg='yellow', font=('Arial', 50))
label2.place(x=0, y=10, width=390, height=250)
 
label3 = tk.Label(root, text='谢谢惠顾', bg='yellow', font=('Arial', 50))
label3.place(x=390, y=10, width=390, height=250)
 
label4 = tk.Label(root, text='苹果手机', bg='yellow', font=('Arial', 50))
label4.place(x=780, y=10, width=390, height=250)
 
label5 = tk.Label(root, text='再来一次', bg='yellow', font=('Arial', 50))
label5.place(x=1170, y=10, width=390, height=250)
 
label6 = tk.Label(root, text='谢谢惠顾', bg='yellow', font=('Arial', 50))
label6.place(x=1560, y=10, width=390, height=250)
 
label7 = tk.Label(root, text='5000优惠券', bg='yellow', font=('Arial', 50))
label7.place(x=1560, y=600, width=390, height=250)
 
label8 = tk.Label(root, text='谢谢惠顾', bg='yellow', font=('Arial', 50))
label8.place(x=1170, y=600, width=390, height=250)
 
label9 = tk.Label(root, text='一台小汽车', bg='yellow', font=('Arial', 50))
label9.place(x=780, y=600, width=390, height=250)
 
label10 = tk.Label(root, text='再来一次', bg='yellow', font=('Arial', 50))
label10.place(x=390, y=600, width=390, height=250)
 
label11 = tk.Label(root, text='顾木子吖', bg='white', font=('Arial', 20))
label11.place(x=1250, y=900, width=700, height=100)
 
# 将所有抽奖选项添加到列表
things = [label1, label2, label3, label4, label5, label6, label7, label8, label9, label10]
# 获取列表的最大索引值
maxvalue = len(things) - 1
 
# 设置起始值为随机整数
starts = random.randint(0, 6)
# 是否停止标志
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.017)
 
        # 在所有抽奖选项中循环滚动
        for i in things:
            i['bg'] = 'lightSkyBlue' #开始时 底色变成天蓝
        things[starts]['bg'] = 'red' #滚动框为 红色
        starts += 1
        if starts > maxvalue:
            starts = 0
# 定义停止函数
def stops():
    global notround # notround 为全局变量
    global starts
 
    notround = True  #停止标志位
    if starts == 1:  # 如果抽中“简易四轴”就跳转为“谢谢惠顾”【奸商^_^】
        starts = 2
 
# 设置启动按键      背景文本为“RUN”  底色为“天蓝”    字体“Arial” 字体大小“50”   回调函数command 为【滚动】
btn1 = tk.Button(root, text='RUN', bg='lightSkyBlue', font=('Arial', 50), command=round)
#设置按键坐标
btn1.place(x=800, y=850, width=200, height=200)
# 设置停止按键      背景文本为“RUN”  底色为“红色”    字体“Arial” 字体大小“50”   回调函数command 为【停止】
btn2 = tk.Button(root, text='STOP', bg='red', font=('Arial', 50), command=stops)
#设置按键坐标
btn2.place(x=1000, y=850, width=200, height=200)

​四、效果展示

1)界面效果  

这个程序界面比较大,截图之后科能看的不是很清楚,大家可以自己运行看的清楚些。

2)RUN运行

鼠标点击RUN运行按住STOP停止,随机抽奖的哈。我运气还是不错的小汽车能带回家,哈哈

这里没有视频展示,都是随便截图看下效果的哈,没有那么好~

运行起来是一直再轮流随机闪动的哈。

以上就是基于Python实现商场抽奖小系统的详细内容,更多关于Python抽奖系统的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文