I am trying to make a weather app with Python and Tkinter. I am using the weather api from weatherapi.com. I have pretty much everything but the icon, I don't know how to get the icon url from the .json file and update the icon when searching at a different city or place.
here is the image of the json file where the current condition icon is:
Here is the createFrameInfo code in the view.py:
#the information of the current condition the text that says "image" is a place holder for the actual icon
def _createFrameInfo(self):
self.frameInfo = Frame(self.mainframe)
labelTemp = Label(self.frameInfo, textvariable=self.varTemp)
labelLocation = Label(self.frameInfo, textvariable= self.varLocation)
labelIcon = Label(self.frameInfo, text = 'image')
labelTemp.pack(pady = 5)
labelLocation.pack(pady = 5)
labelIcon.pack(pady = 5)
self.frameInfo.pack()
Here is a bit of code from weather.py
# The pass is there so the app can run without having an error.
def getConditionIcon(self):
pass
Here is the github of the all the codes: https://github.com/EasyCanadianGamer/Python-Weather-App
I checked the docs and yet there was not a clear explanation of how to get the icon url and updating it. Here is the Weatherapi docs: https://www.weatherapi.com/docs/
You can get the icon URL like how you get the condition text and use requests.get()
to get the icon data:
def getConditionIcon(self):
condition = self.getCurrentData("condition")
icon_url = f"http:{condition['icon']}"
try:
icon_data = requests.get(icon_url).content
except Exception as ex:
print(ex)
icon_data = None
return icon_data
Then you can pass the icon data to tkinter.PhotoImage()
to create an image object:
weather = Weather(...) # pass the location you want
image = tkinter.PhotoImage(data=weather.getConditionIcon())
Note that if the icon image is not PNG image, you may need to use Pillow
module to convert the icon data to image.