Fish on time, close the net on time, and Python realizes the countdown after work! Never work overtime

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 the countdown must require a graphical interface, that is, GUI programming. Here I use tkinter to realize the interface of the 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 at the end of countdown (annotated, it can be turned on if necessary), so I also used the system of os module to realize the timed shutdown function.

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

picture

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 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 hours

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()

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
    # Off duty time to timestamp
    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:
            # The countdown is over
            down_label.config(text='It's time to get off work')
            # Automatic shutdown, timed one minute shutdown, can be cancelled
            # down_label.config(text = 'auto shutdown next minute')
            # os.system('shutdown -s -f -t 60')
            break
        diff_time -= 1

In order to facilitate everyone's testing and fishing smoothly, I also posted the complete countdown program, and you can give feedback in time if you have any questions

-- 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
    # Off duty time to timestamp
    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:
            # The countdown is over
            down_label.config(text='It's time to get off work')
            # Automatic shutdown, timed one minute shutdown, 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 hours
    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()

last

If you have any questions, you can leave me a message and I will reply in time. If you say something wrong, please help correct it. If you have any fun fishing methods, you can also leave a message to me in the comment area. Let's have a happy fishing together!

Keywords: Python

Added by Darklink on Tue, 28 Dec 2021 02:18:16 +0200