Average Speed Calculator using Tkinter

In this article, we will create a GUI-based application that calculates the average speed of a moving object. The average speed can be calculated using the following formula:

Average Speed = Distance / [Hours + (Minutes/60)]

We will use Tkinter's Spinbox widget to create input controls for Distance (Kilometers), Hours, and Minutes. The Spinbox allows users to select values from a defined range using up/down arrows.

Complete Example

from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Set the geometry and resize the frame
win.geometry("700x400")
win.resizable(0, 0)
win.title("Average Speed Calculator")

# Create Label for Main Window
Label(win, text="Average Speed Calculator", font=("Times New Roman", 18, "bold"), fg="black").pack()

# Calculate Average Speed
def average_cal():
    # Get values from spinboxes
    hrs = int(hours.get())
    mins = int(minutes.get())
    dist = int(distance.get())
    
    # Apply formula
    avg = dist / (hrs + (mins / 60))
    
    # Update the result label
    average_speed.config(text=f"{avg:.2f} Km/Hr")

# Create Multiple Frames
frame = Frame(win)
frame.pack()

frame1 = Frame(win)
frame1.pack()

frame2 = Frame(win)
frame2.pack()

# Create Labels and Spin Boxes for Hours
Label(frame, text="Hours", width=15, font=("Times New Roman", 12, "bold"), 
      borderwidth=2, relief="solid").pack(side=LEFT, padx=10, pady=10)
hours = Spinbox(frame, from_=0, to=1000000, width=5, font=("Times New Roman", 12, "bold"))
hours.pack(side=LEFT, pady=10)

# Create Labels and Spin Boxes for Minutes
Label(frame1, text="Minutes", width=15, font=("Times New Roman", 12, "bold"), 
      borderwidth=2, relief="solid").pack(side=LEFT, padx=10, pady=10)
minutes = Spinbox(frame1, from_=0, to=10000000, width=5, font=("Times New Roman", 12, "bold"))
minutes.pack(side=LEFT, pady=10)

# Create Labels and Spin Boxes for Distance
Label(frame2, text="Distance in(Km)", width=15, font=("Times New Roman", 12, "bold"), 
      borderwidth=2, relief="solid").pack(side=LEFT, padx=10, pady=10)
distance = Spinbox(frame2, from_=0, to=1000000, width=5, font=("Times New Roman", 12, "bold"))
distance.pack(side=LEFT, pady=10)

# Calculate Button
Button(win, text="Calculate Speed", width=15, font=("Times New Roman", 12, "bold"), 
       command=average_cal, fg="white", bg="black").pack(pady=20)

# Result Label
average_speed = Label(win, text="", width=50, font=("Times New Roman", 12, "bold"), relief="solid")
average_speed.pack()

# Execute Tkinter
win.mainloop()

How It Works

The application creates three Spinbox widgets for user input:

  • Hours: Accepts values from 0 to 1,000,000
  • Minutes: Accepts values from 0 to 10,000,000
  • Distance: Accepts distance in kilometers

When the "Calculate Speed" button is clicked, the average_cal() function:

  • Retrieves values from all three spinboxes
  • Applies the average speed formula
  • Updates the result label with the calculated speed

Key Features

  • Spinbox Controls: Easy value selection with up/down arrows
  • Frame Layout: Organized UI using multiple frames
  • Real-time Calculation: Instant results when button is clicked
  • Formatted Output: Speed displayed with 2 decimal places

Output

Running the above code will create and display the Average Speed Calculator with input fields for hours, minutes, and distance, along with a calculate button and result display area.

Average Speed Calculator Hours 2 Minutes 30 Distance in(Km) 100 Calculate Speed 40.00 Km/Hr

Conclusion

This Tkinter application demonstrates how to create a practical speed calculator using Spinbox widgets for input and frames for organized layout. The formula correctly converts minutes to hours for accurate average speed calculation.

Updated on: 2026-03-25T16:47:06+05:30

576 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements