Python tkinter:OpenWeatherMapAPI中的摄氏度
如何使用openweathermap API将华氏(*F)变为摄氏(*C)?我正在尝试创建一个程序,您键入一个城市名称,您将收到温度等,它应该显示在摄氏度。。。但我不知道怎么做。 THX 4任何帮助 代码:Python tkinter:OpenWeatherMapAPI中的摄氏度,python,tkinter,Python,Tkinter,如何使用openweathermap API将华氏(*F)变为摄氏(*C)?我正在尝试创建一个程序,您键入一个城市名称,您将收到温度等,它应该显示在摄氏度。。。但我不知道怎么做。 THX 4任何帮助 代码: 有几种方法可以实现这一点。首先,您可以将参数中的“metric”而不是“imperial”传递给api 或者,您可以实现一个函数,使用公式将从farenheit得到的答案转换为摄氏度摄氏=(华氏-32)*5/9 或者,您也可以在程序中对其进行转换: 打印(w['weather'][0]['d
有几种方法可以实现这一点。首先,您可以将参数中的“metric”而不是“imperial”传递给api 或者,您可以实现一个函数,使用公式将从farenheit得到的答案转换为摄氏度<代码>摄氏=(华氏-32)*5/9 或者,您也可以在程序中对其进行转换:
打印(w['weather'][0]['description'])
打印(w['main']['temp'])
改为:
打印(w['weather'][0]['description'])
摄氏=(宽['main']['temp']-32)*5/9
打印(摄氏度)
就我个人而言,我会选择函数定义选项。要回答这个问题,您只需阅读openweathermap api文档即可。
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()