Advertisement
aricleather

Bitcoin Price Calculator - Python

Nov 17th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import requests
  2. import json
  3. from tkinter import *
  4.  
  5. root = Tk()
  6. usd = StringVar()
  7. btc = StringVar()
  8. currency = StringVar()
  9. currency.set("USD")
  10.  
  11. def tobtc(x):
  12.     # Parameters to pass
  13.     x = float(x)
  14.     payload = {'currency': 'USD', 'value': x}
  15.     url = requests.get("https://blockchain.info/tobtc", params=payload)
  16.     price = url.json()
  17.     btc.set(price)
  18.     return
  19.  
  20. def tousd(x, y):
  21.     x = float(x)
  22.     url = requests.get("http://blockchain.info/ticker")
  23.     price = url.json()[y]['buy']
  24.     price = price * x
  25.     price = ("%.2f" % round(price,2))
  26.     usd.set(price)
  27.     label1.config(text=y)
  28.     labeltext = ['To ',y]
  29.     labeltext = ''.join(str(v) for v in labeltext)
  30.     to_usd.config(text=labeltext)
  31.     return
  32.  
  33. root.wm_title("Bitcoin Price")
  34.  
  35. # This allows two buttons to be side by side later
  36.  
  37. top = Frame(root)
  38. top.pack(side=TOP, fill=BOTH, expand=True)
  39.  
  40. middle = Frame(root)
  41. middle.pack(side=TOP, fill=BOTH, expand=True)
  42.  
  43. bottom = Frame(root)
  44. bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
  45.  
  46. filler1 = Label(root, text="")
  47. filler1.pack(in_=top)
  48.  
  49. label1 = Label(root, text="USD:")
  50. label1.place(in_=top, relx=0.25, rely=0.5, anchor=CENTER)
  51.  
  52. label2 = Label(root, text="BTC:")
  53. label2.place(in_=top, relx=0.75, rely=0.5, anchor=CENTER)
  54.  
  55. usd_entry = Entry(root, textvariable=usd)
  56. usd_entry.pack(in_=middle, side=LEFT)
  57.  
  58. btc_entry = Entry(root, textvariable=btc)
  59. btc_entry.pack(in_=middle, side=RIGHT)
  60.  
  61. filler2 = Label(root, text="", height=2)
  62. filler2.pack(in_=bottom)
  63.  
  64. to_btc = Button(root, text="To BTC", command= lambda: tobtc(usd.get()))
  65. to_btc.place(in_=bottom, relx=0.25, rely=0.5, anchor=CENTER)
  66.  
  67. to_usd = Button(root, text="To USD", command= lambda: tousd(btc.get(), \
  68.                                                             currency.get()))
  69. to_usd.place(in_=bottom, relx=0.75, rely=0.5, anchor=CENTER)
  70.  
  71. currency_select = OptionMenu(root, currency, "USD", "EUR", "CAD")
  72. currency_select.place(in_=bottom, relx=0.5, rely=0.5, anchor=CENTER)
  73.  
  74. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement