Python Weight Converter – Your Weight Conversion Genie
FREE Online Courses: Your Passport to Excellence - Start Now
A weight converter is a programme that helps the user in converting one weighing unit to another. Here we will convert kg to ounces, pounds, and grams. A weight converter simplifies weight conversion by removing the need for calculation.
About Python Weight Converter
We will utilize the Tkinter Module to create this project. We will develop the GUI for our project using the Tkinter Module. We will create a graphical user interface window with an entry field for entering weight in kilograms and a button to convert it into pounds, grams, and ounces.
Prerequisites for Weight Converter using Python
- Basic knowledge of the Python programming language and how functions are defined in it.
- How the window is made in Tkinter GUI
- Basic formulas to convert weight into different units
Download Python Weight Converter Project
Please download the source code of the Python Weight Converter Project from the following link: Python Weight Converter Project Code
Steps to Create Weight Converter using Python
Following are the steps for developing the Python Weight Converter Project:
Step 1: Importing the necessary modules
Step 2: Making a window for our project
Step 3: Functions
Step 4: Making Labels and Mapping the Button to its Functionality
Step 1: Importing the necessary modules
To use Tkinter, we need to import the Tkinter module.
Code
#import packages from tkinter import *
Step 2: Making a window for our project
This code sets the title of the window as ‘ProjectGurukul’.
Code
converter = Tk() converter.title("ProjectGurukul") converter.resizable()
Step 3: Functions
This function converts the input weight which is in kg to pounds, grams, and ounces.
Code
# Function to convert given weight(kg) to- grams, pounds and ounces def covert_to_kg(): pound = float(value.get()) * 2.20462 gram = float(value.get()) * 1000 ounce = float(value.get()) * 35.274 text1.delete("1.0", END) text1.insert(END, str(gram)) text2.delete("1.0", END) text2.insert(END, str(pound)) text3.delete("1.0", END) text3.insert(END, str(ounce))
Explanation:
We use value.get() to get the input weight, which is in kg, in float type and convert it to pounds, grams, and ounces and update their entries by text1.delete and text1.insert.
Step 4: Making Labels and Mapping the Button to its Functionality
Now we add the required widgets for our GUI. To display the values of kg, gram, pound, and ounce, we create four labels, an entry widget, and a text widget. We create a button widget and assign the covert_to_kg function to it.
Code
l0 = Label(converter, text="Enter Weight in Kg:") l0.grid(row=0, column=0) value = Entry(converter) value.grid(row=0, column=1) # creating the button widget b = Button(converter, text="Convert", command=covert_to_kg) b.grid(row=0, column=2) # creating labels widget l1 = Label(converter, text="Gram:") l1.grid(row=1, column=0) l2 = Label(converter, text="Pound:") l2.grid(row=2, column=0) l3 = Label(converter, text="Ounce:") l3.grid(row=3, column=0) # creating texts widget text1 = Text(converter, width=10, height=1) text1.grid(row=1, column=1) text2 = Text(converter, width=10, height=1) text2.grid(row=2, column=1) text3 = Text(converter, width=10, height=1) text3.grid(row=3, column=1) converter.mainloop()
Full Code
# importing tkinter module from tkinter import * # Function to convert given weight(kg) to- grams, pounds and ounces def covert_to_kg(): pound = float(value.get()) * 2.20462 gram = float(value.get()) * 1000 ounce = float(value.get()) * 35.274 text1.delete("1.0", END) text1.insert(END, str(gram)) text2.delete("1.0", END) text2.insert(END, str(pound)) text3.delete("1.0", END) text3.insert(END, str(ounce)) converter = Tk() converter.title("ProjectGurukul") converter.resizable() l0 = Label(converter, text="Enter Weight in Kg:") l0.grid(row=0, column=0) value = Entry(converter) value.grid(row=0, column=1) # creating the button widget b = Button(converter, text="Convert", command=covert_to_kg) b.grid(row=0, column=2) # creating labels widget l1 = Label(converter, text="Gram:") l1.grid(row=1, column=0) l2 = Label(converter, text="Pound:") l2.grid(row=2, column=0) l3 = Label(converter, text="Ounce:") l3.grid(row=3, column=0) # creating texts widget text1 = Text(converter, width=10, height=1) text1.grid(row=1, column=1) text2 = Text(converter, width=10, height=1) text2.grid(row=2, column=1) text3 = Text(converter, width=10, height=1) text3.grid(row=3, column=1) converter.mainloop()
Python Weight Converter Output
Summary
The Weight Converter Project has been finished successfully. We learned how to create a GUI with the Tkinter Module and observed some fundamental Python principles. This project can be expanded by adding more conversions and items.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook