[Python] 纯文本查看 复制代码import pandas as pd
import tkinter as tk
from tkinter import messagebox
import random
import pygame
from PIL import Image, ImageTk
# 初始化pygame
pygame.mixer.init()
# 读取Excel文件
file_path = 'data.xlsx'
df = pd.read_excel(file_path)
# 过滤出未参加的人员
unparticipated_df = df[df['参加否'] != '已参加']
# 创建主窗口
root = tk.Tk()
root.title("抽奖系统")
root.geometry("1024x768")
# 加载背景图片
bg_image = Image.open("bg.jpg")
bg_photo = ImageTk.PhotoImage(bg_image)
bg_label = tk.Label(root, image=bg_photo)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
# 播放背景音乐
pygame.mixer.music.load("背景音乐2.wav")
pygame.mixer.music.play(-1)
# 定义开始抽奖函数
def start_lottery():
pygame.mixer.music.stop()
pygame.mixer.Sound("滚动音乐1.wav").play(loops=-1)
global rolling
rolling = True
roll_names()
# 定义停止抽奖函数
def stop_lottery():
global rolling
rolling = False
pygame.mixer.Sound("中奖音乐1.wav").play()
winner = current_name.get()
winner_info = unparticipated_df[unparticipated_df['姓名'] == winner].iloc[0]
messagebox.showinfo("中奖信息", f"姓名: {winner_info['姓名']}\n部门: {winner_info['部门']}\n手机号: {winner_info['手机号']}")
update_excel(winner_info)
# 定义更新Excel文件函数
def update_excel(winner_info):
global unparticipated_df
df.loc[df['姓名'] == winner_info['姓名'], '参加否'] = '已参加'
df.loc[df['姓名'] == winner_info['姓名'], '奖项等级'] = selected_prize.get()
df.to_excel(file_path, index=False)
unparticipated_df = df[df['参加否'] != '已参加']
# 定义滚动名字函数
def roll_names():
if rolling:
name = random.choice(unparticipated_df['姓名'].tolist())
current_name.set(name)
root.after(50, roll_names)
# 定义继续下一轮抽奖函数
def next_round():
pygame.mixer.music.play(-1)
current_name.set("")
selected_prize.set("特等奖")
# 创建开始按钮
start_button = tk.Button(root, text="开始", command=start_lottery, font=("Arial", 24))
start_button.place(relx=0.1, rely=0.7, anchor=tk.CENTER)
# 创建停止按钮
stop_button = tk.Button(root, text="停止", command=stop_lottery, font=("Arial", 24))
stop_button.place(relx=0.1, rely=0.8, anchor=tk.CENTER)
# 创建继续下一轮按钮
next_round_button = tk.Button(root, text="继续下一轮摇奖", command=next_round, font=("Arial", 18))
next_round_button.place(relx=0.9, rely=0.9, anchor=tk.SE)
# 创建显示当前名字的标签
current_name = tk.StringVar()
name_label = tk.Label(root, textvariable=current_name, font=("Arial", 36), bg="white")
name_label.place(relx=0.5, rely=0.4, anchor=tk.CENTER)
# 创建选择奖项等级的下拉菜单
selected_prize = tk.StringVar(value="特等奖")
prize_options = ["特等奖", "一等奖", "二等奖", "三等奖"]
prize_menu = tk.OptionMenu(root, selected_prize, *prize_options)
prize_menu.config(font=("Arial", 24))
prize_menu.place(relx=0.5, rely=0.3, anchor=tk.CENTER)
# 运行主循环
root.mainloop()
奖项分:特等奖、一等奖、二等奖、三等奖 可以选择;背景音乐和滚动音乐、中奖音乐、背景图片自己网上下载哦!