I'm an old guy trying to learn programming. I've tried searching for an answer to this question but most of the replies are way over my head. So here goes, I've written code to get data from the web, convert it to json, and the print the results in the desired format. I'm now using Tkinter to make a display of this data. I've been successful in displaying the data and updating the labels but I am having trouble getting the URL to update (which feeds the input for the labels). So how can I update or run the request.get on a scheduled interval (once every 3 hours) without using a loop that would hold up the rest of the program?
This is what I have done so far (to run this program you will need to input your api from openweather.com)....
import requests
import time
from tkinter import *
# Input Values
api = 'Enter API Key from Openweather.com'
lat = '33.608'
lon = '-111.863'
unit = 'imperial' # unit must be 'imperial' or 'metric'
fcast_time = [0, 4, 8, 12, 16, 20, 24, 28] # each element is a 3 hour period (i.e. 4 would be 12 hours out), max is 40
main_window = Tk()
url2 = 'http://api.openweathermap.org/data/2.5/forecast?'+'lat='+str(lat)+'&lon='+str(lon)+'&APPID='+str(api)+'&units='+str(unit)
def fconvertTime(tperiod):
period = fcast_data['list'][fcast_time[tperiod]]['dt']
ftime = time.strftime("%a %p", time.localtime(period))
return(ftime)
r_forecast = requests.get(url2)
fcast_data = (r_forecast.json())
def forecast_layout(frame_name, tperiod):
label_fcast_day = Label(frame_name, text=fconvertTime(tperiod), justify=CENTER, font=("Ariel", 8), bg="black",
fg="white", width=13)
label_test_update = Label(frame_name, text=time.strftime('%H:%M:%S'), justify=CENTER, font=("Ariel", 8), bg="black",
fg="white", width=13)
label_fcast_day.grid(row=0, column=0)
label_test_update.grid(row=3, column=0)
# Configure Main Window
main_window.title("Weather")
main_window.geometry("705x500")
main_window.resizable(True, True)
main_window.configure(bg="black")
# Define sub-frames
forecast_frame = Frame(main_window, bg="blue")
forecast_frame.grid(row=0, column=0, columnspan=3)
# Build forecast_frame
frame_fcast1 = Frame(forecast_frame)
forecast_layout(frame_fcast1, 0)
frame_fcast1.grid(row=0, column=0, pady=2, padx=2, sticky=W)
frame_fcast2 = Frame(forecast_frame)
forecast_layout(frame_fcast2, 1)
frame_fcast2.grid(row=0, column=1, pady=2, padx=2, sticky=W)
main_window.mainloop()
Thanks in advance for any assistance! This is all very new to me (a few weeks) so a detailed explanation would be appreciated.