利用tkinter设计一个简单的抽奖程序
拥有开始和停止功能:引用了tkinter模块和time模块和threading模块
import tkinter
import time
import threading
root = tkinter.Tk()
root.title('抽奖')
root.minsize(300,310)
label1 = tkinter.Label(root,text = 'iphone x',bg = 'red')
label1.place(x=5,y=10,width = 60,height = 40)
label2 = tkinter.Label(root,text = '话费50',bg = 'white')
label2.place(x=80,y=10,width = 60,height = 40)
label3 = tkinter.Label(root,text = '再来一次',bg = 'white')
label3.place(x=155,y=10,width = 60,height = 40)
label4 = tkinter.Label(root,text = '话费30',bg = 'white')
label4.place(x=230,y=10,width = 60,height = 40)
label5 = tkinter.Label(root,text = '100券',bg = 'white')
label5.place(x=230,y=60,width = 60,height = 40)
label6 = tkinter.Label(root,text = '再来一次',bg = 'white')
label6.place(x=230,y=110,width = 60,height = 40)
label7 = tkinter.Label(root,text = '流量300M',bg = 'white')
label7.place(x=230,y=160,width = 60,height = 40)
label8 = tkinter.Label(root,text = '话费100',bg = 'white')
label8.place(x=230,y=210,width = 60,height = 40)
label9 = tkinter.Label(root,text = '再来一次',bg = 'white')
label9.place(x=230,y=260,width = 60,height = 40)
label10 = tkinter.Label(root,text = '话费50',bg = 'white')
label10.place(x=155,y=260,width = 60,height = 40)
label11 = tkinter.Label(root,text = '100券',bg = 'white')
label11.place(x=80,y=260,width = 60,height = 40)
label12 = tkinter.Label(root,text = '流量300M',bg = 'white')
label12.place(x=5,y=260,width = 60,height = 40)
label13 = tkinter.Label(root,text = '再来一次',bg = 'white')
label13.place(x=5,y=210,width = 60,height = 40)
label14 = tkinter.Label(root,text = '话费30',bg = 'white')
label14.place(x=5,y=160,width = 60,height = 40)
label15 = tkinter.Label(root,text = '100券',bg = 'white')
label15.place(x=5,y=110,width = 60,height = 40)
label16 = tkinter.Label(root,text = '再来一次',bg = 'white')
label16.place(x=5,y=60,width = 60,height = 40)
#将所有抽奖选项添加到列表中
things = [label1,label2,label3,label4,label5,label6,label7,label8,label9,label10,label11,label12,label13,label15,label16]
#获取列表的最大索引值
maxvalue = len(things) - 1
#设置起始值
starts = 0
#是否停止标志
notround = False
#定义开始函数
def round():
t = threading.Thread(target = started)
t.start()
def started():
global starts
global notround
while True:
#检测停止按钮是否被按下
if notround == True:
notround = False
return starts
#程序延时
time.sleep(0.05)
for i in things:
i['bg'] = 'white'
things[starts]['bg'] = 'red'
starts += 1
if starts > maxvalue:
starts = 0
def stops():
global notround
notround = True
btn1 = tkinter.Button(root,text = '开始',bg = 'white',command = round)
btn1.place(x=80,y=80,width = 130,height = 40)
btn2 = tkinter.Button(root,text = '停止',bg = 'white',command = stops)
btn2.place(x=80,y=180,width = 130,height = 40)
root.mainloop()
Python学习交流、资源共享群:563626388 QQ