Friday, March 1, 2019

codingdirectional: Create a selection combo box for the forex project

Hello and welcome back to our Forex application project. In this chapter, we will create a combo box which will be filled up with the data returned by the API call from Coinbase. The process to create the combo box is fairly simple, all we need is to create the box and then populate the data into it once the program has received the data from Coinbase. Below is the entire program, the other parts of the program have already been explained in the previous chapter.

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

selectorFrame = Frame(win) # create a button frame
selectorFrame.pack(side = TOP, fill=X, pady = 6)
currency_label = Label(selectorFrame, text = "Select your currency pair :")
currency_label.pack(anchor="w") # the currency pair label
# Create a combo box for currency pair
select_currency = StringVar() # create a string variable
preferPaired = tk.Combobox(selectorFrame, textvariable=select_currency)
preferPaired.pack(side = LEFT)

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)
        supported_currency = client.get_currencies() # get all the available currencies from coinbase
        exchange_rate_s = json.loads(json.dumps(supported_currency))

    except:
        print("An exception occurred")

    exchange_name_dict = dict()

    for key in exchange_rate_s['data']:
        id = key['id']
        name = key['name']
        obj_currency = {id:name}
        exchange_name_dict.update(obj_currency)

    count = 0
    found = False
    curr = tuple() # the tuple which will be populated by currency pairs

    for key in exchange_rate['rates']:
        count += 1
        c_st = exchange_rate['currency'] + "/" + key
        curr = curr + (c_st,)  # add the currency pairs
        for e_key in exchange_name_dict:
            if(e_key == key):
                sell_buy += str(count) + ".) Pair : " + exchange_rate['currency'] + "/" + key + " (" + exchange_name_dict[e_key] + ") : " + \
                exchange_rate['rates'][key] + '\n'
                found = True
                break
        if(found == False):
            sell_buy += str(count) + ".) Pair : " + exchange_rate['currency'] + "/" + key + " : " + \
                exchange_rate['rates'][key] + '\n'
        else:
            found = False
    preferPaired['values'] = curr
    preferPaired.current(0)  # select item one
    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 python program, below is what you will see, after you have pressed the find button the selection box will be populated with the currency pair values.



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...