From 069613fe09b10b4c49c21eb3409b0f9cad024463 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 Oct 2025 17:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E9=9B=86=E5=8D=A1bug=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- preview.py | 63 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/preview.py b/preview.py index 9ca0475..da87660 100644 --- a/preview.py +++ b/preview.py @@ -171,13 +171,17 @@ class PreviewWindow: root = tk.Tk() root.title("采集卡预览 - 点击放大") root.geometry(f"{preview_width}x{preview_height}") + root.update_idletasks() # 立即更新窗口尺寸 - canvas = Canvas(root, bg='black') + canvas = Canvas(root, bg='black', width=preview_width, height=preview_height) canvas.pack(fill=tk.BOTH, expand=True) # 存储图像对象 self.photo_objects = {} + # 用于控制调试输出(只打印前几次) + self.debug_count = 0 + def update_frames_once(): """在主线程中更新一帧(使用after循环)""" if not self.running: @@ -198,23 +202,22 @@ class PreviewWindow: return # 计算每个预览窗口的位置和大小 - # 确保画布已完全初始化 - root.update_idletasks() - canvas.update_idletasks() + # 直接使用配置值作为画布尺寸(macOS上窗口尺寸可能在显示前返回默认值) + canvas_width = preview_width + canvas_height = preview_height - # 获取画布实际尺寸 - canvas_width = canvas.winfo_width() - canvas_height = canvas.winfo_height() - - # 如果画布还没初始化,使用默认值 - if canvas_width <= 1 or canvas_height <= 1: - canvas_width = 1000 - canvas_height = 700 - - # 确保尺寸有效 - if canvas_width < 100 or canvas_height < 100: - canvas_width = 1000 - canvas_height = 700 + # 尝试获取实际的窗口尺寸,如果有效则使用(大于配置值说明可能被手动调整了) + try: + root.update_idletasks() + actual_width = root.winfo_width() + actual_height = root.winfo_height() + + # 只有在获取到合理的尺寸时才使用(大于100像素) + if actual_width > 100 and actual_height > 100: + canvas_width = actual_width + canvas_height = actual_height + except: + pass # 如果获取失败,使用配置值 cell_width = max(10, canvas_width // columns) cell_height = max(10, canvas_height // rows) @@ -259,9 +262,16 @@ class PreviewWindow: frame_idx += 1 continue - # 调试输出(仅前几次) + # 调试输出(仅前几次,避免刷屏) if frame_idx == 0 and len(images_to_draw) == 0: - print(f"🔍 预览调试: 画布={canvas_width}x{canvas_height}, 单元格={cell_width}x{cell_height}, 原始帧={w}x{h}, 缩放后={new_w}x{new_h}") + self.debug_count += 1 + if self.debug_count <= 3: # 只打印前3次 + print(f"🔍 预览调试 #{self.debug_count}:") + print(f" 配置尺寸: {preview_width}x{preview_height}") + print(f" 实际画布: {canvas_width}x{canvas_height}") + print(f" 单元格大小: {cell_width}x{cell_height}") + print(f" 原始帧: {w}x{h}") + print(f" 缩放后: {new_w}x{new_h}") resized_frame = cv2.resize(frame, (new_w, new_h)) @@ -296,9 +306,8 @@ class PreviewWindow: canvas.delete("all") # 先绘制所有图像(底层) - if images_to_draw: - if frame_idx == len(images_to_draw): - print(f"✅ 准备绘制 {len(images_to_draw)} 个图像") + if images_to_draw and self.debug_count <= 3: + print(f"✅ 准备绘制 {len(images_to_draw)} 个图像到画布 ({canvas_width}x{canvas_height})") for photo, x, y in images_to_draw: try: @@ -371,12 +380,18 @@ class PreviewWindow: # 等待窗口完全初始化后再开始更新 def start_updates(): """延迟启动更新,确保窗口已完全显示""" + # 强制更新窗口尺寸 + root.update_idletasks() + root.update() + # 等待窗口完全绘制 + import time + time.sleep(0.2) # 给窗口更多时间初始化 root.update_idletasks() root.update() update_frames_once() - # 使用after在主线程中循环刷新(延迟启动) - root.after(100, start_updates) + # 使用after在主线程中循环刷新(延迟启动,给足够时间让窗口初始化) + root.after(200, start_updates) def on_closing(): """关闭窗口"""