天气和每日一言修复成功

pull/40/head
Horse861 4 months ago
parent 5087b70a4e
commit 7f49a548d7

@ -231,7 +231,7 @@ class MainWindow(QMainWindow):
view_menu.addAction(self.quote_action)
# 显示天气信息动作
self.weather_action = QAction('显示天气信息', self)
self.weather_action = QAction('显示天气', self)
self.weather_action.setCheckable(True)
self.weather_action.setChecked(True)
self.weather_action.triggered.connect(self.toggleWeatherDisplay)

@ -229,7 +229,7 @@ class WordRibbon(QFrame):
quote_layout = QVBoxLayout()
# 每日一言显示标签
self.quote_label = QLabel("每日一言: 暂无")
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)
@ -244,6 +244,10 @@ class WordRibbon(QFrame):
quote_group.setLayout(quote_layout)
self.quote_group = quote_group
# 组件创建完成后自动获取每日一言
self.load_daily_quote()
return quote_group
def show_quote_group(self):
@ -296,14 +300,52 @@ class WordRibbon(QFrame):
"""城市选择变化处理"""
pass
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)
else:
# 如果API返回空或格式不正确显示默认文本
self.update_quote_display("暂无每日一言")
except Exception as e:
print(f"加载每日一言失败: {e}")
self.update_quote_display("暂无每日一言")
def on_refresh_quote(self):
"""刷新每日一言按钮点击处理"""
pass
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)
else:
# 如果API返回空或格式不正确显示默认文本
self.update_quote_display("获取每日一言失败")
except Exception as e:
print(f"获取每日一言失败: {e}")
self.update_quote_display("获取每日一言失败")
def update_quote_display(self, quote_text):
"""更新每日一言显示"""
if hasattr(self, 'quote_label') and self.quote_label:
self.quote_label.setText(f"每日一言: {quote_text}")
self.quote_label.setText(f"{quote_text}")
def create_ribbon_button(self, text, shortcut, icon_name):
"""创建功能区按钮"""

@ -337,8 +337,8 @@ class WordStyleMainWindow(QMainWindow):
view_menu.addSeparator()
# 天气功能
weather_menu = view_menu.addMenu('天气信息')
# 附加工具功能
weather_menu = view_menu.addMenu('附加工具')
# 显示天气工具组
self.show_weather_tools_action = QAction('显示天气工具', self)
@ -612,7 +612,7 @@ class WordStyleMainWindow(QMainWindow):
print(f"接收到天气数据: {weather_data}")
if 'error' in weather_data:
print(f"天气显示错误: {weather_data['error']}")
self.status_bar.showMessage(f"天气信息获取失败: {weather_data['error']}", 3000)
self.status_bar.showMessage(f"天气数据获取失败: {weather_data['error']}", 3000)
else:
city = weather_data.get('city', '未知城市')
temp = weather_data.get('temperature', 'N/A')
@ -654,9 +654,9 @@ class WordStyleMainWindow(QMainWindow):
'forecast': weather_data['forecast']
}
self.update_weather_display(formatted_data)
self.status_bar.showMessage("天气信息已刷新", 2000)
self.status_bar.showMessage("天气数据已刷新", 2000)
else:
self.status_bar.showMessage("天气信息刷新失败请检查API密钥", 3000)
self.status_bar.showMessage("天气数据刷新失败请检查API密钥", 3000)
except Exception as e:
self.status_bar.showMessage(f"天气刷新失败: {str(e)}", 3000)
@ -666,14 +666,14 @@ class WordStyleMainWindow(QMainWindow):
# 检查是否有天气数据
if not hasattr(self, 'current_weather_data') or not self.current_weather_data:
QMessageBox.information(self, "天气信息", "暂无天气数据,请先刷新天气信息")
QMessageBox.information(self, "附加工具", "暂无天气数据,请先刷新天气信息")
return
weather_data = self.current_weather_data
# 创建对话框
dialog = QDialog(self)
dialog.setWindowTitle("详细天气信息")
dialog.setWindowTitle("详细天气")
dialog.setMinimumWidth(400)
layout = QVBoxLayout()
@ -765,11 +765,10 @@ class WordStyleMainWindow(QMainWindow):
self.status_bar.showMessage("每日一言工具已隐藏", 2000)
def refresh_daily_quote(self):
"""刷新每日一言 - 使用线程获取"""
# 创建并启动获取名言的线程
self.quote_thread = QuoteFetchThread()
self.quote_thread.quote_fetched.connect(self.on_quote_fetched)
self.quote_thread.start()
"""刷新每日一言 - 使用WordRibbon中的API"""
if hasattr(self, 'ribbon'):
# 直接调用WordRibbon中的刷新方法
self.ribbon.on_refresh_quote()
def on_quote_fetched(self, quote_data):
"""处理名言获取成功"""
@ -779,23 +778,23 @@ class WordStyleMainWindow(QMainWindow):
quote_text = f"{content}{author}"
# 更新Ribbon中的每日一言显示
if hasattr(self.ribbon, 'quote_label'):
self.ribbon.quote_label.setText(f"每日一言: {quote_text}")
if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'quote_label'):
self.ribbon.quote_label.setText(f"{quote_text}")
# 更新状态栏
self.status_bar.showMessage(f"每日名言: {quote_text}", 10000)
else:
# 处理错误情况
error_msg = quote_data.get('error', '获取名言失败')
if hasattr(self.ribbon, 'quote_label'):
self.ribbon.quote_label.setText(f"每日一言: 获取失败")
if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'quote_label'):
self.ribbon.quote_label.setText(f"获取失败")
self.status_bar.showMessage(f"每日名言获取失败: {error_msg}", 5000)
def on_quote_error(self, error_data):
"""处理名言获取错误"""
error_msg = error_data.get('error', '获取名言失败') if isinstance(error_data, dict) else str(error_data)
if hasattr(self.ribbon, 'quote_label'):
self.ribbon.quote_label.setText(f"每日一言: 获取失败")
if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'quote_label'):
self.ribbon.quote_label.setText(f"获取失败")
self.status_bar.showMessage(f"每日名言获取失败: {error_msg}", 5000)
def update_quote_display(self, quote_data):

Loading…
Cancel
Save