# Weight Converter GUI | Python

In this article, I will take you through how to create a weight converter GUI with Python programming language.

Contents

## Introduction :

Let’s create a GUI-based weight converter that accepts a kilogram input value and converts that value to grams, pounds, and ounces when the user clicks the Convert button.

Weight Conversion is a crucial part of our life and we use it in our daily life to covert some units into some units.

Did anyone think about creating our own program to convert weight units with a Graphical User Interface by ourselves?
and what if I tell you!! You can do the same things with the help of a simple python script.

## Outline:

In this project, we will take Kilograms as input from the user and convert them into grams, ounces, and pounds. We just write our python script in any text editor or IDE and run it.

After running the script a new window will appear with GUI(Graphical User Interface), we can enter a value in KG’s and get the respective values.

## Project Prerequisites:

You don’t have to install any package in your system to run this script. Because we create our own GUI with the help of tkinter.Tkinter is automatically provided by a python while installing.

tkinter: The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including macOS, as well as on Windows systems.

It runs on the following:

• Linux
• Windows
• macOS

Supported Python versions are 2.73.4+

That’s it…! We just don’t need to install any other package for this project.

## Code Implementation:

The first thing we need to do is to import the required packages and modules. In this project, we import tkinter package to make use of its functions.

After the import section, we need to create our GUI window.

# Creating a GUI Window

window = Tk()

```# Creating a GUI Window
window = Tk()
```

Next, we will be defining a function for the conversion process. Weight conversion means to multiply the value of a unit with the standard conversion value, so we will multiply it with the corresponding values.

def from_kg():

gram = float(e2_value.get())*1000

pound = float(e2_value.get())*2.20462

ounce = float(e2_value.get())*35.274

t1.delete(“1.0”,END)

t1.insert(END, gram)

t2.delete(“1.0”, END)

t2.insert(END, pound)

t3.delete(“1.0”, END)

t3.insert(END, ounce)

```def from_kg():
gram = float(e2_value.get())*1000
pound = float(e2_value.get())*2.20462
ounce = float(e2_value.get())*35.274
t1.delete("1.0",END)
t1.insert(END, gram)
t2.delete("1.0", END)
t2.insert(END, pound)
t3.delete("1.0", END)
t3.insert(END, ounce)
```

In the next section, we start giving the labels of the text fields like the following:

e1 = Label(window, text=”Input the weight in KG”)

e2_value = StringVar()

e2 = Entry(window, textvariable=e2_value)

e3 = Label(window, text=”Gram”)

e4 = Label(window, text=”Pound”)

e5 = Label(window, text=”Ounce”)

```e1 = Label(window, text="Input the weight in KG")
e2_value = StringVar()
e2 = Entry(window, textvariable=e2_value)
e3 = Label(window, text="Gram")
e4 = Label(window, text="Pound")
e5 = Label(window, text="Ounce")
```

In this section, we will create three text fields where the output value will be displayed.

t1 = Text(window, height=5, width=30)

t2 = Text(window, height=5, width=30)

t3 = Text(window, height=5, width=30)

```t1 = Text(window, height=5, width=30)
t2 = Text(window, height=5, width=30)
t3 = Text(window, height=5, width=30)
```

Now, we will create a button, on clicking this button we call the from_kg function and display the values to the above text fields.

b1 = Button(window, text=”Convert”, command=from_kg)

```b1 = Button(window, text="Convert", command=from_kg)
```

In the last section, we will start giving the corresponding grid values and create the rows and columns for a better GUI.

e1.grid(row=0, column=0)

e2.grid(row=0, column=1)

e3.grid(row=1, column=0)

e4.grid(row=1, column=1)

e5.grid(row=1, column=2)

t1.grid(row=2, column=0)

t2.grid(row=2, column=1)

t3.grid(row=2, column=2)

b1.grid(row=0, column=2)

window.mainloop()

```e1.grid(row=0, column=0)
e2.grid(row=0, column=1)
e3.grid(row=1, column=0)
e4.grid(row=1, column=1)
e5.grid(row=1, column=2)
t1.grid(row=2, column=0)
t2.grid(row=2, column=1)
t3.grid(row=2, column=2)
b1.grid(row=0, column=2)
window.mainloop()
```

Yes. We have now completed the whole code.
Here is the complete source code of the project.

## Source Code:

```from tkinter import *
# Creating a GUI Window
window = Tk()
def from_kg():
gram = float(e2_value.get())*1000
pound = float(e2_value.get())*2.20462
ounce = float(e2_value.get())*35.274
t1.delete("1.0",END)
t1.insert(END, gram)
t2.delete("1.0", END)
t2.insert(END, pound)
t3.delete("1.0", END)
t3.insert(END, ounce)

e1 = Label(window, text="Input the weight in KG")
e2_value = StringVar()
e2 = Entry(window, textvariable=e2_value)
e3 = Label(window, text="Gram")
e4 = Label(window, text="Pound")
e5 = Label(window, text="Ounce")

t1 = Text(window, height=5, width=30)
t2 = Text(window, height=5, width=30)
t3 = Text(window, height=5, width=30)

b1 = Button(window, text="Convert", command=from_kg)

e1.grid(row=0, column=0)
e2.grid(row=0, column=1)
e3.grid(row=1, column=0)
e4.grid(row=1, column=1)
e5.grid(row=1, column=2)
t1.grid(row=2, column=0)
t2.grid(row=2, column=1)
t3.grid(row=2, column=2)
b1.grid(row=0, column=2)

window.mainloop()
```

## Output:

As we have completed the project let’s check the output.

run the python script like the above-given command.

After running the source code, a new window will appear like the following.

Enter any value of KG in the text field and you will get the corresponding converted values of Grams, Ounces, and Pounds.

Check the following screenshot for reference.

Alright, Congratulations! We have successfully learned how to create our own weight converter with a simple GUI.

## Important Note:

Note that you can still improve this GUI and make it even better by changing its properties.
Go to the documentation of the Tkinter, learn about it, and start playing with it.

Happy Coding : )