python编写实现抽奖器

所属分类: 脚本专栏 / python 阅读数: 504
收藏 0 赞 0 分享

本文实例为大家分享了python编写实现抽奖器的具体代码,供大家参考,具体内容如下

# coding=utf-8
import sys
import os
import openpyxl
if sys.version_info[0] == 2:
 import Tkinter
 from Tkinter import *
else:
 import tkinter as Tkinter
 from tkinter import *
from tkinter import messagebox

import random
data = []
going = True
is_run = False

def getNameList():
 path=os.getcwd()
 wb=openpyxl.load_workbook(r'list.xlsx')
 sheet=wb["Sheet1"]
 macList = []
 for i in range(2,sheet.max_row+1):
  macList.append(sheet.cell(row=i,column=1).value)
 return macList

def lottery_roll(var1, var2):
 global going
 if going:
 show_member = random.choice(data)
 var1.set(show_member)
 window.after(50, lottery_roll, var1, var2)
 else:
 var2.set('还有{}个小幸运鬼哟~'.format(len(data)))
 going = True
 return

def lottery_start(var1, var2):
 global is_run
 if is_run:
 messagebox.showwarning('提醒', '命运的齿轮正在疯狂转动哟!')
 return
 if len(data)==0:
  messagebox.showwarning('提醒', '没有幸运儿了哟,快去抓几个吧!')
  return
 is_run = True
 var2.set('命运的齿轮开始转动起来啦~')
 lottery_roll(var1, var2)
 
def lottery_end():
 global going, is_run, data
 if is_run:
 if len(data)==0:
  messagebox.showwarning('提醒', '没有幸运儿了哟,快去抓几个吧!')
  return
 show_member = random.choice(data)
 data.remove(show_member)
 print(show_member)
 var1.set(show_member)
 going = False
 is_run = False
 else:
 messagebox.showwarning('提醒', '命运的齿轮还没开动呢!')
 
if __name__ == '__main__':
 data = getNameList()
 window = Tkinter.Tk()
 window.geometry('800x500+500+200')
 window.title('谁是幸运儿?')
 
 bg_label = Label(window, width=800, height=500, bg='#ECf5FF')
 bg_label.place(anchor=NW, x=0, y=0)

 var_title = StringVar(value='谁是幸运儿?')
 show_label1_title = Label(window, textvariable=var_title, justify='left', anchor=CENTER, width=18, height=4, bg='#ECf5FF',
   font='楷体 -40 bold', foreground='black')
 show_label1_title.place(anchor=NW, x=200, y=0)

 var1 = StringVar(value='<.<')
 show_label1 = Label(window, textvariable=var1, justify='left', anchor=CENTER, width=7, height=2, bg='#BFEFFF',
   font='楷体 -40 bold', foreground='black')
 show_label1.place(anchor=NW, x=320, y=200)
 
 var2 = StringVar(value='共有{}个幸运儿,请开始游戏'.format(len(data)))
 show_label2 = Label(window, textvariable=var2, justify='left', anchor=CENTER, width=25, height=4, bg='#ECf5FF',
   font='楷体 -25 bold', foreground='red')
 show_label2.place(anchor=NW, x=240, y=320)

 button1 = Button(window, text='开始', command=lambda: lottery_start(var1, var2), width=14, height=2, bg='#A8A8A8',
   font='宋体 -18 bold')
 button1.place(anchor=NW, x=210, y=400)

 button2 = Button(window, text='结束', command=lambda: lottery_end(), width=14, height=2, bg='#A8A8A8',
   font='宋体 -18 bold')
 button2.place(anchor=NW, x=450, y=400)
 window.mainloop()

截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用keras实现非线性回归(两种加激活函数的方式)

这篇文章主要介绍了使用keras实现非线性回归(两种加激活函数的方式),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python线性插值解析

这篇文章主要介绍了python线性插值解析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python中flatten( ),matrix.A用法说明

这篇文章主要介绍了Python中flatten( ),matrix.A用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python cv2.resize函数high和width注意事项说明

这篇文章主要介绍了python cv2.resize函数high和width注意事项说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python 图像插值 最近邻、双线性、双三次实例

这篇文章主要介绍了python 图像插值 最近邻、双线性、双三次实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python-openCV开运算实例

这篇文章主要介绍了Python-openCV开运算实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python使用opencv resize图像不进行插值的操作

这篇文章主要介绍了python使用opencv resize图像不进行插值的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Python-OpenCV消除图像中孤立的小区域操作

这篇文章主要介绍了使用Python-OpenCV消除图像中孤立的小区域操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python右对齐的实例方法

在本篇文章里小编给大家整理的是关于python右对齐的实例方法,有需要的朋友们可以学习参考下。
收藏 0 赞 0 分享

python主要用于哪些方向

在本篇文章里小编给大家整理了一篇关于python用于的方向的相关文章,有需要的阅读下吧。
收藏 0 赞 0 分享
查看更多