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]