Weight Conversion using Python

Build a Project to Weight Conversion using Python

Build a Project to Weight Conversion using Python

Hello coder, welcome to the codewithrandom blog. In this blog, we will Build a Project to Weight Conversion using Python. Converting weights from one unit of measurement to another can be a tedious and time-consuming task. Fortunately, Python provides a simple and efficient way to automate weight conversions using programming, and we can take it one step further by using the graphical user interface (GUI) library, tkinter. In this blog post, we will go over the steps you need to take to create a weight conversion program using Python tkinter.

Weight Conversion using Python

To Build a Project to Weight Conversion using Python, we need to perform the following steps:

For this Bouncing Ball Animation in Python project, we need to install Tkinter  Gui. You can install these packages in your terminal.

$ pip install tkinter

step 2: Import tkinter and create a GUI Window

Once you have tkinter installed, you can import it into your Python code and create a window.

from tkinter import *
window = Tk()

step 3: Create a function to convert weight given in kg to grams, pounds and 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)

step 4: Create the Label widgets

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")

step 5: Create the Text Widgets & Create the Button Widget

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)

step 6: use the grid method for placing the widgets at respective positions in table like structure & Start the 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()

Weight Conversion using Python

Complete Source Code For the Weight Conversion using Python(copy the code and run )👇👇👇

from tkinter import *
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 for the Weight Conversion 👇👇

Build a Project to Weight Conversion using Python

 

Conclusion:

Hurray! You have successfully Build a Project to Weight Conversion using Python. In this blog post, we’ve gone over the steps you need to take to create a weight conversion program using Python. By following these steps, you can create a program that automates weight conversions, making the task much easier and more efficient. You can customize the program by adding additional conversion formulas for other units of measurement or by creating a graphical user interface for the program. With some creativity and practice, you can create more complex programs and build a strong foundation in Python programming. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝

#Weight Conversion using Python



Leave a Reply