基于python寫(xiě)個(gè)國(guó)慶假期倒計(jì)時(shí)程序
國(guó)慶假期快到了,想查查還有幾天幾小時(shí)到假期,這對(duì)程序員小菜一碟,輕輕松松用python寫(xiě)個(gè)倒計(jì)時(shí)程序(天、時(shí)、分、秒),助你熬到假期!
一、先看效果:

二、安裝python:
1、下載安裝python
下載安裝python3.9.6,進(jìn)入python官方網(wǎng)站:http://www.python.org/

點(diǎn)擊Python 3.9.6

直接安裝即可。
2、驗(yàn)證安裝成功。
按win+R輸入cmd,打開(kāi)控制臺(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()
四、運(yùn)行
打開(kāi)工程文件,在地址欄里輸入cmd,按Enter回車(chē),即打開(kāi)控制臺(tái)。



輸入python main.py,按回車(chē)就打開(kāi)了程序GUI界面。


到達(dá)時(shí)間填2021年10月1日,按start按鈕,就開(kāi)始放假倒計(jì)時(shí)啦!
相關(guān)資源:基于python的假期倒計(jì)時(shí)(天、時(shí)、分、秒).zip
到此這篇關(guān)于用python寫(xiě)個(gè)國(guó)慶假期倒計(jì)時(shí)程序的文章就介紹到這了,更多相關(guān)python倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm中import導(dǎo)入包呈現(xiàn)灰色的問(wèn)題及解決
這篇文章主要介紹了pycharm中import導(dǎo)入包呈現(xiàn)灰色的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
詳解如何用django實(shí)現(xiàn)redirect的幾種方法總結(jié)
這篇文章主要介紹了如何用django實(shí)現(xiàn)redirect的幾種方法總結(jié),詳細(xì)的介紹3種實(shí)現(xiàn)方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
python如何標(biāo)準(zhǔn)化日期時(shí)間格式轉(zhuǎn)化成非標(biāo)準(zhǔn)化格式
這篇文章主要介紹了python如何標(biāo)準(zhǔn)化日期時(shí)間格式轉(zhuǎn)化成非標(biāo)準(zhǔn)化格式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
python數(shù)據(jù)庫(kù)PooledDB連接池初始化使用示例
這篇文章主要為大家介紹了python數(shù)據(jù)庫(kù)PooledDB連接池初始化使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Python實(shí)現(xiàn)兩種多分類(lèi)混淆矩陣
這篇文章主要為大家介紹了Python實(shí)現(xiàn)兩種多分類(lèi)混淆矩陣,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
python使用socket高效傳輸視頻數(shù)據(jù)幀(連續(xù)發(fā)送圖片)
本文主要介紹了python使用socket高效傳輸視頻數(shù)據(jù)幀(連續(xù)發(fā)送圖片),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Pycharm配置遠(yuǎn)程SSH服務(wù)器實(shí)現(xiàn)(切換不同虛擬環(huán)境)
本文主要介紹了Pycharm配置遠(yuǎn)程SSH服務(wù)器實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
解決pycharm啟動(dòng)后總是不停的updating indices...indexing的問(wèn)題
今天小編就為大家分享一篇解決pycharm啟動(dòng)后總是不停的updating indices...indexing的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

