Python Website Blocker with GUI
Create a Website Blocker in Python – Block any Unwanted Website
Website Blocker is a tool that denies access to websites permanently or by schedule. To use the internet safely we can block all websites from unwanted categories.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
Website Blocker Python Project
The objective of Website Blocker python project is to block the given websites from any device. This project will help the user to stay away from their distraction by blocking websites from their device so that they can not open them.
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’.
To implement website blocker project, we will use the basic concepts of Python and Tkinter library.
Tkinter is a standard GUI Python library. It is one of the fastest and easiest ways to build GUI applications using Tkinter.
To install the library, you can use pip install command to the command prompt:
pip install tkinter
Download Website Blocker Project code
Please download the source code of website blocker project: Python Website Blocker
Steps to build Website blocker Python Project:
- Importing the module
- Create the display window
- Create an entry widget
- Define function
- Create a block button
1. Importing the module
from tkinter import *
We import modules from tkinter library
2. Create the display window
root = Tk() root.geometry('500x300') root.resizable(0,0) root.title("TechVidvan - Website Blocker")
We use tkinter library to create a window where we’ll enter our text which we want to convert into voice.
- Tk() initialized tkinter which means window created
- geometry() set the width and height of the window
- resizable(0,0) set the fixed size of the window
- bg = ‘’ use to set the background color
- title() used to set the title of the window
Label(root, text ='WEBSITE BLOCKER' , font ='arial 20 bold').pack() Label(root, text ='TechVidvan' , font ='arial 20 bold').pack(side=BOTTOM)
Label() widget is used to display one or more than one line of text that users aren’t able to modify.
- root – name which we refer to our window
- text – which we display on the label
- font – in which the text is written
- pack – organized widget in block
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)
- host_path stores the path of our host file
- ip_address stores the IP address used by localhost
- Text() widget is used for multi-line text areas.
- wrap = WORD will break the line after the last word.
- padx puts an extra bit space on left and right side of the widget
- pady puts extra space on top and bottom side of the widget.
4. Define 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)
- website_lists get all the Websites to enter by the users
- website_list(lists.split(“,”)) split the content of the lists by comma and then convert it into list ad store it into Website
- with open – The with open statement open the file and it will automatically close the file handler when we are done with it
- r+ will use to open a file for reading and writing
- If the website is already in file_content then print a label with text already blocked.
- Else it will block all the given website and print label of text ‘Blocked’.
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 call the Blocker function.
- Button() – used to display button on our window
- command – called when we click the button.
- activebackground – sets the background color to use when the button is click
Website Blocker Project Output
We have successfully developed the Website Blocker python project. We used the popular Tkinter library for rendering graphics on a display window and the basic concept of python programming.