Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter不更新值_Python_Python 3.x_Tkinter - Fatal编程技术网

Python tkinter不更新值

Python tkinter不更新值,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,在下面的代码中,我使用tkinter显示两个值。第一个值更新得很好,但第二个值似乎正在更新。我把它们组织得一样,所以我觉得应该更新一下。有什么理由不这样做吗 #!/usr/bin/env python3 # imports import requests import time from tkinter import * import urllib.request, json # variables def get_coinbase_price(): url = 'https://ap

在下面的代码中,我使用tkinter显示两个值。第一个值更新得很好,但第二个值似乎正在更新。我把它们组织得一样,所以我觉得应该更新一下。有什么理由不这样做吗

#!/usr/bin/env python3
# imports
import requests
import time
from tkinter import *
import urllib.request, json

# variables
def get_coinbase_price():
    url = 'https://api.coinbase.com/v2/prices/USD/spot?'
    req = requests.get(url)
    data = req.json()
    bit = (data['data'][0]['amount'])
    thelabel.config(text = "1 BTC = %s USD" % bit)
    root.after(1000, get_coinbase_price)


def get_nicehash_stats():
    with urllib.request.urlopen(
            "https://api.nicehash.com/api?method=stats.provider.ex&addr=37sCnRwMW7w8V7Y4zyVZD5uCmc9N1kZ2Q8") as url:
        data = json.loads(url.read().decode())
    total = 0
    for val in data['result']['current']:
        total += float(val['data'][1])
    secondlabel.config(text="Nicehash stats = %s " % total)
    root.after(1000, get_nicehash_stats)



# gui workspace
root = Tk()
thelabel = Label(root, text="")
secondlabel = Label(root, text="")
thelabel.pack()
secondlabel.pack()
root.after(1000, get_coinbase_price)
root.after(1000, get_nicehash_stats)
root.mainloop()

使用外部web连接测试任何内容都很困难。下面的简化代码工作并更新两个标签。您可以使用它重建web请求并显示它们。(用web查询、函数内部或其他函数内部替换全局命名空间中的位和总计)


Nicehash API不喜欢你每秒都轮询它

我得到的答复是:

'您的API请求配额已被违反。你可以在28秒内再试一次。”

此响应的
data
JSON不包含“result”字段,因此会引发异常且标签不会更新。此外,它在此时停止更新。您可能需要检查是否存在结果字段:

if 'result' in data:
    for val in data['result']['current']:
        ...
或者,执行一些异常处理

if 'result' in data:
    for val in data['result']['current']:
        ...