forked from pengfexue2/lucky_draw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lucky_draw.py
217 lines (170 loc) · 7.59 KB
/
lucky_draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
# encoding: utf-8
# @Time : 2019-12-13 15:59
__author__ = 'Ted'
# Version for Windows
import tkinter
from tkinter import font,messagebox
import time
import threading
import random
import pandas as pd
from PIL import ImageTk, Image
import copy
import sys
class LuckyDraw:
def __init__(self,data):
# 准备好界面
self.root = tkinter.Tk()
self.root.title("2019 圣诞大抽奖")
self.root.geometry('1360x700+0+0') # 定义界面大小
self.root.resizable(False, False)
# 添加背景图片
self.canvas = tkinter.Canvas(self.root,
width=1360, # 指定Canvas组件的宽度
height=700, # 指定Canvas组件的高度
bg='white') # 指定Canvas组件的背景色
self.image = Image.open("static/background.png")
self.im = ImageTk.PhotoImage(self.image)
self.canvas.create_image(680, 350, image=self.im) # 使用create_image将图片添加到Canvas组件中
self.canvas.pack()
reset_img = Image.open("static/reset.png")
res = ImageTk.PhotoImage(reset_img)
self.label_reset = tkinter.Label(self.root, image=res)
self.label_reset.place(x=180, y=550, width=140, height=36)
self.label_reset.bind("<Button-1>",self.reset)
nose_img = Image.open("static/nose.png")
nose = ImageTk.PhotoImage(nose_img)
self.label_start = tkinter.Label(self.root, image=nose)
self.label_start.place(x=660, y=370, width=60, height=42)
self.label_start.bind("<Button-1>",self.newtask)
img_03 = Image.open("static/003.png")
label_03 = ImageTk.PhotoImage(img_03)
self.third = tkinter.Label(self.root, image=label_03)
self.third.place(x=926, y=533, width=80, height=54)
self.third.bind("<Button-1>",self.set_third)
img_02 = Image.open("static/002.png")
label_02 = ImageTk.PhotoImage(img_02)
self.second = tkinter.Label(self.root, image=label_02)
self.second.place(x=1218, y=362, width=80, height=55)
self.second.bind("<Button-1>",self.set_second)
img_01 = Image.open("static/001.png")
label_01 = ImageTk.PhotoImage(img_01)
self.first = tkinter.Label(self.root, image=label_01)
self.first.place(x=1108, y=35, width=80, height=54)
self.first.bind("<Button-1>",self.set_first)
# 添加键盘控制
self.root.bind("1", self.set_first)
self.root.bind("2", self.set_second)
self.root.bind("3", self.set_third)
self.root.bind("<space>", self.newtask)
self.root.bind("<Escape>", self.close)
# 声明一个是否按下开始的变量
self.isloop = False
self.newloop = False
self.original = copy.deepcopy(data)
self.data = data
#调用设置界面的方法
self.setwindow()
self.root.mainloop()
#界面布局方法
def setwindow(self):
# self.btn_reset = tkinter.Button(self.root, text = '重启', command = self.reset ,bg='gold')
# self.btn_reset.place(x=180, y=550, width=100, height=50)
#开始停止按钮
# self.btn_start = tkinter.Button(self.root, text = '开始/停止', command = self.newtask,bg='gold')
# self.btn_start.place(x=660, y=370, width=60, height=40)
# self.third = tkinter.Button(self.root, text = '三等奖', command = self.set_third,bg='gold')
# self.third.place(x=920, y=530, width=80, height=50)
# self.second = tkinter.Button(self.root, text = '二等奖', command = self.set_second,bg='gold')
# self.second.place(x=960, y=380, width=80, height=50)
# self.first = tkinter.Button(self.root, text = '一等奖', command = self.set_first,bg='gold')
# self.first.place(x=1070, y=180, width=80, height=50)
displayfont = font.Font(size=22)
self.btn1 = tkinter.Button(self.root, text='', bg='lightyellow',font=displayfont)
# self.btn1.config(font=("Courier", 30))
self.btn1.place(x=640, y=500, width=100, height=70)
self.source = tkinter.Text(self.root,bg="navajowhite",fg="dimgray")
self.source.place(x=100, y=250, width=300, height=290)
self.source.insert(tkinter.END,("、").join(self.data))
myFont = font.Font(size=20)
self.target_3 = tkinter.Listbox(self.root,height=5,font=myFont,bg="burlywood",bd=1,fg="saddlebrown")
self.target_3.place(x=1020, y=455, width=110, height=145)
self.target_3_sub = tkinter.Listbox(self.root,height=5,font=myFont,bg="burlywood",bd=1,fg="saddlebrown")
self.target_3_sub.place(x=1170, y=455, width=110, height=145)
self.target_2 = tkinter.Listbox(self.root,height=5,font=myFont,bg="gainsboro",bd=1,fg="dimgrey")
self.target_2.place(x=1095, y=295, width=110, height=145)
self.target_1 = tkinter.Listbox(self.root,height=5,font=myFont,bg="lemonchiffon",bd=1,fg="olive")
self.target_1.place(x=1095, y=135, width=110, height=145)
self.target = self.target_3
def rounds(self):
# 判断是否开始循环
if self.isloop == True:
return
# 初始化计数 变量
i = 0
# 死循环
while True:
if self.newloop == True:
self.newloop = False
if self.target.size()>=5:
if self.target==self.target_3:
self.target=self.target_3_sub
else:
self.btn1['text'] = "Bye~"
tkinter.messagebox.showinfo('提示', "本轮抽奖结束!")
return
self.target.insert(tkinter.END, r.center(5," "))
self.data.remove(temp)
self.source.delete(1.0,'end')
self.source.insert(tkinter.END, ("、").join(self.data))
return
# 延时操作
time.sleep(0.1)
temp = random.choice(self.data)
if len(temp) == 2:
r = temp[0]+f"{chr(12288)}"+temp[1]
else:
r = temp
self.btn1['text'] = r
# 建立一个新线程的函数
def newtask(self,event):
if self.isloop == False:
# 建立线程
t = threading.Thread(target = self.rounds)
# 开启线程运行
t.start()
# 设置循环开始标志ask(self):nknewtas
self.isloop = True
elif self.isloop == True:
self.isloop = False
self.newloop = True
def set_third(self,event):
if self.target_3.size()>=5:
if self.target_3_sub.size()>=5:
tkinter.messagebox.showinfo('提示',"三等奖(10位)已产生!")
else:
self.target = self.target_3_sub
else:
self.target = self.target_3
def set_second(self,event):
if self.target_2.size()>=5:
tkinter.messagebox.showinfo('提示',"二等奖(5位)已产生!")
else:
self.target = self.target_2
def set_first(self,event):
if self.target_1.size()>=5:
tkinter.messagebox.showinfo('提示',"一等奖(5位)已产生!")
else:
self.target = self.target_1
def reset(self,event):
self.data = copy.deepcopy(self.original)
self.setwindow()
def close(self,event):
self.root.withdraw()
sys.exit()
if __name__ == '__main__':
data = pd.read_excel("demo.xlsx")
name_list=[item for item in data['名字']]
print(name_list)
c = LuckyDraw(name_list)