Welcome folks today in this tutorial we will be making a distance-time
calculator desktop app using the google maps distance matrix api
in python full project for beginners. All the source code of the application is shown below.
Get Started
In order to get started you need to install the following libraries inside your python project by using the pip
command
pip install tkinter
pip install requests
pip install json
Now after installing all these libraries make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# 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 base = 'https://maps.googleapis.com/maps/api/distancematrix/json?' # 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() |
Now inside this python script just replace the api_key
and then run this by typing the below command
python app.py