利用Python實現一個下班倒計時程序
Hello伙伴們,好幾天不見啦。最近也是晚上打球太累,加上一直在研究mybatis的多租戶問題,簡直是沒有太多的精力了。正好周六的晚上有一點點的空隙,就是洗完澡之后,頓時覺得整個人輕松下來了。有伙伴跟我一樣的感受嗎?
話不多說,現在我們來開始今天的主題:《如何實現一個桌面倒計時程序》。
身為打工人,一定是想著下班的那一刻吧。就像我昨天和我的伙伴開玩笑說:一個月就盼望著發(fā)工資的那一天。shigen找到了一段程序來實現下班倒計時,一起來看看實現的效果吧:

頁面上動態(tài)的顯示當前時間和剩余時間,假設shigen的文章要在今天的23點寫完,那么我還剩2小時25分鐘的準備時間。是不是挺神奇的,另外,還可以實現到點了自動關機。人走電腦關,看看老板還有什么理由讓你去加班。
那就上今天的代碼吧:
# -*- encoding: utf-8 -*-
__date__ = '2023/11/18 19:27:08'
?
"""
距離下班時間倒計時
"""
?
import time
from tkinter import *
?
?
def refresh_current_time():
"""刷新當前時間"""
clock_time = time.strftime('%Y-%m-%d %H:%M:%S')
curr_time.config(text=clock_time)
curr_time.after(1000, refresh_current_time)
?
?
def refresh_down_time():
"""刷新倒計時時間"""
# 當前時間戳
now_time = int(time.time())
# 下班時間時分秒數據過濾
work_hour_val = int(work_hour.get())
if work_hour_val > 23:
down_label.config(text='小時的區(qū)間為(00-23)')
return
work_minute_val = int(work_minute.get())
if work_minute_val > 59:
down_label.config(text='分鐘的區(qū)間為(00-59)')
return
work_second_val = int(work_second.get())
if work_second_val > 59:
down_label.config(text='秒數的區(qū)間為(00-59)')
return
# 下班時間轉為時間戳
work_date = str(work_hour_val) + ':' + str(work_minute_val) + ':' + str(work_second_val)
work_str_time = time.strftime('%Y-%m-%d ') + work_date
time_array = time.strptime(work_str_time, "%Y-%m-%d %H:%M:%S")
work_time = time.mktime(time_array)
if now_time > work_time:
down_label.config(text='已過下班時間')
return
# 距離下班時間剩余秒數
diff_time = int(work_time - now_time)
while diff_time > -1:
# 獲取倒計時-時分秒
down_minute = diff_time // 60
down_second = diff_time % 60
down_hour = 0
if down_minute > 60:
down_hour = down_minute // 60
down_minute = down_minute % 60
# 刷新倒計時時間
down_time = str(down_hour).zfill(2) + '時' + str(down_minute).zfill(2) + '分' + str(down_second).zfill(2) + '秒'
down_label.config(text=down_time)
tk_obj.update()
time.sleep(1)
if diff_time == 0:
# 倒計時結束
down_label.config(text='已到下班時間')
# 自動關機,定時一分鐘關機,可以取消
# down_label.config(text='下一分鐘將自動關機')
# os.system('shutdown -s -f -t 60')
break
diff_time -= 1
?
?
# 程序主入口
if __name__ == "__main__":
# 設置頁面數據
tk_obj = Tk()
tk_obj.geometry('400x280')
tk_obj.resizable(0, 0)
tk_obj.config(bg='white')
tk_obj.title('倒計時應用')
Label(tk_obj, text='下班倒計時', font='宋體 20 bold', bg='white').pack()
# 設置當前時間
Label(tk_obj, font='宋體 15 bold', text='當前時間:', bg='white').place(x=50, y=60)
curr_time = Label(tk_obj, font='宋體 15', text='', fg='gray25', bg='white')
curr_time.place(x=160, y=60)
refresh_current_time()
# 設置下班時間
Label(tk_obj, font='宋體 15 bold', text='下班時間:', bg='white').place(x=50, y=110)
# 下班時間-小時
work_hour = StringVar()
Entry(tk_obj, textvariable=work_hour, width=2, font='宋體 12').place(x=160, y=115)
work_hour.set('18')
# 下班時間-分鐘
work_minute = StringVar()
Entry(tk_obj, textvariable=work_minute, width=2, font='宋體 12').place(x=185, y=115)
work_minute.set('00')
# 下班時間-秒數
work_second = StringVar()
Entry(tk_obj, textvariable=work_second, width=2, font='宋體 12').place(x=210, y=115)
work_second.set('00')
# 設置剩余時間
Label(tk_obj, font='宋體 15 bold', text='剩余時間:', bg='white').place(x=50, y=160)
down_label = Label(tk_obj, font='宋體 23', text='', fg='gray25', bg='white')
down_label.place(x=160, y=155)
down_label.config(text='00時00分00秒')
# 開始計時按鈕
Button(tk_obj, text='START', bd='5', command=refresh_down_time, bg='green', font='宋體 10 bold').place(x=150, y=220)
tk_obj.mainloop()
代碼就是簡簡單單的204行,要實現到點自動關機的伙伴可以把63-64行的代碼注釋打開即可。
那最后總結一下吧,為什么shigen會選取這個程序作為今天的分享呢?
- 跨平臺。首先python是跨平臺的,其次
tkinter也是跨平臺的,意味著在常見的操作系統(tǒng)都可以執(zhí)行這個代碼,實現倒計時的效果; - 新思路。其實
shigen之前也做了一個類似的桌面時鐘效果,做的更加酷炫一點的話,其實可以當作屏保了; - 小工具的改造。其實
shigen的mac上也有很多的小工具,但是都是在命令行執(zhí)行的,改在了GUI界面豈不是更加的nice和方便,也實現傻瓜式操作。
到此這篇關于利用Python實現一個下班倒計時程序的文章就介紹到這了,更多相關Python下班倒計時內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

