Create a GUI to Check Domain Availability using Tkinter
Are you tired of manually checking domain availability? Do you want to automate this tedious task and make it more efficient? Well, you’re in luck! In this article, we will teach you how to create a GUI using Tkinter to check the availability of domains automatically.
What is Tkinter?
Tkinter is a built-in Python module that is used for developing graphical user interfaces (GUI) with the help of several widgets and features. It is a cross-platform package, which means it can be used for both Windows and Mac operating systems.
Preparing the Environment
Before we get started, we need to ensure that we have Python 3.x installed on our machine, along with Tkinter. To check if Tkinter is already installed, you can run the following code:
import tkinter
print(tkinter.TkVersion)
If Tkinter is not installed, you can install it by running the following command:
sudo apt-get install python3-tk
The Code
Let’s begin by importing the Tkinter module and initializing the root window.
import tkinter as tk
root = tk.Tk()
root.geometry("400x200")
root.title("Domain Availability Checker")
In the above code, we have initialized a root window with a size of 400×200 pixels and set the title as “Domain Availability Checker.”
Now, we can add a label to display the heading.
heading = tk.Label(root, text="Check Domain Availability", font=("Helvetica", 16))
heading.place(x=60, y=10)
In the above code, we have created a label with the name "Check Domain Availability"
and set the font to Helvetica
with a size of 16 px
. We then placed the label at x=60
and y=10
.
Next, we can add a text input field for the user to input the domain name they want to check.
domain_label = tk.Label(root, text="Enter Domain Name:", font=("Helvetica", 12))
domain_label.place(x=20, y=60)
domain_entry = tk.Entry(root, width=40, font=("Helvetica", 12))
domain_entry.place(x=170, y=60)
In the above code, we have created a label with the name "Enter Domain Name"
and set the font to Helvetica
with a size of 12 px
. We then placed the label at x=20
and y=60
. We also created an input field with a width of 40
and set the font to Helvetica
with a size of 12 px
. We placed the input field at x=170
and y=60
.
Next, we can add a button to check the domain availability.
def check_domain_availability():
domain = domain_entry.get()
# Code to check the domain availability
# Code to display the result
check_button = tk.Button(root, text="Check Availability", font=("Helvetica", 12), command=check_domain_availability)
check_button.place(x=120, y=120)
In the above code, we have created a function check_domain_availability()
that will be executed when the user clicks the "Check Availability"
button. We have also created a button with the text "Check Availability"
and set the font to Helvetica
with a size of 12px
. We have assigned the function check_domain_availability()
to the button so that it will be called when the user clicks the button. We placed the button at x=120
and y=120
.
Finally, we can add a label to display the availability of the domain.
result_label = tk.Label(root, text="", font=("Helvetica", 12))
result_label.place(x=140, y=170)
In the above code, we have created a label to display the availability of the domain. We have set the font to Helvetica
with a size of 12px
. We placed the label at x=140
and y=170
. We have left the text
attribute blank for now, as we will update it in the check_domain_availability()
function.
Checking Domain Availability
Now that we have created the GUI, we need to write the code to check the domain availability. There are several ways to do this, but we will be using the whois
module to check the availability of the domain.
import whois
def check_domain_availability():
domain = domain_entry.get()
try:
w = whois.whois(domain)
except whois.parser.PywhoisError:
result_label.configure(text="Domain not available")
return
if w['status'] is None:
result_label.configure(text="Domain available")
else:
result_label.configure(text="Domain not available")
In the above code, we first import the whois
module and define the function check_domain_availability()
. We get the domain that the user has entered from the input field using the get()
method. We then use the whois.whois()
method to get the information about the domain. If the domain is not available, the whois
module will raise a PywhoisError
exception, which we catch using a try-except
block. If the exception is raised, we display "Domain not available"
on the result label. Otherwise, we check the status attribute of the whois
object. If the status is None
, then the domain is available. If the status is not None
, then the domain is not available.
Final Code
import tkinter as tk
import whois
root = tk.Tk()
root.geometry("400x200")
root.title("Domain Availability Checker")
heading = tk.Label(root, text="Check Domain Availability", font=("Helvetica", 16))
heading.place(x=60, y=10)
domain_label = tk.Label(root, text="Enter Domain Name:", font=("Helvetica", 12))
domain_label.place(x=20, y=60)
domain_entry = tk.Entry(root, width=40, font=("Helvetica", 12))
domain_entry.place(x=170, y=60)
result_label = tk.Label(root, text="", font=("Helvetica", 12))
result_label.place(x=140, y=170)
def check_domain_availability():
domain = domain_entry.get()
try:
w = whois.whois(domain)
except whois.parser.PywhoisError:
result_label.configure(text="Domain not available")
return
if w['status'] is None:
result_label.configure(text="Domain available")
else:
result_label.configure(text="Domain not available")
check_button = tk.Button(root, text="Check Availability", font=("Helvetica", 12), command=check_domain_availability)
check_button.place(x=120, y=120)
root.mainloop()
Conclusion
In this article, we learned how to create a GUI using Tkinter to check the availability of domains automatically. We imported the Tkinter module, initialized the root window, added labels, input fields, buttons, and result labels. We also wrote the code to check domain availability using the whois
module. With this program, you will be able to check domain availability without having to manually search for it.