Prerequisites : Introduction to Tkinter | Using google distance matrix API
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task.
To create a tkinter :
- Importing the module – tkinter
- Create the main window (container)
- Add any number of widgets to the main window
- Apply the event Trigger on the widgets.
Let’s create a GUI based Distance-Time calculator using Python Tkinter module, which can tell distance between two city / location and time taken in travelling from one location to another location .
Modules required:
tkinter requests json
Below is the implementation :
# Python3 program to create Distance # Time GUI calculator using Tkinter # import everything from tkinter modules from tkinter import * # import modules import requests, json # Function for finding distance # and duration between two places def result(source, destination, travel_modes): # Enter your API key here api_key = 'Your_api_key' # base variable to store base url # Check travel modes if travel_modes == "train": # complete_url variable to store complete url address complete_url = base + 'origins =' + source + \ '&destinations; =' + destination + \ '&mode; = transit&transit;_mode = train' + \ '&key; ='+api_key # get method of requests module # return response object r = requests.get(complete_url) else: # complete_url variable to # store complete url address complete_url = base + 'origins =' + source+ \ '&destinations; ='+ destination + \ '&mode; ='+travel_modes+'&key; ='+ api_key # get method of requests module # return response object r = requests.get(complete_url) # json method of response object convert # json format data into python format data x = r.json() # x contains list of nested dictionaries # we know dictionary contains key value pair # Extracting useful information # from x dictionary row = x['rows'][0] cell = row['elements'][0] # Check value corresponding to # status key in cell dictionary if cell['status'] == 'OK' : # insert method inserting the # value in the text entry box. # Extracting useful information # from cell dictionary and inserting # into the respective text fields. distance_field.insert(10, cell['distance']['text']) duration_field.insert(10, cell['duration']['text']) else : # insert method inserting the # value in the text entry box. # Extract value corresponding to # status key from cell dictionary and # inserting into the respective text fields. mode_field.insert(10, cell['status']) distance_field.insert(10, cell['status']) # Function for getting values from # respective text entry boxes and # calling result function . def find() : # get method returns current text # as a string from text entry box source = source_field.get() destination = destination_field.get() travel_modes = mode_field.get() # Calling result() Function result(source, destination, travel_modes) # Function for inserting the train string # in the mode_field text entry box def train() : mode_field.insert(10, "train") # Function for inserting the driving string # in the mode_field text entry box def driving() : mode_field.insert(10, "driving") # Function for inserting the walking string # in the mode_field text entry box def walking() : mode_field.insert(10, "walking") # Function for clearing the contents # of source_field, distance_field, # duration_field text entry boxes. def del_source() : source_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # Function for clearing the contents of # destination_field, distance_field, # duration_field text entry boxes. def del_destination() : destination_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # function for clearing the contents of mode_field, # distance_field, duration_field text entry boxes. def del_modes() : mode_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # Function for clearing the # contents of all text entry boxes def delete_all() : source_field.delete(0, END) destination_field.delete(0, END) mode_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # Driver code if __name__ == "__main__" : # Create a GUI window root = Tk() # Set the background colour of GUI window root.configure(background = 'light green') # Set the configuration of GUI window root.geometry("500x300") # Create a welcome to distance time calculator label headlabel = Label(root, text = 'welcome to distance time calculator', fg = 'black', bg = "red") # Create a Source: label label1 = Label(root, text = "Source:", fg = 'black', bg = 'dark green') # Create a Destination: label label2 = Label(root, text = "Destination:", fg = 'black', bg = 'dark green') # Create a Choose travelling modes: label label3 = Label(root, text = "Choose travelling modes: ", fg = 'black', bg = 'red') # Create a Distance: label label4 = Label(root, text = "Distance:", fg = 'black', bg = 'dark green') # Create a Duration: label label5 = Label(root, text = "Duration:", fg = 'black', bg = 'dark green') # grid method is used for placing # the widgets at respective positions # in table like structure . headlabel.grid(row = 0, column = 1) label1.grid(row = 1, column = 0, sticky ="E") label2.grid(row = 2, column = 0, sticky ="E") label3.grid(row = 3, column = 1) label4.grid(row = 7, column = 0, sticky ="E") label5.grid(row = 8, column = 0, sticky ="E") # Create a text entry box # for filling or typing the information. source_field = Entry(root) destination_field = Entry(root) mode_field = Entry(root) distance_field = Entry(root) duration_field = Entry(root) # grid method is used for placing # the widgets at respective positions # in table like structure . # ipadx keyword argument set width of entry space . source_field.grid(row = 1, column = 1, ipadx ="100") destination_field.grid(row = 2, column = 1, ipadx ="100") mode_field.grid(row = 5, column = 1, ipadx ="50") distance_field.grid(row = 7, column = 1, ipadx ="100") duration_field.grid(row = 8, column = 1, ipadx ="100") # Create a CLEAR Button and attached # to del_source function button1 = Button(root, text = "CLEAR", bg = "red", fg = "black", command = del_source) # Create a CLEAR Button and attached to del_destination button2 = Button(root, text = "CLEAR", bg = "red", fg = "black", command = del_destination) # Create a RESULT Button and attached to find function button3 = Button(root, text = "RESULT", bg = "red", fg = "black", command = find) # Create a CLEAR ALL Button and attached to delete_all function button4 = Button(root, text = "CLEAR ALL", bg = "red", fg = "black", command = delete_all) # Create a Train Button and attached to train function button5 = Button(root, text = "Train", command = train) # Create a Driving Button and attached to driving function button6 = Button(root, text = "Driving", command = driving) # Create a Walking Button and attached to walking function button7 = Button(root, text = "Walking", command = walking) # Create a CLEAR Button and attached to del_modes function button8 = Button(root, text = "CLEAR", fg = "black", bg = "red", command = del_modes) # grid method is used for placing # the widgets at respective positions # in table like structure . button1.grid(row = 1, column = 2) button2.grid(row = 2, column = 2) button3.grid(row = 6, column = 1) button4.grid(row = 9, column = 1) button5.grid(row = 4, column = 0) button6.grid(row = 4, column = 1) button7.grid(row = 4, column = 2) button8.grid(row = 5, column = 2) # Start the GUI root.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.
Recommended Posts:
- Python | Simple GUI calculator using Tkinter
- Python - Compound Interest GUI Calculator using Tkinter
- Python - Dynamic GUI Calculator using Tkinter module
- Rank Based Percentile Gui Calculator using Tkinter
- Python | Create a GUI Marksheet using Tkinter
- Python: Weight Conversion GUI using Tkinter
- Python | ToDo GUI Application using Tkinter
- Python | GUI Calendar using Tkinter
- Sentiment Detector GUI using Tkinter - Python
- Python - Morse Code Translator GUI using Tkinter
- Create First GUI Application using Python-Tkinter
- Python - SpongeBob Mocking Text Generator GUI using Tkinter
- Python - Spell Corrector GUI using Tkinter
- Python - UwU text convertor GUI using Tkinter
- GST Rate Finder GUI using Python-Tkinter
- Create Copy-Move GUI using Tkinter in Python
- Python - English (Latin) to Hindi (Devanagiri) text convertor GUI using Tkinter
- Standard GUI Unit Converter using Tkinter in Python
- GUI chat application using Tkinter in Python
- Text to speech GUI convertor using Tkinter in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

