Get Phone Number Information using Python

Get Phone Number Information using Python

Introduction

In this project, we’ll demonstrate how to get phone number information using python. Tkinter and phonenumbers library were used to create this project. The Tkinter package facilitates quick and easy creation of graphical user interfaces. Phonenumbers is a library for parsing, formatting, and verifying foreign phone numbers. This program has an easy-to-use entry, button, and label design.

Explanation

This code generates an entry, buttons, and labels for a simple Tkinter-based GUI. This code generates a basic Tkinter window with a label to show the outcome, an entry field to enter the phone number, and a button to start the lookup.  Once a phone number is entered and the “Lookup” button is pressed, the program shows details about the nation, service provider, and area of the phone number. The phonenumbers library is used to find up information on a certain phone number. This program’s error handling handles the issues that arise from incorrect phone number formatting. If something goes wrong, a notice informing the user of the issue is displayed in the result label.

Required Package Installation

pip install phonenumbers

Source Code

import phonenumbers
from phonenumbers import carrier, geocoder, timezone
import tkinter as tk
from tkinter import messagebox

def lookup_phone_number():
    mobile_number = entry.get()
    try:
        mobile_number = phonenumbers.parse(mobile_number)
        if phonenumbers.is_valid_number(mobile_number):
            result_text.set('Service Provider: {}'.format(carrier.name_for_number(mobile_number, "en")))
            result_text.set(result_text.get() + '\nPhone number belongs to country: {}'.format(geocoder.description_for_number(mobile_number, "en")))
            result_text.set(result_text.get() + '\nPhone Number belongs to region: {}'.format(timezone.time_zones_for_number(mobile_number)))
        else:
            result_text.set("Please enter a valid mobile number")
    except Exception as e:
        result_text.set("Error: {}".format(e))

# Create the main window
window = tk.Tk()
window.title("Phone Number Lookup")

# Create and pack widgets
label = tk.Label(window, text="Enter Phone number with country code (+xx xxxxxxxxx):")
label.pack(pady=10)

entry = tk.Entry(window)
entry.pack(pady=10)

lookup_button = tk.Button(window, text="Lookup", command=lookup_phone_number)
lookup_button.pack(pady=10)

result_text = tk.StringVar()
result_label = tk.Label(window, textvariable=result_text)
result_label.pack(pady=10)

# Start the GUI event loop
window.mainloop()

Output

Get Phone Number Information using Python
Get Phone Number Information using Python

Leave a Comment