如何从weatherapi.com获取天气图标?

huangapple go评论82阅读模式
英文:

(Python) How do I get the weather icon from weatherapi.com?

问题

我正在尝试使用Python和Tkinter制作一个天气应用程序。我正在使用weatherapi.com的天气API。我几乎都已经完成了,但是不知道如何从.json文件中获取图标URL,并在搜索不同城市或地点时更新图标。

这是json文件中当前条件图标位置的图像:
如何从weatherapi.com获取天气图标?

这是view.py中的createFrameInfo代码:

# 当前条件信息,显示“图像”的文本是实际图标的占位符
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()

这是weather.py中的一部分代码:

# 这里的pass是为了使应用程序在没有错误的情况下运行。
def getConditionIcon(self):
    pass

这是所有代码的GitHub链接:https://github.com/EasyCanadianGamer/Python-Weather-App

我已经检查了文档,但没有清晰的解释如何获取图标URL并进行更新。这是Weatherapi的文档链接:https://www.weatherapi.com/docs/

英文:

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:
如何从weatherapi.com获取天气图标?

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/

答案1

得分: 1

你可以像获取condition text一样获取图标的URL,并使用 requests.get() 来获取图标数据:

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

然后,你可以将图标数据传递给 tkinter.PhotoImage() 来创建一个图像对象:

weather = Weather(...)  # 传递你想要的位置
image = tkinter.PhotoImage(data=weather.getConditionIcon())

请注意,如果图标图像不是PNG格式,你可能需要使用 Pillow 模块将图标数据转换为图像。

英文:

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.

huangapple
  • 本文由 发表于 2023年8月4日 05:09:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定