From b1fa6af9ce5245e9dcbcab06f6c2231b848e715c Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 23 May 2026 21:58:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(gui):=20=E4=BF=AE=E5=A4=8D=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=90=8E=20GUI=20=E5=8D=A1=E6=AD=BB=E7=BA=A6=201.2=20?= =?UTF-8?q?=E7=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit delayed_refresh_preview 在 mainloop 启动 300ms 后同步加载大图(PIL Image.open + load + convert),阻塞主线程。改为: 1. 延迟到 3 秒后再触发预览(GUI 已完全初始化) 2. 图片加载放到后台线程,完成后通过 root.after 回主线程更新 Co-Authored-By: Claude Sonnet 4.5 --- gui/gui_app.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/gui/gui_app.py b/gui/gui_app.py index 7d408c6..ef6c007 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -5376,9 +5376,57 @@ def main(): def delayed_refresh_preview() -> None: if image_var.get(): - refresh_preview() + _async_refresh_preview() - root.after(300, delayed_refresh_preview) # type: ignore[arg-type] + def _async_refresh_preview() -> None: + """后台加载图片预览,避免阻塞主线程导致 GUI 卡顿。""" + name = image_var.get().strip() + if not name: + return + name_normalized = name.replace("/", os.sep) + path = os.path.join(images_dir, name_normalized) + if not os.path.exists(path): + return + + def _worker(): + try: + img = Image.open(path) + img.load() + img = img.convert("RGB") + except Exception: + return + + def _apply(): + try: + preview_body.update_idletasks() + w = preview_body.winfo_width() + h = preview_body.winfo_height() + if w <= 1 or h <= 1: + w, h = 900, 520 + max_w = max(300, w - 20) + max_h = max(200, h - 20) + try: + resample = Image.Resampling.LANCZOS + except AttributeError: + resample = 1 + img.thumbnail((max_w, max_h), resample) + photo = ImageTk.PhotoImage(img) + _img_ref["photo"] = photo + image_label.configure(image=photo) + preview_status.set( + f"{os.path.basename(path)} {img.width}x{img.height}" + ) + except Exception: + pass + + try: + root.after(0, _apply) + except Exception: + pass + + threading.Thread(target=_worker, daemon=True).start() + + root.after(3000, delayed_refresh_preview) # type: ignore[arg-type] root.after(500, refresh_stats) # type: ignore[arg-type] root.after(500, refresh_reservations) # type: ignore[arg-type] root.after(500, refresh_high_value_rooms) # type: ignore[arg-type]