As title mentions, I'm trying to update the values in my labels in a tkinter gui. The values are taken from OpenWeatherMap API using pyown and at my subscription level, I can only make 60 calls/minute. Since I plan to make many calls, I would like to have my gui update every minute or 5-minutes. I've spent the last few days reading the similar questions and I have figured out that I need the sleep function to delay the update. Some have suggested that I put what I want to repeat in a while True infinite loop, but wheN I tried that, the gui only updated when I closed out the window, and I was unable to control the time in between updates. Others have suggested that I use the .after function, but when I do this, my program compiles but the gui never pops up. I'm looking for someone to show me how either of these solutions work in my code specifically, or if there is a third solution that lends itself better to my code that would be better, please let me see how it would look, because I am stumped.
import tkinter as tk
import pyowm
from datetime import datetime, timedelta
class WeatherInfo(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.wm_title('Forecast')
self.currentTime = tk.StringVar(self, value='')
self.d2temp_7 = tk.StringVar(self,value='')
self.owm = pyowm.OWM('*INSERT YOUR OWM KEY HERE*')
self.headLabel = tk.Label(self, text='5-Day Forecast of Cayce, US.')
self.headLabel.pack()
self.footLabel = tk.Label(self, textvariable=self.currentTime)
self.footLabel.pack(side=tk.BOTTOM)
self.day2Frame = tk.LabelFrame(self, text='D2')
self.day2Frame.pack(fill='both', expand='yes', side=tk.LEFT)
tk.Label(self.day2Frame, text="Temperature:").pack()
tk.Label(self.day2Frame, textvariable=self.d2temp_7).pack()
self.search()
def search(self):
fc = self.owm.three_hours_forecast_at_id(4573888)
try:
self.currentTime.set(datetime.today())
self.d2temp_7.set("7am: " + str(fc.get_weather_at((datetime.today().replace(hour=13, minute=00) + timedelta(days=1))
.strftime ('%Y-%m-%d %H:%M:%S+00')).get_temperature('fahrenheit')['temp']))
except:
self.temp.set('Pick a city to display weather.')
def _quit(self):
self.quit()
self.destroy()
if __name__== "__main__":
app = WeatherInfo()
app.mainloop()
More on what I have tried:
while True:
def __init__
def search
But as this answer points out, other answer , I won't see any changes I make in my while True preceding the root.mainloop()
This question came close to my answer using root.after(milliseconds,results), but when I implimented this answer my gui never showed. infinitely update
Thank you to anyone who tries to answer this.
Edit: I have made my code shorter as per recommendation.