Sunday, February 24, 2019

codingdirectional: Find the currency exchange rate with python

Hello and welcome back, in this chapter we are going to continue developing our cryptocurrency application with the help of coinbase api and the tkinter module. Before we go even further on cryptocurrency we will look at another great api offered by coinbase, the usd/world currency exchange rate api. We will create a new tkinter interface and display all those exchange rates with the help of the Text and Label widget. It seems like at the moment the free API only offers the USD/ world currency pair but there is no problem for us to find the other currency pair with just a little math. In this chapter, we will find the exchange rate for usd/all world currency pairs and display them on the label part of the tkinter user interface.

If you have not yet created a free account in coinbase you can sign up through this link because you will need a coinbase account to use its API.

Below is the entire program which will retrieve and display the exchange rate for the usd/world currency pair.

from coinbase.wallet.client import Client
import json
from tkinter import *
import tkinter.ttk as tk

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

s = StringVar() # change text
currencyFrame = Frame(win) # create currency frame
currencyFrame.pack(side=TOP)
currency = Label(currencyFrame)
currency.pack()
text_widget = Text(currency, fg='white', background='black')
text_widget.pack()

s.set("Click the below button to find out the bitcoin exchange rate")
text_widget.insert(END, s.get())

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


def get_exchange_rate():

    f = open("coin.txt", "r")
    api_key = f.readline()
    api_secret = f.readline()
    f.close()

    sell_buy = ''

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

    try:
        client = Client(api_key, api_secret)
        market_currencies_o = client.get_exchange_rates()
        market_currencies_s = json.dumps(market_currencies_o)
        exchange_rate = json.loads(market_currencies_s)

    except:
        print("An exception occurred")

    for key in exchange_rate['rates']:
        sell_buy += " Pair : " + exchange_rate['currency'] + "/" + key + " : " + exchange_rate['rates'][key] + '\n'

    text_widget.delete('1.0', END) # clear all those previous text first
    s.set(sell_buy)
    text_widget.insert(INSERT, s.get())

action_vid = tk.Button(buttonFrame, text="Find", command=get_exchange_rate) # find out the exchange rate of bitcoin
action_vid.pack(fill=X)
win.mainloop()

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

Like, share the above tweet or follow me on twitter.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...