How to track phone number in Python

Kalebu Jordan
3 min readSep 30, 2020

--

Hello guys,

Today I’m going to share with you how to build a simple Desktop application to track the country through the phone number.

It’s a very basic app, therefore you just need to have the basics of Python to be able to complete this tutorial.

Requirements

Install the following python libraries for you to able to completely follow through this Tutorial

Installation

pip install python-tk, phone-iso3166 , pycountry

We are going to use phone-iso3166 to determine the get alpha_2 letters of the country from the number and pycountry to determine the official name of the country using alpha_2 letters we obtained from phone-iso3166.

Sample code

>>> import pycountry 
>>> from phone_iso3166.country import phone_country
>>> code = phone_country("255757295721")
>>> code 'TZ'
>>> pycountry.countries.get(alpha_2 = code) Country(alpha_2='TZ', alpha_3='TZA', common_name='Tanzania', name='Tanzania, United Republic of', numeric='834', official_name='United Republic of Tanzania')
>>>

Well now we know how to get country information from a phone number, We need to put our logic code in a form of an App so as we can easily use it.

Below is a code of the skeleton for our GUI app with function using the logic we learned above

app.py

import json 
import pycountry
from tkinter import Tk, Label, Button, Entry
from phone_iso3166.country import phone_country
class Location_Tracker:
def __init__(self, App):
self.window = App
self.window.title("Phone number Tracker")
self.window.geometry("500x400")
self.window.configure(bg="#3f5efb")
self.window.resizable(False, False)
#___________Application menu_____________
Label(App, text="Enter a phone number",fg="white", font=("Times", 20), bg="#3f5efb").place(x=150,y= 30)
self.phone_number = Entry(App, width=16, font=("Arial", 15), relief="flat")
self.track_button = Button(App, text="Track Country", bg="#22c1c3", relief="sunken")
self.country_label = Label(App,fg="white", font=("Times", 20), bg="#3f5efb")
#___________Place widgets on the window______
self.phone_number.place(x=170, y=120)
self.track_button.place(x=200, y=200)
self.country_label.place(x=100, y=280)
#__________Linking button with countries ________
self.track_button.bind("<Button-1>", self.Track_location)
#255757294146

def Track_location(self,event):
phone_number = self.phone_number.get()
country = "Country is Unknown"
if phone_number:
tracked = pycountry.countries.get(alpha_2=phone_country(phone_number))
print(tracked)
if tracked:
country = tracked.official_name
self.country_label.configure(text=country)
PhoneTracker = Tk()
MyApp = Location_Tracker(PhoneTracker)
PhoneTracker.mainloop()

Output :

Once you run the output will look like this, now you can experiment with a different number from a different location to determine their country

Congratulations, you just made your own phone location tracker, If you find this post useful, share it with your fellow friends that you have made a cool desktop app.

Don’t forget to subscribe to be updated on upcoming Python tutorials, In case of comment, suggestion, or any difficulty drop it in the comment box below and I will get back to you as fast as I can.

To get the whole code you can check out my GITHUB PROFILE

I also write in-depth articles about Python on my Personal Blog

You can also connect with on Twitter

Originally published at https://kalebujordan.com on September 30, 2020.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kalebu Jordan
Kalebu Jordan

Written by Kalebu Jordan

Mechatronics Engineer by Professional || Self taught Python Developer || Passionate about open source and bringing impact to education sector

No responses yet

Write a response