Prerequisite: Python GUI – tkinter
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. In this article we will discuss how we can create an average speed calculator using Tkinter.
The idea here is to get time and distance as inputs from the user using tkinter window, then on click of a button the calculated average speed will be displayed to the user.
Formula Used:
![]()
Examples:
Input:
hours = 10
minutes = 20
distance = 200
Output:
Average speed = 19.35
Approach:
- Import module
- Create object
- Create windows and frames
- Create button to take input values
- Convert minutes into hours, by dividing minutes by 60
- Then add converted hours value into hours
- Divide Total distance value by Total hours value
- Display the value so obtained
- Execute code
Below is the implementation:
Python3
# Import Modulefrom tkinter import * # Create Objectroot = Tk() # Set height and widthwidth = 450height = 300 # Set Geometry and min, max sizeroot.geometry(f"{width}x{height}")root.maxsize(width, height)root.minsize(width, height) # Create LabelLabel(root, text="Average Speed Calculator", font=( "Helvetica", 18, "bold"), fg="blue").pack() # Calculate Average Speed def average_speed_calculator(): # Get the value of spinbox using get() method # Hours hrs = int(hours.get()) # minutes mins = int(minutes.get()) # distance dist = int(distance.get()) # Formule Used value = dist/(hrs+(mins/60)) # change the text of label using config method average_speed.config(text=f"{value} Km/Hr") # Create Mulitiple Framesframe = Frame(root)frame.pack() frame1 = Frame(root)frame1.pack() frame2 = Frame(root)frame2.pack() # Create Labels and Spin BoxesLabel(frame, text="Hours", width=15, font=("Helvetica", 14, "bold"), borderwidth=2, relief="solid").pack(side=LEFT, padx=10, pady=10)hours = Spinbox(frame, from_=0, to=10000000, width=5, font=("Helvetica", 14, "bold"))hours.pack(side=LEFT, pady=10) Label(frame1, text="Minutes", width=15, font=("Helvetica", 14, "bold"), borderwidth=2, relief="solid").pack(side=LEFT, padx=10, pady=10)minutes = Spinbox(frame1, from_=0, to=10000000, width=5, font=("Helvetica", 14, "bold"))minutes.pack(side=LEFT, pady=10) Label(frame2, text="Distance (Km)", width=15, font=("Helvetica", 14, "bold"), borderwidth=2, relief="solid").pack(side=LEFT, padx=10, pady=10)distance = Spinbox(frame2, from_=0, to=10000000, width=5, font=("Helvetica", 14, "bold"))distance.pack(side=LEFT, pady=10) Button(root, text="Average Speed", width=15, font=("Helvetica", 14, "bold"), command=average_speed_calculator, fg="red", bg="black").pack(pady=20)average_speed = Label(root, text="", width=20, font=( "Helvetica", 14, "bold"), relief="solid")average_speed.pack() # Execute Tkinterroot.mainloop() |
Output:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


