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
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 modified, the default is
18:00:00
, can be modified according to their own off work time - Remaining time: Here is the remaining time of the countdown, point
START
Refresh after every second
# Set page data
tk_obj = Tk()
tk_obj.geometry('400x280')
tk_obj.resizable(0.0)
tk_obj.config(bg='white')
tk_obj.title('Countdown App')
Label(tk_obj, text='Clock out', font='宋体 20 bold', bg='white').pack()
Set the current time
Label(tk_obj, font='宋体 15 bold', text='Current time:', bg='white').place(x=50, y=60)
curr_time = Label(tk_obj, font='song typeface 15', text=' ', fg='gray25', bg='white')
curr_time.place(x=160, y=60)
refresh_current_time()
# Set an exit time
Label(tk_obj, font='宋体 15 bold', text='After hours:', bg='white').place(x=50, y=110)
# Off time - hours
work_hour = StringVar()
Entry(tk_obj, textvariable=work_hour, width=2, font='宋体 12').place(x=160, y=115)
work_hour.set('18')
# Off time - minutes
work_minute = StringVar()
Entry(tk_obj, textvariable=work_minute, width=2, font='宋体 12').place(x=185, y=115)
work_minute.set('00')
# Off time - seconds
work_second = StringVar()
Entry(tk_obj, textvariable=work_second, width=2, font='宋体 12').place(x=210, y=115)
work_second.set('00')
# Set the remaining time
Label(tk_obj, font='宋体 15 bold', text='Remaining time:', 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 hours 00 minutes 00 seconds')
# 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() :
""" Refresh the countdown time """
# current timestamp
now_time = int(time.time())
# Minute-second data filtering during off-duty time
work_hour_val = int(work_hour.get())
if work_hour_val > 23:
down_label.config(text='Hour interval is (00-23)')
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:
down_label.config(text='The interval of seconds is (00-59)')
return
# change 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='It's past closing time')
return
# Number of seconds left before the time to leave work
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 the countdown time
down_time = str(down_hour).zfill(2) + 'when' + str(down_minute).zfill(2) + 'points' + str(down_second).zfill(2) + '秒'
down_label.config(text=down_time)
tk_obj.update()
time.sleep(1)
if diff_time == 0:
# Countdown ends
down_label.config(text='It's closing time')
# Automatic shutdown, scheduled for one minute shutdown, can be cancelled
# down_label.config(text=' next minute will automatically shut down ')
# os.system('shutdown -s -f -t 60')
break
diff_time -= 1
Copy the code
The complete code
In order to facilitate you to test and smooth fishing, I put the complete countdown procedure posted, you can also have any questions timely feedback, want to know more can go to the dating website github.com/gxcuizy to find me oh
#! /usr/bin/env python
# -*- coding: utf-8 -*-
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 the countdown time """
# current timestamp
now_time = int(time.time())
# Minute-second data filtering during off-duty time
work_hour_val = int(work_hour.get())
if work_hour_val > 23:
down_label.config(text='Hour interval is (00-23)')
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:
down_label.config(text='The interval of seconds is (00-59)')
return
# change 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='It's past closing time')
return
# Number of seconds left before the time to leave work
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 the countdown time
down_time = str(down_hour).zfill(2) + 'when' + str(down_minute).zfill(2) + 'points' + str(down_second).zfill(2) + '秒'
down_label.config(text=down_time)
tk_obj.update()
time.sleep(1)
if diff_time == 0:
# Countdown ends
down_label.config(text='It's closing time')
# Automatic shutdown, scheduled for one minute shutdown, can be cancelled
# down_label.config(text=' next minute will automatically shut down ')
# os.system('shutdown -s -f -t 60')
break
diff_time -= 1
# Program main entry
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 App')
Label(tk_obj, text='Clock out', font='宋体 20 bold', bg='white').pack()
Set the current time
Label(tk_obj, font='宋体 15 bold', text='Current time:', bg='white').place(x=50, y=60)
curr_time = Label(tk_obj, font='song typeface 15', text=' ', fg='gray25', bg='white')
curr_time.place(x=160, y=60)
refresh_current_time()
# Set an exit time
Label(tk_obj, font='宋体 15 bold', text='After hours:', bg='white').place(x=50, y=110)
# Off time - hours
work_hour = StringVar()
Entry(tk_obj, textvariable=work_hour, width=2, font='宋体 12').place(x=160, y=115)
work_hour.set('18')
# Off time - minutes
work_minute = StringVar()
Entry(tk_obj, textvariable=work_minute, width=2, font='宋体 12').place(x=185, y=115)
work_minute.set('00')
# Off time - seconds
work_second = StringVar()
Entry(tk_obj, textvariable=work_second, width=2, font='宋体 12').place(x=210, y=115)
work_second.set('00')
# Set the remaining time
Label(tk_obj, font='宋体 15 bold', text='Remaining time:', 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 hours 00 minutes 00 seconds')
# 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
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 at the bottom, everyone happy fishing!