fix(gui): 修复启动后 GUI 卡死约 1.2 秒
delayed_refresh_preview 在 mainloop 启动 300ms 后同步加载大图(PIL Image.open + load + convert),阻塞主线程。改为: 1. 延迟到 3 秒后再触发预览(GUI 已完全初始化) 2. 图片加载放到后台线程,完成后通过 root.after 回主线程更新 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
96fdc96f24
commit
b1fa6af9ce
1 file changed
+50
-2
+50
-2
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user