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.

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.

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

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

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.

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.

as you see above I used partial method there, so I need to 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.

so I can now call the gethostbyname() like,

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

call_convert function:

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.

Now I can set the root geometry like,

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.

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

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
Domain to IP converter - programminginpython.com
Domain to IP converter – programminginpython.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.