From ed636f68f64c84f6e0ffc23a4d0da40c637a7307 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 Oct 2025 17:42:27 +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 | 72 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/preview.py b/preview.py index da87660..ca89278 100644 --- a/preview.py +++ b/preview.py @@ -161,6 +161,10 @@ class PreviewWindow: def create_grid_window(self): """创建网格窗口""" + # 重新加载配置,确保获取最新值 + config_manager.load_config() + self.config = config_manager.config + # 获取显示配置 display = self.config.get('display', {}) preview_width = display.get('preview_width', 1000) @@ -168,6 +172,20 @@ class PreviewWindow: columns = display.get('preview_columns', 2) rows = display.get('preview_rows', 2) + # 验证配置值是否有效(防止读取到无效的小值) + if preview_width < 100 or preview_height < 100: + print(f"⚠️ 警告: 配置中的预览尺寸无效 ({preview_width}x{preview_height}),使用默认值") + preview_width = 1000 + preview_height = 700 + + if columns < 1 or columns > 10: + columns = 2 + if rows < 1 or rows > 10: + rows = 2 + + # 调试输出配置值 + print(f"📐 预览窗口配置: {preview_width}x{preview_height}, 网格: {columns}x{rows}") + root = tk.Tk() root.title("采集卡预览 - 点击放大") root.geometry(f"{preview_width}x{preview_height}") @@ -227,6 +245,10 @@ class PreviewWindow: texts_to_draw = [] frame_idx = 0 + # 确保至少有一些采集卡数据 + if not self.caps: + texts_to_draw.append((canvas_width // 2, canvas_height // 2, "未找到采集卡", 'red')) + for idx in self.caps.keys(): row = frame_idx // columns col = frame_idx % columns @@ -306,26 +328,52 @@ class PreviewWindow: canvas.delete("all") # 先绘制所有图像(底层) - if images_to_draw and self.debug_count <= 3: - print(f"✅ 准备绘制 {len(images_to_draw)} 个图像到画布 ({canvas_width}x{canvas_height})") + if self.debug_count <= 3: + print(f"📊 绘制状态: {len(images_to_draw)} 个图像, {len(texts_to_draw)} 个文本, 画布={canvas_width}x{canvas_height}") - for photo, x, y in images_to_draw: - try: - canvas.create_image(x, y, image=photo, anchor='center') - except Exception as e: - if "pyimage" not in str(e).lower(): - print(f"绘制图像错误: {e}") + if images_to_draw: + for i, (photo, x, y) in enumerate(images_to_draw): + try: + # 确保坐标在画布范围内 + if 0 <= x <= canvas_width and 0 <= y <= canvas_height: + canvas.create_image(x, y, image=photo, anchor='center') + if self.debug_count <= 3 and i == 0: + print(f" 绘制图像 #{i} 到位置 ({x}, {y})") + else: + if self.debug_count <= 3: + print(f" ⚠️ 图像 #{i} 位置 ({x}, {y}) 超出画布范围") + except Exception as e: + print(f" 绘制图像 #{i} 错误: {e}") + import traceback + traceback.print_exc() + else: + # 如果没有图像,至少显示一些提示 + if self.debug_count <= 3: + print(" ⚠️ 没有图像可绘制") + # 在画布中心显示提示 + canvas.create_text( + canvas_width // 2, + canvas_height // 2, + text="等待画面...\n\n如果长时间无画面,请检查:\n1. 采集卡是否正常工作\n2. 配置是否正确", + fill='yellow', + font=('Arial', 14), + justify=tk.CENTER + ) # 再绘制所有文本(上层) for x, y, text, color in texts_to_draw: try: - if color == 'white': - canvas.create_text(x, y, text=text, fill=color, font=('Arial', 10, 'bold')) - else: - canvas.create_text(x, y, text=text, fill=color, font=('Arial', 10)) + if 0 <= x <= canvas_width and 0 <= y <= canvas_height: + if color == 'white': + canvas.create_text(x, y, text=text, fill=color, font=('Arial', 10, 'bold')) + else: + canvas.create_text(x, y, text=text, fill=color, font=('Arial', 10)) except Exception as e: print(f"绘制文本错误: {e}") + # 强制更新画布显示 + canvas.update_idletasks() + # 绘制分割线,区分不同的采集卡窗口 try: # 绘制垂直分割线(列之间的分割线)