SoFunction
Updated on 2024-11-18

Implementing a countdown timer based on Python

A countdown timer tool based on Python is written for your reference as follows

Features:

Real-time display of the current time
Automatically determines the date entered by the user and calculates the approximate difference between the current date and the target date in years, months, days and the exact number of days.

Run window

Run Screen-1

Run Screen-2

Enter date-3

Results window - 4

coding

import time
import tkinter as tk
from tkinter import messagebox

def main():
    window1 = ()
    ('Timer [v0.0]')
    ('300x200')

    l1 = (window1, text = 'Current time:', font = ('Song Style', 15))
    (x = 5, y = 10)
   
    def time_now():
        global seconds_now
        seconds_now = ()
        lt = (seconds_now)
        time1 = []
        time2 = '%04d year %02d month %02d day \n %02d hour %02d minute %02d second' % (lt[0], lt[1], lt[2], lt[3], lt[4], lt[5])

        if time2 != time1:
            time1 = time2
            l1_2 = (window1, text = time1, font = ('Song Style', 20))
            l1_2.configure(text = time2)
            l1_2.place(x = 30, y = 50)
            l1_2.after(200, time_now)
            
    time_now()
    
    def input_time():
        window2 = ()
        ('Timer [v0.0]')
        ('300x120')

        l2_1 = (window2, text = 'Year', font = ('Song Style', 15))
        l2_1.place(x = 90, y = 20)
        l2_2 = (window2, text = 'Moon', font = ('Song Style', 15))
        l2_2.place(x = 170, y = 20)
        l2_3 = (window2, text = 'Day', font = ('Song Style', 15))
        l2_3.place(x = 250, y = 20)
        l2_4 = (window2, text = 'Effective date [1970/1/2-3001/1/1]', font = ('Song Style', 10))
        l2_4.place(x = 50, y = 50)

        year = (window2, text = None, font = ('Song Style', 15), width = 5)
        month = (window2, text = None, font = ('Song Style', 15), width = 5)
        day = (window2, text = None, font = ('Song Style', 15), width = 5)
        (x = 40, y = 20)
        (x = 120, y = 20)
        (x = 200, y = 20)

        def get_time():
            try:
                y = int(())
                m = int(())
                d = int(())
                lt_ = (f'{y} {m} {d}', '%Y %m %d')
                seconds_get = (lt_)
            except BaseException:
                (message='Input error!')
            else:
                ()    
            
            string1 = 'The query date is up to now:'
            string2 = 'The query date is past:'

            seconds_lasting = seconds_get - seconds_now
            
            day_lasting = abs(seconds_lasting) // 86400
            month_lasting = 0
            year_lasting = 0
            days = day_lasting
           
            if day_lasting > 356:
                year_lasting = day_lasting // 365
                day_lasting -= year_lasting * 365
                if day_lasting > 30:
                    month_lasting = day_lasting // 30
                    day_lasting -= month_lasting * 30
            elif day_lasting > 30:
                year_lasting = 0
                month_lasting = day_lasting // 30
                day_lasting -= month_lasting * 30 
            else:
                year_lasting, month_lasting = 0, 0
            
            if seconds_lasting > 0:
                prompt = string1
                days += 1
                day_lasting += 1
            else: 
                prompt = string2  
                  
            (message='%s%d day\n probably %d year%d month%d day' % (prompt, days, year_lasting, month_lasting, day_lasting))   
                
        button2 = (window2, text = 'Start query', font = ('Song Style', 15), command = get_time)
        (x = 110, y = 75)
       
        ()

    button1 = (window1, text = 'Enter date of inquiry', font = ('Song Style', 15), command = input_time)
    (x = 85, y = 125)

    ()

if __name__ == '__main__':
    main()

This is the whole content of this article.