Learn to build currency, temperature, length, area, and weight converters in Python with this comprehensive guide. Step-by-step instructions and code examples included.
Table of Contents
- Introduction
- Setting Up the Environment
- Full All in One Converter Source Code
- Explanation of All in One Converter Source Code
- Conclusion
- FAQs
1. Introduction
Imagine you need to convert different units frequently—currency for your travel, temperature for your cooking recipes, length for your DIY projects, area for your real estate needs, and weight for your fitness goals. Wouldn't it be convenient to have an all-in-one converter right on your computer? That's where Python comes in. Known for its simplicity and versatility, Python is the perfect tool for creating such converters. In this article, we'll walk you through creating an all-in-one converter in Python, covering currency, temperature, length, area, and weight conversions.
2. Setting Up the Environment
Installing Necessary Libraries
Before we dive in, make sure you have Python installed on your computer. You can download it from the official Python website. Additionally, we'll be using some libraries, so you might want to install them using pip:
pip install tkinter, urllib
Modules Used:
- tkinter
- urllib
- webbrowser
- functools
Setting Up Your Development Environment
Setting up your development environment is crucial for a smooth coding experience. You can use any text editor or IDE, but Visual Studio Code and PyCharm are popular choices due to their features and ease of use. Make sure your Python environment is correctly configured, and you have all the necessary libraries installed.
3. Full All in One Converter Source Code
from tkinter import * import urllib.request import webbrowser from functools import partial from tkinter import Tk, StringVar , ttk ############################### root = Tk() root.title('ALL IN ONE CONVERTER') root.geometry("450x400+100+200") labelfont = ('ariel', 56, 'bold') l=Label(root,text='ALL IN ONE CONVERTER',font = ("Arial", 20, "italic"), justify = CENTER) l.place(x=80,y=20) widget = Button(None, text="QUIT", bg="white", fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=root.destroy).place(x=350,y=350) ################################ def CurrencyConverter(): ids = {"US Dollar" : 'USD', "Euros" : 'EUR', "Indian Rupees" : 'INR', "Qatar Doha" : 'QAR', "Zimbwabe Harare" : 'ZWD', "Arab Emirates Dirham" : 'AED', "Pound Sterling" : 'GBP', "Japanese Yen" : 'JPY', "Yuan Renminbi" : 'CNY'} def convert(amt, frm, to): html =urllib.request.urlopen("http://www.exchangerate-api.com/%s/%s/%f?k=a28d653d2d4fd2727003e437" % (frm , to, amt)) return html.read().decode('utf-8') def callback(): try: amt = float(in_field.get()) except ValueError: out_amt.set('Invalid input') return None if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit': out_amt.set('Input or output unit not chosen') return None else: frm = ids[in_unit.get()] to = ids[out_unit.get()] out_amt.set(convert(amt, frm, to)) root = Toplevel() root.title("Currency Converter") # initiate frame mainframe = ttk.Frame(root, padding="3 3 12 12") mainframe.pack(fill=BOTH, expand=1) titleLabel = Label (mainframe, text = "Currency Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) in_amt = StringVar() in_amt.set('0') out_amt = StringVar() in_unit = StringVar() out_unit = StringVar() in_unit.set('Select Unit') out_unit.set('Select Unit') # Add input field in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt) in_field.grid(row=1, column=2, sticky=(W, E)) # Add drop-down for input unit in_select = OptionMenu(mainframe, in_unit, "US Dollar", "Euros", "Indian Rupees", "Qatar Doha", "Zimbwabe Harare", "Arab Emirates Dirham", "Pound Sterling", "Japanese Yen", "Yuan Renminbi").grid(column=3, row=1, sticky=W) # Add output field and drop-down ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E)) in_select = OptionMenu(mainframe, out_unit, "US Dollar", "Euros", "Indian Rupees", "Qatar Doha", "Zimbwabe Harare", "Arab Emirates Dirham", "Pound Sterling", "Japanese Yen", "Yuan Renminbi").grid(column=3, row=3, sticky=W) calc_button = ttk.Button(mainframe, text="Calculate",command=callback).grid(column=2, row=2, sticky=E) for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) in_field.focus() ################################## def WeightConverter(): # factors to multiply to a value to convert from the following units to meters(m) factors = {'kg' : 1000, 'hg' : 100, 'dg' : 10, 'g' : 1,'deg' : 0.1, 'cg' : 0.01, 'mg' : 0.001} ids = {"Kilogram" : 'kg', "Hectagram" : 'hg', "Decagram" : 'dg', "Decigram" : 'deg', "Kilogram" : 'kg', "gram" : 'g', "centigram" : 'cg', "milligram" : 'mg'} # function to convert from a given unit to another def convert(amt, frm, to): if frm != 'g': amt = amt * factors[frm] return amt / factors[to] else: return amt / factors[to] def callback(): try: amt = float(in_field.get()) except ValueError: out_amt.set('Invalid input') return None if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit': out_amt.set('Input or output unit not chosen') return None else: frm = ids[in_unit.get()] to = ids[out_unit.get()] out_amt.set(convert(amt, frm, to)) # initiate window root = Toplevel() root.title("Weight Converter") # initiate frame mainframe = ttk.Frame(root, padding="3 3 12 12") mainframe.pack(fill=BOTH, expand=1) titleLabel = Label (mainframe, text = "Weight Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) in_amt = StringVar() in_amt.set('0') out_amt = StringVar() in_unit = StringVar() out_unit = StringVar() in_unit.set('Select Unit') out_unit.set('Select Unit') # Add input field in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt) in_field.grid(row=1, column=2, sticky=(W, E)) # Add drop-down for input unit in_select = OptionMenu(mainframe, in_unit, "Kilogram","Hectagram","Decagram", "gram", "Decigram","Centigram", "Milligram") .grid(column=3, row=1, sticky=W) # Add output field and drop-down ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E)) in_select = OptionMenu(mainframe, out_unit, "Kilogram","Hectagram","Decagram", "gram", "Decigram","Centigram", "Milligram").grid(column=3, row=3, sticky=W) calc_button = ttk.Button(mainframe, text="Calculate", command=callback).grid(column=2, row=2, sticky=E) for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) in_field.focus() ####################################### def AreaConverter(): wind = Toplevel() wind.minsize(width=400, height=150) wind.maxsize(width=400, height=150) meterFactor = {'square meter':1,'square km':1000000,'square rood':1011.7141056,'square cm':0.0001,'square foot':0.09290304 , 'square inch':0.00064516, 'square mile':2589988.110336, 'milimeter':0.000001,'square rod':25.29285264, 'square yard':0.83612736, 'square township':93239571.9721, 'square acre':4046.8564224 ,'square are': 100, 'square barn':1e-28, 'square hectare':10000, 'square homestead':647497.027584 } def convert(x, fromUnit, toUnit): if fromVar.get() in meterFactor.keys() and toVar.get() in meterFactor.keys(): resultxt.delete(0, END) result = (float(str(x))*meterFactor[fromUnit])/(meterFactor[toUnit]) resultxt.insert(0, str(result)) titleLabel = Label (wind, text = "Area Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) e = Entry(wind) e.grid(row = 1, column = 2) values = list(meterFactor.keys()) fromVar = StringVar(wind) toVar = StringVar(wind) fromVar.set("From Unit") toVar.set("To Unit") fromOption = OptionMenu(wind, fromVar, *values, command= lambda y: convert(e.get(), fromVar.get() ,toVar.get())) fromOption.grid(row=1, column = 3) toLabel = Label(wind, text="To : ", font="Arial").grid(row=2, column = 2) toOption = OptionMenu(wind, toVar, *values, command= lambda x: convert(e.get(), fromVar.get() ,toVar.get())) toOption.grid(row=3, column = 3) resultxt = Entry(wind) resultxt.grid(row=3, column=2) ########################################### def LengthConverter(): # factors to multiply to a value to convert from the following units to meters(m) factors = {'nmi' : 1852, 'mi' : 1609.34, 'yd' : 0.9144, 'ft' : 0.3048, 'inch' : 0.0254, 'km' : 1000, 'm' : 1, 'cm' : 0.01, 'mm' : 0.001} ids = {"Nautical Miles" : 'nmi', "Miles" : 'mi', "Yards" : 'yd', "Feet" : 'ft', "Inches" : 'inch', "Kilometers" : 'km', "meters" : 'm', "centimeters" : 'cm', "millileters" : 'mm'} # function to convert from a given unit to another def convert(amt, frm, to): if frm != 'm': amt = amt * factors[frm] return amt / factors[to] else: return amt / factors[to] def callback(): try: amt = float(in_field.get()) except ValueError: out_amt.set('Invalid input') return None if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit': out_amt.set('Input or output unit not chosen') return None else: frm = ids[in_unit.get()] to = ids[out_unit.get()] out_amt.set(convert(amt, frm, to)) # initiate window root = Toplevel() root.title("Length Converter") # initiate frame mainframe = ttk.Frame(root, padding="3 3 12 12") mainframe.pack(fill=BOTH, expand=1) titleLabel = Label (mainframe, text = "Length Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) in_amt = StringVar() in_amt.set('0') out_amt = StringVar() in_unit = StringVar() out_unit = StringVar() in_unit.set('Select Unit') out_unit.set('Select Unit') # Add input field in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt) in_field.grid(row=1, column=2, sticky=(W, E)) # Add drop-down for input unit in_select = OptionMenu(mainframe, in_unit, "Nautical Miles", "Miles", "Yards", "Feet", "Inches", "Kilometers", "meters", "centimeters", "millileters").grid(column=3, row=1, sticky=W) # Add output field and drop-down ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E)) in_select = OptionMenu(mainframe, out_unit, "Nautical Miles", "Miles", "Yards", "Feet", "Inches", "Kilometers", "meters", "centimeters", "millileters").grid(column=3, row=3, sticky=W) calc_button = ttk.Button(mainframe, text="Calculate", command=callback).grid(column=2, row=2, sticky=E) for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) in_field.focus() ###################################### def TemperatureConverter(): def convert(): celTemp = celTempVar.get() fahTemp = fahTempVar.get() if celTempVar.get() != 0.0: celToFah = (celTemp * 9/5 + 32) fahTempVar.set(celToFah) elif fahTempVar.get() != 0.0: fahToCel = ((fahTemp - 32) * (5/9)) celTempVar.set(fahToCel) def reset(): top = Toplevel(padx=50, pady=50) top.grid() message = Label(top, text = "Reset Complete") button = Button(top, text="OK", command=top.destroy) message.grid(row = 0, padx = 5, pady = 5) button.grid(row = 1, ipadx = 10, ipady = 10, padx = 5, pady = 5) fahTempVar.set(int(0)) celTempVar.set(int(0)) top = Toplevel() top.title("Temperature Converter") ###MAIN### celTempVar = IntVar() celTempVar.set(int(0)) fahTempVar = IntVar() fahTempVar.set(int(0)) titleLabel = Label (top, text = "Temperature Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) celLabel = Label (top, text = "Celcius: ", font = ("Arial", 16), fg = "red") celLabel.grid(row = 2, column = 1, pady = 10, sticky = NW) fahLabel = Label (top, text = "Fahrenheit: ", font = ("Arial", 16), fg = "blue") fahLabel.grid(row = 3, column = 1, pady = 10, sticky = NW) celEntry = Entry (top, width = 10, bd = 5, textvariable = celTempVar) celEntry.grid(row = 2, column = 1, pady = 10, sticky = NW, padx = 125 ) fahEntry = Entry (top, width = 10, bd = 5, textvariable = fahTempVar) fahEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 ) convertButton =Button (top, text = "Convert", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = convert) convertButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55) resetButton = Button (top, text = "Reset", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = reset) resetButton.grid(row = 4, column = 2,ipady = 8, ipadx = 12, pady = 5, sticky = NW) ##################################### def sensex(event): webbrowser.open_new(r"https://www.google.com/search?q=sensex%20today%20live%20chart") def nifty(event): webbrowser.open_new(r"https://www.google.com/search?q=nifty%20today%20live%20chart") def gold(event): webbrowser.open_new(r"https://www.google.com/search?q=gold%20today%20live%20chart") def silver(event): webbrowser.open_new(r"https://www.moneycontrol.com/commodity/silver-price.html") ################################### #Hovering def color_config(widget, color, event): widget.configure(foreground=color) text =Label(root, text="SENSEX",font = ("Arial", 14, "bold")) text.bind("<Enter>", partial(color_config, text, "red")) text.bind("<Leave>", partial(color_config, text, "blue")) text.pack() text.bind("<Button-1>",sensex) text.place(x=350,y=120) text =Label(root, text="NIFTY",font = ("Arial", 14, "bold")) text.bind("<Enter>", partial(color_config, text, "red")) text.bind("<Leave>", partial(color_config, text, "blue")) text.pack() text.bind("<Button-1>",nifty) text.place(x=350,y=150) text =Label(root, text="GOLD",font = ("Arial", 14, "bold")) text.bind("<Enter>", partial(color_config, text, "red")) text.bind("<Leave>", partial(color_config, text, "blue")) text.pack() text.bind("<Button-1>",gold) text.place(x=350,y=180) text =Label(root, text="SILVER",font = ("Arial", 14, "bold")) text.bind("<Enter>", partial(color_config, text, "red")) text.bind("<Leave>", partial(color_config, text, "blue")) text.pack() text.bind("<Button-1>",silver) text.place(x=350,y=210) ########################################### #TEMPERATURE CONVERTER widget = Button(root, text="Temperature converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=TemperatureConverter).place(x=50,y=120) widget = Button(root, text="Length Converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=LengthConverter).place(x=50,y=180) widget = Button(root, text="Area Converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=AreaConverter).place(x=50,y=240) widget = Button(root, text="Currency converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=CurrencyConverter).place(x=50,y=60) widget = Button(root, text="Weight Converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=WeightConverter).place(x=50,y=300) root.mainloop()
4. Explanation of All in One Converter Source Code
Here’s a breakdown of the code:
1. Importing Libraries
- tkinter: The standard GUI library for Python.
- urllib.request: Used for opening and reading URLs.
- webbrowser: Provides a high-level interface to allow displaying Web-based documents.
- functools.partial: Used to create partial functions which can fix a certain number of arguments of a function and generate a new function.
2. Main Application Window
Creating the main window:
root = Tk() root.title('ALL IN ONE CONVERTER') root.geometry("450x400+100+200")
- This sets the window title and its geometry.
Title Label:
l=Label(root, text='ALL IN ONE CONVERTER', font=("Arial", 20, "italic"), justify=CENTER) l.place(x=80, y=20)
QUIT Button: A button to quit the application.
widget = Button(None, text="QUIT", bg="white", fg="red", font=("Arial", 14, "bold"), relief=RAISED, bd=5, justify=CENTER, highlightbackground="red", overrelief=GROOVE, activebackground="green", activeforeground="blue", command=root.destroy).place(x=350, y=350)
3. Currency Converter Function
This function creates a new window for converting currencies.
Currency IDs: A dictionary mapping currency names to their respective codes.
Conversion Function:
def convert(amt, frm, to): html = urllib.request.urlopen("http://www.exchangerate-api.com/%s/%s/%f?k=a28d653d2d4fd2727003e437" % (frm, to, amt)) return html.read().decode('utf-8')
- This fetches the conversion rate from a URL and converts the amount.
GUI Elements: Creates input fields, dropdowns, and a button to perform the conversion.
4. Weight Converter Function
Similar to the Currency Converter but for weight units.
Weight Factors: A dictionary of conversion factors for different weight units.
Conversion Function: Converts between units using the factors.
def convert(amt, frm, to): if frm != 'g': amt = amt * factors[frm] return amt / factors[to] else: return amt / factors[to]
5. Area Converter Function
A separate window for converting area measurements.
Area Factors: A dictionary mapping area units to their factors relative to square meters.
Conversion Function: Performs conversion using these factors.
def convert(x, fromUnit, toUnit): if fromVar.get() in meterFactor.keys() and toVar.get() in meterFactor.keys(): resultxt.delete(0, END) result = (float(str(x))*meterFactor[fromUnit])/(meterFactor[toUnit]) resultxt.insert(0, str(result))
GUI Elements: Includes input fields, dropdowns, and labels.
6. Length Converter Function
For converting length measurements.
Length Factors: Similar to the other converters but for length units.
Conversion Function: Uses the factors for conversion.
def convert(amt, frm, to): if frm != 'm': amt = amt * factors[frm] return amt / factors[to] else: return amt / factors[to]
GUI Elements: Includes input fields, dropdowns, and a button.
7. Temperature Converter Function
For converting temperatures between Celsius and Fahrenheit.
Conversion Function:
def convert(): celTemp = celTempVar.get() fahTemp = fahTempVar.get() if celTempVar.get() != 0.0: celToFah = (celTemp * 9/5 + 32) fahTempVar.set(celToFah) elif fahTempVar.get() != 0.0: fahToCel = ((fahTemp - 32) * (5/9)) celTempVar.set(fahToCel)
- This function checks which temperature input is provided and converts it to the other scale.
Reset Function: Resets the input fields.
8. Web Links
There are functions to open web pages for Sensex, Nifty, Gold, and Silver prices using the webbrowser module.
9. Event Bindings
Functions to change the text color on mouse hover and handle clicks to open links.
10. Buttons to Open Converter Windows
Buttons on the main window to open the respective converter windows:
widget = Button(root, text="Temperature converter", bg="white", fg="red", font=("Arial", 14, "bold"), relief=RAISED, bd=5, justify=CENTER, highlightbackground="red", overrelief=GROOVE, activebackground="green", activeforeground="blue", command=TemperatureConverter).place(x=50, y=120)
- This pattern is followed for other converter types like Length, Area, Currency, and Weight.
11. Main Loop
Finally, the main loop to run the application:
root.mainloop()
5. Conclusion
Creating an all-in-one converter in Python is a rewarding project that combines various programming skills. From fetching live data and handling user inputs to ensuring smooth integration of multiple functions, this project covers a wide range of topics. Not only does it make your daily conversions easier, but it also enhances your understanding of Python programming.
Created by: Avdhesh Varshney
6. FAQs
Q1. How do I update currency rates?
You can update the currency rates by fetching the latest rates from an API each time you run the converter.
Q2. Can I add more units to the converters?
Absolutely! Just update the conversion factors in the respective functions to include more units.
Q3. What if I encounter an error?
Check your input values and ensure they are correct. Implement error handling to catch and handle common issues.
Q4. Is it possible to make this a web application?
Yes, you can use web frameworks like Flask or Django to turn this converter into a web application.
Q5. How do I learn more about Python?
There are many resources available online, including tutorials, courses, and documentation. Websites like Codecademy, Coursera, and the official Python documentation are great places to start.
That’s a wrap!
I hope you enjoyed this article
Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.
And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!
Thanks!
Faraz 😊