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 method available in the sockets module gethostbyname() and the method will return the IP address of it. That is simple, now lets the design the GUI for this app, Python GUI is 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.

Domain to IP – TKInter GUI Design

Here I used some widgets like Label, Button, EntryField (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

First I must import and initialize TKInter to use it.

1

2

3

import tkinter astk

root=tk.Tk()

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

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.

1

call_convert=partial(call_convert,result_label1,domainInput)

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

1

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.

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.

1

2

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

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

Now I can set the root geometry like,

1

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.

1

2

3

4

5

6

7

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.