工具介绍
- 这个天气查询工具是一个基于 Python 的桌面应用程序,使用了tkinter库来创建图形用户界面(GUI),并通过requests库调用 Open - Meteo API 获取天气数据。它具有赛博朋克风格的界面设计,提供了当前天气信息、15 天天气预报以及详细的天气数据展示,同时还包含温度趋势图表。
工具使用说明
界面布局
- 顶部标题栏:显示应用名称 “赛博朋克风格天气查询”。
搜索区域:
- 输入框:可以输入要查询的城市名称,默认显示为 “北京”。
- 查询按钮:点击该按钮可以查询输入城市的天气信息。
标签页:
- 当前天气:显示当前城市的天气信息,包括城市名称、日期、天气状况、温度、体感温度以及一些基本的天气数据(如湿度、风速等)。
- 15 天预报:包含未来 15 天的温度趋势图表和每天的天气预报卡片,卡片显示日期、星期、天气状况、最高温度和最低温度。
- 详细信息:显示更详细的天气数据,如日出时间、日落时间、日照时长、风向等。
查询天气
- 在输入框中输入要查询的城市名称。
- 点击 “查询天气” 按钮。
- 应用会显示加载中指示器,同时在后台线程中获取天气数据。
- 获取数据成功后,更新各个标签页的内容,显示该城市的天气信息;如果获取数据失败,会弹出错误提示框。
功能特点
- 多信息展示:提供当前天气、15 天预报和详细天气信息,满足用户对不同时间尺度天气数据的需求。
- 图表可视化:通过matplotlib库绘制未来 15 天的温度趋势图表,直观展示温度变化。
- 赛博朋克风格:使用赛博朋克风格的颜色和字体,界面设计独特。
- 多线程处理:在新线程中获取天气数据,避免阻塞 UI,保证用户交互的流畅性。
- 错误处理:在获取数据或更新界面失败时,会弹出错误提示框,告知用户具体的错误信息。
python脚本内容
import tkinter as tk
from tkinter import ttk, messagebox
import requests
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import threading# 设置中文字体
plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]class WeatherApp:def __init__(self, root):self.root = rootself.root.title("赛博天气 - 未来气象预测")self.root.geometry("800x700")self.root.configure(bg="#0A0E17")# 赛博朋克风格颜色self.colors = {'neon_blue': '#00F0FF','neon_purple': '#BF00FF','neon_pink': '#FF00FF','neon_green': '#00FF9C','dark_900': '#0A0E17','dark_800': '#141E30','dark_700': '#1A2940'}# 创建加载中指示器self.loading_frame = Noneself.loading_label = None# 创建UIself.create_widgets()# 初始加载北京天气self.get_weather("北京")def create_widgets(self):# 顶部标题栏title_frame = tk.Frame(self.root, bg=self.colors['dark_900'], bd=2, relief=tk.SOLID)title_frame.pack(fill=tk.X, pady=(0, 10))title_label = tk.Label(title_frame, text="赛博朋克风格天气查询", font=("SimHei", 20, "bold"),bg=self.colors['dark_900'],fg=self.colors['neon_blue'],bd=0,highlightthickness=0)title_label.pack(pady=10)# 搜索区域search_frame = tk.Frame(self.root, bg=self.colors['dark_900'])search_frame.pack(fill=tk.X, padx=20, pady=10)self.city_entry = ttk.Entry(search_frame, font=("SimHei", 12),width=30,background=self.colors['dark_800'],foreground=self.colors['neon_blue'])self.city_entry.pack(side=tk.LEFT, padx=(0, 10), ipady=5)self.city_entry.insert(0, "北京")search_btn = ttk.Button(search_frame, text="查询天气", command=self.search_weather,style='Neon.TButton')search_btn.pack(side=tk.LEFT, ipady=5)# 创建标签页notebook = ttk.Notebook(self.root)notebook.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)# 当前天气标签页current_frame = ttk.Frame(notebook)notebook.add(current_frame, text="当前天气")# 15天预报标签页forecast_frame = ttk.Frame(notebook)notebook.add(forecast_frame, text="15天预报")# 详细信息标签页details_frame = ttk.Frame(notebook)notebook.add(details_frame, text="详细信息")# 构建各标签页内容self.build_current_weather_frame(current_frame)self.build_forecast_frame(forecast_frame)self.build_details_frame(details_frame)# 配置自定义样式self.configure_styles()def configure_styles(self):style = ttk.Style()# 配置TButton样式style.configure('TButton', font=("SimHei", 10),background=self.colors['dark_800'],foreground=self.colors['neon_blue'],borderwidth=1,relief=tk.RAISED)# 赛博朋克风格按钮style.configure('Neon.TButton', font=("SimHei", 10, "bold"),background=self.colors['neon_blue'],foreground=self.colors['dark_900'],borderwidth=0,relief=tk.FLAT)style.map('Neon.TButton', background=[('active', self.colors['neon_pink'])])# 配置标签样式style.configure('TLabel', font=("SimHei", 10),background=self.colors['dark_900'],foreground="white")# 标题样式style.configure('Title.TLabel', font=("SimHei", 16, "bold"),foreground=self.colors['neon_blue'])# 子标题样式style.configure('Subtitle.TLabel', font=("SimHei", 12, "bold"),foreground=self.colors['neon_purple'])# 数据标签样式style.configure('Data.TLabel', font=("SimHei", 14, "bold"),foreground="white")# 数据值样式style.configure('Value.TLabel', font=("SimHei"