Python Weight Converter with GUI using Tkinter

FREE Online Courses: Enroll Now, Thank us Later!

There are times when we need to convert a weighing unit to another. This is where a weight converter comes in use. A weight converter saves a lot of effort and time doing calculations. These advantages make a weight converter a handy application to convert weights from one unit to another. Let us build weight converter project using python.

About Weight Converter

A weight converter is an application that helps the user convert one weighing unit to another. Here we are going to convert kg into ounces, pounds and grams. A weight converter makes the job of weight conversion easy by reducing the calculation part.

Python weight converter Project Details

To build this project we are going to use the Tkinter Module. Using the Tkinter Module, we are going to create the GUI of our project. We are going to make a GUI window with an entry field to enter weight in kgs. We are going to have three buttons:

1. Conversion to Grams

2. Conversion to Ounces

3. Conversion to Pounds

Download Python Weight Converter Project Code

Before proceeding with the python weight converter project, please download the source code from the following link: Weight Converter Python Project

Project Prerequisites

To build this project, we need to install the Tkinter Module in our system. Use the following command to install Tkinter Module.

pip install tk

Steps to Create the Project

We will follow the following steps to build the project:

  1. Importing the Required Modules
  2. Creating the GUI
  3. Conversion to Gram, Ounce, and Pound

1. Importing Required Modules:

#importing tkinter module
import tkinter as tk
from tkinter import *

We are importing the Tkinter Module here. Import * means importing everything in the Tkinter Module.

2. Creating the GUI:

window=Tk()#creating a window
window.title("PythonGeeks")#title of the window
window.geometry("500x500")#geometry of the window
#create a label
Label(window,text="WEIGHT CONVERTER",font=("Arial", 20 ),bg="black",fg='yellow').pack()
  • We are going to create an empty GUI Window. After this we will add Labels, Entry Field and Button on this GUI Window.
  • title() – To specify the title of our GUI Window.
  • geometry() – To specify the size of the GUI Window.
  • Label() – This method helps us display a piece of text on the GUI Window.
Label(window,text="Enter the weight in Kgs",font=("Arial", 14 )).pack()
Entry(window,textvariable=kg).pack()#entry field
#creating buttons
Button(window,text="Convert to Gram",bg="blue",command=convert_to_gram).pack()
Button(window,text="Convert to Ounce",bg="blue",command=convert_to_ounce).pack()
Button(window,text="Convert to Pound",bg="blue",command=convert_to_pound).pack()
  • Entry() – This method helps us create an entry field on the GUI Window.
  • Button() – This method creates a button on the GUI Window.
  • pack() – This method helps to display the widgets on the GUI Window.

3. Conversion to Grams, Ounce and Pound:

  • We have created three separate functions for each conversion. The functions are almost similar apart from the calculation parts.
def convert_to_gram():
    kg1=kg.get() #getting the value
    gram = float(kg1)*1000 #convert to float
    Label(window,text="This weight in grams would be",font=("Arial", 12 )).pack()
    Label(window,text=gram,bg="red").pack()
  • get() – This method extracts the value of the kg variable.
  • float() – This converts the value into float type.
  • Using the Label(), we are displaying the converted value.
def convert_to_ounce():
    kg1=kg.get()#getting the value
    ounce = float(kg1)*35.274#convert to float
    Label(window,text="This weight in ounce would be",font=("Arial", 12 )).pack()
    Label(window,text=ounce,bg="red").pack()
  • The same technique is used as in the previous function.
def convert_to_pound():
    kg1=kg.get()#getting the value
    pound = float(kg1)*2.20462#convert to float
    Label(window,text="This weight in pound would be",font=("Arial", 12 )).pack()
    Label(window,text=pound,bg="red").pack()

Python Weight Converter Output

This is the output of the Weight Converter Project.

python weight converter output

Summary

Hurray! We have successfully completed the Weight Converter Project. We learned to build GUI using the Tkinter Module and saw some basic concepts of Python. You can add conversions to more units and add more things to this project.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google | Facebook


Leave a Reply

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