In this blog we will discuss about “How to make Website Blocker using Python“. Website Blocker is a tool that will help you to block some website that is specified by you that you don’t want to allow to be open on your PC or Laptop. This project will help you to stay away from their distraction by blocking that websites from your device.

In this Python Website Blocker Project, the user can enter multiple websites to block, and then clicking on the block button will check the condition that if the website already blocked then print ‘already blocked’ else blocked all that websites and print ‘blocked’.

Project Requirements

  • Python – Basic concepts
  • Tkinter – It is a standard GUI Python library.

To install the Tkinter Library, we can use this pip install command pip install tkinter.

Steps to Build Website Blocker:

  • Import the Tkinter Module
  • Create display window
  • Create an entry widget
  • Define blocker function
  • Create a block function

1. Importing the tkinter module

from tkinter import *

This will import all methods of tkinter library.

2. Create display window

root = Tk()
root.geometry('600x300')
root.resizable(0,0)
root. title("StudyGyaan - Website Blocker")

Label(root, text ='WEBSITE BLOCKER' , font ='arial 20 bold').pack() 
Label(root, text ='StudyGyaan' , font ='arial 20 bold').pack(side=BOTTOM)
  • geometry() set the width and height of the window
  • resizable(0,0) set the fixed size of the window
  • title() used to set the title of the window

3. Create an entry widget

host_path ='C:\Windows\System32\drivers\etc\hosts' 
ip_address = '127.0.0.1' 
Label(root, text ='Enter Website :' , font ='arial 13 bold').place(x=5 ,y=60) Websites = Text(root,font = 'arial 10',height='2', width = '40', wrap = WORD, padx=5, pady=5) 
Websites.place(x= 140,y = 60)

4. Define blocker function

def Blocker():
    website_lists = Websites.get(1.0,END)
    Website = list(website_lists.split(","))

    with open (host_path , 'r+') as host_file:
        file_content = host_file.read()
        for website in Website:
            if website in file_content:
                Label(root, text = 'Already Blocked' , font = 'arial 12
                bold').place(x=200,y=200)
                pass
            else:
                host_file.write(ip_address + " " + website + '\n')
                Label(root, text = "Blocked", font = 'arial 12 bold').place(x=230,y
                =200)

5. Create a block button

block = Button(root, text = 'Block',font = 'arial 12 bold',pady = 5,command = Blocker ,width = 6, bg = 'royal blue1', activebackground = 'sky blue') block.place(x = 230, y = 150) 
root.mainloop()

When we click on the block button it will invoke the Blocker function.

  • Button() – used to display button on our window
  • command – called when we click the button.

Output :

Website Blocker with GUI using Python

You may also like :