Currency Converter with Python
Tags: python, ui, devjournal, forex
Published: 2021-05-03 10:33:42

With COVID surging, enjoying the weekend with outdoor recreation has become a challenge. Last weekend, my better half was converting USD into multiple currencies so i created the below program to assist her.


*Concept:* 
1.	Create a space for user input ( in our case USD is the base currency that needs to be converted into other currencies)
2.	Use API to fetch data and parse it.
3.	Present the data on the UI screen. 

Below is the process that I followed 
•	Using Python 3.9 (Latest version) -download [here](https://www.python.org/downloads/windows/)
•	Use  tkinter library for UI (pip install tkinter )
•	Use request library to pull data via API
•	Create buttons like “Convert” to fetch data and Clear to erase the content

#Final Output 
![When the program starts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwaow9b3zy2uyhpqan8x.GIF)

![When user inputs numeric value and convert button is hit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v05h03x96w5cx4ib2t6b.GIF)


#Code Snippet 
	Laying out the labels on the tkinter grid.
	# Create a Label widget with "USD" as label
        l0 =Label(window,text="USD")
        l0.grid(row=0,column=0) # The Label is placed in 
        position 0, 0 in the window

       l1 = Label(window,text="INR")
       l1.grid(row=1,column=0) # The Label is placed in position 
       1, 0 in the window
       l2 = Label(window,text="GBP")
       l2.grid(row=1,column=1) # The Label is placed in position 
       1, 1 in the window

      ……

      e2 = Entry(window,textvariable=e2_value)  # Create an 
      Entry box for users to enter the value
      e2.grid(row=0,column=1)


      # Create a button widget
      # The from_currency() function is called when the button 
       is pushed
      b1 = Button(window,text="Convert",command=from_currency)
      b1.grid(row=0,column=2)

      # The delete function is called when the button is pushed
       b2 = Button(window,text="Clear",command=delete)
       b2.grid(row=0,column=3)


       # Create four empty text boxes, t1, t2, t3 and t4 for 
       values to show up
      t1 = Text(window,height=1,width=20)
      t1.grid(row=2,column=0)

	Fetch Data via URL 
      # Where USD is the base currency you want to use
      url = 'https://v6.exchangerate-api.com/v6/KEY/latest/USD/'

      # Making our request
      response = requests.get(url)
      data = response.json()

     # Your JSON object
     print(data['conversion_rates']['USD'])

Functions 

    def from_currency():
    # Get user value from input box and multiply by today's 
    conversion rate to 	get INR and round it to 2 decimals
    	rupees = round(float(e2_value.get()) * 
    data['conversion_rates']['INR'],2)
 
    # Get user value from input box and multiply by today's conversion rate to get GBP
    	pound = round(float(e2_value.get())*data['conversion_rates']['GBP'],2)

	…….

      Empty the Text boxes if they had text from the previous 
     use and fill them again
    	t1.delete("1.0", END)  # Deletes the content of the Text 
        box from start to END

        ……
	
	def delete():
       # Deletes content from t1 Text box
      t1.delete("1.0", END)

      # Deletes content from t2 Text box
      t2.delete("1.0", END)

Code in Github.


Update: 
Based on the demand Bitcoin has been added 

![Updated widget with Bitcoin tracker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijv9qp1mo6ngxkw671j5.GIF)

Reach me on my  [Twitter](www.twitter.com/royswapan)

Note: Thanks to John McArthur for this photo via @unsplash 🎁 



Other item to look into: 
{% post https://dev.to/swapanroy/beautiful-design-illustrations-for-free-43f8 %}

{% post https://dev.to/swapanroy/dictionary-using-api-calls-in-python-with-audio-pronunciation-3hjp %}