From ff89cb976436f6c651e2d13edd019d764a3d70e9 Mon Sep 17 00:00:00 2001 From: jimmy-sketch Date: Sat, 22 Nov 2025 08:18:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=B5=AE=E7=AA=97?= =?UTF-8?q?=E4=B8=8D=E6=98=BE=E7=A4=BA=E6=96=87=E5=AD=97=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E6=B7=B1=E8=89=B2/=E6=B5=85=E8=89=B2=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=AD=97=E4=BD=93=E9=A2=9C=E8=89=B2=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification/notification_service.py | 107 ++++++++++++++++++ app/tools/result_display.py | 31 ++++- 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/app/common/notification/notification_service.py b/app/common/notification/notification_service.py index 620a5d78..9523d578 100644 --- a/app/common/notification/notification_service.py +++ b/app/common/notification/notification_service.py @@ -36,6 +36,37 @@ def update_content(self, widgets): self.layout.addWidget(widget) self.content_widgets.append(widget) + # 确保新添加的 BodyLabel 可见:根据主题强制设置前景色 + try: + from app.tools.personalised import is_dark_theme + from qfluentwidgets import qconfig + from qfluentwidgets import BodyLabel as QFBodyLabel + + fg = "#ffffff" if is_dark_theme(qconfig) else "#000000" + + def apply_fg_to(w): + # 如果是直接的 BodyLabel,设置样式 + if isinstance(w, QFBodyLabel): + existing = w.styleSheet() or "" + if "color:" not in existing: + # 保留已有样式,追加颜色 + w.setStyleSheet(existing + f" color: {fg};") + else: + # 遍历子控件查找 BodyLabel + for child in w.findChildren(QFBodyLabel): + existing = child.styleSheet() or "" + if "color:" not in existing: + child.setStyleSheet(existing + f" color: {fg};") + + for w in self.content_widgets: + try: + apply_fg_to(w) + except Exception: + pass + except Exception: + # 忽略主题检测错误,保持原样 + pass + class NotificationWindowTemplate(PageTemplate): """通知窗口页面模板""" @@ -69,6 +100,14 @@ def __init__(self, parent=None): # 设置UI self.setup_ui() + # 订阅主题变化,确保切换主题时更新文字颜色 + try: + from qfluentwidgets import qconfig + + qconfig.themeChanged.connect(self._on_theme_changed) + except Exception: + pass + # 设置窗口圆角 self.setBorderRadius(15) @@ -258,6 +297,18 @@ def apply_settings(self, settings=None): # 设置透明度(背景和字体透明度统一) self.setWindowOpacity(transparency) + # 设置倒计时标签颜色,确保与背景对比 + try: + from app.tools.personalised import is_dark_theme + from qfluentwidgets import qconfig + + fg = "#ffffff" if is_dark_theme(qconfig) else "#000000" + existing = self.countdown_label.styleSheet() or "" + if "color:" not in existing: + self.countdown_label.setStyleSheet(existing + f" color: {fg};") + except Exception: + pass + # 根据设置定位窗口 self.position_window(settings) @@ -287,6 +338,51 @@ def update_countdown_display(self): self.countdown_timer.stop() self.countdown_label.setText("连续点击3次关闭窗口") + def _on_theme_changed(self): + """主题切换时更新浮窗内文字和背景颜色""" + try: + from app.tools.personalised import is_dark_theme + from qfluentwidgets import qconfig + + fg = "#ffffff" if is_dark_theme(qconfig) else "#000000" + + # 更新所有 BodyLabel 子控件颜色 + for lbl in self.findChildren(BodyLabel): + try: + existing = lbl.styleSheet() or "" + parts = [ + p.strip() + for p in existing.split(";") + if p.strip() and not p.strip().startswith("color:") + ] + parts.append(f"color: {fg} !important") + lbl.setStyleSheet("; ".join(parts) + ";") + except Exception: + continue + + # 更新倒计时标签颜色 + try: + existing = self.countdown_label.styleSheet() or "" + parts = [ + p.strip() + for p in existing.split(";") + if p.strip() and not p.strip().startswith("color:") + ] + parts.append(f"color: {fg} !important") + self.countdown_label.setStyleSheet("; ".join(parts) + ";") + except Exception: + pass + + # 更新背景与拖动线样式 + try: + self.update_background_style() + self.update_drag_line_style() + self.update_drag_line_container_style() + except Exception: + pass + except Exception: + pass + def _get_screen_from_settings(self, settings): """根据设置获取屏幕""" screen = QApplication.primaryScreen() @@ -540,6 +636,11 @@ def start_show_animation(self, settings=None): # 立即更新倒计时显示(显示"正在抽取中") self.update_countdown_display() + # 确保颜色与当前主题同步 + try: + self._on_theme_changed() + except Exception: + pass def on_animation_finished(self): """动画完成后的处理""" @@ -616,6 +717,12 @@ def update_content(self, student_labels, settings=None): for label in student_labels: self.content_layout.addWidget(label) + # 确保颜色与当前主题同步 + try: + self._on_theme_changed() + except Exception: + pass + # 调整窗口大小以适应内容 self.adjustSize() diff --git a/app/tools/result_display.py b/app/tools/result_display.py index 204db1b8..2b94a9ba 100644 --- a/app/tools/result_display.py +++ b/app/tools/result_display.py @@ -191,9 +191,21 @@ def _apply_label_style(label, font_size, animation_color): style_sheet = f"font-size: {font_size}pt; " if animation_color == 1: - style_sheet += f"color: {ResultDisplayUtils._generate_vibrant_color()};" + style_sheet += f"color: {ResultDisplayUtils._generate_vibrant_color()} !important;" elif animation_color == 2: - style_sheet += f"color: {fixed_color};" + style_sheet += f"color: {fixed_color} !important;" + else: + try: + from app.tools.personalised import is_dark_theme + from qfluentwidgets import qconfig + + default_color = ( + "#ffffff" if is_dark_theme(qconfig) else "#000000" + ) + style_sheet += f"color: {default_color} !important;" + except Exception: + # 兜底使用黑色 + style_sheet += "color: #000000 !important;" widget.setStyleSheet(style_sheet) else: @@ -203,9 +215,20 @@ def _apply_label_style(label, font_size, animation_color): "roll_call_settings", "animation_fixed_color" ) if animation_color == 1: - style_sheet += f"color: {ResultDisplayUtils._generate_vibrant_color()};" + style_sheet += ( + f"color: {ResultDisplayUtils._generate_vibrant_color()} !important;" + ) elif animation_color == 2: - style_sheet += f"color: {fixed_color};" + style_sheet += f"color: {fixed_color} !important;" + else: + try: + from app.tools.personalised import is_dark_theme + from qfluentwidgets import qconfig + + default_color = "#ffffff" if is_dark_theme(qconfig) else "#000000" + style_sheet += f"color: {default_color} !important;" + except Exception: + style_sheet += "color: #000000 !important;" label.setStyleSheet(style_sheet) From edcff82b52de4f04df5b61014991b70ee045ca9a Mon Sep 17 00:00:00 2001 From: jimmy-sketch Date: Sat, 22 Nov 2025 11:55:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=E5=A4=84=E7=90=86=E6=8E=89=E8=8B=A5?= =?UTF-8?q?=E5=B9=B2try-expect-pass=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification/notification_service.py | 80 +++++-- app/page_building/page_template.py | 13 +- app/page_building/window_template.py | 8 +- app/tools/history.py | 7 +- app/view/another_window/remaining_list.py | 199 +++++++++++++----- app/view/main/roll_call.py | 17 +- .../floating_window_management.py | 18 +- .../sidebar_tray_management.py | 29 ++- app/view/settings/settings.py | 32 ++- main.py | 78 ++++++- 10 files changed, 365 insertions(+), 116 deletions(-) diff --git a/app/common/notification/notification_service.py b/app/common/notification/notification_service.py index 9523d578..27e8f840 100644 --- a/app/common/notification/notification_service.py +++ b/app/common/notification/notification_service.py @@ -61,8 +61,10 @@ def apply_fg_to(w): for w in self.content_widgets: try: apply_fg_to(w) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error applying fg to content widget: {}", e) except Exception: # 忽略主题检测错误,保持原样 pass @@ -105,8 +107,10 @@ def __init__(self, parent=None): from qfluentwidgets import qconfig qconfig.themeChanged.connect(self._on_theme_changed) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error connecting themeChanged signal (ignored): {}", e) # 设置窗口圆角 self.setBorderRadius(15) @@ -237,7 +241,10 @@ def update_background_style(self): ) self.update_drag_line_style() self.update_drag_line_container_style() - except: + except Exception as e: + from loguru import logger + + logger.exception("Error updating background style (fallback used): {}", e) # 如果无法获取主题信息,默认使用白色背景和深色拖动线 background_color = "#ffffff" self.background_widget.setStyleSheet( @@ -266,7 +273,12 @@ def update_drag_line_container_style(self): self.drag_line_container.setStyleSheet( f"background-color: {background_color}; border-top-left-radius: 15px; border-top-right-radius: 15px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px;" ) - except: + except Exception as e: + from loguru import logger + + logger.exception( + "Error updating drag line container style (fallback used): {}", e + ) # 如果无法获取主题信息,默认使用白色背景 self.drag_line_container.setStyleSheet( "background-color: #ffffff; border-top-left-radius: 15px; border-top-right-radius: 15px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px;" @@ -306,8 +318,10 @@ def apply_settings(self, settings=None): existing = self.countdown_label.styleSheet() or "" if "color:" not in existing: self.countdown_label.setStyleSheet(existing + f" color: {fg};") - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error setting countdown label color: {}", e) # 根据设置定位窗口 self.position_window(settings) @@ -357,7 +371,12 @@ def _on_theme_changed(self): ] parts.append(f"color: {fg} !important") lbl.setStyleSheet("; ".join(parts) + ";") - except Exception: + except Exception as e: + from loguru import logger + + logger.exception( + "Error applying color to label child (continuing): {}", e + ) continue # 更新倒计时标签颜色 @@ -370,7 +389,12 @@ def _on_theme_changed(self): ] parts.append(f"color: {fg} !important") self.countdown_label.setStyleSheet("; ".join(parts) + ";") - except Exception: + except Exception as e: + from loguru import logger + + logger.exception( + "Error applying countdown label color (ignored): {}", e + ) pass # 更新背景与拖动线样式 @@ -378,9 +402,17 @@ def _on_theme_changed(self): self.update_background_style() self.update_drag_line_style() self.update_drag_line_container_style() - except Exception: + except Exception as e: + from loguru import logger + + logger.exception( + "Error updating background/drag line styles (ignored): {}", e + ) pass - except Exception: + except Exception as e: + from loguru import logger + + logger.exception("Error in _on_theme_changed (ignored): {}", e) pass def _get_screen_from_settings(self, settings): @@ -639,8 +671,10 @@ def start_show_animation(self, settings=None): # 确保颜色与当前主题同步 try: self._on_theme_changed() - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error syncing theme on show (ignored): {}", e) def on_animation_finished(self): """动画完成后的处理""" @@ -662,9 +696,10 @@ def on_animation_finished(self): try: if not (self.windowFlags() & Qt.WindowDoesNotAcceptFocus): self.activateWindow() - except Exception: - # 保险兜底:如果出现问题则不激活窗口 - pass + except Exception as e: + from loguru import logger + + logger.exception("Error activating window (ignored): {}", e) # 更新倒计时显示 self.update_countdown_display() @@ -720,8 +755,10 @@ def update_content(self, student_labels, settings=None): # 确保颜色与当前主题同步 try: self._on_theme_changed() - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error syncing theme on update_content (ignored): {}", e) # 调整窗口大小以适应内容 self.adjustSize() @@ -780,7 +817,10 @@ def get_notification_title(self): return get_content_name_async( "notification_settings", "notification_result" ) - except: + except Exception as e: + from loguru import logger + + logger.exception("Error getting notification title (fallback used): {}", e) # 如果无法获取多语言文本,则使用默认文本 return "通知结果" diff --git a/app/page_building/page_template.py b/app/page_building/page_template.py index d3eebcc1..3f272274 100644 --- a/app/page_building/page_template.py +++ b/app/page_building/page_template.py @@ -10,6 +10,7 @@ from PySide6.QtGui import * from PySide6.QtCore import * from PySide6.QtNetwork import * +import loguru from qfluentwidgets import * from app.tools.variable import * @@ -108,10 +109,12 @@ def create_content(self): self.content_created = True elapsed = time.perf_counter() - start - logger.debug(f"创建内容组件 {content_name} 耗时: {elapsed:.3f}s") + loguru.logger.debug(f"创建内容组件 {content_name} 耗时: {elapsed:.3f}s") except Exception as e: elapsed = time.perf_counter() - start - logger.error(f"创建内容组件失败 ({elapsed:.3f}s): {e}") + from loguru import logger + + logger.exception(f"创建内容组件失败 ({elapsed:.3f}s): {e}") def create_empty_content(self, message="该页面正在开发中,敬请期待!"): """创建空页面内容""" @@ -437,8 +440,10 @@ def load_all_pages(self, interval_ms: int = 50, max_per_tick: int = 5): ] ), ) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error scheduling batch page loads (ignored): {}", e) def on_current_index_changed(self, index: int): """堆叠窗口索引改变时的处理""" diff --git a/app/page_building/window_template.py b/app/page_building/window_template.py index 2b799676..729536e2 100644 --- a/app/page_building/window_template.py +++ b/app/page_building/window_template.py @@ -169,7 +169,13 @@ def _apply_current_theme(self) -> None: self.default_page.setStyleSheet( "background-color: transparent;" ) - except: + except Exception as e: + from loguru import logger + + logger.exception( + "Error detecting dark mode with darkdetect (fallback to light): {}", + e, + ) # 如果检测失败,使用浅色主题 self.setStyleSheet("background-color: #ffffff;") self.default_page.setStyleSheet("background-color: transparent;") diff --git a/app/tools/history.py b/app/tools/history.py index be4b583f..869ebb78 100644 --- a/app/tools/history.py +++ b/app/tools/history.py @@ -450,7 +450,12 @@ def calculate_weight(students_data: list, class_name: str) -> list: current_time = datetime.now() days_diff = (current_time - last_time).days time_factor = min(1.0, days_diff / 30.0) * 0.5 - except: + except Exception as e: + from loguru import logger + + logger.exception( + "Error calculating time factor for student weights: {}", e + ) time_factor = 0.0 else: time_factor = 0.0 diff --git a/app/view/another_window/remaining_list.py b/app/view/another_window/remaining_list.py index 166f6916..c3f1dac8 100644 --- a/app/view/another_window/remaining_list.py +++ b/app/view/another_window/remaining_list.py @@ -174,12 +174,17 @@ def run(self): # 发送结果回主线程 self.finished.emit(filtered_students) - except Exception: + except Exception as e: + from loguru import logger + + logger.exception("Error loading students in StudentLoader.run: {}", e) # 出错时返回空列表 try: self.finished.emit([]) - except Exception: - pass + except Exception as inner_e: + logger.exception( + "Error emitting finished signal with empty list: {}", inner_e + ) class RemainingListPage(QWidget): @@ -209,7 +214,10 @@ def __init__(self, parent=None): # 减少每次创建卡片时的重复开销 try: self._font_family = load_custom_font() - except Exception: + except Exception as e: + from loguru import logger + + logger.exception("Failed to load custom font: {}", e) self._font_family = None # 预先设置为空;init_ui 中会尝试异步预取模板文本 self._student_info_text = None @@ -296,8 +304,10 @@ def load_student_data(self): and self._loading_thread.isRunning() ): return - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error loading remaining list data: {}", e) students_file = self.get_students_file() # 使用 StudentLoader 在后台处理 I/O 和筛选 @@ -327,8 +337,10 @@ def _on_students_loaded(self, students_list): # 清理线程引用 if hasattr(self, "_loading_thread"): self._loading_thread = None - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error handling student group processing: {}", e) def update_ui(self): """更新UI显示""" @@ -434,18 +446,28 @@ def update_layout(self): # 恢复更新 try: self.setUpdatesEnabled(True) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error processing student in StudentLoader: {}", e) try: if top_win is not None: top_win.setUpdatesEnabled(True) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error re-enabling updates on top window (ignored): {}", e + ) try: # 触发一次完整刷新 self.update() - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error calling update() after layout update (ignored): {}", e + ) def _calculate_columns(self, width: int) -> int: """根据窗口宽度和卡片尺寸动态计算列数""" @@ -462,7 +484,10 @@ def _calculate_columns(self, width: int) -> int: # 至少显示1列,且不超过一个合理上限 return max(1, min(int(max_cols), 6)) - except Exception: + except Exception as e: + from loguru import logger + + logger.exception("Error calculating columns (fallback to 1): {}", e) return 1 def _start_incremental_render(self): @@ -505,8 +530,12 @@ def run(self): try: if getattr(self.reporter, "cancel_requested", False): break - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error checking reporter cancel flag (ignored): {}", e + ) batch = [] for _ in range(self.batch_size): if not self.students: @@ -539,31 +568,56 @@ def run(self): try: if getattr(self.reporter, "cancel_requested", False): break - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error checking reporter cancel flag before emit (ignored): {}", + e, + ) try: self.reporter.batch_ready.emit(batch) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error emitting batch_ready (ignored): {}", e + ) try: self.reporter.finished.emit() - except Exception: - pass - except Exception: + except Exception as e: + from loguru import logger + + logger.exception("Error emitting finished (ignored): {}", e) + except Exception as e: + from loguru import logger + + logger.exception("Unhandled error in StudentRenderTask.run: {}", e) try: self.reporter.finished.emit() - except Exception: - pass + except Exception as inner_e: + from loguru import logger + + logger.exception( + "Error emitting finished after exception: {}", inner_e + ) # 请求取消之前正在运行的渲染任务(如果存在) try: if self._rendering and self._render_reporter is not None: try: self._render_reporter.cancel_requested = True - except Exception: - pass - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error requesting cancel on previous render reporter (ignored): {}", + e, + ) + except Exception as e: + from loguru import logger + + logger.exception("Error in RemainingListPage initialization: {}", e) self._render_reporter = reporter task = StudentRenderTask( @@ -587,8 +641,13 @@ def _on_batch_ready(self, reporter, batch: list): try: if getattr(reporter, "cancel_requested", False): return - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error checking reporter cancel_requested flag in _on_batch_ready (ignored): {}", + e, + ) if not batch: return @@ -611,8 +670,10 @@ def _on_batch_ready(self, reporter, batch: list): try: if card.parent() is not None and card.parent() is not self: card.setParent(None) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error resetting card parent (ignored): {}", e) self.cards.append(card) self._cards_set.add(key) @@ -628,8 +689,12 @@ def _on_batch_ready(self, reporter, batch: list): try: if self.grid_layout.indexOf(card) != -1: continue - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error checking grid_layout.indexOf (ignored): {}", e + ) row = i // columns col = i % columns @@ -642,14 +707,27 @@ def _on_batch_ready(self, reporter, batch: list): if existing_widget is not None and existing_widget is not card: try: self.grid_layout.removeWidget(existing_widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error removing existing widget from grid (ignored): {}", + e, + ) try: existing_widget.hide() - except Exception: - pass - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error hiding existing widget (ignored): {}", e + ) + except Exception as e: + from loguru import logger + + logger.exception( + "Error handling existing widget in grid (ignored): {}", e + ) try: self.grid_layout.addWidget(card, row, col) @@ -660,16 +738,23 @@ def _on_batch_ready(self, reporter, batch: list): for col in range(columns): self.grid_layout.setColumnStretch(col, 1) - except Exception: - logger.exception("增量渲染时布局更新失败") + except Exception as e: + from loguru import logger + + logger.exception("增量渲染时布局更新失败: {}", e) def _on_render_finished(self, reporter): """后台渲染完成后的槽,接收 reporter 用于忽略过期任务""" try: if getattr(reporter, "cancel_requested", False): return - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error checking reporter cancel_requested in _on_render_finished (ignored): {}", + e, + ) self._rendering = False self._pending_students = [] @@ -683,8 +768,12 @@ def _finalize_render(self): if self._render_timer is not None: self._render_timer.stop() self._render_timer = None - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error stopping render timer in _finalize_render (ignored): {}", e + ) self._rendering = False @@ -705,14 +794,20 @@ def _clear_grid_layout(self): if widget: try: self.grid_layout.removeWidget(widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error removing widget from grid during clear (ignored): {}", e + ) widget.hide() # 清空已记录的已添加卡片集合 try: self._cards_set.clear() - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error clearing cards set (ignored): {}", e) def create_student_card(self, student: Dict[str, Any]) -> CardWidget: """创建学生卡片 diff --git a/app/view/main/roll_call.py b/app/view/main/roll_call.py index f5da28f7..b89d0e8f 100644 --- a/app/view/main/roll_call.py +++ b/app/view/main/roll_call.py @@ -414,8 +414,12 @@ def start_draw(self): self.start_button.setEnabled(True) try: self.start_button.clicked.disconnect() - except: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error disconnecting start_button clicked (ignored): {}", e + ) self.draw_random() animation = readme_settings_async("roll_call_settings", "animation") autoplay_count = readme_settings_async("roll_call_settings", "autoplay_count") @@ -468,8 +472,13 @@ def stop_animation(self): self.is_animating = False try: self.start_button.clicked.disconnect() - except: - pass + except Exception as e: + from loguru import logger + + logger.exception( + "Error disconnecting start_button clicked during stop_animation (ignored): {}", + e, + ) self.start_button.clicked.connect(lambda: self.start_draw()) half_repeat = readme_settings_async("roll_call_settings", "half_repeat") diff --git a/app/view/settings/custom_settings/floating_window_management.py b/app/view/settings/custom_settings/floating_window_management.py index 49756ad7..055bbc2f 100644 --- a/app/view/settings/custom_settings/floating_window_management.py +++ b/app/view/settings/custom_settings/floating_window_management.py @@ -85,8 +85,10 @@ def _create_deferred(self, name: str): if placeholder is None: try: self.vBoxLayout.addWidget(real_widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error handling floating window action: {}", e) setattr(self, name, real_widget) return @@ -119,8 +121,10 @@ def _create_deferred(self, name: str): except Exception: try: self.vBoxLayout.addWidget(real_widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error in floating window sub-action: {}", e) setattr(self, name, real_widget) return @@ -131,8 +135,10 @@ def _create_deferred(self, name: str): try: self.vBoxLayout.addWidget(real_widget) setattr(self, name, real_widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error reading floating window settings: {}", e) # ================================================== diff --git a/app/view/settings/custom_settings/sidebar_tray_management.py b/app/view/settings/custom_settings/sidebar_tray_management.py index 6575631a..8395faa3 100644 --- a/app/view/settings/custom_settings/sidebar_tray_management.py +++ b/app/view/settings/custom_settings/sidebar_tray_management.py @@ -57,8 +57,10 @@ def make_placeholder(attr_name: str): try: for i, name in enumerate(list(self._deferred_factories.keys())): QTimer.singleShot(120 * i, lambda n=name: self._create_deferred(n)) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error reading tray management settings: {}", e) def _create_deferred(self, name: str): factories = getattr(self, "_deferred_factories", {}) @@ -77,8 +79,10 @@ def _create_deferred(self, name: str): if placeholder is None: try: self.vBoxLayout.addWidget(real_widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error handling tray action: {}", e) setattr(self, name, real_widget) return @@ -110,20 +114,27 @@ def _create_deferred(self, name: str): except Exception: try: self.vBoxLayout.addWidget(real_widget) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error in tray sub-action: {}", e) setattr(self, name, real_widget) return try: layout.addWidget(real_widget) setattr(self, name, real_widget) - except Exception: + except Exception as e: try: self.vBoxLayout.addWidget(real_widget) setattr(self, name, real_widget) - except Exception: - pass + except Exception as inner_e: + from loguru import logger + + logger.exception( + "Error adding real_widget as fallback in sidebar_tray_management: {}", + inner_e, + ) class sidebar_management_window(GroupHeaderCardWidget): diff --git a/app/view/settings/settings.py b/app/view/settings/settings.py index da6df086..962b81b0 100644 --- a/app/view/settings/settings.py +++ b/app/view/settings/settings.py @@ -252,20 +252,26 @@ def make_about_factory(iface=self.aboutInterface): # 在窗口显示后启动针对非 pivot 页面的后台预热(分批创建) try: QTimer.singleShot(300, lambda: self._background_warmup_non_pivot()) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error during settings warmup: {}", e) # 连接堆叠窗口切换信号,在首次切换到占位时创建真实页面 try: self.stackedWidget.currentChanged.connect(self._on_stacked_widget_changed) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error creating deferred page: {}", e) # 在窗口显示后启动后台预热,分批创建其余页面,避免一次性阻塞 try: QTimer.singleShot(300, lambda: self._background_warmup_pages()) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error scheduling background warmup pages: {}", e) def _on_stacked_widget_changed(self, index: int): """当导航切换到某个占位页时,按需创建真实页面内容""" @@ -295,8 +301,10 @@ def _on_stacked_widget_changed(self, index: int): QTimer.singleShot( 50, lambda rp=real_page: rp.load_all_pages() ) - except Exception: - pass + except Exception as e: + from loguru import logger + + logger.exception("Error in deferred page creation step: {}", e) logger.debug(f"设置页面已按需创建: {name}") except Exception as e: logger.error(f"延迟创建设置页面 {name} 失败: {e}") @@ -326,7 +334,13 @@ def _background_warmup_pages( ] pivot = [n for n in names if meta.get(n, {}).get("is_pivot", False)] ordered = non_pivot + pivot - except Exception: + except Exception as e: + from loguru import logger + + logger.exception( + "Error ordering deferred factories (fallback to original order): {}", + e, + ) ordered = names # 仅预热有限数量的页面,避免一次性占用主线程 diff --git a/main.py b/main.py index ac9bd4b3..428cf162 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from PySide6.QtCore import * from PySide6.QtWidgets import * from PySide6.QtNetwork import * +import loguru from qfluentwidgets import * from loguru import logger @@ -214,7 +215,9 @@ def update_widget_fonts(widget, font, font_family): updated = True return updated except Exception as e: - logger.error(f"更新控件字体时发生异常: {e}") + from loguru import logger + + logger.exception("更新控件字体时发生异常: {}", e) return False @@ -233,9 +236,11 @@ def start_main_window(): main_window.show() try: elapsed = time.perf_counter() - app_start_time - logger.debug(f"主窗口创建并显示完成,启动耗时: {elapsed:.3f}s") - except Exception: - pass + loguru.logger.debug(f"主窗口创建并显示完成,启动耗时: {elapsed:.3f}s") + except Exception as e: + from loguru import logger + + logger.exception("Error calculating elapsed startup time (ignored): {}", e) except Exception as e: logger.error(f"创建主窗口失败: {e}", exc_info=True) @@ -393,17 +398,70 @@ def main_async(): print(f"应用程序启动失败: {e}") try: logger.error(f"应用程序启动失败: {e}", exc_info=True) - except: - pass + except Exception as log_e: + try: + from loguru import logger as _logger + + _logger.exception("Failed to log startup error: {}", log_e) + except Exception as inner_log_e: + try: + from loguru import logger + + logger.exception("Failed to log logging failure: {}", inner_log_e) + except Exception as final_e: + try: + import sys + + print( + f"Failed to log logging failure: {final_e}", file=sys.stderr + ) + except Exception as e: + try: + import sys + + print( + f"Failed to print logging failure: {e}", file=sys.stderr + ) + except Exception as final_e: + try: + import sys + + print( + f"Final logging fallback failed: {final_e}", + file=sys.stderr, + ) + except Exception as e: + try: + import sys + + print( + f"Final logging fallback failed to print: {e}", + file=sys.stderr, + ) + except Exception as eee: + try: + import sys + + sys.stderr.write( + f"Final logging fallback failed to print: {eee}\n" + ) + except Exception: + _ = None # 程序异常退出时释放共享内存 try: shared_memory.detach() - except: - pass + except Exception as detach_e: + from loguru import logger + + logger.exception( + "Error detaching shared memory during shutdown: {}", detach_e + ) # 关闭本地服务器 try: if local_server: local_server.close() - except: - pass + except Exception as close_e: + from loguru import logger + + logger.exception("Error closing local server during shutdown: {}", close_e) sys.exit(1)