我正在尝试制作一个货币转换器,它可以自动从 Internet 上的 JSON 文件中检索汇率。我想设计这个程序,如果它不能访问文件,它会让你手动输入费率。到目前为止,除了这部分之外,一切正常。它可以很好地从 JSON 文件中检索数据,但是当您手动输入时,它只是显示为空白。当谈到 Python 和 Tkinter 时,我是一个完全的菜鸟,所以如果我错过了一些简单的东西,我不会感到惊讶。
注意:第一部分,在顶部窗口之前,是实际代码的副本。窗口本身只是显示我遇到的问题的模型。
from Tkinter import *
#**Import Bitcoin market data.**
import json
import urllib2
try:
btcMarketsJSON = urllib2.urlopen('http://api.bitcoincharts.com/v1/markets.json')
marketData = json.load(btcMarketsJSON)
mtGoxUSDdata = [i for i in marketData if i['symbol'] == 'mtgoxUSD']
bitstampUSDdata = [i for i in marketData if i['symbol'] == 'bitstampUSD']
mtGoxUSDask = mtGoxUSDdata[0]['ask']
bitstampUSDask = bitstampUSDdata[0]['ask']
btcMarketsJSON.close()
except:
import tkMessageBox as MessageBox
import tkSimpleDialog as SimpleDialog
root = Tk()
root.withdraw()
if MessageBox.askyesno("Error", "Market data could not be retrieved. Exchange rate will have to be entered manually. Continue anyway?"):
manualAsk = SimpleDialog.askfloat(" ", "Enter an ask price. This number will be used for all exchanges.")
mtGoxUSDask = manualAsk
bitstampUSDask = mtGoxUSDask
else:
import sys
sys.exit()
#**Main Window**
top = Tk()
top.title("Bitcoin/US Dollar Currency Converter")
top.geometry("+300+300")
selectedAsk = DoubleVar()
selectedAsk.set(mtGoxUSDask)
label = Label(top, textvariable=selectedAsk)
quitButton = Button(top, text="Quit", command=top.destroy)
label.pack()
quitButton.pack()
top.mainloop()