使用 Tkinter 创建一个 GUI 来检查域的可用性
可能存在这样一种情况:用户想要创建一个网站并对该网站的名称有某些想法,但对其可用性感到震惊。因此,让我们开发一个 GUI 来检查域的可用性。我们将使用Python-whois 模块以获取有关网站的信息。它能够为所有流行的 TLD(com、org、net 等)提取数据
安装
在编写代码之前,我们需要安装python-whois模块。要安装此类型,请在终端中输入以下命令。
pip install python-whois
安装后,让我们用例子写代码。
导入python-whois模块并从站点中提取信息
Python3
import whois
whois.whois('geeksforgeeks.com')
Python3
# importing modules
import whois
import sys
# Use exception handle program
# and get information from whois
try:
domain = whois.whois("geeksforgeeks.com")
if domain.domain_name == None:
sys.exit(1)
except :
print("This domain is available")
else:
print("Oops! this domain already purchased")
Python3
domain = whois.whois("myeeks.com")
Python3
# import modules
from tkinter import *
import whois
import sys
# user define funtion
# for get domain information
def Domain_info():
try:
domain = whois.whois(str(e1.get()))
if domain.domain_name == None:
sys.exit(1)
except:
result = "This domain is available"
else:
result = "Oops! this domain already purchased"
res.set(result)
# object of tkinter
# and background set for red
master = Tk()
master.configure(bg='red')
# Variable Classes in tkinter
res = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Website URL : ", bg="red").grid(row=0, sticky=W)
Label(master, text="Result :", bg="red").grid(row=3, sticky=W)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="red").grid(
row=3, column=1, sticky=W)
e1 = Entry(master)
e1.grid(row=0, column=1)
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Show", command=Domain_info, bg="Blue")
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5,)
mainloop()
输出:
如果我们收到此类信息,则表示该域已为 geeksforgeeks 注册。
现在让我们编写代码来检查域可用性。
蟒蛇3
# importing modules
import whois
import sys
# Use exception handle program
# and get information from whois
try:
domain = whois.whois("geeksforgeeks.com")
if domain.domain_name == None:
sys.exit(1)
except :
print("This domain is available")
else:
print("Oops! this domain already purchased")
输出:
Oops! this domain already purchased by GEEKSFORGEEKS.COM
让我们在域站点上检查一下。
检查myeeks.com域的可用性。
蟒蛇3
domain = whois.whois("myeeks.com")
输出:
This domain is available
让我们在域站点上检查一下。
让我们使用 Tkinter 为其创建一个 GUI。
执行:
蟒蛇3
# import modules
from tkinter import *
import whois
import sys
# user define funtion
# for get domain information
def Domain_info():
try:
domain = whois.whois(str(e1.get()))
if domain.domain_name == None:
sys.exit(1)
except:
result = "This domain is available"
else:
result = "Oops! this domain already purchased"
res.set(result)
# object of tkinter
# and background set for red
master = Tk()
master.configure(bg='red')
# Variable Classes in tkinter
res = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Website URL : ", bg="red").grid(row=0, sticky=W)
Label(master, text="Result :", bg="red").grid(row=3, sticky=W)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="red").grid(
row=3, column=1, sticky=W)
e1 = Entry(master)
e1.grid(row=0, column=1)
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Show", command=Domain_info, bg="Blue")
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5,)
mainloop()
输出: