Situation: I have a tkinter GUI that displays weather data collected from an API. I have a 'get_weather' function which requests data from an API. The response is piped into a 'formatting' function then back to the 'get_weather' function. A formatted string is then displayed as the text of a tkinter Label. The program is intended to run at start up (cronjob) and keep running continuously. It all works great except...
Problem: I can't get the 'get_weather' function to auto refresh. I want the 'get_weather' function to run every 30 minutes to update the data from the API to the GUI.
I have tried: an infinite While loop; which prevents the Tk loop from starting. putting a time.sleep(60) in the code; stops the GUI from launching for a minute. and the after method (see bellow for usage). it didn't seem to do anything unless I made the time parameter a huge number. Then it delayed the GUI launch.
On a side note: I did not make my own class. That's the plan for the next iteration. I'm just not confident making classes. I wanted to get the other problems out of the way before I did that. Also I have all of the functions defined above the start of the Tk loop. They are called within the loop. That just made sense to me but if anyone has a reason why they should be in the loop I want to hear it. I have learned threading but I don't think that's the solution here.
Code looks like: (the program is 300 lines and has other functions. I'm just gonna summarize the problem code)
from Tkinter import Tk*
import [a few other modules]
def change_location(city):
# passes a 'city' value from an Entry widget
# sends request to a geo location api for gps coordinates.
# saves location coordinates to a local config file.
getweather()
def formatting(json):
# takes json from api and formats it to a string
return string
def get_weather():
# opens a local config file with gps coordinates.
# Note: the weather api I'm using requires gps coordinates
# uses gps coordinates in weather api request
weather['text'] = formatting(response.json())
# start of tkinter loop
root = Tk()
weather = Label(root)
# manual refresh button that works
refresh = Button(root, text="Refresh", command=get_weather)
new_city = Entry(root)
weather.grid(row=0, column=0)
refresh.grid(row=0, column=1)
new_city.grid(row2, column=1)
# after method like his only seems to delay GUI launch only calls 'get_weather' once.
# I also tried weather.after(10000, get_weather())
root.after(10000, get_weather)
root.mainloop()
I can post the full code I just don't think anyone wants to read through 300 lines for what should be a simple solution.
edit: corrected button naming typo + added 'change_location' function to summery