Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Tkinter Project to Find City,State,Zip Code From Given Address Using GeoPy Module GUI Desktop App

Posted on November 16, 2022

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

 

 

Python
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.

 

 

Python
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

 

 

Python
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

 

 

Python
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.

 

 

Python
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

 

 

Python
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()

Recent Posts

  • React.js Twitter API Tutorial to Embed Profile & Timeline, Hashtags of User in Browser Using Javascript
  • Android Java Tutorial to Change Styles & Visibility of System Bars (Top, Action & Status) Full Example
  • Android Java Project to Display & Play Audio Files From Storage inside ListView Using MediaPlayer Class
  • Android Java Project to Build MP4 Video to MP3 Audio Converter Using MediaMuxer Class
  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme