Have you ever had time to fish
In the Internet world , It is often said that 996 Work system , But there is no lack of 965 Of , Even more 007 Of , and 007 It's a little ICU I feel it. , therefore , Everyone will sneak in , Occasionally touch fish , There are many ways to fish , Have you ever fished at work ? What did you do in your fishing time ? If you finish the task of the day early , Isn't it great to sit and wait for work ? I want to say that this time is still very difficult , It's better to find something to do quickly , Then do something ? Write a countdown to work , It was such a pleasant decision ……
Realize the idea
The countdown time refreshes , Definitely need a graphical interface , That's what we need GUI Programming , So here I'm going to use theta tkinter Realize the interface of local window , Use tkinter It can realize the regular refresh display of page layout and time , And the operation involving time , It must be used time modular , Here I also added the function of automatic shutdown at the end of countdown ( Annotated , It can be opened if necessary ), So it also uses os Modular system Realize the function of regular shutdown .
Running environment
Python Running environment :Windows + python3.8
Modules used :tkinter、time、os
If the module is not installed , Please use pip instatll xxxxxx Installation , for example :pip install tkinter
Interface layout
Let's take a look at the interface after implementation
picture
You can see from the screenshot , There are three main messages :
• current time : This is a real-time display of the current time , The format is formatted year, month, day, hour, minute and second
• Time from work : This can be modified , The default is 18:00:00, You can modify it according to your off-duty time
• The rest of the time : Here is the rest of the countdown , spot START Refresh per second after
tk_obj = Tk() tk_obj.geometry('400x280') tk_obj.resizable(0, 0) tk_obj.config(bg='white') tk_obj.title(' Countdown application ') Label(tk_obj, text=' The countdown to work ', font=' Song style 20 bold', bg='white').pack()
Label(tk_obj, font=' Song style 15 bold', text=' current time :', bg='white').place(x=50, y=60) curr_time = Label(tk_obj, font=' Song style 15', text='', fg='gray25', bg='white') curr_time.place(x=160, y=60) refresh_current_time()
Label(tk_obj, font=' Song style 15 bold', text=' Time from work :', bg='white').place(x=50, y=110)
work_hour = StringVar() Entry(tk_obj, textvariable=work_hour, width=2, font=' Song style 12').place(x=160, y=115) work_hour.set('18')
work_minute = StringVar() Entry(tk_obj, textvariable=work_minute, width=2, font=' Song style 12').place(x=185, y=115) work_minute.set('00')
work_second = StringVar() Entry(tk_obj, textvariable=work_second, width=2, font=' Song style 12').place(x=210, y=115) work_second.set('00')
Label(tk_obj, font=' Song style 15 bold', text=' The rest of the time :', bg='white').place(x=50, y=160) down_label = Label(tk_obj, font=' Song style 23', text='', fg='gray25', bg='white') down_label.place(x=160, y=155) down_label.config(text='00 when 00 branch 00 second ')
Button(tk_obj, text='START', bd='5', command=refresh_down_time, bg='green', font=' Song style 10 bold').place(x=150, y=220) tk_obj.mainloop()
Scheduled refresh remaining time
By obtaining the set off-duty time , Compare the time difference of the current time , So as to get the remaining time , Reuse while Cycle processing time remaining per second , And refresh to the interface in real time , Until the remaining time is 0 The program will end , Even operate the automatic shutdown function of the computer .
def refresh_down_time(): """ Refresh countdown time """ # Current timestamp now_time = int(time.time()) # After hours, minute and second data filtering work_hour_val = int(work_hour.get()) if work_hour_val > 23: down_label.config(text=' The interval of hours is (00-23)') return work_minute_val = int(work_minute.get()) if work_minute_val > 59: down_label.config(text=' The interval of minutes is (00-59)') return work_second_val = int(work_second.get()) if work_second_val > 59: down_label.config(text=' The interval of seconds is (00-59)') return # Turn off time into time stamp 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=' It's past work time ') return # Seconds left from time off diff_time = int(work_time - now_time) while diff_time > -1: # Get countdown - Minutes and seconds 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 # Refresh countdown time down_time = str(down_hour).zfill(2) + ' when ' + str(down_minute).zfill(2) + ' branch ' + str(down_second).zfill(2) + ' second ' down_label.config(text=down_time) tk_obj.update() time.sleep(1) if diff_time == 0: # The countdown is over down_label.config(text=' It's time to get off work ') # Automatic shutdown , Turn it off in one minute , May cancel # down_label.config(text=' The next minute will automatically shut down ') # os.system('shutdown -s -f -t 60') break diff_time -= 1
In order to facilitate everyone to test and fish smoothly , I also posted the complete countdown program , If you have any questions, you can also give feedback in time
""" Countdown to off duty time author: gxcuizy date: 2021-04-27 """ from tkinter import * import time import os def refresh_current_time(): """ Refresh the 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(): """ Refresh countdown time """ # Current timestamp now_time = int(time.time()) # After hours, minute and second data filtering work_hour_val = int(work_hour.get()) if work_hour_val > 23: down_label.config(text=' The interval of hours is (00-23)') return work_minute_val = int(work_minute.get()) if work_minute_val > 59: down_label.config(text=' The interval of minutes is (00-59)') return work_second_val = int(work_second.get()) if work_second_val > 59: down_label.config(text=' The interval of seconds is (00-59)') return # Turn off time into time stamp 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=' It's past work time ') return # Seconds left from time off diff_time = int(work_time - now_time) while diff_time > -1: # Get countdown - Minutes and seconds 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 # Refresh countdown time down_time = str(down_hour).zfill(2) + ' when ' + str(down_minute).zfill(2) + ' branch ' + str(down_second).zfill(2) + ' second ' down_label.config(text=down_time) tk_obj.update() time.sleep(1) if diff_time == 0: # The countdown is over down_label.config(text=' It's time to get off work ') # Automatic shutdown , Turn it off in one minute , May cancel # down_label.config(text=' The next minute will automatically shut down ') # os.system('shutdown -s -f -t 60') break diff_time -= 1
if __name__ == "__main__": # Set page data tk_obj = Tk() tk_obj.geometry('400x280') tk_obj.resizable(0, 0) tk_obj.config(bg='white') tk_obj.title(' Countdown application ') Label(tk_obj, text=' The countdown to work ', font=' Song style 20 bold', bg='white').pack() # Set the current time Label(tk_obj, font=' Song style 15 bold', text=' current time :', bg='white').place(x=50, y=60) curr_time = Label(tk_obj, font=' Song style 15', text='', fg='gray25', bg='white') curr_time.place(x=160, y=60) refresh_current_time() # Set the off hours Label(tk_obj, font=' Song style 15 bold', text=' Time from work :', bg='white').place(x=50, y=110) # Time from work - Hours work_hour = StringVar() Entry(tk_obj, textvariable=work_hour, width=2, font=' Song style 12').place(x=160, y=115) work_hour.set('18') # Time from work - minute work_minute = StringVar() Entry(tk_obj, textvariable=work_minute, width=2, font=' Song style 12').place(x=185, y=115) work_minute.set('00') # Time from work - Number of seconds work_second = StringVar() Entry(tk_obj, textvariable=work_second, width=2, font=' Song style 12').place(x=210, y=115) work_second.set('00') # Set the remaining time Label(tk_obj, font=' Song style 15 bold', text=' The rest of the time :', bg='white').place(x=50, y=160) down_label = Label(tk_obj, font=' Song style 23', text='', fg='gray25', bg='white') down_label.place(x=160, y=155) down_label.config(text='00 when 00 branch 00 second ') # Start timing button Button(tk_obj, text='START', bd='5', command=refresh_down_time, bg='green', font=' Song style 10 bold').place(x=150, y=220) tk_obj.mainloop()
Last
We have any questions , You can leave me a message , I will reply in time. , If something is wrong , Please help correct . If you have any fun ways to fish , You can also leave a message to me in the comment area , Let's fish happily together !