1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
from tkinter import * import time import os
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='小时的区间为(00-23)') return work_minute_val = int(work_minute.get()) if work_minute_val > 59: down_label.config(text='分钟的区间为(00-59)') return work_second_val = int(work_second.get()) if work_second_val > 59: down_label.config(text='秒数的区间为(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='已过下班时间') time.sleep(2) down_label.config(text='十分钟后将自动关机') os.system('shutdown -s -t 600') 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='已到下班时间') time.sleep(2) down_label.config(text='十分钟后将自动关机') os.system('shutdown -s -t 600') 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()
|