写个代码 完成智能抽奖系统 2 系统中完成用户的号码输入、兑奖号的随机生 成、抽奖数据对比与校验、积分系统融合。 3抽奖成功后显示中奖信息(中奖号码、中奖等 级、奖品、兑奖时间等)
时间: 2023-07-10 15:40:20 浏览: 36
好的,以下是一个简单的智能抽奖系统的Python代码实现,其中使用了tkinter模块实现了窗体界面,使用了sqlite3模块实现了数据库存储,具体实现如下:
```python
import tkinter as tk
import random
import time
import sqlite3
# 创建主窗口
window = tk.Tk()
window.title("智能抽奖系统")
window.geometry("500x400")
# 创建数据库
conn = sqlite3.connect('lottery.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS lottery
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
number INT,
prize TEXT,
time TEXT,
points INT)''')
conn.commit()
# 创建控件
label1 = tk.Label(window, text="用户号码输入:")
label1.pack()
entry1 = tk.Entry(window, width=50)
entry1.pack()
label2 = tk.Label(window, text="兑奖号随机生成:")
label2.pack()
entry2 = tk.Entry(window, width=50)
entry2.pack()
btn_generate = tk.Button(window, text="随机生成兑奖号")
btn_generate.pack()
label3 = tk.Label(window, text="")
label3.pack()
btn_lottery = tk.Button(window, text="开始抽奖")
btn_lottery.pack()
label4 = tk.Label(window, text="中奖信息显示:")
label4.pack()
text = tk.Text(window, width=50, height=10)
text.pack()
# 生成兑奖号
def generate():
number = random.randint(100000, 999999)
entry2.delete(0, tk.END)
entry2.insert(0, number)
label3.config(text="已生成兑奖号码")
# 抽奖
def lottery():
# 获取用户输入的号码和兑奖号码
user_number = entry1.get()
prize_number = entry2.get()
# 对比号码并统计中奖信息
if user_number == prize_number:
prize = "一等奖"
points = 500
elif user_number[-3:] == prize_number[-3:]:
prize = "二等奖"
points = 200
elif user_number[-2:] == prize_number[-2:]:
prize = "三等奖"
points = 100
else:
prize = "未中奖"
points = 0
# 保存中奖信息到数据库
name = "用户" + str(random.randint(1, 100))
time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
c.execute("INSERT INTO lottery (name,number,prize,time,points) VALUES (?,?,?,?,?)",
(name, user_number, prize, time_str, points))
conn.commit()
# 显示中奖信息
text.insert(tk.END, "中奖号码:" + prize_number + "\n")
text.insert(tk.END, "中奖等级:" + prize + "\n")
text.insert(tk.END, "奖品:" + "礼品一份" + "\n")
text.insert(tk.END, "兑奖时间:" + time_str + "\n\n")
# 关闭窗口时关闭数据库连接
def on_closing():
conn.close()
window.destroy()
# 绑定事件
btn_generate.config(command=generate)
btn_lottery.config(command=lottery)
window.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()
```
需要注意的是,这仅是一个简单的实现,具体实现还需要根据您的需求进行相应的调整和完善。