采集卡bug修复

This commit is contained in:
Ray
2025-10-29 17:24:44 +08:00
parent ac686997d1
commit 069613fe09

View File

@@ -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
# 计算每个预览窗口的位置和大小
# 确保画布已完全初始化
# 直接使用配置值作为画布尺寸macOS上窗口尺寸可能在显示前返回默认值
canvas_width = preview_width
canvas_height = preview_height
# 尝试获取实际的窗口尺寸,如果有效则使用(大于配置值说明可能被手动调整了)
try:
root.update_idletasks()
canvas.update_idletasks()
actual_width = root.winfo_width()
actual_height = root.winfo_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
# 只有在获取到合理的尺寸时才使用大于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():
"""关闭窗口"""