Python tkinter:openweathermap API中的Clecius

问题描述 投票:0回答:1

如何使用openweathermap API将华氏温度(* F)转换为摄氏温度(* C)?我正在尝试创建一个程序,在其中键入城市名称,然后输入温度等信息,并且应以摄氏度显示...但是我不知道该怎么做。THX 4任何帮助

CODE:

import tkinter as tk
import requests
hi = 500
wi=600
root = tk.Tk()

def test_fun(entry):
    print(entry)

def fr(weather):
    try:
        name = weather['name']
        des = weather['weather'][0]['description']
        temp = weather['main']['temp']

        fs='City: %s \nConditions: %s \nTemp (C*): %s' % (name, des, temp)
    except Exception:
        fs="That's error 404. Type correctly "

    return fs

def weather1(city):
    key='aba06543f5f70442cc5b3efe6674d2b4'
    url='http://api.openweathermap.org/data/2.5/weather'
    **p={'APPID': key, 'q': city, 'units': 'imperial'}** 
    r= requests.get(url, params=p)
    w=r.json()
    label['text'] = fr(w)
    print(w['name'])
    print(w['weather'][0]['description'])
    print(w['main']['temp'])



canvas = tk.Canvas (root, height=hi, width= wi)
canvas.pack()  

frame = tk.Frame(root, bg='#000080', bd=2,)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n') 

button = tk.Button(frame, text="GO", font=36, command=lambda: weather1(entry.get()))
button.place(relx=0.7, relwidth=0.3, relheight=1)                                        

entry=tk.Entry(frame, font=30)
entry.place(relwidth=0.69, relheight=1)

frame2 = tk.Frame(root, bg='#000080', bd=2)
frame2.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')

label=tk.Label(frame2, bg='#00CED1')
label.place(relwidth=1, relheight=1)

root.mainloop()
python tkinter
1个回答
0
投票

有几种方法可以解决这个问题。首先,您可以将参数中的“度量”传递给api而不是“英制”。

或者您可以使用以下公式来实现将从法氏度转换为摄氏度的答案转换为函数的功能。 celsius = (fahrenheit - 32) * 5/9

或者您可以在程序中按行将其转换:

print(w['weather'][0]['description'])
print(w['main']['temp'])

更改为类似内容:

print(w['weather'][0]['description'])
celsius = (w['main']['temp'] - 32) * 5/9
print(celsius)
© www.soinside.com 2019 - 2024. All rights reserved.