适用
Weather
声明:资源链接索引至第三方,平台不作任何存储,仅提供信息检索服务,若有版权问题,请https://help.coders100.com提交工单反馈
这是一个使用Python和Tkinter库实现的简单天气预报程序。首先,我们需要安装requests库来访问在线天气API。然后,我们创建一个Tkinter窗口,添加一个输入框用于输入城市ID,一个按钮用于刷新数据,以及一个显示区域用于显示天气信息。当用户点击“刷新”按钮时,我们将重新请求天气数据并更新显示区域的信息。
请将`your_api_key`替换为您的OpenWeatherMap API密钥。运行此程序后,您需要输入城市ID以获取天气预报。点击“Get Weather”按钮将获取该城市的天气预报数据并显示在界面上。点击“Refresh”按钮将重新请求天气数据并更新显示区域的信息。
import requests
from tkinter import
def get_weather(city_id):
url = f"https://api.openweathermap.org/data/2.5/weather?q={city_id}&appid=your_api_key"
response = requests.get(url)
data = response.json()
weather_data = data['weather']
for item in weather_data:
if 'description' in item:
description = item['description']
temperature = item['main']['temp']
print(f"{city_id} - {description}")
print(f"Temperature: {temperature}°C")
update_button = Button(root, text="Refresh", command=refresh_button)
update_button.pack()
def refresh_button():
city_id = entry.get()
get_weather(city_id)
root = Tk()
root.title("Weather Forecast")
entry = Entry(root)
entry.pack()
button = Button(root, text="Get Weather", command=get_weather)
button.pack()
update_button = Button(root, text="Refresh", command=refresh_button)
update_button.pack()
root.mainloop()
请将`your_api_key`替换为您的OpenWeatherMap API密钥。运行此程序后,您需要输入城市ID以获取天气预报。点击“Get Weather”按钮将获取该城市的天气预报数据并显示在界面上。点击“Refresh”按钮将重新请求天气数据并更新显示区域的信息。
-
NoUnityCN
- 2025-07-16 00:37:12
-
2025summer_vacation
- 2025-07-16 00:57:49
-
canal-assembly
- 2025-07-16 01:42:44
-
SiteWatcher
- 2025-07-16 02:36:39
-
php-log-guard
- 2025-07-16 02:36:51
-
Wandering-in-wonder-1
- 2025-07-16 04:49:19
-
Wandering-in-wonder
- 2025-07-16 04:49:31
-
QHY-PXGL-JSGL
- 2025-07-16 05:51:14
-
Online-education-and-training-office-system
- 2025-07-16 05:51:22
-
bwcx
- 2025-07-16 06:30:05
-
WebGame-jsoo
- 2025-07-16 06:30:20
-
lspcieee_ahk
- 2025-07-16 07:51:18
-
natapp_autostart
- 2025-07-16 07:51:32
-
Plug-in-R
- 2025-07-16 08:12:05
-
Quickinput
- 2025-07-16 08:21:42
-
Automotive-Security-Timeline
- 2025-07-16 08:44:27
-
autoMSS
- 2025-07-16 08:51:08
-
Hadoop-Environment-Configuration
- 2025-07-16 09:13:12
-
pyspark_streaming_kafka_phonedata
- 2025-07-16 09:13:26
-
NiceFont
- 2025-07-16 09:36:34
-
UploadFilesToServerTools
- 2025-07-16 09:47:03
访问申明(访问视为同意此申明)
2.如有索引链接发生失效情况请【联系客服】自助退回)
3.是否访问均为用户自主行为,本站只提供搜索服务不提供技术支持,感谢您的支持