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.
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’.
Project Prerequisites
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
Follow TechVidvan on Google & Stay updated with latest technology trends
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
Summary
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.
Sir it was not working
Please post the stack trace, we will look into the issue
Yes.It was not working.The block button also not showing.
but how i make unblock button
For unblocking the webite you can create another function unblock() and after that you will just search for the required website in webite_lists and if the webite is present you can delete it from website_lists otherwise you can display an error message( Eg: no such website blocked)
Can you explain briefly.How to unblock a website
How do you find out the r+, hostpath and ip address? Program is great just unsure on that part
r+ opens an existing file for reading purpose.And on almost all networking system, localhost uses the ip address ‘127.0.0.1’.
can you send code the Code for unblock
and also button for unblock
can you send code for unblock
and also button for unblock
Reply
You just need to change the date, it will then unblock and u r done!!
You just have to delete the website from website_lists, to unblock the website. Look at the implementation of block() and try implementing unblock().
Can you explain briefly .How to unblock a website.
It keeps showing “Permission denied :c:\\windows\\system32\\drivers\\etc\\host” error
same issue
hi, dev did you find any solution for this issue…
For future reference, the program need to be run with admin privileges.
Sir it is showing an error :
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\Nischay Joshi\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py”, line 1895, in __call__
return self.func(*args)
File “ruf.py”, line 18, in Blocker
with open (host_path , ‘r+’) as host_file:
PermissionError: [Errno 13] Permission denied: ‘C:\\Windows\\System32\\drivers\\etc\\hosts’
Please help me rectify it