Do you ever have fish time

In the Internet circle, often say 996 work system, but there is no lack of 965, more 007, and 007 is a bit of ICU feeling, so, everyone will be busy sneak time, occasionally touch fish, touch fish in a variety of ways, have you ever go to work touch fish? What do you do with your fishing hours? If you finish your day’s tasks early, doesn’t it feel good to sit back and wait for the day to come home? I mean, it’s still a tough time, and it’s better to find something to do. What do you do? Write a countdown to the end of work, so happy decision…

Implementation approach

Here I use Tkinter to realize the interface of the local window. Using Tkinter can realize the page layout and the periodic refresh display of time. And when it comes to the operation of time, it is inevitable to use the time module. Here I also added the function of automatic shutdown when the countdown ends (annotated, can be turned on if necessary), so I also used the SYSTEM of OS module to realize the function of periodic shutdown.

Runtime environment

Python operating environment: Windows + Python3.8 Used modules: Tkinter, time, and OS If the modules are not installed, run PIP instatll XXXXXX to install them, for example, PIP install tkinter

Interface layout

Let’s take a look at the implemented interface

The picture

As you can see from the screenshot, there are three main messages:

• Current time: Displays the current time in real time in formatted year, month, day, hour, minute, second format

• Off time: this can be changed. The default value is 18:00:00 and can be changed according to your off time

• Remaining time: Here is the remaining time of the countdown, refreshed every second after hitting START

Setting page data

tk_obj = Tk() tk_obj.geometry('400x280') tk_obj.resizable(0, 0) tk_obj.config(bg='white') tk_obj.title(' countdown ') Label(tk_obj, text=' countdown ', font='宋体 20 bold', bg='white').pack()Copy the code

Set the current time

Label(tk_obj, font='宋体 15 bold', text=' ', bg = 'white'). The place (= 50 x, y = 60) curr_time = Label (tk_obj, the font = 'song typeface 15', the text = ', fg = 'gray25', bg='white') curr_time.place(x=160, y=60) refresh_current_time()Copy the code

Set off time

Place (x=50, y=110). Label(tk_obj, font='宋体 15 bold', text=' ', bg='white'). Place (x=50, y=110).Copy the code

Closing time – hours

Work_hour = StringVar() Entry(tk_obj, textvariable=work_hour, width=2, font='宋体 12'). Place (x=160, y=115) work_hour.set('18')Copy the code

Closing time – minutes

Work_minute = StringVar() Entry(tk_obj, textvariable=work_minute, width=2, font='宋体 12'). Place (x=185, y=115) work_minute.set('00')Copy the code

Off-duty time – seconds

Work_second = StringVar() Entry(tk_obj, textvariable=work_second, width=2, font='宋体 12'). Place (x=210, y=115) work_second.set('00')Copy the code

Set the remaining time

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 00 00 00 00 00 ')Copy the code

Start timer button

Button(tk_obj, text='START', bd='5', command=refresh_down_time, bg='green', font='宋体 10 bold'). Place (x=150, y=220) tk_obj.mainloop()Copy the code

Refresh the remaining time periodically

By obtaining the set off time and comparing the time difference of the current time, the remaining time can be obtained, and then the remaining time can be processed in a while cycle every second, and the program will be refreshed on the interface in real time until the remaining time is 0, and even the automatic shutdown function of the computer will be operated.

def refresh_down_time(): Work_hour_val = int(work_hour.get()) if work_hour_val > work_hour_val = int(work_hour.get()) Return work_minute_val = int(work_minute.get()) if work_minute_val > 59: Down_label. config(text=' minute interval is (00-59) ') return work_second_val = int(work_second.get()) if work_second_val > 59: 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=' diff_time ') 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_hour = STR (down_hour).zfill(2) + 'hour' + STR (down_minute).zfill(2) + STR (down_second).zfill(2) + 'second' down_label.config(text=down_time) tk_obj.update() time.sleep(1) if diff_time == 0: Down_label. config(text=' start time ') # down_label.config(text=' next minute will automatically shutdown ') # os.system('shutdown -s -f -t 60') break diff_time -= 1Copy the code

In order to facilitate everyone to test and smooth fishing, I put the complete countdown program also posted, you can also timely feedback any questions

– coding: utf-8 –

Author: gxcuizy Date: 2021-04-27 """ 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(): Work_hour_val = int(work_hour.get()) if work_hour_val > work_hour_val = int(work_hour.get()) Return work_minute_val = int(work_minute.get()) if work_minute_val > 59: Down_label. config(text=' minute interval is (00-59) ') return work_second_val = int(work_second.get()) if work_second_val > 59: 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=' diff_time ') 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_hour = STR (down_hour).zfill(2) + 'hour' + STR (down_minute).zfill(2) + STR (down_second).zfill(2) + 'second' down_label.config(text=down_time) tk_obj.update() time.sleep(1) if diff_time == 0: Down_label. config(text=' start time ') # down_label.config(text=' next minute will automatically shutdown ') # os.system('shutdown -s -f -t 60') break diff_time -= 1Copy the code

Program main entry

if __name__ == "__main__": Tk_obj = Tk() tk_obj. Geometry ('400x280') tk_obj.resizable(0, 0) tk_obj.config(bg='white') tk_obj.title(' countdown ') Label(tk_obj, text=' countdown ', font='宋体 20 bold', Bg ='white').pack() # set the current time. ', bg = 'white'). The place (= 50 x, y = 60) curr_time = Label (tk_obj, the font = 'song typeface 15', the text = ', fg = 'gray25', Bg ='white') curr_time.place(x=160, y=60) refresh_current_time() # Place (x=50, y=110) # work_hour = StringVar() Entry(tk_obj, textVariable =work_hour, width=2, 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') ', 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 00 00 00 ') # Bd ='5', command=refresh_down_time, bg='green', font='宋体 10 bold'). Place (x=150, y=220) tk_obj.mainloop()Copy the code

The last

You have any questions, you can give me a message to me, I will reply in time, if there is something wrong, please help to correct. If you have any fun fishing method, you can also leave a message to me in the comment section ha, everyone happy fishing!