您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用python寫個(gè)國(guó)慶假期倒計(jì)時(shí)程序,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
下載安裝python3.9.6,進(jìn)入python官方網(wǎng)站:http://www.python.org/
點(diǎn)擊Python 3.9.6
直接安裝即可。
按win+R輸入cmd,打開控制臺(tái),輸入python -V,輸出python版本號(hào)說(shuō)明安裝成功。
##import library from tkinter import * import time from datetime import datetime,timedelta ################GUI to display window ########################## root = Tk() root.geometry('450x300') root.resizable(0,0) root.config(bg ='blanched almond') root.title('國(guó)慶倒計(jì)時(shí)') Label(root, text = '國(guó)慶倒計(jì)時(shí)' , font = 'arial 20 bold', bg ='papaya whip').pack() ############GUI to display current time####################### Label(root, font ='arial 15 bold', text = ' 當(dāng)前時(shí)間:', bg = 'papaya whip').place(x = 40 ,y = 70) #######################GUI to set the future time ########## Label(root, font ='arial 15 bold', text = ' 到達(dá)時(shí)間:', bg = 'papaya whip').place(x = 40 ,y = 110) #set year year_set = StringVar() Entry(root, textvariable =year_set , width = 4, font = 'arial 12').place(x=175, y=115) Label(root, font ='arial 15', text = '-', bg = 'papaya whip').place(x = 215 ,y = 110) year_set.set('0000') #set month month_set= StringVar() Entry(root, textvariable =month_set, width =2, font = 'arial 12').place(x=235, y=115) Label(root, font ='arial 15', text ='-', bg = 'papaya whip').place(x = 260 ,y = 110) month_set.set('00') #set day day_set= StringVar() Entry(root, textvariable =day_set, width =2, font = 'arial 12').place(x=275, y=115) day_set.set('00') # set hour hour_set= StringVar() Entry(root, textvariable =hour_set, width =2, font = 'arial 12').place(x=305, y=115) Label(root, font ='arial 15', text = ':', bg = 'papaya whip').place(x = 330 ,y = 110) hour_set.set('00') # set min min_set= StringVar() Entry(root, textvariable =min_set, width =2, font = 'arial 12').place(x=345, y=115) Label(root, font ='arial 15', text = ':', bg = 'papaya whip').place(x = 370 ,y = 110) min_set.set('00') # set sec sec_set= StringVar() Entry(root, textvariable =sec_set, width =2, font = 'arial 12').place(x=385, y=115) sec_set.set('00') #######################GUI to display timer countdown ########## Label(root, font ='arial 15 bold', text = ' 倒計(jì)時(shí):', bg ='papaya whip').place(x = 40 ,y = 150) #storing seconds sec = StringVar() Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=325, y=155) Label(root, font ='arial 15', text = '秒', bg = 'papaya whip').place(x = 350 ,y = 150) sec.set('00') #storing minutes mins= StringVar() Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=275, y=155) Label(root, font ='arial 15', text = '分', bg = 'papaya whip').place(x = 300 ,y = 150) mins.set('00') # storing hours hrs= StringVar() Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=225, y=155) Label(root, font ='arial 15', text = '時(shí)', bg = 'papaya whip').place(x = 250 ,y = 150) hrs.set('00') # storing days days= StringVar() Entry(root, textvariable = days, width =2, font = 'arial 12').place(x=175, y=155) Label(root, font ='arial 15', text = '天', bg = 'papaya whip').place(x = 200 ,y = 150) days.set('00') #########fun to display current time############# def clock(): clock_time = time.strftime('%Y-%m-%d %H:%M:%S %p') curr_time.config(text = clock_time) curr_time.after(1000,clock) curr_time =Label(root, font ='arial 15 bold', text = '', fg = 'gray25' ,bg ='papaya whip') curr_time.place(x = 175 , y = 70) clock() ##########fun to start countdown######## def countdown(): #now = datetime.now() #end = datetime((year_set).get(),(month_set).get(),(day_set).get(),(hour_set).get(),(min_set).get(),(sec_set).get(),00); global seconds_now now = time.time() lt_ = time.strptime(f'{(year_set).get()} {(month_set).get()} {(day_set).get()} {(hour_set).get()} {(min_set).get()} {(sec_set).get()}', '%Y %m %d %H %M %S') end = time.mktime(lt_) times=int (end-now) #.total_seconds()); while times > -1: minute,second = (times // 60 , times % 60) hour = 0 if minute > 60: hour , minute = (minute // 60 , minute % 60) day=0 if hour>24: day,hour=(hour//24,hour%24) sec.set(second) mins.set(minute) hrs.set(hour) days.set(day) root.update() time.sleep(1) times -= 1 Button(root, text='START', bd ='5', command = countdown, bg = 'antique white', font = 'arial 10 bold').place(x=150, y=210) root.mainloop()
打開工程文件,在地址欄里輸入cmd,按Enter回車,即打開控制臺(tái)。
輸入python main.py,按回車就打開了程序GUI界面。
到達(dá)時(shí)間填2021年10月1日,按start按鈕,就開始放假倒計(jì)時(shí)啦!
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用python寫個(gè)國(guó)慶假期倒計(jì)時(shí)程序”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。