Python pyowm库平均时效

时间:2017-07-13 16:33:39

标签: python string char openweathermap

我有以下代码:

import tkinter as tk
import time
import pyowm

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.root.attributes("-fullscreen", True)
        self.label = tk.Label(text="")
        self.weatherlabel = tk.Label(text="")
        self.label.pack()
        self.update_clock()
        self.root.mainloop()

    def update_clock(self):
        now = time.strftime("%H:%M:%S")
        owm = pyowm.OWM()
        owm = pyowm.OWM('*censored*')
        observation = owm.weather_at_place('Modiin,il')
        test = owm.weather_at_place('Modiin,il').get_weather().get_temperature('celsius')
        print(test)
        self.label.configure(text=now,font=("Helvetica",40),fg="white",bg="black")
        self.weatherlabel.configure(text= test,font=("Helvetica",40),fg="white",bg="black")
        self.root.configure(background='black')
        self.root.after(1000, self.update_clock)
        self.label.place(x=1310,y=10)
        self.weatherlabel.place(x=400,y=400)

app = App()

有没有办法分别请求平均温度,最大温度和最小温度而不是它给出的巨大字符串?这里的输出是{'temp': 30.48, 'temp_max': 31.0, 'temp_min': 30.0, 'temp_kf': None}

我知道用test [x]以字符串方式对每个字符进行寻址,但这并不可靠,除非我添加了很多参数来检测它是否为单个数字或每个段中的两位数字。我想要一个更简单的解决方案。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

它似乎是一本字典,你试图像这样显示结果吗?

dict = {'temp': 30.48, 'temp_max': 31.0, 'temp_min': 30.0, 'temp_kf': None}

print dict['temp']
print "Maximum temperature : {}".format(dict['temp_max'])

输出:

30.48
Maximum temperature : 31.0

如果结果在test变量中,您可以执行以下操作:

def update_clock(self):
    now = time.strftime("%H:%M:%S")
    owm = pyowm.OWM()
    owm = pyowm.OWM('*censored*')
    observation = owm.weather_at_place('Modiin,il')
    test = owm.weather_at_place('Modiin,il').get_weather().get_temperature('celsius')
    print "Average temperature : {}".format(test['temp'])
    print "Minimum temperature : {}".format(test['temp_min'])
    print "Maximum temperature : {}".format(test['temp_max'])
    self.label.configure(text=now,font=("Helvetica",40),fg="white",bg="black")
    self.weatherlabel.configure(text= test,font=("Helvetica",40),fg="white",bg="black")
    self.root.configure(background='black')
    self.root.after(1000, self.update_clock)
    self.label.place(x=1310,y=10)
    self.weatherlabel.place(x=400,y=400)