Python Tkinter creates a timer

Creating a Timer with Python Tkinter

This tutorial focuses on creating a timer using Python’s Tkinter.

Thanks to the widget classes, we have access to a lot of basic functionality. They provide techniques for managing various user-driven events and methods for defining the GUI’s appearance, such as positioning elements. Once our GUI framework is established, we must customize it by integrating it with our internal application classes.

Tkinter: Tkinter is the name of Python’s built-in GUI library. Thanks to Python and Tkinter, developing GUI applications has become quick and easy. Tkinter provides an efficient object-oriented interface to the Tk GUI toolkit. Here are some example programs to get started with Tkinter in Python; getting started with Tkinter is very simple.

A GUI is simply a desktop application that provides an interface for communicating with a computer and improves the experience of directing your code (command-line input). They are used in personal computers, laptops, and other digital devices to perform a variety of tasks.

Creating a calculator with a user interface and retaining the functionality of a calculator is one application that leverages the power of a GUI.

Text editors and integrated development environments for coding are available for GUI applications.

You can play board games like Sudoku, chess, Solitaire, and others through GUI programs.

GUI programs are used for browsing the internet, including Chrome, Firefox, Microsoft Edge, and others.

Other Python GUI frameworks exist, but only Tkinter is part of the standard library. Tkinter offers several benefits. Because it’s cross-platform, the same code can run on Windows, macOS, and Linux. Because Tkinter uses native operating system components to generate visual elements, programs created with it look the same as they do on the operating system they’re running on. Tkinter is Python’s standard GUI library. Python and Tkinter can be used to quickly and easily create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.

#Using Tkinter and a Python application, create a new window while importing the necessary libraries.
import tkinter
# Constructing an object of type Tk with the name "top"
top = tkinter.Tk()
This will open a new window that is empty.
top.mainloop()

Creating a Stopwatch with Tkinter

Now let’s try developing a stopwatch program using the Tkinter module.

A stopwatch is essentially a portable wristwatch that counts the number of seconds that elapse between the time it is triggered and the time it is deactivated. A stopwatch is a large digital stopwatch designed for viewing from a distance, such as at a sports stadium. In manual timing, a button is pressed to start and stop the clock. In fully automatic timing, a sensor triggers the start and stop automatically.

Mandatory Modules

This software does not use any libraries; Tkinter is the only library used to create the GUI.

Code

# Python program that uses Tkinter to show a stopwatch #importing the necessary libraries
import tkinter as Tkinter
from datetime import datetime
counter=68600
running=False
def counter_label(label):
    def count():
        if running:
            global counter
            # To manage up the initial delay.
            if counted==68600:
                display="Starting..."
            else:
                tt = datetime.fromtimestamp(countered)
                strings = tt.strftime("%H:%M:%S")
                display=strings
            label['text']=display # Or label.config(text=display)
            # Label.after(arg1, arg2) executes the function
specified as the second parameter after
delaying by the first argument's millisecond value.
# In situations like these, we typically need
to call the # function where it is repeatedly present.
# Recount calls after 1000 millisecond delays, or 1 second.
            label.after(1000, count)
            counter += 1
    # Triggering up of the start of the counter.
    count()
# starting function of the stopwatch
def Start(label):
    global running
    running=True
    counter_label(label)
    start['stated']='disabled'
    stop['stated']='normal'
    reset['stated']='normal'
# Stopping of function of the stopwatch
def Stop():
    global running
    start['stated']='normal'
    stop['stated']='disabled'
    reset['stated']='normal'
    running=False
# Resetting function of the stopwatch in code
def Reset(label):
    global counter
    counter=68600

    # if stop is pressed first, then rest.
    if running==False:
        reset['state']='disabled'
        label['text']='Welcome!'

    # if the stopwatch is starting when the reset button is clicked.
    else:
        label['text']='Starting...'
root = Tkinter.Tk()
root.title("Stopwatch_inter")
# Fixing up the window size.
root.minsize(width=260, height=80)
label = Tkinter.Label(root, text="Welcome", fg="black", font="Verdana 35 bold")
label.pack()
f = Tkinter.Frame(root)
start = Tkinter.Button(f, text='Start up', width=8, command=lambda:Start(label))
stop = Tkinter.Button(f, text='Stop in',width=8,state='disabled', command=Stop)
reset= Tkinter.Button(f, text='Reset at', width=8, state='disabled', command=lambda:Reset(label))
f.pack(anchor = 'center', padding=6)
start.pack(side="left")
stop.pack(side="left")
reset.pack(side="left")
root.mainloop()

Output

Create a Stopwatch in Python

Leave a Reply

Your email address will not be published. Required fields are marked *