|
|
|
|
@ -158,16 +158,8 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
weather_data = self.weather_api.get_weather_data(city)
|
|
|
|
|
if weather_data:
|
|
|
|
|
print(f"获取到天气数据: {weather_data}")
|
|
|
|
|
# 格式化数据以匹配状态栏期望的格式
|
|
|
|
|
formatted_data = {
|
|
|
|
|
'city': weather_data['city'],
|
|
|
|
|
'temperature': weather_data['current']['temp'],
|
|
|
|
|
'description': weather_data['current']['weather'],
|
|
|
|
|
'humidity': weather_data['current']['humidity'],
|
|
|
|
|
'wind_scale': weather_data['current']['wind_scale']
|
|
|
|
|
}
|
|
|
|
|
print(f"格式化后的数据: {formatted_data}")
|
|
|
|
|
self.update_weather_display(formatted_data)
|
|
|
|
|
# 直接传递原始数据,update_weather_display会处理嵌套结构
|
|
|
|
|
self.update_weather_display(weather_data)
|
|
|
|
|
else:
|
|
|
|
|
print(f"无法获取城市 {city} 的天气数据")
|
|
|
|
|
|
|
|
|
|
@ -205,7 +197,8 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
'temperature': weather_data['current']['temp'],
|
|
|
|
|
'description': weather_data['current']['weather'],
|
|
|
|
|
'humidity': weather_data['current']['humidity'],
|
|
|
|
|
'wind_scale': weather_data['current']['wind_scale']
|
|
|
|
|
'wind_scale': weather_data['current']['wind_scale'],
|
|
|
|
|
'life_tips': weather_data.get('life_tips', [])
|
|
|
|
|
}
|
|
|
|
|
print(f"格式化后的数据: {formatted_data}")
|
|
|
|
|
self.update_weather_display(formatted_data)
|
|
|
|
|
@ -929,25 +922,34 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
|
|
|
|
|
def update_weather_display(self, weather_data):
|
|
|
|
|
"""更新天气显示"""
|
|
|
|
|
print(f"接收到天气数据: {weather_data}")
|
|
|
|
|
if 'error' in weather_data:
|
|
|
|
|
print(f"天气显示错误: {weather_data['error']}")
|
|
|
|
|
self.status_bar.showMessage(f"天气数据获取失败: {weather_data['error']}", 3000)
|
|
|
|
|
else:
|
|
|
|
|
# 处理嵌套的天气数据结构
|
|
|
|
|
city = weather_data.get('city', '未知城市')
|
|
|
|
|
temp = weather_data.get('temperature', 'N/A')
|
|
|
|
|
desc = weather_data.get('description', 'N/A')
|
|
|
|
|
humidity = weather_data.get('humidity', 'N/A')
|
|
|
|
|
wind_scale = weather_data.get('wind_scale', 'N/A')
|
|
|
|
|
|
|
|
|
|
# 从current字段获取温度和天气状况
|
|
|
|
|
current_data = weather_data.get('current', {})
|
|
|
|
|
temp = current_data.get('temp', 'N/A')
|
|
|
|
|
desc = current_data.get('weather', 'N/A')
|
|
|
|
|
|
|
|
|
|
# 获取温度范围信息
|
|
|
|
|
temp_range = ""
|
|
|
|
|
if 'forecast' in weather_data and weather_data['forecast']:
|
|
|
|
|
forecast_data = weather_data['forecast'][0] # 今天的预报
|
|
|
|
|
if isinstance(forecast_data, dict):
|
|
|
|
|
temp_max = forecast_data.get('temp_max', 'N/A')
|
|
|
|
|
temp_min = forecast_data.get('temp_min', 'N/A')
|
|
|
|
|
if temp_max != 'N/A' and temp_min != 'N/A':
|
|
|
|
|
temp_range = f" ({temp_min}°C~{temp_max}°C)"
|
|
|
|
|
|
|
|
|
|
# 在状态栏显示简要天气信息
|
|
|
|
|
weather_message = f"{city}: {desc}, {temp}°C, 湿度{humidity}%, 风力{wind_scale}级"
|
|
|
|
|
print(f"显示天气信息: {weather_message}")
|
|
|
|
|
weather_message = f"{city}: {desc}, {temp}°C{temp_range}"
|
|
|
|
|
self.status_bar.showMessage(weather_message, 5000)
|
|
|
|
|
|
|
|
|
|
# 存储天气数据供其他功能使用
|
|
|
|
|
# 存储天气数据供其他功能使用(确保包含生活提示)
|
|
|
|
|
self.current_weather_data = weather_data
|
|
|
|
|
print("天气数据已存储")
|
|
|
|
|
print(f"update_weather_display - 存储的current_weather_data包含life_tips: {self.current_weather_data.get('life_tips', [])}")
|
|
|
|
|
|
|
|
|
|
def refresh_weather(self):
|
|
|
|
|
"""手动刷新天气信息"""
|
|
|
|
|
@ -964,15 +966,15 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
weather_data = self.weather_api.get_weather_data(current_city)
|
|
|
|
|
|
|
|
|
|
if weather_data:
|
|
|
|
|
# 格式化天气数据
|
|
|
|
|
# 格式化天气数据为扁平结构,便于update_weather_display使用
|
|
|
|
|
formatted_data = {
|
|
|
|
|
'city': weather_data['city'],
|
|
|
|
|
'temperature': weather_data['current']['temp'],
|
|
|
|
|
'description': weather_data['current']['weather'],
|
|
|
|
|
'humidity': weather_data['current']['humidity'],
|
|
|
|
|
'wind_scale': weather_data['current']['wind_scale'],
|
|
|
|
|
'forecast': weather_data['forecast']
|
|
|
|
|
'current': weather_data['current'],
|
|
|
|
|
'forecast': weather_data['forecast'],
|
|
|
|
|
'life_tips': weather_data.get('life_tips', [])
|
|
|
|
|
}
|
|
|
|
|
print(f"refresh_weather - 原始数据包含life_tips: {weather_data.get('life_tips', [])}")
|
|
|
|
|
print(f"refresh_weather - formatted_data包含life_tips: {formatted_data.get('life_tips', [])}")
|
|
|
|
|
self.update_weather_display(formatted_data)
|
|
|
|
|
self.status_bar.showMessage("天气数据已刷新", 2000)
|
|
|
|
|
else:
|
|
|
|
|
@ -990,6 +992,7 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
weather_data = self.current_weather_data
|
|
|
|
|
print(f"详细天气对话框 - 天气数据: {weather_data}")
|
|
|
|
|
|
|
|
|
|
# 创建对话框
|
|
|
|
|
dialog = QDialog(self)
|
|
|
|
|
@ -1006,11 +1009,26 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
current_layout = QVBoxLayout()
|
|
|
|
|
current_layout.addWidget(QLabel("<b>当前天气:</b>"))
|
|
|
|
|
|
|
|
|
|
# 获取温度信息,支持嵌套结构
|
|
|
|
|
current_data = weather_data.get('current', {})
|
|
|
|
|
temp = current_data.get('temp', 'N/A')
|
|
|
|
|
if temp != 'N/A' and isinstance(temp, str):
|
|
|
|
|
temp = float(temp) if temp.replace('.', '').isdigit() else temp
|
|
|
|
|
|
|
|
|
|
# 从预报数据中获取最高和最低气温
|
|
|
|
|
temp_max = 'N/A'
|
|
|
|
|
temp_min = 'N/A'
|
|
|
|
|
if 'forecast' in weather_data and weather_data['forecast']:
|
|
|
|
|
forecast_data = weather_data['forecast'][0] # 今天的预报
|
|
|
|
|
if isinstance(forecast_data, dict):
|
|
|
|
|
temp_max = forecast_data.get('temp_max', 'N/A')
|
|
|
|
|
temp_min = forecast_data.get('temp_min', 'N/A')
|
|
|
|
|
|
|
|
|
|
current_info = f"""
|
|
|
|
|
温度: {weather_data.get('temperature', 'N/A')}°C
|
|
|
|
|
天气状况: {weather_data.get('description', 'N/A')}
|
|
|
|
|
湿度: {weather_data.get('humidity', 'N/A')}%
|
|
|
|
|
风力: {weather_data.get('wind_scale', 'N/A')}级
|
|
|
|
|
当前温度: {temp}°C
|
|
|
|
|
最高气温: {temp_max}°C
|
|
|
|
|
最低气温: {temp_min}°C
|
|
|
|
|
天气状况: {current_data.get('weather', 'N/A')}
|
|
|
|
|
"""
|
|
|
|
|
current_text = QTextEdit()
|
|
|
|
|
current_text.setPlainText(current_info.strip())
|
|
|
|
|
@ -1019,22 +1037,23 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
|
|
|
|
|
layout.addLayout(current_layout)
|
|
|
|
|
|
|
|
|
|
# 天气预报信息
|
|
|
|
|
if 'forecast' in weather_data and weather_data['forecast']:
|
|
|
|
|
forecast_layout = QVBoxLayout()
|
|
|
|
|
forecast_layout.addWidget(QLabel("<b>天气预报:</b>"))
|
|
|
|
|
# 生活提示信息(替换原来的天气预报)
|
|
|
|
|
life_tips = weather_data.get('life_tips', [])
|
|
|
|
|
print(f"详细天气对话框 - 生活提示: {life_tips}")
|
|
|
|
|
print(f"详细天气对话框 - 完整天气数据: {weather_data}")
|
|
|
|
|
if life_tips:
|
|
|
|
|
tips_layout = QVBoxLayout()
|
|
|
|
|
tips_layout.addWidget(QLabel("<b>生活提示:</b>"))
|
|
|
|
|
|
|
|
|
|
forecast_text = QTextEdit()
|
|
|
|
|
forecast_info = ""
|
|
|
|
|
for i, day in enumerate(weather_data['forecast'][:3]): # 显示最近3天的预报
|
|
|
|
|
if i < len(weather_data['forecast']):
|
|
|
|
|
day_data = weather_data['forecast'][i]
|
|
|
|
|
forecast_info += f"第{i+1}天: {day_data.get('fxDate', 'N/A')} - {day_data.get('textDay', 'N/A')}, {day_data.get('tempMin', 'N/A')}~{day_data.get('tempMax', 'N/A')}°C\n"
|
|
|
|
|
tips_text = QTextEdit()
|
|
|
|
|
tips_info = ""
|
|
|
|
|
for tip in life_tips:
|
|
|
|
|
tips_info += f"• {tip}\n"
|
|
|
|
|
|
|
|
|
|
forecast_text.setPlainText(forecast_info.strip())
|
|
|
|
|
forecast_text.setReadOnly(True)
|
|
|
|
|
forecast_layout.addWidget(forecast_text)
|
|
|
|
|
layout.addLayout(forecast_layout)
|
|
|
|
|
tips_text.setPlainText(tips_info.strip())
|
|
|
|
|
tips_text.setReadOnly(True)
|
|
|
|
|
tips_layout.addWidget(tips_text)
|
|
|
|
|
layout.addLayout(tips_layout)
|
|
|
|
|
|
|
|
|
|
# 按钮
|
|
|
|
|
button_layout = QHBoxLayout()
|
|
|
|
|
|