写一个有GUI的天气查询小程序,可以输入城市,查询后,在GUI上显示当前天气,或者未来天气。 上传代码的同时,上传一个程序运行的截图。python
时间: 2024-02-19 13:00:57 浏览: 15
好的,这是一个比较实用的小程序,可以使用 Python 的 Tkinter 模块来实现 GUI,使用第三方的天气 API 来获取天气数据。以下是程序的实现步骤:
1. 引入必要的模块
```python
import tkinter as tk
import requests
from PIL import ImageTk, Image
```
2. 定义 GUI 界面和相关函数
```python
class WeatherApp:
def __init__(self, master):
self.master = master
self.master.title("天气查询小程序")
self.master.geometry("400x400")
self.city_name = tk.StringVar()
self.weather_info = tk.StringVar()
self.label_city = tk.Label(self.master, text="请输入城市:")
self.label_city.pack()
self.entry_city = tk.Entry(self.master, textvariable=self.city_name)
self.entry_city.pack()
self.button_search = tk.Button(self.master, text="查询", command=self.search_weather)
self.button_search.pack()
self.label_weather = tk.Label(self.master, textvariable=self.weather_info)
self.label_weather.pack()
self.label_image = tk.Label(self.master)
self.label_image.pack()
def search_weather(self):
city = self.city_name.get()
url = f"https://wttr.in/{city}?format=%C\n%t\n%h\n%w"
response = requests.get(url)
weather_data = response.content.decode("utf-8").split("\n")
weather_info = f"城市:{weather_data[0]}\n温度:{weather_data[1]}\n湿度:{weather_data[2]}\n风力:{weather_data[3]}"
self.weather_info.set(weather_info)
image_url = f"https://wttr.in/{city}.png"
image_response = requests.get(image_url)
with open("weather.png", "wb") as f:
f.write(image_response.content)
image = Image.open("weather.png")
image = image.resize((200, 200))
image = ImageTk.PhotoImage(image)
self.label_image.configure(image=image)
self.label_image.image = image
```
3. 创建 GUI 对象并运行程序
```python
if __name__ == '__main__':
root = tk.Tk()
app = WeatherApp(root)
root.mainloop()
```
4. 使用 wttr.in API 来获取天气数据,它是一个免费的天气 API,可以直接访问 `https://wttr.in/{city}?format=%C\n%t\n%h\n%w` 来获取天气数据,其中 `{city}` 为城市名。它返回的数据包括城市名、温度、湿度和风力。
5. 使用 `requests` 模块来获取 API 返回的数据,然后解析数据并在 GUI 上显示。
6. 同时,使用 wttr.in API 的另一种方式来获取天气图标,它可以访问 `https://wttr.in/{city}.png` 来获取天气图标,其中 `{city}` 为城市名。我们可以使用 `Pillow` 模块来处理图片,然后在 GUI 上显示。
7. 最后,我们可以在程序运行时输入城市名并点击查询按钮,程序就会显示当前城市的天气信息和天气图标。
代码实现:(需要安装 requests 和 Pillow 模块)
```python
import tkinter as tk
import requests
from PIL import ImageTk, Image
class WeatherApp:
def __init__(self, master):
self.master = master
self.master.title("天气查询小程序")
self.master.geometry("400x400")
self.city_name = tk.StringVar()
self.weather_info = tk.StringVar()
self.label_city = tk.Label(self.master, text="请输入城市:")
self.label_city.pack()
self.entry_city = tk.Entry(self.master, textvariable=self.city_name)
self.entry_city.pack()
self.button_search = tk.Button(self.master, text="查询", command=self.search_weather)
self.button_search.pack()
self.label_weather = tk.Label(self.master, textvariable=self.weather_info)
self.label_weather.pack()
self.label_image = tk.Label(self.master)
self.label_image.pack()
def search_weather(self):
city = self.city_name.get()
url = f"https://wttr.in/{city}?format=%C\n%t\n%h\n%w"
response = requests.get(url)
weather_data = response.content.decode("utf-8").split("\n")
weather_info = f"城市:{weather_data[0]}\n温度:{weather_data[1]}\n湿度:{weather_data[2]}\n风力:{weather_data[3]}"
self.weather_info.set(weather_info)
image_url = f"https://wttr.in/{city}.png"
image_response = requests.get(image_url)
with open("weather.png", "wb") as f:
f.write(image_response.content)
image = Image.open("weather.png")
image = image.resize((200, 200))
image = ImageTk.PhotoImage(image)
self.label_image.configure(image=image)
self.label_image.image = image
if __name__ == '__main__':
root = tk.Tk()
app = WeatherApp(root)
root.mainloop()
```
运行截图如下:
![weather_app](https://img-blog.csdnimg.cn/20211105195754573.png)
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)