编写python代码实现简单抽奖器
作者:静就是空 时间:2023-04-07 12:32:48
这篇文章主要为大家详细介绍了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()
截图:
来源:https://www.cnblogs.com/ypha/p/13796190.html
标签:python,抽奖
0
投稿
猜你喜欢
python中return不返回值的问题解析
2021-04-26 20:36:07
vue中keep-alive组件的入门使用教程
2023-07-02 16:38:51
利用django和mysql实现一个简单的web登录页面
2024-01-13 18:16:16
pandas:get_dummies()与pd.factorize()的用法及区别说明
2023-07-10 22:17:32
MySQL获得当前日期时间函数示例详解
2024-01-27 07:54:48
浅析pandas随机排列与随机抽样
2022-03-29 21:19:00
Python连接Redis的基本配置方法
2023-09-29 21:28:27
JavaScript实现x秒后自动跳转到一个页面
2024-04-18 10:00:55
python非单一.py文件用Pyinstaller打包发布成exe
2021-12-19 00:40:05
python连接mysql并提交mysql事务示例
2024-01-15 04:43:37
Flask模板引擎之Jinja2语法介绍
2021-11-15 21:08:11
Python绘图模块 turtle案例代码
2022-12-16 01:28:10
更新pip3与pyttsx3文字语音转换的实现方法
2021-04-29 13:55:36
oracle锁表该如何解决
2024-01-23 20:28:38
vue中的路由传值与重调本路由改变参数
2024-04-27 16:10:12
Go语言原子操作及互斥锁的区别
2024-04-26 17:23:45
python编程通过蒙特卡洛法计算定积分详解
2022-05-31 10:45:01
TensorFlow索引与切片的实现方法
2022-11-08 16:00:12
PHP PDOStatement::getColumnMeta讲解
2023-06-22 08:34:14
asp下用OracleInProcServer完成对Oracle的连接和操作
2008-04-13 07:10:00