diff --git a/src/ui/word_style_ui.py b/src/ui/word_style_ui.py index 0d90ce2..98c29f3 100644 --- a/src/ui/word_style_ui.py +++ b/src/ui/word_style_ui.py @@ -228,22 +228,39 @@ class WordRibbon(QFrame): quote_group = self.create_ribbon_group("每日一言") quote_layout = QVBoxLayout() - # 每日一言显示标签 - self.quote_label = QLabel("暂无") - self.quote_label.setStyleSheet("QLabel { color: #666666; font-style: italic; font-size: 10px; }") - self.quote_label.setWordWrap(True) - self.quote_label.setFixedWidth(150) + # 创建第一行:类型选择下拉框和刷新按钮 + top_row_layout = QHBoxLayout() + + # 类型选择下拉框 + self.quote_type_combo = QComboBox() + self.quote_type_combo.addItems(["普通箴言", "古诗词句"]) + self.quote_type_combo.setFixedSize(120, 25) + self.quote_type_combo.currentTextChanged.connect(self.on_quote_type_changed) # 刷新按钮 self.refresh_quote_btn = QPushButton("刷新箴言") self.refresh_quote_btn.clicked.connect(self.on_refresh_quote) self.refresh_quote_btn.setFixedSize(80, 25) + # 添加到第一行布局 + top_row_layout.addWidget(self.quote_type_combo) + top_row_layout.addWidget(self.refresh_quote_btn) + top_row_layout.addStretch() # 添加弹性空间,使控件靠左对齐 + + # 每日一言显示标签 - 增大尺寸 + self.quote_label = QLabel("暂无") + self.quote_label.setStyleSheet("QLabel { color: #666666; font-style: italic; font-size: 10px; }") + self.quote_label.setWordWrap(True) + self.quote_label.setFixedWidth(250) # 增加到250像素宽度 + self.quote_label.setMinimumHeight(40) # 设置最小高度,增加显示空间 + + # 添加到主布局 + quote_layout.addLayout(top_row_layout) quote_layout.addWidget(self.quote_label) - quote_layout.addWidget(self.refresh_quote_btn) quote_group.setLayout(quote_layout) self.quote_group = quote_group + self.current_quote_type = "普通箴言" # 默认类型 # 组件创建完成后自动获取每日一言 self.load_daily_quote() @@ -303,44 +320,63 @@ class WordRibbon(QFrame): def load_daily_quote(self): """加载每日一言""" try: - # 创建每日一言API实例 - quote_api = daily_sentence_API("https://api.nxvav.cn/api/yiyan") - - # 获取每日一言数据 - quote_data = quote_api.get_sentence('json') - - if quote_data and isinstance(quote_data, dict): - # 从返回的数据中提取每日一言文本 - quote_text = quote_data.get('yiyan', '暂无每日一言') - self.update_quote_display(quote_text) + # 根据当前选择的类型获取不同的内容 + if self.current_quote_type == "古诗词句": + # 获取古诗词 + quote_text = self.get_chinese_poetry() else: - # 如果API返回空或格式不正确,显示默认文本 - self.update_quote_display("暂无每日一言") + # 获取普通箴言 + quote_api = daily_sentence_API("https://api.nxvav.cn/api/yiyan") + quote_data = quote_api.get_sentence('json') + + if quote_data and isinstance(quote_data, dict): + quote_text = quote_data.get('yiyan', '暂无每日一言') + else: + quote_text = "暂无每日一言" + + self.update_quote_display(quote_text) except Exception as e: print(f"加载每日一言失败: {e}") - self.update_quote_display("暂无每日一言") + self.update_quote_display("获取失败") def on_refresh_quote(self): """刷新每日一言按钮点击处理""" + self.load_daily_quote() + + def on_quote_type_changed(self, quote_type): + """每日一言类型切换处理""" + self.current_quote_type = quote_type + # 类型切换时自动刷新内容 + self.load_daily_quote() + + def get_chinese_poetry(self): + """获取古诗词 - 使用古诗词·一言API(随机返回不同诗词)""" try: - # 创建每日一言API实例 - quote_api = daily_sentence_API("https://api.nxvav.cn/api/yiyan") - - # 获取每日一言数据 - quote_data = quote_api.get_sentence('json') + # 使用古诗词·一言API - 每次返回随机不同的诗词 + response = requests.get("https://v1.jinrishici.com/all.json", timeout=5) - if quote_data and isinstance(quote_data, dict): - # 从返回的数据中提取每日一言文本 - quote_text = quote_data.get('yiyan', '暂无每日一言') - self.update_quote_display(quote_text) + if response.status_code == 200: + data = response.json() + content = data.get('content', '') + author = data.get('author', '') + title = data.get('origin', '') + + # 格式化显示文本 + if content and author and title: + return f"{content} — {author}《{title}》" + elif content and author: + return f"{content} — {author}" + elif content: + return content + else: + return "暂无古诗词" else: - # 如果API返回空或格式不正确,显示默认文本 - self.update_quote_display("获取每日一言失败") + return "获取古诗词失败" except Exception as e: - print(f"获取每日一言失败: {e}") - self.update_quote_display("获取每日一言失败") + print(f"获取古诗词失败: {e}") + return "获取古诗词失败" def update_quote_display(self, quote_text): """更新每日一言显示""" @@ -643,6 +679,28 @@ class WeatherAPI: city_info = data['cityInfo'] current_data = data['data'] + # 获取生活提示信息 + life_tips = [] + forecast = current_data.get('forecast', []) + if forecast: + # 从预报中提取提示信息 + for day in forecast[:3]: # 取前3天的提示 + notice = day.get('notice', '') + if notice: + life_tips.append(notice) + + # 如果没有获取到足够的提示,添加一些默认的 + default_tips = [ + "愿你拥有比阳光明媚的心情", + "雾霾来袭,戴好口罩再出门", + "今天天气不错,适合出去走走", + "记得多喝水,保持身体健康" + ] + + # 补充到至少3个提示 + while len(life_tips) < 3 and default_tips: + life_tips.append(default_tips.pop(0)) + weather_info = { 'temp': current_data['wendu'], 'feels_like': current_data['wendu'], # 没有体感温度,用实际温度代替 @@ -651,7 +709,8 @@ class WeatherAPI: 'wind_dir': current_data['forecast'][0]['fx'], 'wind_scale': '1', # 没有风力等级,用默认值 'vis': current_data['forecast'][0]['high'], # 用最高温作为可见度 - 'pressure': '1013' # 没有气压,用默认值 + 'pressure': '1013', # 没有气压,用默认值 + 'life_tips': life_tips # 添加生活提示信息 } print(f"解析后的天气信息: {weather_info}") return weather_info @@ -699,7 +758,8 @@ class WeatherAPI: weather_data = { 'city': city_name, 'current': current, - 'forecast': forecast + 'forecast': forecast, + 'life_tips': current.get('life_tips', []) # 添加生活提示信息 } return weather_data else: @@ -1153,7 +1213,8 @@ class WeatherAPI: 'humidity': weather_info['humidity'], 'wind_scale': weather_info['wind_scale'] }, - 'forecast': forecast + 'forecast': forecast, + 'life_tips': weather_info.get('life_tips', []) # 添加生活提示信息 } print(f"无法获取城市ID: {original_city_name}") diff --git a/src/word_main_window.py b/src/word_main_window.py index 9a9c78e..aa4e950 100644 --- a/src/word_main_window.py +++ b/src/word_main_window.py @@ -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("当前天气:")) + # 获取温度信息,支持嵌套结构 + 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("天气预报:")) + # 生活提示信息(替换原来的天气预报) + 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("生活提示:")) - 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()