Welcome folks today in this blog post we will be finding the zipcode,state and city from the given address and display it inside the window of tkinter using the geopy module. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the geopy
module using the pip
command as shown below
pip install geopy
After this you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 |
# importing the modules from geopy.geocoders import Nominatim from tkinter import * from tkinter import messagebox # object of tkinter # and background set for light grey master = Tk() master.configure(bg = 'light grey') mainloop() |
As you can see in the above code we are importing the geopy
module and also we are importing the messagebox
module. And then we are setting the background color of the tkinter window as light grey.
1 2 3 |
# variable Classes in tkinter place_res = StringVar(); res = StringVar(); |
As you can see we are declaring the two
variables which will be used to store the address entered by the user and also we will be storing the result
inside the res
variable.
Now we will be adding the labels inside the tkinter window. These labels will contain the normal text which is required for the app
1 2 3 4 5 6 7 8 |
# creating label for each information # name using widget Label Label(master, text = "Enter place :" , bg = "light grey").grid(row = 0, sticky = W) Label(master, text = "Place :" , bg = "light grey").grid(row = 1, sticky = W) Label(master, text = "Country Address :" , bg = "light grey").grid(row = 2, sticky = W) |
As you can see we are using the Label consturctor. And inside it we are setting the color of the label and then we are also providing the
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Label(master, text = "", textvariable = place_res, bg = "light grey").grid(row = 1, column = 1, sticky = W) Label(master, text = "", textvariable = res, bg = "light grey").grid(row = 2, column = 1, sticky = W) e = Entry(master) e.grid(row = 0, column = 1) # creating a button using the widget # Button that will call the submit function b = Button(master, text = "Show", command = getinfo ) b.grid(row = 0, column = 2, columnspan = 2, rowspan = 2, padx = 5, pady = 5) |
As you can see after the labels we have the button to submit the form and also we have the input field where the user will enter his or her address. Now we need to define the function inside the button.
1 2 3 4 5 6 |
def getinfo(): geolocator = Nominatim(user_agent = "geoapiExercises") place = e.get() place_res.set(place) location = geolocator.geocode(place) res.set(location) |
As you can see inside this method we are getting the address from the input field
using the get()
method and then we using the set()
method to set the place and then we are geolocating addresses. And then returning the city,statename and the zipcode of the full address that is submitted by the user.
As you can see we have just the snippet of the address. It detects and give full addresses. and display it inside the tkinter window.
Full Source Code
Wrapping the blog post this is the full source code of the app.py
file
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 |
# importing the modules from geopy.geocoders import Nominatim from tkinter import * from tkinter import messagebox def getinfo(): geolocator = Nominatim(user_agent = "geoapiExercises") place = e.get() place_res.set(place) location = geolocator.geocode(place) res.set(location) # object of tkinter # and background set for light grey master = Tk() master.configure(bg = 'light grey') # variable Classes in tkinter place_res = StringVar(); res = StringVar(); # creating label for each information # name using widget Label Label(master, text = "Enter place :" , bg = "light grey").grid(row = 0, sticky = W) Label(master, text = "Place :" , bg = "light grey").grid(row = 1, sticky = W) Label(master, text = "Country Address :" , bg = "light grey").grid(row = 2, sticky = W) # creating label for class variable # name using widget Entry Label(master, text = "", textvariable = place_res, bg = "light grey").grid(row = 1, column = 1, sticky = W) Label(master, text = "", textvariable = res, bg = "light grey").grid(row = 2, column = 1, sticky = W) e = Entry(master) e.grid(row = 0, column = 1) # creating a button using the widget # Button that will call the submit function b = Button(master, text = "Show", command = getinfo ) b.grid(row = 0, column = 2, columnspan = 2, rowspan = 2, padx = 5, pady = 5) mainloop() |