Python Bitcoin Price Converter


SUBMITTED BY: Guest

DATE: Nov. 18, 2014, 12:53 a.m.

FORMAT: Python

SIZE: 2.2 kB

HITS: 1418

  1. import requests
  2. import json
  3. from tkinter import *
  4. root = Tk()
  5. usd = StringVar()
  6. btc = StringVar()
  7. currency = StringVar()
  8. currency.set("USD")
  9. def tobtc(x):
  10. # Parameters to pass
  11. x = float(x)
  12. payload = {'currency': 'USD', 'value': x}
  13. url = requests.get("https://blockchain.info/tobtc", params=payload)
  14. price = url.json()
  15. btc.set(price)
  16. return
  17. def tousd(x, y):
  18. x = float(x)
  19. url = requests.get("http://blockchain.info/ticker")
  20. price = url.json()[y]['buy']
  21. price = price * x
  22. price = ("%.2f" % round(price,2))
  23. usd.set(price)
  24. label1.config(text=y)
  25. labeltext = ['To ',y]
  26. labeltext = ''.join(str(v) for v in labeltext)
  27. to_usd.config(text=labeltext)
  28. return
  29. root.wm_title("Bitcoin Price")
  30. # This allows two buttons to be side by side later
  31. top = Frame(root)
  32. top.pack(side=TOP, fill=BOTH, expand=True)
  33. middle = Frame(root)
  34. middle.pack(side=TOP, fill=BOTH, expand=True)
  35. bottom = Frame(root)
  36. bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
  37. filler1 = Label(root, text="")
  38. filler1.pack(in_=top)
  39. label1 = Label(root, text="USD:")
  40. label1.place(in_=top, relx=0.25, rely=0.5, anchor=CENTER)
  41. label2 = Label(root, text="BTC:")
  42. label2.place(in_=top, relx=0.75, rely=0.5, anchor=CENTER)
  43. usd_entry = Entry(root, textvariable=usd)
  44. usd_entry.pack(in_=middle, side=LEFT)
  45. btc_entry = Entry(root, textvariable=btc)
  46. btc_entry.pack(in_=middle, side=RIGHT)
  47. filler2 = Label(root, text="", height=2)
  48. filler2.pack(in_=bottom)
  49. to_btc = Button(root, text="To BTC", command= lambda: tobtc(usd.get()))
  50. to_btc.place(in_=bottom, relx=0.25, rely=0.5, anchor=CENTER)
  51. to_usd = Button(root, text="To USD", command= lambda: tousd(btc.get(), \
  52. currency.get()))
  53. to_usd.place(in_=bottom, relx=0.75, rely=0.5, anchor=CENTER)
  54. currency_select = OptionMenu(root, currency, "USD", "EUR", "CAD")
  55. currency_select.place(in_=bottom, relx=0.5, rely=0.5, anchor=CENTER)
  56. mainloop()

comments powered by Disqus