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.
newurl = url. replace('/f/','/d/').
One of the most appropriate functions that we can use to exit from a Python program is the sys. exit() function. This function is available in the sys module and when called it raises the SystemException in Python that then triggers the interpreter to stop further execution of the current python script.
You want to use the after
function here, which will run code alongside TKinter's main loop. You've touched on something important here; the process can only do one thing at a time, so you need the mainloop in tk to keep track of when it's time to run your code, so it can do the rest of its work in the interim.
def task():
print("hello")
# do your extractions
main_window.after(60000, task) # reschedule event in 60 seconds
main_window.after(60000, task)
main_window.mainloop()
This will, before the main loop starts, schedule a task that will run in 60000 milliseconds (or 60 seconds). task
can be any function that you define.
When task
runs, it too schedules another execution of itself when it finishes. You can then do your work in the task
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With