程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python實現輪盤抽獎小程序(帶界面)

編輯:Python

效果展示:

 具體需求:

1.點擊開始按鈕後,輪盤開始轉動;點擊結束後,彈窗彈出對應獎品

2.輪盤開始轉動後,在每個獎品停留0.2秒

2.菜單項中有作者和退出選項;選擇作者選項,彈窗彈出作者姓名,點擊退出程序後退出程序

源碼:

import tkinter as tk
import time
import threading
import tkinter.messagebox as tkbox
win = tk.Tk()
win.title("幸運抽獎")
win.geometry("400x400")
bg = "white"
sleep = True
vary_sleep = True
text1 = tk.Label(win,text="獎品1:\n卡通水杯",bg=bg,height=3,width=8)
text1.place(x=30,y=20)
text2 = tk.Label(win,text="獎品2:\n體重秤",bg=bg,height=3,width=8)
text2.place(x=115,y=20)
text3 = tk.Label(win,text="獎品3:\n旺仔QQ糖",bg=bg,height=3,width=8)
text3.place(x=200,y=20)
text4 = tk.Label(win,text="獎品4:\n秘制小漢堡",bg=bg,height=3,width=8)
text4.place(x=285,y=20)
text5 = tk.Label(win,text="獎品5:\n好果汁吃",bg=bg,height=3,width=8)
text5.place(x=285,y=85)
text6 = tk.Label(win,text="獎品6:\n王甜心餃子",bg=bg,height=3,width=8)
text6.place(x=285,y=150)
text7 = tk.Label(win,text="獎品7:\n暑假密卷",bg=bg,height=3,width=8)
text7.place(x=285,y=215)
text8 = tk.Label(win,text="獎品8:\n旺仔套裝",bg=bg,height=3,width=8)
text8.place(x=285,y=280)
text9 = tk.Label(win,text="獎品9:\n大逼斗",bg=bg,height=3,width=8)
text9.place(x=200,y=280)
text10 = tk.Label(win,text="獎品10:\n戰斧牛排",bg=bg,height=3,width=8)
text10.place(x=115,y=280)
text11 = tk.Label(win,text="獎品11:\n放假",bg=bg,height=3,width=8)
text11.place(x=30,y=285)
text12 = tk.Label(win,text="獎品12:\n搬家去雲南",bg=bg,height=3,width=8)
text12.place(x=30,y=215)
text13 = tk.Label(win,text="獎品13:\n高考加分",bg=bg,height=3,width=8)
text13.place(x=30,y=150)
text14 = tk.Label(win,text="獎品14:\n海賊王",bg=bg,height=3,width=8)
text14.place(x=30,y=85)
text_list = [text1,text2,text3,text4,text5,text6,text7,text8,text9,text10,text11,text12,text13,text14]
def menu():
tkbox.showinfo("作者", "李狗蛋")
mainmenu = tk.Menu(win)
filemenu = tk.Menu(mainmenu,tearoff=False)
mainmenu.add_cascade (label="操作",menu=filemenu)
filemenu.add_command (label="作者",command=menu)
filemenu.add_command (label="退出",command=win.quit)
win.config (menu=mainmenu)
def end_code():
global vary_sleep
vary_sleep = False
def rounds():
global sleep,vary_sleep
if sleep==True:
x = 0
while True:
if vary_sleep==False:
value = text_list[x-1]['text']
tkbox.showinfo("感謝您的關注","恭喜獲得:{}".format(value))
tkbox.showinfo("如果對您有幫助", "請前往某某地 領取您的獎品!!!")
return
else:
time.sleep(0.2)
for i in text_list:
i['bg'] = "white"
text_list[x]['bg'] = 'red'
x += 1
if x >= len(text_list):
x = 0
else:
return
def start():
t = threading.Thread(target=rounds)
t.start()
start_button = tk.Button(win,text="開始",height=4,width=10,command=start)
start_button.place(x=105,y=125)
start_button = tk.Button(win,text="結束",height=4,width=10,command=end_code)
start_button.place(x=195,y=125)
win.mainloop()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved