Python+Tkinter 抽獎小程序


學習Python的GUI編程,編寫了一個隨機抽取姓名的小程序,主要參考資料

http://www.10tiao.com/html/383/201703/2247484257/1.html

 

import tkinter
import random
import threading
import time

#初始化窗口
root=tkinter.Tk()
root.title("隨機名單")
root.geometry('500x500+400+200')
root.resizable(False,False)
root.flag = True


#三個Lable標簽
first = tkinter.Label(root,text='',font = ("宋體", 20,"normal"))
first.place(x=180,y=100,width=150,height=100)

second = tkinter.Label(root,text='',font = ("宋體", 20,"normal"))
second['fg'] = 'red'
second.place(x=180,y=200,width=150,height=100)

third = tkinter.Label(root,text='',font = ("宋體", 20,"normal"))
third.place(x=180,y=300,width=150,height=100)


students=['楊姐姐1','楊姐姐2','楊姐姐3','楊姐姐4','楊姐姐5','楊姐姐6']

def switch():
    root.flag=True
    while root.flag:
        i=random.randint(0, len(students)-1)
        first['text']=second['text']
        second['text']=third['text']
        third['text']=students[i]
        time.sleep(0.1)


#開始按鈕
def butStartClick():
    t=threading.Thread(target=switch)
    t.start()
btnStart=tkinter.Button(root,text='開始',command=butStartClick)
btnStart.place(x=30, y=30, width=80, height=20)

#結束按鈕
def btnStopClick():
    root.flag=False
    
butStop=tkinter.Button(root,text='停止',command=btnStopClick)
butStop.place(x=160, y=30, width=80, height=20)

#啟動主程序
root.mainloop()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM