Home » How to create a Domain to IP converter in Python GUI using TKInter

How to create a Domain to IP converter in Python GUI using TKInter

How to create a Domain to IP converter in Python GUI using TKInter

Hello everyone! Welcome back to programminginpython.com. Here in this post, I will show you how to create a simple domain to IP converter app in python GUI using TKInter. This app basically converts a domain name like www.google.com to its IP address like 127.0.0.1.

Here I use Python socketsto achieve this domain name to IP conversion, I will use one of the methods available in the sockets module `gethostbyname()` and the method will return the IP address of it. That is simple, now let us design the GUI for this app, Python GUI is the main discussed topic here, these converter apps are just some examples of it, previously I also discussed temperature converter app and now on domain to IP converter app.

You can also watch the video on YouTube here

Program on Github

Domain to IP – TKInter GUI Design

Here I used some widgets like LabelButtonEntryField (Input field).

These widgets are used as follows. An EntryField where one can enter any valid domain name like www.example.com.  A Button when pressed calls the function to find the entered domain name’s IP address. A Label to show the result.

 

Domain to IP converter - programminginpython.com
Domain to IP converter – programminginpython.com

First I must import and initialize TKInter to use it.

import tkinter as tk

root = tk.Tk()

Then I will initialize a Label and an EntryField and also set their position using gridlayout options.

input_label = tk.Label(root, text="Enter domain", background='#009688', foreground="#FFFFFF")
input_entry = tk.Entry(root, textvariable=domainInput)
input_label.grid(row=1)
input_entry.grid(row=1, column=1)

Here I need to set ‘domainInput’ to input_entry so I can use that value entered in that  field.

domainInput = tk.StringVar()

Program on Github

The Domain to IP converter

So when a user inputs some value i.e a valid domain name, the next thing is to press a button which shows the desired results.

I initialize a button with a command to call the conversion function.

result_button = tk.Button(root, text="Convert", command=call_convert, background='#FFFFFF', foreground="#009688")

result_button.grid(row=2, columnspan=4)

I need to pass a label to show the result and the input value which is entered in the Entry . Here I use partial from functools to pass parameters to the function call_convert

So before I call call_convert I will place the following line.call_convert = partial(call_convert, result_label1, domainInput)

as you see above I used partial method there, so I need to import partial.

from functools import partial

So now in the function call_convert, I use python sockets, which is used to connect to the internet and all other networking stuff.
There is a method called `gethostbyname()` which takes the input parameter as a domain name and returns its corresponding IP address.

The call_convert has 2 parameters, a label for showing the result and other is the input domain.

def call_convert(rlabel1, inputn):

so I can now call the `gethostbyname()` like,

import socket

...
...


ip = (socket.gethostbyname(inputn.get()))

Now I will show this result as text on the label.

rlabel1.config(text="The IP address of domain " + inputn.get() + " is " + ip)

call_convert function:

# the main conversion


def call_convert(rlabel1, inputn):
    try:
        ip = (socket.gethostbyname(inputn.get()))
        rlabel1.config(text="The IP address of domain " + inputn.get() + " is " + ip)
    except Exception:
        rlabel1.config(text="Please enter a valid url in the form 'www.example.com'")
    return

That’s it, IP address of a domain can be found in this simple way.

Now I will tweak the UI a bit, centering window, the elements in it and all such stuff.

Program on Github

Domain to IP – Centering window and elements in it

Centering main app window

As you see in the above image, all the items are centered and also the window also shows at the center of the screen. It is not the default behavior of Python TKInter GUI, I need to set this up.

First, to center the main app window, I use some functions called winfo_reqwidth() and winfo_reqheight()which returns the natural width and natural height of the widget respectively.  The natural size is the minimal size needed to display the widget’s contents, including padding, borders, etc.

Next, I use 2 other functions like winfo_screenwidth() and winfo_screenheight()which gets the width and height of this widget’s screen respectively, in pixels.

Now I can set the position right and position down so all the elements are centered in the window.

positionRight = int(root.winfo_screenwidth() / 2 - windowWidth / 2)

positionDown = int(root.winfo_screenheight() / 2 - windowHeight / 2)

Now I can set the root geometry like,

root.geometry("600x200+{}+{}".format(positionRight, positionDown))

So the main app window is centered now.

Centering elements in the window

For centering elements in the window, I just place weights on rows and columns to the grid layout.

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.rowconfigure(2, weight=1)
root.columnconfigure(2, weight=1)

contents = tk.Frame(root)
contents.grid(row=1, column=1)

The above 6 lines perfectly align any item to the center of the window. This results in a simple centering effect as the columns and rows either side of the main content are expanding equally to fill the window.

Program

__author__ = 'Avinash'

import tkinter as tk
import socket
from functools import partial


# the main conversion
def call_convert(rlabel1, inputn):
    try:
        ip = (socket.gethostbyname(inputn.get()))
        rlabel1.config(text="The IP address of domain " + inputn.get() + " is " + ip)
    except Exception:
        rlabel1.config(text="Please enter a valid url and in the form 'www.example.com'")
    return

# app window configuration and UI
root = tk.Tk()

# Gets the requested values of the height and width.
windowWidth = root.winfo_reqwidth()
windowHeight = root.winfo_reqheight()

# Gets both half the screen width/height and window width/height
positionRight = int(root.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(root.winfo_screenheight() / 2 - windowHeight / 2)

root.geometry("600x200+{}+{}".format(positionRight, positionDown))
root.title('Domain to IP Converter')
root.configure(background='#009688')
root.resizable(width=False, height=False)
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.rowconfigure(2, weight=1)
root.columnconfigure(2, weight=1)

contents = tk.Frame(root)
contents.grid(row=1, column=1)

domainInput = tk.StringVar()

# label and entry field
input_label = tk.Label(root, text="Enter domain", background='#009688', foreground="#FFFFFF")
input_entry = tk.Entry(root, textvariable=domainInput)
input_label.grid(row=1)
input_entry.grid(row=1, column=1)

# result label's for showing the IP of a domain
result_label1 = tk.Label(root, background='#009688', foreground="#FFFFFF")
result_label1.grid(row=3, columnspan=4)

# button click
call_convert = partial(call_convert, result_label1, domainInput)
result_button = tk.Button(root, text="Convert", command=call_convert, background='#FFFFFF', foreground="#009688")
result_button.grid(row=2, columnspan=4)

root.mainloop()

Program on Github

Output

Domain to IP converter - programminginpython.com
Domain to IP converter – programminginpython.com
Domain to IP converter - programminginpython.com
Domain to IP converter – programminginpython.com

Online Python Compiler

Leave a Reply

Your email address will not be published. Required fields are marked *