
As mentioned above, in this article we will start to create the user interface of our latest cryptocurrency project. Along the path we will also use the CryptoCompare API to retrieve data. In this article we will do these:
- Create a simple interface with the cryptocurrency and the currency combo boxes for future use as well as a display panel to display the currency exchange rate.
- Make the API call which will retrieve the exchange rate of BTC vs USD, JPY, and the EUR.
- Display those exchange rates in the display panel.
Below is the entire python program.
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 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) 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 base_crypto ="BTC" try: url = "https://min-api.cryptocompare.com/data/price" #url for API call data = {'fsym' : base_crypto, 'tsyms':"USD,JPY,EUR"} r = requests.get(url, params=data) exchange_rate_s = json.loads(json.dumps(r.json())) except: print("An exception occurred") count = 0 found = False curr = tuple() # the tuple which will be populated by cryptocurrency curr += (base_crypto,) 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) + " " curr1 += (key,) # fill up combo boxes for both currency and cryptocurrency based['values'] = curr1 based.current(0) crypto['values'] = curr crypto.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()
In this article, all the currencies are hardcoded into the program, in the next chapter, we will create two files to load all those currencies into those currency combo boxes.
Please follow and like us: