class Luckgame:
#导入弹窗模块
import tkinter.messagebox as mbox
#导入tkinter
import tkinter as tk
#导入线程模块
import threading
import time
def __init__(self):
self.window = self.tk.Tk()
self.window.title('CSGO幸运抽奖')
self.window.minsize(300, 300)
#设置初始变量
#设置界面
self.set_label_btn()
#设置菜单
self.set_menu()
#奖品列表
self.gifts = [self.lab1, self.lab2, self.lab3, self.lab4, self.lab5, self.lab6, self.lab7, self.lab8, self.lab9, self.lab10, self.lab11, self.lab12]
# #是否开启循环的标志
# self.isloop = False
#是否终止转盘
self.run_flag = False
self.window.mainloop()
#设置菜单
def set_menu(self):
#创建总菜单
menubar = self.tk.Menu(self.window)
#创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=0)
#创建下来菜单的选项
filemenu.add_command(label="退出游戏", command=self.window.quit)
#print author的函数
def show_author():
self.mbox.showinfo(message='Wiz333@XDL 2017')
filemenu.add_command(label="作者", command=show_author)
#将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label="操作", menu=filemenu)
#显示总菜单
self.window.config(menu=menubar)
def set_label_btn(self):
self.lab1 = self.tk.Label(self.window, text='USP\n深蓝之网', bg='red')
self.lab1.place(x=20, y=20, width=50, height=50)
self.lab2 = self.tk.Label(self.window, text='AK47\n红线', bg='white')
self.lab2.place(x=90, y=20, width=50, height=50)
self.lab3 = self.tk.Label(self.window, text='AK47\n火神', bg='white')
self.lab3.place(x=160, y=20, width=50, height=50)
self.lab4 = self.tk.Label(self.window, text='格洛克\n鼹鼠', bg='white')
self.lab4.place(x=230, y=20, width=50, height=50)
self.lab5 = self.tk.Label(self.window, text='M4A4\n弹雨', bg='white')
self.lab5.place(x=230, y=90, width=50, height=50)
self.lab6 = self.tk.Label(self.window, text='AK47\n火蛇', bg='white')
self.lab6.place(x=230, y=160, width=50, height=50)
self.lab7 = self.tk.Label(self.window, text='爪子刀\n红网', bg='white')
self.lab7.place(x=230, y=230, width=50, height=50)
self.lab8 = self.tk.Label(self.window, text='AWP\n巨龙之魂', bg='white')
self.lab8.place(x=160, y=230, width=50, height=50)
self.lab9 = self.tk.Label(self.window, text='刺刀\n多普勒', bg='white')
self.lab9.place(x=90, y=230, width=50, height=50)
self.lab10 = self.tk.Label(self.window, text='蝴蝶刀\n多普勒', bg='white')
self.lab10.place(x=20, y=230, width=50, height=50)
self.lab11 = self.tk.Label(self.window, text='M9刺刀\n红网', bg='white')
self.lab11.place(x=20, y=160, width=50, height=50)
self.lab12 = self.tk.Label(self.window, text='M4A4\n咆哮', bg='white')
self.lab12.place(x=20, y=90, width=50, height=50)
#开始按钮
self.btn_go = self.tk.Button(self.window, text='go', command=self.start_stak)
self.btn_go.place(x=90, y=125, width=50, height=50)
#停止按钮
self.btn_stop = self.tk.Button(self.window, text='stop', command=self.stop_run)
self.btn_stop.place(x=160, y=125, width=50, height=50)
def run_gifts(self):
#while循环计数用变量i
i = 0
#循环所有label,依次改变label的颜色,将其变为红色
while True:
if self.run_flag:
self.mbox.showinfo(title='中奖提示',message='获得:'+self.gifts[i-1]['text']+'一个')
#是否循环转盘参数重置
self.run_flag = False
return
try:
#添加一个延时操作
self.time.sleep(0.1)
#循环所有label,将所有label的颜色变为白色
for v in self.gifts:
v['bg'] = 'white'
self.gifts[i]['bg'] = 'red'
i += 1
#label组件计数归零
if i >= len(self.gifts):
i = 0
except:
pass
def stop_run(self):
self.run_flag = True
#开启线程,单独跑一个循环的函数
def start_stak(self):
#新建线程
tread = self.threading.Thread(target=self.run_gifts)
#开始线程
tread.start()
# #打开循环线程的标志
# self.isloop = True
#调用Luckgame游戏对象
wiz = Luckgame()
友情提示:
Python学习交流、资源共享群:563626388 QQ