本文實例為大家分享了python實現大轉盤抽獎的具體代碼,供大家參考,具體內容如下
選擇轉盤中的某一個方框,來進行抽獎
import tkinter#導入線程模塊import threadingimport time #導入代碼的sleep 代碼休眠 root = tkinter.Tk()root.title('大轉盤')root.minsize(300,300) #擺放按鈕btn1 = tkinter.Button(root,text = '櫻桃',bg = 'red')btn1.place(x = 20,y = 20,width = 50,height = 50) btn2 = tkinter.Button(root,text = '香蕉',bg = 'white')btn2.place(x = 90,y = 20,width = 50,height = 50) btn3 = tkinter.Button(root,text = '蘋果',bg = 'white')btn3.place(x = 160,y = 20,width = 50,height = 50) btn4 = tkinter.Button(root,text = '西瓜',bg = 'white')btn4.place(x = 230,y = 20,width = 50,height = 50) btn5 = tkinter.Button(root,text = '鴨梨',bg = 'white')btn5.place(x = 230,y = 90,width = 50,height = 50) btn6 = tkinter.Button(root,text = '榴蓮',bg = 'white')btn6.place(x = 230,y = 160,width = 50,height = 50) btn7 = tkinter.Button(root,text = '柚子',bg = 'white')btn7.place(x = 230,y = 230,width = 50,height = 50) btn8 = tkinter.Button(root,text = '葡萄',bg = 'white')btn8.place(x = 160,y = 230,width = 50,height = 50) btn9 = tkinter.Button(root,text = '草莓',bg = 'white')btn9.place(x = 90,y = 230,width = 50,height = 50) btn10 = tkinter.Button(root,text = '芒果',bg = 'white')btn10.place(x = 20,y = 230,width = 50,height = 50) btn11 = tkinter.Button(root,text = '荔枝',bg = 'white')btn11.place(x = 20,y = 160,width = 50,height = 50) btn12 = tkinter.Button(root,text = '甘蔗',bg = 'white')btn12.place(x = 20,y = 90,width = 50,height = 50) #將所有選項組成列表fruitlists = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12] #是否開啟循環的標志isloop = False#是否停止標志stopsign=False #是否接收到 stop信號#存儲停止id------用于進行stop后的重新啟動stopid=Nonedef round(): global isloop global stopid #判斷是否開始循環 if isloop == True: return i=1 if isinstance(stopid,int): i=stopid while True: #延時操作 time.sleep(0.2) #將所有的組件背景變為白色 for x in fruitlists: x['bg'] = 'white' #將當前數值對應的組件變色 fruitlists[i]['bg'] = 'red' #變量+1 i += 1 print('當前i為',i) #當前i,用來追蹤當前位置 #如果i大于最大索引直接歸零 if i >= len(fruitlists): i = 0 if stopsign == True:#當停止標志 為真時 isloop=False stopid =i#賦值stopid breakdef stop1(): global stopsign if stopsign ==True:#當多接收stop1()函數時 ,直接跳過 return stopsign=True#建立一個新線程的函數def newtask(): global isloop global stopsign #建立線程 stopsign=False #print(stopsign) #打印 點擊開始時的stopsign t = threading.Thread(target = round) #開啟線程運行 t.start() # 設置循環開始標志 isloop = True #開始按鈕btn_start = tkinter.Button(root,text = 'start',command = newtask)btn_start.place(x = 90,y = 125,width = 50,height = 50) #停止按鈕btn_stop = tkinter.Button(root,text = 'stop',command=stop1)btn_stop.place(x = 160,y = 125,width = 50,height = 50) root.mainloop()
新聞熱點
疑難解答