Create a Forex application for windows 10 with python

Our Own Score

Hello and welcome to this new Forex application project for windows 10 where in this chapter we are going to create an application which will display the currency exchange rate for any major world currency pair within the tkinter Text widget user interface.

In order to get the Forex data, we will use a free Forex API which you can get it’s API key which you need to develop this application through this link.

Next, you will need to install the free Forex API’s wrapper written in python which we will need in order to develop this Forex application. Do this in the windows command line.

pip install alpha_vantage

In this chapter we are going to go easy first by creating two tkinter combo boxes, one for the base currency and another one for the quote currency. We will also create the text widget used to display the Forex data as well as a find button which will make the API called when we press on it.

import json
from tkinter import *
import tkinter.ttk as tk
from alpha_vantage.foreignexchange import ForeignExchange

win = Tk() # Create tk instance
win.title("Real Forex") # Add a title
win.resizable(0, 0) # Disable resizing the GUI
win.configure(background='white') # change window background color

selectorFrame = Frame(win, background="white") # create top frame to hold base and quote currency's combobox
selectorFrame.pack(anchor = "nw", pady = 2, padx=10)
currency_label = Label(selectorFrame, text = "Select base currency / quote currency :", background="white")
currency_label.pack(anchor="w") # the currency pair label

curr1 = tuple() # the tuple which will be populated by base and quote currency
currency_list = ['AUD', 'BTC', 'CAD', 'CNY', 'EUR', 'GBP', 'JPY', 'USD']

# populate the combo box for both the base and quote currency
for key in currency_list:
    curr1 += (key, )

# Create a combo box for base currency
base_currency = StringVar() # create a string variable
based = tk.Combobox(selectorFrame, textvariable=base_currency)
based['values'] = curr1
based.pack(side = LEFT, padx=3)

# Create a combo box for quote currency
quote_currency = StringVar() # create a string variable
quote = tk.Combobox(selectorFrame, textvariable=quote_currency)
quote['values'] = curr1
quote.pack(side = LEFT, padx=3)

s = StringVar() # create string variable which will be used to fill up the Forex data
# create currency frame and text widget to display the incoming forex data
currencyFrame = Frame(win)
currency = Label(currencyFrame)
text_widget = Text(currency, fg='white', background='black')
s.set("Click the find button to find out the currency exchange rate")
text_widget.insert(END, s.get())

buttonFrame = Frame(win) # create a bottom frame to hold the find button
buttonFrame.pack(side = BOTTOM, fill=X, pady = 6)

def get_exchange_rate(): # this method will display the incoming forex data after the api called
    # first get the api key and secret from the file
    f = open("alpha.txt", "r")
    api_key = f.readline()

    api_key = api_key.replace('\n', '')

        cc = ForeignExchange(key= api_key)
        from_ = based.get()
        to_ = quote.get()

        if(from_ != '' and to_ != '' and from_ != to_):
            data, _ = cc.get_currency_exchange_rate(from_currency=from_, to_currency=to_)
            exchange_rate = dict(json.loads(json.dumps(data)))
            count = 1
            sell_buy = str(count) + ".) Pair : " + exchange_rate['1. From_Currency Code'] + "(" + exchange_rate['2. From_Currency Name'] + ")" + " / " + exchange_rate['3. To_Currency Code']+"(" + exchange_rate['4. To_Currency Name'] + ") : "  + str(exchange_rate['5. Exchange Rate']) + '\n'
            text_widget.delete('1.0', END)  # clear all those previous text first
            text_widget.insert(INSERT, s.get())  # display forex rate in text widget

        print("An exception occurred")

action_vid = tk.Button(buttonFrame, text="Find", command=get_exchange_rate) # button used to find out the exchange rate of currency pair


If you run the above program you will see this outcome.

This is just the beginning of a new Forex application development project, if you like this article do consider to share it with your friend.


Leave a Reply

Notify of