# Standard GUI Unit Converter in Python

In the following tutorial, we will learn the method of creating a standard unit converter with the help of the Tkinter library in the Python programming language.

But before we start building the project, let us briefly discuss the unit conversion and some related aspects.

## Understanding the Unit Conversion

The unit conversion consists of multiple steps involving multiplication or division by a numerical factor or, specifically, a conversion factor. The process may also involve selecting the applicable number of substantial digits and rounding. There are different units of conversion that are utilized to measure different parameters.

1. Measuring Length
2. Measuring Temperature
3. Measuring Weight
4. Measuring Capacity

In mathematics, we generally perform the conversion of the units of measurement for better understanding. For instance, we can determine the length of a table in inches, whereas the length of a garden is evaluated in yards to make it easy to apprehend. Similarly, it is pointless to measure the length of a finger in miles. We can measure different quantities using the units of measurement.

The conversion of units becomes a necessity while solving different mathematical problems. For instance, if the length of a rectangle is provided in feet whereas the width is provided in meters, then to determine the perimeter of the rectangle, we must convert the units to make them uniform. Thus, we need to learn the concept of unit conversion.

## Conversion of Units Measurements

Different units are utilized to measure different quantities. The units utilized to measure are shown as follows:

1. Length
2. Area
3. Volume
4. Weight
5. Temperature

The following tables display the Quantities and some units used to measure each quantity.

S. No. Quantity Units
1 Length Meter(m), Inch (in), Feet (ft), Yards (yd), Miles
2 Area Square Meter, Square Inch, Square Feet, Acre, Square Yard, Square Mile
3 Volume Fluid Ounces (Fl oz), Pint (pt.), Quarts (qt), Gallons (gal)
4 Weight Ounces, Pounds (lb.), Tons
5 Temperature Kelvin (K), Celsius (C), Fahrenheit (F)

There are some non-standard units of measurement used in the initial learning stage to introduce children to the concept of units and measurement without having them read any scales. Reading any scale is a tedious job in itself, so the introduction to the non-standard measures allows the child to focus on the concepts like heavy, light, long, short, and more before moving on to the next step of measuring using the standard units.

An example of the non-standard measures can be the hand spans. Hand spans are generally used to measure the length of the objects in an informal way. However, the measurement may differ due to the subjectivity involved. Let us now consider the unit conversion table below depicting the relationship between different units.

S. No. Quantity Relationship
1 Length 1 mile = 1760 yards = 5280 feet = 63,360 inches
2 Volume (Capacity) 1 gallon = 4 quarts = 8 pints = 128 fluid ounces
3 Weight (Mass) 1 ton = 2000 pounds = 32,000 ounces
4 Temperature C/5 = (F - 32)/9 = (K - 273)/5

## Charts of Unit Conversion

The Charts of Unit Conversion offer conversion factors for conversions of various units of length, area, mass, volume, and temperature, serving as a reference for easy and quick calculations. We can consider the charts as the formula for converting units where it helps convert any quantity given in one unit to another.

The following tables represent the charts for the unit conversion:

### Chart for Length Unit Conversions

S. No. Unit Conversion factor
1 1 millimeter 0.001 meter
2 1 centimeter 0.01 meter
3 1 decimeter 0.1 meter
4 1 decameter 10 meters
5 1 hectometer 100 meters
6 1 kilometer 1000 meters
7 1 inch 2.54 x 10-2 meters
8 1 foot 0.3048 meter / 12 inches
9 1 yard 0.9144 meter / 3 feet
10 1 mile 1.609344 kilometers / 1760 yards / 5280 feet / 63,360 inches / 1609.344 meters

### Chart for Area Unit Conversions

S. No. Unit Conversion factor
1 1 sq. inch 6.4516 x 10-4 sq. meters
2 1 sq. foot 9.2903 x 10-2 sq. meters
3 1 acre 4.0468 x 103 sq. meters
4 1 hectare 1 x 104 sq. meters
5 1 sq. mile 2.5888 x 106 sq. meters
6 1 barn 10 x 10-28 sq. meters

### Chart for Volume Unit Conversions

S. No. Unit Conversion factor
1 1 millilitre 0.001 litre
2 1 centilitre 0.01 litre
3 1 decilitre 0.1 litre
4 1 decaliter 10 litres
5 1 hectolitre 100 litres
6 1 kilolitre 1000 litres
7 1 cubic inch 1.639 x 10-2 litre
8 1 pint 473.16 millilitres / 0.57 litre
9 1 quart 946.353 millilitres / 0.946353 litre / 2 pints
10 1 gallon 3.785 litres / 4 quarts / 8 pints / 128 fluid ounces
11 1 cubic foot 28.316 litres

### Chart for Mass Unit Conversions

S. No. Unit Conversion factor
1 1 milligram 0.001 grams
2 1 centigram 0.01 grams
3 1 decigram 0.1 grams
4 1 decagram 10 grams
5 1 ounce 28.3495 grams
6 1 hectagram 100 grams
7 1 kilogram 1000 grams
8 1 stone 6350.29 grams
9 1 pound 453.592 grams/ 0.453592kg/ 16 ounces
10 1 ton 907.185kg/ 2000 pounds/ 32,000 ounces

### Chart for Temperature Unit Conversions

S. No. Unit Conversion factor
1 Celsius (C) 5 x (F - 32) / 9 = K - 273

Now that we have learned the basics of Unit Conversion let us start building one using the Tkinter library in the Python programming language.

## Building the Unit Converter using Tkinter

Since the project code is lengthy and complicated to understand at once, we have divided the complete project code into segments for better understanding.

To understand the code, we will be following the steps shown below:

Step 1: We will start by importing the required libraries and modules.

Step 2: We will then define the functions necessary for the execution of the application.

Step 3: We will define the required dictionary and lists to store the data.

Step 4: We will then create the main window for the application.

Step 5: We will then add the widgets to the main window and apply the event trigger to them.

Let us understand these steps in detail.

### Importing the required libraries and modules

We will start by importing the necessary libraries and modules required for the project. For this project, we will use the Tkinter library that will provide the Graphical User Interface to the application.

The following snippet of code demonstrates the same:

File: unitConverter.py

Explanation:

In the above snippet of code, we have imported the Tkinter library for the project.

### Defining the functions for the application

Once we have successfully imported the required library, it is time for us to define the functions necessary to execute the application. These functions include the function to reset the entered data and resultant value and the function that will allow us to convert a specific unit into the requested one.

At first, we will start by creating a function to reset all the entered data and the result. The following is the snippet of code illustrating the same:

File: unitConverter.py

Explanation:

In the above snippet of code, we have defined a function as reset. Within this function, we have used the delete() method to delete the entries in the entry fields. We have then set the value of the options menu to the first index of the list using the set() method. At last, we have used the focus_set() method and set the focus to the input field.

Let us now define another function to convert a specific unit into the request one. The following snippet of code demonstrates the same:

File: unitConverter.py

Explanation:

In the above snippet of code, we have retrieved the string value from the input entry field using the get() method and the float() method to convert it into float value, storing it in the inputVal variable. We have then retrieved the values from the selection menus and stored them in input_unit and output_unit variables, respectively. We have then created a list of the required combinations of the conversion factors so that the input and output fields must lie in the same unit chart. We have then used the nested if-else conditional statement to check if any of the requested conversions lies within the above list and perform the operation accordingly.

### Adding required data structures to the application

We will now add the required data structures to the application, which includes a Dictionary and some lists. These data structures will store the necessary data like the units and their initial values, unit conversion charts, and the selection menu options.

Let us now consider the following snippet of code demonstrating the same.

File: unitConversion.py

Explanation:

In the above snippet of code, we have created a dictionary as unitDict, storing the units and their initial measurements. We have then created some lists distinguishing the units in the dictionary in their respective charts. We have another list with all the units to be displayed as the options in the selection menus.

### Creating the main window for the application

We will now create the main window for the application where all the necessary widgets will be displayed. We will use the Tk() class of the Tkinter library to create the window.

Let us consider the following snippet of code demonstrating the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have created an instance of the Tk() class. We have then set the title and size of the main window using the title() and geometry() methods. We have also disabled the resizing option by setting the values of the parameters of the resizable() method to 0. At last, we have set the background color to #16a085 with the help of the configure() method.

### Adding the widgets to the main window

Once we have created the main window for the application, it is time for us to add some widgets to it. We will start by adding some frames to the window to provide structure to other widgets.

We can add the frames using the Frame() widget of the Tkinter library. The following snippet of code illustrates the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the Frame() widget to create the frames for the main window. We have set the master parameter to guiWindow and the background color to #16a085.

We will now add a label to display the heading on the main window using the Label() widget.

Let us consider the following snippet of code demonstrating the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the Label() widget to create a label displaying the heading. We have set the master parameter of this widget to the header_frame frame with the text - STANDARD UNIT CONVERTER. We have also set the font style to Arial black and size 16. We have also set the background color to #16a085 and foreground color to #e8f6f3. At last, we have used the pack() method and set the value of the expand parameter to True and the fill parameter to both in order to set the position of the label.

We will now create some objects of the StringVar() class to store the data from the input and output selections menus. We will also set the initial value of these objects to the index value 0 of the SELECTIONS list we created earlier.

File: unitConverter.py

Explanation:

In the above snippet of code, we have created two objects of the StringVar() class. We have then used the set() method to set the primary value of the objects to index value 0 of the SELECTIONS list.

We will now create some labels using the Label() widget to display information on the body of the main window.

Let us now consider the snippet of code shown below illustrating the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the Label() widget and created two labels, setting the master parameters of these widgets to the body_frame frame. We have added text to these labels along with the background color to #16a085 and the foreground color to #d0ece7. We have then used the grid() method to set the position of the above labels.

We will now add some entry fields to the main window with the help of the Entry() widget to enter and display data.

The following is the snippet of code illustrating the usage of the Entry() widget.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the Entry() widget to add entry fields to the body of the main window by setting the master parameter to the body_frame frame and background color to #e8f8f5. We have then used the grid() method to set the position of the above entry fields.

Let us consider the following snippet of code demonstrating the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the OptionMenu() widget to add the options menus to the display list for the user to select from. We have set the master parameter of these widgets to the body_frame frame the store the selected data in the input_value and output_value variables, respectively. We then set the data list to the SELECTIONS list for both widgets. At last, we have used the grid() method to set the position of these option menus on the main window.

We will now add some buttons using the Button() widget. These buttons will call the convert() and reset() functions.

Let us consider the following snippet of code demonstrating the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the Button() widget to create two buttons. The first button is the CONVERT button that will call the convert() function allowing the user to convert the given unit into the requested one. The second one is the RESET button that will call the reset() function to reset all the entries on the main window. At last, we have set the positions of these buttons using the grid() method.

We will now use the mainloop() method on the object of the Tk() class to run the application.

The following is the snippet of code demonstrating the same.

File: unitConverter.py

Explanation:

In the above snippet of code, we have used the mainloop() method with guiWindow, the object of the Tk() class, to run the application.

Hence, the project code is now completed. We can save the file and run the following command in a command shell or terminal to see the output.

Syntax:

Before we see the output, let us consider the complete code of the "GUI Unit Converter" project in Python.

## The Complete Project Code

The following is the snippet of code of the "GUI Unit Converter" project in the Python programming language.

File: unitConverter.py

Output: ## Conclusion

This tutorial taught us how to create a Unit Converter application based on Graphical User Interface using the Tkinter library in Python. We have also learned about different Tkinter library widgets and methods and their implementation in Python script.

### Feedback   