天天看点

python随机抽号_Python 利用tkinter,编写的随机抽奖小程序

# -*- 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()