import requests
import json
from tkinter import *
root = Tk()
usd = StringVar()
btc = StringVar()
currency = StringVar()
currency.set("USD")
def tobtc(x):
# Parameters to pass
x = float(x)
payload = {'currency': 'USD', 'value': x}
url = requests.get("https://blockchain.info/tobtc", params=payload)
price = url.json()
btc.set(price)
return
def tousd(x, y):
x = float(x)
url = requests.get("http://blockchain.info/ticker")
price = url.json()[y]['buy']
price = price * x
price = ("%.2f" % round(price,2))
usd.set(price)
label1.config(text=y)
labeltext = ['To ',y]
labeltext = ''.join(str(v) for v in labeltext)
to_usd.config(text=labeltext)
return
root.wm_title("Bitcoin Price")
# This allows two buttons to be side by side later
top = Frame(root)
top.pack(side=TOP, fill=BOTH, expand=True)
middle = Frame(root)
middle.pack(side=TOP, fill=BOTH, expand=True)
bottom = Frame(root)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
filler1 = Label(root, text="")
filler1.pack(in_=top)
label1 = Label(root, text="USD:")
label1.place(in_=top, relx=0.25, rely=0.5, anchor=CENTER)
label2 = Label(root, text="BTC:")
label2.place(in_=top, relx=0.75, rely=0.5, anchor=CENTER)
usd_entry = Entry(root, textvariable=usd)
usd_entry.pack(in_=middle, side=LEFT)
btc_entry = Entry(root, textvariable=btc)
btc_entry.pack(in_=middle, side=RIGHT)
filler2 = Label(root, text="", height=2)
filler2.pack(in_=bottom)
to_btc = Button(root, text="To BTC", command= lambda: tobtc(usd.get()))
to_btc.place(in_=bottom, relx=0.25, rely=0.5, anchor=CENTER)
to_usd = Button(root, text="To USD", command= lambda: tousd(btc.get(), \
currency.get()))
to_usd.place(in_=bottom, relx=0.75, rely=0.5, anchor=CENTER)
currency_select = OptionMenu(root, currency, "USD", "EUR", "CAD")
currency_select.place(in_=bottom, relx=0.5, rely=0.5, anchor=CENTER)
mainloop()