如何把天气地下预报图标

时间:2017-04-14 12:06:47

标签: json python-3.x tkinter

我是使用python编程的新手,并尝试从天气地下预测中放置图标,但是在行中 labelimg1 不显示任何图标,也不会给出任何错误。任何人都可以帮忙。

我在这里放了部分代码。 提前谢谢。

url =“http://api.wunderground.com/api/xxxxxxxxx/forecast/q/PT/Lisbon.json

    temp = urllib.request.urlopen(url)
    json_string = temp.read().decode('utf-8')
    parsed_json = json.loads(json_string)
    high = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius']
    low = parsed_json['forecast']['simpleforecast']['forecastday'][0]['low']['celsius']
    con = parsed_json['forecast']['simpleforecast']['forecastday'][0]['conditions']
    day = parsed_json['forecast']['simpleforecast']['forecastday'][0]['date']['day']
    high1 = parsed_json['forecast']['simpleforecast']['forecastday'][1]['high']['celsius']
    low1 = parsed_json['forecast']['simpleforecast']['forecastday'][1]['low']['celsius']
    con1 = parsed_json['forecast']['simpleforecast']['forecastday'][1]['conditions']
    day1 = parsed_json['forecast']['simpleforecast']['forecastday'][1]['date']['day']
    **icon = parsed_json['forecast']['simpleforecast']['forecastday'][0]['icon']**
    **img7 = PhotoImage(icon)**
    img8 = PhotoImage(file='c:/House/PNG/clear.png')
    **labelimg1 = Label(self, image=img7)**
    labelimg2 = Label(self, image=img8)
    label1 = Label(self, text="Dia: %s ,   Céu: %s" % (day, con,), font="Arial 12 bold")
    label2 = Label(self, text="Temperatura miníma de: %sº  - Temperatura máxima de: %sº " % (low, high,),
                   font="Arial 12 bold")
    label3 = Label(self, text="Dia: %s ,   Céu: %s " % (day1, con1,), font="Arial 12 bold")
    label4 = Label(self, text="Temperatura miníma de: %sº  - Temperatura máxima de: %sº " % (low1, high1,),
                   font="Arial 12 bold")
    label1.pack(pady=15)
    **labelimg1.image = img7
    labelimg1.pack()**
    label2.pack()
    label3.pack(pady=20)
    labelimg2.image = img8
    labelimg2.pack()
    label4.pack()
    temp.close()

1 个答案:

答案 0 :(得分:0)

parsed_json["current_observation"]["icon_url"]将返回该位置的当前天气图标。

更具体:

>>> import urllib
>>> import json
>>> url = "https://api.wunderground.com/api/<Your API key>/geolookup/conditions/q/<Your location>.json"
>>> temp = urllib.request.urlopen(url)
>>> json_string = temp.read().decode('utf-8')
>>> parsed_json = json.loads(json_string)
>>> parsed_json["current_observation"]["icon_url"]
'http://icons.wxug.com/i/c/k/nt_clear.gif'
相关问题