欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python编写实现抽奖器

程序员文章站 2022-04-01 16:09:11
本文实例为大家分享了python编写实现抽奖器的具体代码,供大家参考,具体内容如下# coding=utf-8import sysimport osimport openpyxlif sys.vers...

本文实例为大家分享了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()

截图:

python编写实现抽奖器

python编写实现抽奖器

python编写实现抽奖器

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

相关标签: python 抽奖器