使用Python实现简单的随机数字抽奖

记录我的Python之路

1、使用Python实现简单的随机数字抽奖


#使用Python实现简单的随机数字抽奖
运行效果图如下:
使用Python实现简单的随机数字抽奖_第1张图片

源代码如下:

import tkinter
import time
import threading
import random

class Choujiang:
#初始化魔术方法
    def __init__(self):
        #准备好界面
        self.root = tkinter.Tk()
        self.root.title('python实现简单的随机数字抽奖')
        self.root.minsize(600, 600)
        # 声明一个是否按下开始的变量
        self.isloop = False
        self.newloop = False
        #调用设置界面的方法
        self.setwindow()
        self.root.mainloop()

    #界面布局方法
    def setwindow(self):
        #开始停止按钮
        self.btn_start = tkinter.Button(self.root, text = '开始/停止', command = self.newtask,bg='gold')
        self.btn_start.place(x=250, y=250, width=100, height=50)

        self.btn1 = tkinter.Button(self.root, text='', bg='white')
        self.btn1.config(font=("Courier", 50))
        self.btn1.place(x=200, y=130, width=200, height=50)
        # self.girlfrends = list(range(1000))


    def rounds(self):
        # 判断是否开始循环
        if self.isloop == True:
            return

        # 初始化计数 变量
        i = 0
        # 死循环
        while True:
            if self.newloop == True:
                self.newloop = False
                return

            # 延时操作
            time.sleep(0.1)
            # 从1到66之间取一个随机整数
            r = random.choice(range(1,67))
            self.btn1['text'] = r

    # 建立一个新线程的函数
    def newtask(self):
        if self.isloop == False:
            # 建立线程
            t = threading.Thread(target = self.rounds)
            # 开启线程运行
            t.start()
            # 设置循环开始标志ask(self):nknewtas
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True

c = Choujiang()

2、使用Python实现简单的幸运转盘抽奖


运行效果图如下:
使用Python实现简单的随机数字抽奖_第2张图片

源代码如下:

import tkinter
import tkinter.messagebox
import threading
import time

class LuckAward:
    #初始化对象
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('Python实现幸运转盘抽奖')
        self.root.minsize(600, 600)
        self.root['bg'] = 'cyan'

        #设置布局
        self.pack_labels()
        # 将所有标签组件组成一个列表
        self.label_lists = [self.label11, self.label12, self.label13, self.label14, self.label24, self.label34, self.label44, self.label43, self.label42, self.label41,self.label31, self.label21]
        #初始化开始循环状态的值
        self.isrotate = False
        #是否终止转盘的值
        self.stop_run_sign = False
        #设置初始化列表索引值
        self.i = 0

        self.root.mainloop()
    #设置抽奖程序布局
    def pack_labels(self):
        #设置图片
        self.bm1 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/苹果.png')
        self.bm2 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/菠萝.png')
        self.bm3 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/西瓜.png')
        self.bm4 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/樱桃.png')
        self.bm5 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/草莓.png')
        self.bm6 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/橙子.png')
        self.bm7 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/芒果.png')
        self.bm8 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/石榴.png')
        self.bm9 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/香蕉.png')
        self.bm10 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/葡萄.png')
        self.bm11 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/蜜桃.png')
        self.bm12 = tkinter.PhotoImage(file='C:/Users/Administrator/Pictures/酸角.png')

        self.label11 = tkinter.Label(self.root,text='苹果', bg='white',image = self.bm1)
        self.label11.place(relx=0.08, rely=0.08, relwidth=0.15, relheight=0.15)

        self.label12 = tkinter.Label(self.root,text='菠萝', bg='white',image = self.bm2)
        self.label12.place(relx=0.31, rely=0.08, relwidth=0.15, relheight=0.15)

        self.label13 = tkinter.Label(self.root,text='西瓜', bg='white',image = self.bm3)
        self.label13.place(relx=0.54, rely=0.08, relwidth=0.15, relheight=0.15)

        self.label14 = tkinter.Label(self.root,text='樱桃', bg='white',image = self.bm4)
        self.label14.place(relx=0.77, rely=0.08, relwidth=0.15, relheight=0.15)

        self.label21 = tkinter.Label(self.root,text='草莓', bg='white',image = self.bm5)
        self.label21.place(relx=0.08, rely=0.31, relwidth=0.15, relheight=0.15)

        self.label24 = tkinter.Label(self.root,text='橙子', bg='white',image = self.bm6)
        self.label24.place(relx=0.77, rely=0.31, relwidth=0.15, relheight=0.15)

        self.label31 = tkinter.Label(self.root,text='芒果', bg='white',image = self.bm7)
        self.label31.place(relx=0.08, rely=0.54, relwidth=0.15, relheight=0.15)

        self.label34 = tkinter.Label(self.root,text='石榴', bg='white',image = self.bm8)
        self.label34.place(relx=0.77, rely=0.54, relwidth=0.15, relheight=0.15)

        self.label41 = tkinter.Label(self.root,text='香蕉', bg='white',image = self.bm9)
        self.label41.place(relx=0.08, rely=0.77, relwidth=0.15, relheight=0.15)

        self.label42 = tkinter.Label(self.root,text='葡萄', bg='white',image = self.bm10)
        self.label42.place(relx=0.31, rely=0.77, relwidth=0.15, relheight=0.15)

        self.label43 = tkinter.Label(self.root,text='蜜桃', bg='white',image = self.bm11)
        self.label43.place(relx=0.54, rely=0.77, relwidth=0.15, relheight=0.15)

        self.label44 = tkinter.Label(self.root,text='酸角', bg='white',image = self.bm12)
        self.label44.place(relx=0.77, rely=0.77, relwidth=0.15, relheight=0.15)

        #摆放开始/停止按钮组件
        self.btn1 = tkinter.Button(self.root,text='Start', bg='green', font=('黑体', 20), command=self.new_threading)
        self.btn1.place(relx=0.26, rely=0.40, relwidth=0.2, relheight=0.15)

        self.btn2 = tkinter.Button(self.root,text='Stop', bg='red', font=('黑体', 20), command=self.stop)
        self.btn2.place(relx=0.54, rely=0.40, relwidth=0.2, relheight=0.15)

    def start_run(self):

        #判断循环是否开始
        if self.isrotate == True:#已经开始,则不再进行新的循环
            return
        # while 循环遍历label_lists列表,依次将label变为红色
        while True:
            #判断循环开始或是停止
            if self.stop_run_sign:#若是按下停止按钮,退出当前函数
                return
            try:
                # 设置时间间隔
                time.sleep(0.05)
                # 将所有组件变成白色的
                for label in self.label_lists:
                    label['bg'] = 'white'
                # 将当前数值对应的组件变色
                self.label_lists[self.i]['bg'] = 'red'
                # 变量+1
                self.i += 1
                # 如果i大于最大索引,将i值设置为0
                if self.i == len(self.label_lists):
                    self.i = 0
            except:
                print('程序错误,请重新尝试')


    #新建一个线程的函数
    def new_threading(self):
        #设置转盘停止状态的值为False
        self.stop_run_sign = False
        # 建立新线程
        new = threading.Thread(target=self.start_run)
        # 开始运行新的线程
        new.start()
        # 设置循环开始的值
        self.isrotate = True

    #新建一个停止转盘的方法
    def stop(self):
        #设置停止转盘的值
        self.stop_run_sign = True
        #判断循环开始的状态值
        if self.isrotate == False:
            #弹出‘未开始点击结束’的提示信息
            result = tkinter.messagebox.askokcancel(title = '提示框',message = '您还没有开始抽奖,是否开始抽奖?')
            if result == True:
                self.new_threading()
            else:
                tkinter.messagebox.showinfo(title='提示框', message='您已取消抽奖,欢迎下次使用!')
        #已开始抽奖,按下结束键
        else:
            tkinter.messagebox.showinfo(title='提示信息', message='恭喜您,您抽中了:' + self.label_lists[self.i]['text'])
            # 设置循环状态的值
            self.isrotate = False

luckaward = LuckAward()

水果图像文件:https://download.csdn.net/download/Darcy_JU/20628127?spm=1001.2014.3001.5501

你可能感兴趣的:(Python,python)