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

二、安裝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,打開控制臺,輸入python -V,輸出python版本號說明安裝成功。

三、代碼
##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('國慶倒計時')
Label(root, text = '國慶倒計時' , font = 'arial 20 bold', bg ='papaya whip').pack()
############GUI to display current time#######################
Label(root, font ='arial 15 bold', text = ' 當(dāng)前時間:', bg = 'papaya whip').place(x = 40 ,y = 70)
#######################GUI to set the future time ##########
Label(root, font ='arial 15 bold', text = ' 到達(dá)時間:', 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 = ' 倒計時:', 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 = '時', 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)行
打開工程文件,在地址欄里輸入cmd,按Enter回車,即打開控制臺。



輸入python main.py,按回車就打開了程序GUI界面。


到達(dá)時間填2021年10月1日,按start按鈕,就開始放假倒計時啦!
相關(guān)資源:基于python的假期倒計時(天、時、分、秒).zip
到此這篇關(guān)于用python寫個國慶假期倒計時程序的文章就介紹到這了,更多相關(guān)python倒計時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07
如何基于Python實(shí)現(xiàn)word文檔重新排版
這篇文章主要介紹了如何基于Python實(shí)現(xiàn)word文檔重新排版,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Python PyCryptodome庫介紹與實(shí)例教程
PyCryptodome提供了豐富的加密功能,可以滿足多種安全需求,本文介紹了幾個常見的使用場景,包括對稱加密、非對稱加密、哈希函數(shù)和消息認(rèn)證碼,感興趣的朋友跟隨小編一起看看吧2024-07-07
詳解OpenCV自適應(yīng)直方圖均衡化的應(yīng)用
在本文中,將介紹如何應(yīng)用對比度受限的自適應(yīng)直方圖均衡化 ( Contrast Limited Adaptive Histogram Equalization, CLAHE ) 來均衡圖像,需要的可以參考一下2022-02-02
pytest?用例執(zhí)行失敗后其他不再執(zhí)行
本文主要介紹了pytest?用例執(zhí)行失敗后其他不再執(zhí)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python使用tqdm模塊處理文件閱讀進(jìn)度條顯示
這篇文章主要為大家介紹了python使用tqdm模塊處理文件閱讀顯示進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Python操作Excel神器openpyxl使用教程(超詳細(xì)!)
openpyxl庫是一個很好處理xlsx的python庫,下面這篇文章主要給大家介紹了關(guān)于Python辦公自動化openpyxl使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

