import tkinter
import time
import threading
class choujiang:
def __init__(self):
self.root = tkinter.Tk()
self.root.title('抽奖')
self.root.minsize(300, 300)
self.isloop = False
self.newloop = False
self.setwindow()
self.count = 0
self.root.mainloop()
def setwindow(self):
self.btnstart = tkinter.Button(self.root, text="start/stop", command=self.newtask)
self.btnstart.place(x=90, y=125, width=80, height=50)
self.btn1 = tkinter.Button(self.root, text="apple", bg='white')
self.btn1.place(x=20, y=20, width=50, height=50)
self.btn2 = tkinter.Button(self.root, text="boy", bg='yellow')
self.btn2.place(x=90, y=20, width=50, height=50)
self.btn3 = tkinter.Button(self.root, text="coat", bg='blue')
self.btn3.place(x=160, y=20, width=50, height=50)
self.btn4 = tkinter.Button(self.root, text="dog", bg='black')
self.btn4.place(x=230, y=20, width=50, height=50)
self.btn5 = tkinter.Button(self.root, text="egg", bg='orange')
self.btn5.place(x=230, y=90, width=50, height=50)
self.btn6 = tkinter.Button(self.root, text="foot", bg='green')
self.btn6.place(x=230, y=160, width=50, height=50)
self.btn7 = tkinter.Button(self.root, text="girl", bg='#ccffff')
self.btn7.place(x=230, y=230, width=50, height=50)
self.btn8 = tkinter.Button(self.root, text="hat", bg='purple')
self.btn8.place(x=160, y=230, width=50, height=50)
self.btn9 = tkinter.Button(self.root, text="ice", bg='brown')
self.btn9.place(x=90, y=230, width=50, height=50)
self.btn10 = tkinter.Button(self.root, text="jelly", bg='skyblue')
self.btn10.place(x=20, y=230, width=50, height=50)
self.btn11 = tkinter.Button(self.root, text="key", bg='pink')
self.btn11.place(x=20, y=160, width=50, height=50)
self.btn12 = tkinter.Button(self.root, text="lion", bg='gray')
self.btn12.place(x=20, y=90, width=50, height=50)
self.list1 = [self.btn1, self.btn2, self.btn3, self.btn4, self.btn5, self.btn6, self.btn7, self.btn8, self.btn9,
self.btn10, self.btn11, self.btn12, ]
def rounds(self):
if self.isloop == True:
return
i = 0
while self.count > 0: # 修改的地方
if self.newloop == True:
self.isloop = False
return
time.sleep(0.1)
for each in self.list1:
each['bg'] = 'white'
self.list1[i]['bg'] = 'white'
time.sleep(0.000001)
self.list1[i]['bg'] = 'red'
i += 1
if i >= len(self.list1):
i = 0
self.count -= 1 # 修改的地方
def newtask(self):
if self.isloop == False:
if self.count == 0: # 修改的地方
self.count = 10 # 修改的地方
t = threading.Thread(target=self.rounds)
t.start()
self.isloop = True
elif self.isloop == True:
self.isloop = False
self.newloop = True
c = choujiang()
以上修改后,点击 start/stop 按钮一次将会增加可以抽奖的次数,如果要无限抽奖可以直接修改方法 newtask 中的 self.count = 0 改为 self.count = float('inf') 或者其他较大的数字即可。