python實(shí)現(xiàn)大轉(zhuǎn)盤抽獎效果
本文實(shí)例為大家分享了python實(shí)現(xiàn)大轉(zhuǎn)盤抽獎的具體代碼,供大家參考,具體內(nèi)容如下
選擇轉(zhuǎn)盤中的某一個方框,來進(jìn)行抽獎
import tkinter
#導(dǎo)入線程模塊
import threading
import time #導(dǎo)入代碼的sleep 代碼休眠
root = tkinter.Tk()
root.title('大轉(zhuǎn)盤')
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)
#將所有選項(xiàng)組成列表
fruitlists = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]
#是否開啟循環(huán)的標(biāo)志
isloop = False
#是否停止標(biāo)志
stopsign=False #是否接收到 stop信號
#存儲停止id------用于進(jìn)行stop后的重新啟動
stopid=None
def round():
global isloop
global stopid
#判斷是否開始循環(huán)
if isloop == True:
return
i=1
if isinstance(stopid,int):
i=stopid
while True:
#延時操作
time.sleep(0.2)
#將所有的組件背景變?yōu)榘咨?
for x in fruitlists:
x['bg'] = 'white'
#將當(dāng)前數(shù)值對應(yīng)的組件變色
fruitlists[i]['bg'] = 'red'
#變量+1
i += 1
print('當(dāng)前i為',i) #當(dāng)前i,用來追蹤當(dāng)前位置
#如果i大于最大索引直接歸零
if i >= len(fruitlists):
i = 0
if stopsign == True:#當(dāng)停止標(biāo)志 為真時
isloop=False
stopid =i#賦值stopid
break
def stop1():
global stopsign
if stopsign ==True:#當(dāng)多接收stop1()函數(shù)時 ,直接跳過
return
stopsign=True
#建立一個新線程的函數(shù)
def newtask():
global isloop
global stopsign
#建立線程
stopsign=False
#print(stopsign) #打印 點(diǎn)擊開始時的stopsign
t = threading.Thread(target = round)
#開啟線程運(yùn)行
t.start()
# 設(shè)置循環(huán)開始標(biāo)志
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()
效果圖:

就是上圖這個界面了:
start 開始按鈕
stop 結(jié)束按鈕
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
我們?yōu)槭裁匆獪p少Python中循環(huán)的使用
這篇文章主要介紹了我們?yōu)槭裁匆獪p少Python中循環(huán)的使用,我將闡述 Python 提供的一些簡單但是非常有用的結(jié)構(gòu),一些小技巧以及一些我在數(shù)據(jù)科學(xué)工作中遇到的案例。我將討論 Python 中的 for 循環(huán),以及如何盡量避免使用它們,需要的朋友可以參考下2019-07-07
訊飛webapi語音識別接口調(diào)用示例代碼(python)
這篇文章主要介紹了如何使用Python3調(diào)用訊飛WebAPI語音識別接口,重點(diǎn)解決了在處理語音識別結(jié)果時判斷是否為最后一幀的問題,通過運(yùn)行代碼并總結(jié)經(jīng)驗(yàn),解決了常見的模塊和屬性錯誤,需要的朋友可以參考下2025-03-03
Python報錯too?many?values?to?unpack問題及解決
這篇文章主要介紹了Python報錯too?many?values?to?unpack問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
python斯皮爾曼spearman相關(guān)性分析實(shí)例
這篇文章主要為大家介紹了python斯皮爾曼spearman相關(guān)性分析實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Python中用表格格式打印列表的兩種實(shí)現(xiàn)
本文將詳細(xì)介紹如何在 Python 中以表格格式打印列表,以便更好地展示和呈現(xiàn)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式
今天小編就為大家分享一篇Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

