user7986650
user7986650

Reputation:

tkinter not updating value

in my code below im using tkinter to display two values. the first value updates just fine but the second one dosent seem to update. ive structured them the same so i figured it should update. is there any reason that it wont?

#!/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()

Upvotes: 2

Views: 500

Answers (2)

Junuxx
Junuxx

Reputation: 14251

The Nicehash API doesn't appreciate it that you're polling it every second.

I get this response:

'Your API request quota has been breached. You can try again in 28 seconds.'

The data JSON for this response does not contain a "result" field, so an exception is thrown and the label is not updated. Moreover, it stops updating at this point. You might want to check that there is a result field:

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

Or alternatively, do some exception handling.

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36652

It is difficult to test anything with outside web connections. The following simplified code works and updates both labels. You can use it to rebuild your web requests and display them. (replace bits and total in the global namespace, with your web queries, inside the functions, or inside other functions)

import time
from tkinter import *

bits = 12

def get_coinbase_price():
    global bits
    bits += 1
    thelabel.config(text = "1 BTC = %s USD" % bits)
    root.after(1000, get_coinbase_price)

total = 42

def get_nicehash_stats():
    global total
    total += 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.grid(column=0, row=0)
secondlabel.grid(column=1, row=0)
root.after(1000, get_coinbase_price)
root.after(900, get_nicehash_stats)
root.mainloop()

Upvotes: 0

Related Questions