这是我的代码,我基本上希望有一行用于输入名为CITY
的变量,我在这样做时遇到了问题。我曾尝试使用Tkinter输入,但无法做到,它只是给了我错误
from tkinter import *
import requests, json
import random
window = Tk()
window.title("Weather")
window.geometry('350x200')
def start():
CITY = "London"
BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
API_KEY = "xxxxxxx"
URL = BASE_URL + "q=" + CITY + "&appid=" + API_KEY
response = requests.get(URL)
if response.status_code == 200:
data = response.json()
main = data['main']
temperature = main['temp']
humidity = main['humidity']
pressure = main['pressure']
report = data['weather']
temperature = round(int(temperature) - 273.15)
Label(window, foreground='blue',text=f"{CITY:-^30}").grid(column=0, row=1)
Label(window, text=f"Temperature: {temperature}"+"C").grid(column=0, row=2)
Label(window, text=f"Humidity: {humidity}"+"%").grid(column=0, row=3)
Label(window, text=f"Pressure: {pressure}").grid(column=0, row=4)
Label(window, text=f"Weather Report: {report[0]['description']}").grid(column=0, row=5)
else:
# showing the error message
print("Error in the HTTP request")
start()
window.mainloop()
这可以通过在窗口中添加一个条目小部件,然后访问其内容来实现。要添加条目小部件,可以使用以下内容(在设置
window
之后):然后可以通过
ent_city.get()
访问ent_city
字段的内容。在start()
中,这可能是这样的:相关问题 更多 >
编程相关推荐