Archive

Archive for November, 2013

Currency Converter in Python

November 30th, 2013 No comments

EDIT 02-Oct-2015: Code now works with the free API at http://fixer.io/

Hello everyone! Well, many times I find myself converting between EURO, USD and GBP. So I though, why don’t write a small program to do that? Of course there are many other solutions out there like but what’s the fun in that if you don’t do it yourself, right, RIGHT?

As I always do, I search and learn as I go. So first things first, I wanted this to be a program with GUI. There are a lot of solutions for that but I decided to use wxPython. I skimmed over the documentation and it was obvious that I had to spent more time that I wanted in order to do something useful. I searched for a ‘designer’ and I found one, called wxGlade. After a quick ‘sudo apt-get install python-wxglade’ in my beloved Ubuntu I was up and running in no time. Within few minutes I created the minimalistic GUI I wanted.

For the currency conversion I found some good info here and I used part of that code. I also needed to learn about threads since I wanted to fetch the exchange rates without blocking the program and again after a quick google search this was solved as well.

The little program works well and its fast. The code is below. Let me know if you like it or improve it.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.5 on Thu Nov 28 12:44:35 2013
 
import wx
from threading import Thread
import time
import requests
 
# begin wxGlade: extracode
EUR_USD = 1
EUR_GBP = 1
USD_GBP = 1
def myfunc(i):
    global EUR_USD
    global EUR_GBP
    global USD_GBP
    # For the following 3 commands credit goes to http://www.boxcontrol.net/write-simple-currency-converter-in-python.html#.UpkVvapyQQ-
    url = 'http://api.fixer.io/latest?base=EUR&symbols=USD'
    r = requests.get(url)
    EUR_USD = r.json()['rates']['USD']
    time.sleep(2)
    url = 'http://api.fixer.io/latest?base=EUR&symbols=GBP'
    r = requests.get(url)
    EUR_GBP = r.json()['rates']['GBP']
    time.sleep(2)
    url = 'http://api.fixer.io/latest?base=USD&symbols=GBP'
    r = requests.get(url)
    USD_GBP = r.json()['rates']['GBP']
 
t = Thread(target=myfunc,args=(1,))
t.start()
# end wxGlade
 
class MyFrame1(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame1.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.EUR = wx.StaticText(self, wx.ID_ANY, "EUR")
        self.tc_EUR = wx.TextCtrl(self, wx.ID_ANY, "")
        self.USD = wx.StaticText(self, wx.ID_ANY, "USD")
        self.tc_USD = wx.TextCtrl(self, wx.ID_ANY, "")
        self.GBP = wx.StaticText(self, wx.ID_ANY, "GBP")
        self.tc_GBP = wx.TextCtrl(self, wx.ID_ANY, "")
 
        self.__set_properties()
        self.__do_layout()
 
        self.Bind(wx.EVT_TEXT, self.eh_EUR, self.tc_EUR)
        self.Bind(wx.EVT_TEXT, self.eh_USD, self.tc_USD)
        self.Bind(wx.EVT_TEXT, self.eh_GBP, self.tc_GBP)
        # end wxGlade
 
    def __set_properties(self):
        # begin wxGlade: MyFrame1.__set_properties
        self.SetTitle("CurrencyPy")
        # end wxGlade
 
    def __do_layout(self):
        # begin wxGlade: MyFrame1.__do_layout
        grid_sizer_1 = wx.GridSizer(3, 2, 2, 2)
        grid_sizer_1.Add(self.EUR, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        grid_sizer_1.Add(self.tc_EUR, 0, 0, 0)
        grid_sizer_1.Add(self.USD, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        grid_sizer_1.Add(self.tc_USD, 0, 0, 0)
        grid_sizer_1.Add(self.GBP, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        grid_sizer_1.Add(self.tc_GBP, 0, 0, 0)
        self.SetSizer(grid_sizer_1)
        grid_sizer_1.Fit(self)
        self.Layout()
        self.Centre()
        # end wxGlade
 
    def eh_EUR(self, event):  # wxGlade: MyFrame1.
        if self.tc_EUR.GetValue() != '':
            self.tc_USD.ChangeValue(str(float(self.tc_EUR.GetValue())*EUR_USD))
            self.tc_GBP.ChangeValue(str(float(self.tc_EUR.GetValue())*EUR_GBP))
            event.Skip()
    def eh_USD(self, event):  # wxGlade: MyFrame1.
        if self.tc_USD.GetValue() != '':
            self.tc_EUR.ChangeValue(str(float(self.tc_USD.GetValue())/EUR_USD))
            self.tc_GBP.ChangeValue(str(float(self.tc_USD.GetValue())*USD_GBP))
            event.Skip()
    def eh_GBP(self, event):  # wxGlade: MyFrame1.
        if self.tc_GBP.GetValue() != '':
            self.tc_EUR.ChangeValue(str(float(self.tc_GBP.GetValue())/EUR_GBP))
            self.tc_USD.ChangeValue(str(float(self.tc_GBP.GetValue())/USD_GBP))
            event.Skip()
# end of class MyFrame1
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_2 = MyFrame1(None, -1, "")
    app.SetTopWindow(frame_2)
    frame_2.Show()
    app.MainLoop()

Oh and here is a screenshot!

Incoming search terms:

  • python currency converter (169)
  • currency converter python (93)
  • how to make a currency converter in python (76)
  • python currency converter code (47)
  • currency converter python code (36)
  • currency converter in python (33)
  • python code for currency converter (21)
  • simple currency converter python (19)
  • currency converter code in python (15)
  • currency exchange python (1)
Categories: programming Tags: