Have you ever had time to fish
In the Internet circle, it is often said that 996 work system, but there are also 965, especially 007, and 007 feels like an ICU. Therefore, everyone will sneak around and occasionally touch fish. There are many ways to fish. Have you ever fished at work? What did you do during your fishing time? If you finish the task of the day early, does it feel good to wait for work? I want to say that this time is still very difficult. It's better to find something to do quickly. What should we do? Write a countdown to work. It's such a happy decision
Realization idea
The time refresh of countdown must require a graphical interface, that is, GUI programming. Here I use tkinter to realize the interface of local window. Using tkinter can realize the regular refresh display of page layout and time. When it comes to the operation of time, the time module must be used, Here I also added the function of automatic shutdown after the countdown (annotated, it can be turned on if necessary), so I also used the system of os module to realize the function of timed shutdown.
Operating environment
Python running environment: Windows + Python 3 eight
Modules used: tkinter, time, os
If the module is not installed, please use PIP installll xxxxx for installation, for example: pip install tkinter
Interface layout
Let's take a look at the interface after implementation
As can be seen from the screenshot, there are three main messages:
- Current time: This is the real-time display of the current time in the format of formatted year, month, day, hour, minute and second
- Off duty time: this can be modified. The default is 18:00:00. It can be modified according to your off duty time
- Remaining time: here is the remaining time of the countdown, which is refreshed every second after clicking START
# 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='Off duty countdown', font='Tahoma 20 bold', bg='white').pack() # Set current time Label(tk_obj, font='Tahoma 15 bold', text='Current time:', bg='white').place(x=50, y=60) curr_time = Label(tk_obj, font='Tahoma 15', text='', fg='gray25', bg='white') curr_time.place(x=160, y=60) refresh_current_time() # Set off duty time Label(tk_obj, font='Tahoma 15 bold', text='closing time:', bg='white').place(x=50, y=110) # Off duty hours - hours work_hour = StringVar() Entry(tk_obj, textvariable=work_hour, width=2, font='Tahoma 12').place(x=160, y=115) work_hour.set('18') # Off duty time - minutes work_minute = StringVar() Entry(tk_obj, textvariable=work_minute, width=2, font='Tahoma 12').place(x=185, y=115) work_minute.set('00') # Off duty time - seconds work_second = StringVar() Entry(tk_obj, textvariable=work_second, width=2, font='Tahoma 12').place(x=210, y=115) work_second.set('00') # Set remaining time Label(tk_obj, font='Tahoma 15 bold', text='Time remaining:', bg='white').place(x=50, y=160) down_label = Label(tk_obj, font='Tahoma 23', text='', fg='gray25', bg='white') down_label.place(x=160, y=155) down_label.config(text='00 00:00') # Start timing button Button(tk_obj, text='START', bd='5', command=refresh_down_time, bg='green', font='Tahoma 10 bold').place(x=150, y=220) tk_obj.mainloop() Copy code
Scheduled refresh remaining time
By obtaining the set off-duty time and comparing the time difference of the current time, we can get the remaining time, and then use while to cycle the remaining time per second, and refresh it to the interface in real time. The program will not end until the remaining time is 0, and even operate the function of automatic shutdown of the computer.
def refresh_down_time(): """Refresh countdown time""" # Current timestamp now_time = int(time.time()) # Data filtering in hours, minutes and seconds after work 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 # Convert off duty time to 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='Off duty time has passed') return # Seconds left from off duty diff_time = int(work_time - now_time) while diff_time > -1: # Get Countdown - hours, 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) + 'Time' + 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: # End of countdown down_label.config(text='It's time to get off work') # Automatic shutdown, timed shutdown for one minute, which can be cancelled # down_label.config(text = 'auto shutdown next minute') # os.system('shutdown -s -f -t 60') break diff_time -= 1 Copy code
Complete code
In order to facilitate testing and fishing, I also posted the complete countdown program. If you have any questions, you can give feedback in time. If you want to know more, you can go to the dating website github.com/gxcuizy Look for me up there
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Countdown to off duty time author: gxcuizy date: 2021-04-27 """ from tkinter import * import time import os def refresh_current_time(): """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(): """Refresh countdown time""" # Current timestamp now_time = int(time.time()) # Data filtering in hours, minutes and seconds after work 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 # Convert off duty time to 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='Off duty time has passed') return # Seconds left from off duty diff_time = int(work_time - now_time) while diff_time > -1: # Get Countdown - hours, 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) + 'Time' + 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: # End of countdown down_label.config(text='It's time to get off work') # Automatic shutdown, timed shutdown for one minute, which can be cancelled # down_label.config(text = 'auto shutdown next minute') # os.system('shutdown -s -f -t 60') break diff_time -= 1 # Program main entrance 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='Off duty countdown', font='Tahoma 20 bold', bg='white').pack() # Set current time Label(tk_obj, font='Tahoma 15 bold', text='Current time:', bg='white').place(x=50, y=60) curr_time = Label(tk_obj, font='Tahoma 15', text='', fg='gray25', bg='white') curr_time.place(x=160, y=60) refresh_current_time() # Set off duty time Label(tk_obj, font='Tahoma 15 bold', text='closing time:', bg='white').place(x=50, y=110) # Off duty hours - hours work_hour = StringVar() Entry(tk_obj, textvariable=work_hour, width=2, font='Tahoma 12').place(x=160, y=115) work_hour.set('18') # Off duty time - minutes work_minute = StringVar() Entry(tk_obj, textvariable=work_minute, width=2, font='Tahoma 12').place(x=185, y=115) work_minute.set('00') # Off duty time - seconds work_second = StringVar() Entry(tk_obj, textvariable=work_second, width=2, font='Tahoma 12').place(x=210, y=115) work_second.set('00') # Set remaining time Label(tk_obj, font='Tahoma 15 bold', text='Time remaining:', bg='white').place(x=50, y=160) down_label = Label(tk_obj, font='Tahoma 23', text='', fg='gray25', bg='white') down_label.place(x=160, y=155) down_label.config(text='00 00:00') # Start timing button Button(tk_obj, text='START', bd='5', command=refresh_down_time, bg='green', font='Tahoma 10 bold').place(x=150, y=220) tk_obj.mainloop() Copy code
last
If you have any questions, you can leave a message to me, and I will reply in time. If there is anything wrong, please help correct it. If you have any fun fishing methods, you can also leave a message to me at the bottom. Let's have a happy fishing together!
Recently, many friends have consulted about Python learning through private letters. To facilitate communication, click blue to join Discussion and solution resource base