【问题标题】:Python pyowm library average tempaturePython pyowm 库平均温度
【发布时间】:2017-07-13 16:33:39
【问题描述】:

我有以下代码:

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] 以字符串方式对每个字符进行寻址,但这并不可靠,除非我添加大量参数来检测每个段中它是单个数字还是两位数字。我想要一个更简单的解决方案。

感谢您的帮助

【问题讨论】:

    标签: python string char openweathermap


    【解决方案1】:

    好像是字典,有没有试过这样显示结果?

    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)
    

    【讨论】:

    • 那不是元组,是字典。
    猜你喜欢
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2016-06-22
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    相关资源
    最近更新 更多