重构添加统一异常处理

This commit is contained in:
xinzhu.yin
2026-04-20 15:34:45 +08:00
parent 2e92b48496
commit b1b14c2d49
8 changed files with 114 additions and 61 deletions

View File

@@ -283,6 +283,29 @@ class PQAutomationApp:
)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
def _dispatch_ui(self, fn, *args, **kwargs):
"""把 fn(*args, **kwargs) 调度到 Tk 主线程执行。
统一替代散落各处的 self.root.after(0, lambda: ...) 写法:
- 自动捕获异常并记录日志,避免工作线程静默丢失 UI 更新失败;
- 参数用位置/关键字传入,绕开 lambda 闭包捕获变量的常见坑;
- 允许在 UI 销毁(如关闭窗口)后安全失败。
"""
def _runner():
try:
fn(*args, **kwargs)
except Exception as exc:
log = getattr(self, "log_gui", None)
if log is not None:
try:
log.log(f"UI 调度异常: {exc}")
except Exception:
pass
try:
self.root.after(0, _runner)
except Exception:
pass
get_config_path = _cfg_get_config_path
def initialize_default_test_type(self):
"""初始化默认测试类型(在所有控件创建完成后调用)"""