# -*- encoding:utf-8 -*-
import tkinter
import time
import threading
import random
import math
class choujiang:
# 初始化魔術方法
def __init__(self):
# 初始化人員大小
self.userList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21"] self.lengthSize = len(self.userList)
# 初始化方體的規格為 self.chusx * self.chusx ,可調整
self.chusx = 80
self.ceilnum = self.getCshCeilnum()
print("長度=", self.lengthSize)
print("第一排可放小框的個數 =", self.ceilnum)
# 計算布局框的大小,小框self.chusx*self.chusx ,x = self.ceilnum * self.chusx,y =x
self.x = self.ceilnum * self.chusx
self.y = self.x + self.chusx # y軸 預留 100 放置 開始 按鈕
print("x = ", self.x, "y = ", self.y)
# 準備好界面
self.root = tkinter.Tk()
self.root.title('方框抽獎')
self.root.minsize(self.x, self.y)
# 聲明一個是否按下開始的變量
self.isloop = False
self.newloop = False
# 調用設定界面的方法
self.girlfrends = [] self.setwindow()
self.root.mainloop()
# 計算第一排需要放的小框個數
def getCshCeilnum(self):
sqnum = math.sqrt(self.lengthSize) # 長度的開方數
ceilnum = math.ceil(sqnum) # 向上取整
while True:
# 可用方框總個數
num = ceilnum * 2 + (ceilnum - 2) * 2
# 如果清單個數大于 可用方框數, 擴張頁面布局個數+1
if self.lengthSize > num:
ceilnum = ceilnum+1
else:
break
return ceilnum
def getXYList(self):
# 生成x,y位置list,根據布局大小和 框體大小生成位置 集合
xlList = [] ylList = [] for xl in range(0, self.x, self.chusx):
xlList.append(xl)
for yl in range( self.chusx, self.y, self.chusx):
ylList.append(yl)
print(xlList)
print(ylList)
# 生成方框的x,y位置
xyList = [] for u in range(0, len(xlList)):
for j in range(0, len(ylList)):
tempx = xlList[u] tempy = ylList[j] print("組合xy=", tempx, tempy)
if tempx == 0 or tempx == self.chusx * (len(xlList) - 1) or tempy == self.chusx or tempy == self.chusx * len(ylList):
xyList.append([tempx, tempy])
print("方框組合最終位置集合=", xyList)
return xyList
# 界面布局方法
def setwindow(self):
# 開始停止按鈕
self.btn_start = tkinter.Button(self.root, text='開始/暫停', command=self.newtask, bg='#7FFF00')
self.btn_start.place(x=20, y=0, width=self.x - 40, height=self.chusx - 20)
# 生成方框的x,y位置
xyList = self.getXYList()
# 打亂随機布局
random.shuffle(xyList)
for i in range(0, len(xyList)):
try:
btntxt = "btn" + str(i)
self.btntxt = tkinter.Button(self.root, text="" + self.userList[i], bg='white')
self.btntxt.place(x=xyList[i][0], y=xyList[i][1], width=self.chusx, height=self.chusx)
self.girlfrends.append(self.btntxt)
except Exception as e:
btntxt = "btn" + str(i)
self.btntxt = tkinter.Button(self.root, text="重來", bg='white')
self.btntxt.place(x=xyList[i][0], y=xyList[i][1], width=self.chusx, height=self.chusx)
self.girlfrends.append(self.btntxt)
def rounds(self):
# 判斷是否開始循環
if self.isloop == True:
return
# 初始化計數 變量
i = 0
# 死循環
while True:
if self.newloop == True:
self.newloop = False
return
# 延時操作
time.sleep(0.1)
# 将所有的元件背景變為白色
for x in self.girlfrends:
x['bg'] = 'white'
# 将目前數值對應的元件變色
random.choice(self.girlfrends)['bg'] = 'red'
# 變量+1
i += 1
# 如果i大于最大索引直接歸零
if i >= len(self.girlfrends):
i = 0
# 建立一個新線程的函數
def newtask(self):
if self.isloop == False:
# 建立線程
t = threading.Thread(target=self.rounds)
# 開啟線程運作
t.start()
# 設定循環開始标志
self.isloop = True
elif self.isloop == True:
self.isloop = False
self.newloop = True
if __name__ == '__main__':
# 抽獎小腳本開始拉
c = choujiang()