import csv
import tkinter as tk
import tkinter.messagebox
import datetime
import pandas as pd
import os
import random
dt = datetime.datetime.now()
time = dt.strftime("%Y-%m-%d %H:%M:%S")
#保存参与抽奖的数据
def save():
#判断文件是否存在
flag = os.path.isfile('G:/code/079/data.csv')
if flag:
#打开文件
with open('G:/code/079/data.csv', 'r',newline='') as f:
#读取数据
reader = csv.reader(f)
h1 = next(reader)
myval=[]
for row in reader:
#将“幸运码”列保存到myval列表中
myval.append(int(row[3]))
num = random.randint(0, 1000)
with open('G:/code/079/data.csv', 'a+',newline='') as f:
writer = csv.writer(f)
writer.writerow([txt_name.get(),txt_tel.get(),time,num]) # 写入一行数据
else:
num = random.randint(0, 1000)
with open('G:/code/079/data.csv', 'w',newline='') as f:# 如不指定newline='',有时会写入空行
writer = csv.writer(f)
writer.writerow(['姓名', '手机号', '参与抽奖时间','幸运码']) # 写入一行标题
writer.writerow([txt_name.get(),txt_tel.get(),time,num]) # 写入第一行数据
tkinter.messagebox.showinfo("消息提示",'您的幸运码是:'+str(num)) #弹出消息提示框
#读取数据抽取幸运码
def read():
df = pd.DataFrame(pd.read_csv('G:/code/079/data.csv',encoding = 'gbk'))
sam = df.sample(3)
tkinter.messagebox.showinfo("消息提示",'获奖幸运码:'+ str(sam.iloc[0,3]) +'、'+ str(sam.iloc[1,3]) +'、'+ str(sam.iloc[2,3]))
tkinter.messagebox.showinfo("消息提示",'获奖姓名:'+ str(sam.iloc[0,0]) +'、'+ str(sam.iloc[1,0]) +'、'+ str(sam.iloc[2,0]))
#建立主窗口window
window = tk.Tk()
#设置窗口标题栏名称
window.title('抽奖小程序')
#设置窗口的大小
window.geometry('600x390')
canvas = tk.Canvas(window, width=600, height=400)
image_file = tk.PhotoImage(file='G:/code/079/1.png')
image = canvas.create_image(320, 0, anchor='n', image=image_file)
canvas.pack(side='top')
# 在主窗口添加标签
l1 = tk.Label(window, text='姓 名:', font=('Arial', 12)).place(x=30, y=320)
l2=tk.Label(window, text='手机号:',font=('Arial', 12)).place(x=30, y=350)
# 在主窗口添加文本框
txt_name = tk.StringVar()
text1 = tk.Entry(window,textvariable=txt_name, show = None,width=45)
txt_tel= tk.StringVar()
text2 = tk.Entry(window,textvariable=txt_tel,show = None,width=45)
text1.place(x=100,y=320)
text2.place(x=100,y=350)
# 在主窗口添加命令按钮
button1 = tk.Button(window,width=10, height=3,text='点击参与',bg='tomato',command=save).place(x=430, y=310)
button2 = tk.Button(window,width=10, height=3,text='开奖',bg='orange',command=read).place(x=510, y=310)
# 主窗口循环显示
window.mainloop()
运行结果: