I am trying to make a simple program that gets information off an API and displays it on python GUI with tkinter.
So far I have been able to do this but a new challenge is getting the information collected from the API to refresh every hour.
Basically I need the data()
function to be re-run every hour so that the information on the GUI updates.
from tkinter import *
import requests
def data():
url = requests.get("https://stats.foldingathome.org/api/donor/PointofHorizon")
json = str((url.json()))
i = json.count(',')
data = json.split(",")
score = data[i]
score = score.replace(" 'credit': ","")
score = score.replace("}","")
unit = data[0]
unit = unit.replace("{'wus': ","")
scores = Label(app, text = score)
units = Label(app, text = unit)
scores.pack()
units.pack()
app = Tk()
app.geometry("500x200")
title = Label(app,text = "Folding Score")
title.pack()
I have looked around and haven't been able to find a way that works for me, would be amazing if someone could point me in the right direction. I'm still learning and all.
I think what you're looking for is the after method in tkinter. I changed the data
function to refresh data on the widgets. I moved the code that created the labels to be outside of the refresh_data function. Once the widgets were created, I called the refresh_data
function to put information on the widgets. This function would tell tkinter to wait an hour before running it again which created a loop.
from tkinter import *
import requests
def refresh_data():
url = requests.get("https://stats.foldingathome.org/api/donor/PointofHorizon")
json = str((url.json()))
i = json.count(',')
data = json.split(",")
score = data[i]
score = score.replace(" 'credit': ","")
score = score.replace("}","")
unit = data[0]
unit = unit.replace("{'wus': ","")
scores.config(text=score)
units.config(text=unit)
app.after(3600000, refresh_data) #3600000 milliseconds in an hour
app = Tk()
app.geometry("500x200")
title = Label(app,text = "Folding Score")
title.pack()
scores = Label(app)
units = Label(app)
scores.pack()
units.pack()
refresh_data()
app.mainloop()