5.22 天气预报系统 小

 

2024春季学期《Python程序设计》大作业

 

系统说明报告

 

 

 

编制:

王晨宇(20224074

审查:

   

软件工程

   

2205-1


 

 

1项目目的与意义... 3

1.1项目背景说明... 3

1.2项目目的与意义... 3

2 软件开发环境与技术说明... 3

2.1软件开发环境... 3

2.2软件开发技术描述... 3

3系统分析与设计... 5

3.1项目需求分析说明... 5

3.2系统设计方案... 5

4系统源代码... 6

4.1系统源代码文件说明... 6

4.2源代码... 6

5系统使用说明书... 9

6参考资料... 10

7附件说明... 11

 

 

 

1项目目的与意义

1.1项目背景说明

通过爬取天气网站的数据以实现特定地区特定时间的天气 预报,要求界面用户输入需要预报的地区位置信息和需要预报的时间信息。

要求图形界面实现。

 

1.2项目目的与意义

可以获取全国各地各级城市的天气预报

 

2 软件开发环境与技术说明

2.1软件开发环境

说明软件开发环境,包括Java开发环境、Java支持包、数据库环境等。

Python 3.9 以及requests tkinter等库

2.2软件开发技术描述

天气预报系统可以分为以下功能模块:

  1. 用户界面模块:
    • 显示天气信息的表格;
    • 提供输入框用于用户输入城市名称;
    • 提供按钮触发获取天气信息的操作。
  2. 数据获取模块:
    • 调用第三方天气 API 请求城市天气数据。
  3. 数据处理模块:
    • 从 API 返回的数据中提取并解析出需要的天气信息。
  4. 数据展示模块:
    • 将获取的天气信息展示在用户界面的表格中。

 

 

 

 

3系统分析与设计

3.1项目需求分析说明

  1. 提供一个输入框,让用户输入要查询天气的城市名称。
  2. 提供一个按钮,用户点击后系统可以获取并显示该城市未来5天的天气信息。
  3. 显示天气信息的界面要清晰易读,包括日期、最高温度、最低温度、白天天气和夜晚天气等内容

 

3.2系统设计方案

  1. 前端界面使用Tkinter库来构建,定制样式以美化界面,提高用户体验。
  2. 后端通过调用天气API来获取城市天气信息,可以选择合适的第三方天气API服务。
  3. 数据库可以选择使用轻量级的SQLite数据库,存储城市信息和天气信息等数据。
  4. 可以考虑使用Python的Requests库来处理HTTP请求,获取API返回的JSON数据,并解析成Python数据结构用于显示在界面上。
  5. 在系统设计中考虑错误处理和异常情况,例如用户输入错误的城市名称或API请求失败时如何处理。

 

 

 

 

4系统源代码

4.1系统源代码文件说明

Gui.py 主程序代码

4.2源代码

import requests
import tkinter as tk
from tkinter import ttk

def get_weather_info():
    tree.delete(*tree.get_children())  # 清除表格内容

    city = city_entry.get()
    url = f"https://api.seniverse.com/v3/weather/daily.json?key=【你的key】&location={city}&language=zh-Hans&unit=c&start=-1&days=5" //这里我用的是心知天气 高德的那个
    response = requests.get(url)
    data = response.json()

    weather_data = data["results"][0]["daily"]

    for day_data in weather_data:
        date = day_data["date"]
        high_temp = day_data["high"]
        low_temp = day_data["low"]
        day_weather = day_data["text_day"]
        night_weather = day_data["text_night"]

        tree.insert("", 'end', values=(date, high_temp, low_temp, day_weather, night_weather))

# 初始化Tkinter窗口
root = tk.Tk()
root.geometry('800x450+500+500')
root.title("天气预报系统")

# 设置窗口背景色
root.configure(bg='#f0f0f0')

# 自定义Style
style = ttk.Style()
style.theme_use('clam')  # 使用clam主题,你可以根据喜好选择其他主题

# 调整Treeview的样式
style.configure("Treeview.Heading", font=('Arial', 12, 'bold'), background="#d9d9d9", foreground="black")  # 标题样式
style.configure("Treeview", font=('Arial', 12), rowheight=30, background="#f0f0f0", fieldbackground="#f0f0f0", foreground="black")

# 输入框和按钮的样式
style.configure("TEntry", font=('Arial', 14))  # 输入框字体大小
style.configure("TButton", font=('Arial', 14), padding=6)  # 按钮字体大小和内边距

# 主容器,便于整体布局
main_frame = ttk.Frame(root, padding="20", style="TFrame", relief="solid", borderwidth=1)
main_frame.grid(row=0, column=0, sticky=tk.NSEW)

# 输入框和标签
city_label = ttk.Label(main_frame, text="输入城市名称:", style="TLabel")
city_label.pack(pady=(20, 5))

city_entry = ttk.Entry(main_frame, width=20, style="TEntry")
city_entry.pack(ipady=4, pady=5, fill=tk.X)

# 查询按钮
fetch_button = ttk.Button(main_frame, text="查询天气", command=get_weather_info, style="TButton")
fetch_button.pack(pady=5)

# Treeview
tree = ttk.Treeview(main_frame, columns=("日期", "最高温度", "最低温度", "白天天气", "夜晚天气"), show="headings", style="Treeview")
tree.column("日期", width=120, anchor='center')
tree.column("最高温度", width=100, anchor='center')
tree.column("最低温度", width=100, anchor='center')
tree.column("白天天气", width=120, anchor='center')
tree.column("夜晚天气", width=120, anchor='center')

tree.heading("日期", text="日期")
tree.heading("最高温度", text="最高温度")
tree.heading("最低温度", text="最低温度")
tree.heading("白天天气", text="白天天气")
tree.heading("夜晚天气", text="夜晚天气")

tree.pack(pady=20, fill=tk.BOTH, expand=True)

# 窗口和列的权重设置
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

root.mainloop()

 

 

 

 

5系统使用说明书

1在输入框中输入你要查询的城市

2点击查询按钮,会返回近五天的天气预报

信息有 :日期,最高温度,最低温度,白天天气,夜晚天气

 

 

6参考资料

最好的 6 个免费天气 API 接口对比测评_免费天气api-CSDN博客

 

 

7附件说明

 

posted @ 2024-06-05 09:23  晨观夕  阅读(18)  评论(1编辑  收藏  举报