Sunday, March 3, 2019

codingdirectional: Tidy up the user interface of the Forex application

Hello and welcome back to this Forex application project. After all the hard work in the previous chapter we will go lightly in this chapter by tidying up the user interface of this Forex application. We will include an icon for this application on the application window plus edit the background color and the padding of the tkinter’s widget accordingly. Below is the edit version of this application.

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, background="white") # create a button frame
selectorFrame.pack(anchor = "nw", pady = 2, padx=10)
currency_label = Label(selectorFrame, text = "Select currency pair :", background="white")
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, padx=3)

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 currency exchange rate")
text_widget.insert(END, s.get())

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

def search_currency():
    countVar = StringVar()
    text_widget.tag_remove("search", "1.0", "end") # cleared the hightlighted currency pair
    if(preferPaired.get() != ''):

        pos = text_widget.search(preferPaired.get(), "1.0", stopindex="end", count=countVar)
        text_widget.tag_configure("search", background="green")
        new_pos = float(pos) + float(0.7)
        text_widget.tag_add("search", pos, str(new_pos)) #highlighted the background of the searched currency pair
        pos = float(pos) + 2.0
        text_widget.see(str(pos))

action_search = tk.Button(selectorFrame, text="Search", command=search_currency) # find out the exchange rate of bitcoin
action_search.pack(side=LEFT)

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 currency pair
action_vid.pack()
win.iconbitmap(r'ico.ico')
win.mainloop()

Below is the outcome, in the next chapter we will wrap up this project by introducing the method to look up the exchange rate of different currency pair.

The new user interface

In order to develop this Forex application you will need to create a free account on Coinbase to get the API key and secret, you can sign up a free Coinbase account through this link.

If you would like to support the continue development of this project, do consider to donate to this website through the donation form situated on the sidebar of this website.



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