- Load the data from currency.txt to use as the currency string which will form part of the rest url.
- Load the data from crypto.txt to populate the crypto combo box.
- Get the desired cryptocurrency to be used in the cryptocurrency part of the rest url.
In the previous article, we have hardcoded both the cryptocurrency symbol and the currency symbol string into the rest
BTC ETH XRP LTC BCH BNB USDT EOS BSV XLM TRX DASH
# read the crypto.txt file then populate the crypto combo box curr = tuple() # the tuple which will be populated by cryptocurrencies with open('crypto.txt') as fp: for currency in fp.readlines(): curr += (currency[:-1],) crypto['values'] = curr crypto.current(0)
When the user presses the load button, the program will load the currency.txt file then read those currency symbols that will form a string as a part of the rest
EUR JPY GBP USD AUD
# read the currency file c_string = '' with open('currency.txt') as fp: for currency in fp.readlines(): c_string += currency[:-1] + "," c_string = c_string[:-1]
Finally get the selected cryptocurrency symbol from the crypto combo box to create the full rest url.
base_crypto = crypto.get() # get the desired crypto currency try: url = "https://min-api.cryptocompare.com/data/price" #url for API call data = {'fsym' : base_crypto, 'tsyms':c_string} r = requests.get(url, params=data) exchange_rate_s = json.loads(json.dumps(r.json())) except: print("An exception occurred")
When the program gets the returned data it will populate the currency combo box as well as showing the cryptocurrency/currency exchange rate on the display panel.
import json from tkinter import * import tkinter.ttk as tk import requests win = Tk() # Create tk instance win.title("Crypto Calculator") # 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 search button and combobox selectorFrame.pack(anchor = "nw", pady = 2, padx=10) currency_label = Label(selectorFrame, text = "Select crypto / currency pair :", background="white") currency_label.pack(anchor="w") # Create a combo box for crypto currency select_currency = StringVar() # create a string variable crypto = tk.Combobox(selectorFrame, textvariable=select_currency) crypto.pack(side = LEFT, padx=3) s = StringVar() # create string variable # create currency frame and text widget to display the incoming exchange rate data currencyFrame = Frame(win) currencyFrame.pack(side=TOP) currency = Label(currencyFrame) currency.pack() text_widget = Text(currency, fg='white', background='black') text_widget.pack() s.set("Click the find button below to load the crypto currency - currency exchange rate") text_widget.insert(END, s.get()) buttonFrame = Frame(win) # create a bottom frame to hold the load button buttonFrame.pack(side = BOTTOM, fill=X, pady = 6) # read the data from crypto.txt file then populate the crypto combo box curr = tuple() # the tuple which will be populated by cryptocurrencies with open('crypto.txt') as fp: for currency in fp.readlines(): curr += (currency[:-1],) crypto['values'] = curr crypto.current(0) def search_currency(): # search currency pair pass #for future use action_search = tk.Button(selectorFrame, text="Search", command=search_currency) # button used to search the currency pair within the text widget action_search.pack(side=LEFT) # Create currency combo box base_currency = StringVar() # create a string variable based = tk.Combobox(selectorFrame, textvariable=base_currency) based.pack(side = LEFT, padx=3) def get_exchange_rate(): # this method will display the incoming exchange rate data after the api called global exchange_rate global base_crypto # read the currency file c_string = '' with open('currency.txt') as fp: for currency in fp.readlines(): c_string += currency[:-1] + "," c_string = c_string[:-1] base_crypto = crypto.get() # get the desired crypto currency try: url = "https://min-api.cryptocompare.com/data/price" #url for API call data = {'fsym' : base_crypto, 'tsyms':c_string} r = requests.get(url, params=data) exchange_rate_s = json.loads(json.dumps(r.json())) except: print("An exception occurred") curr1 = tuple() # the tuple which will be populated by currency sell_buy = '' for key, value in exchange_rate_s.items(): # populate exchange rate string and the currency tuple sell_buy += base_crypto + ":" + key + " " + str(value) + "\n" curr1 += (key,) # fill up combo boxes for both currency and cryptocurrency based['values'] = curr1 based.current(0) text_widget.delete('1.0', END) # clear all those previous text first s.set(sell_buy) text_widget.insert(INSERT, s.get()) # populate the text widget with new exchange rate data action_vid = tk.Button(buttonFrame, text="Load", command=get_exchange_rate) # button used to load the exchange rate of currency pairs action_vid.pack() win.iconbitmap(r'ico.ico') win.mainloop()

The program above looks little bit messy but don’t worry because we will tidy up that program in near future.