如何在标签中打印JSON请求?

时间:2019-05-30 21:30:18

标签: python linux tkinter openweathermap

我正在尝试设置api天气。代码有效,我可以在python提示行中打印结果。但是我不能用tkinter做到这一点。当我按下按钮时,请求未显示在标签中。我尝试了Def,textvariable,str。我不知道如何定义函数Def。

from tkinter import *
import requests
import json

def weather():
    name = city1.get()
    net = "http://api.openweathermap.org/data/2.5/weather?q={}&appid='MY_API_KEY'&units=metric&lang=pt".format(name)
    req = requests.get(net)
    data = req.json()
    temp = data['main']['temp']
    description = data['weather'][0]['description']
    print('Temperature : {} ˚'.format(temp))
    print('Description : {}'.format(description))


def data():
    req = requests.get(net)
    data = req.json()


win = Tk()
win.title('WEATHER')
win.geometry("500x300+260+120")

city1 = Entry(win,justify='center', font=('arial',14,'bold'))
city1.place(x=160, y=20, width=190, heigh=30)

temp1 = Label(win,text=str(data['main']['temp']),relief= 'groove', 
borderwidth=4, font=('arial',10,'bold'))
temp1.place(x=100, y=90, width= 280, height=40)

res = Label(win,relief= 'groove', borderwidth=4, font=('arial',14,'bold'))
res.place(x=100, y=160, width= 280, height=40)

btn = Button(win, text='OK', command=weather)
btn.place(x=400, y=20)

win.mainloop()

0 个答案:

没有答案