TypeError: Cannot read properties of undefined (reading ‘background_image’)

用户在 Gradio Blocks 应用中,使用 gr.Column 容器包裹 gr.ImageEditor 组件,并通过按钮点击事件动态切换该列的 visible 属性(先隐藏再显示)。当列被重新显示时,浏览器控制台报错,页面卡死,且编辑器内部内容被清空。

TypeError: Cannot read properties of undefined (reading 'background_image')

TypeError: Cannot read properties of undefined (reading ‘background_image’)

快速结论:该报错发生在 Gradio 的 ImageEditor 组件被隐藏后重新显示时,导致页面无响应且内部内容清空。优先排查 Gradio 版本是否低于 6.19.0,升级到该版本或更高版本通常即可解决。

问题场景

用户在 Gradio Blocks 应用中,使用 gr.Column 容器包裹 gr.ImageEditor 组件,并通过按钮点击事件动态切换该列的 visible 属性(先隐藏再显示)。当列被重新显示时,浏览器控制台报错,页面卡死,且编辑器内部内容被清空。

报错原文

resize.ts:1708 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'background_image')
    at xy.hide_resize_ui (resize.ts:1708:33)
    at xy.set_tool (resize.ts:294:9)
    at So.set_tool (editor.ts:648:9)
    at oe (ImageEditor.svelte:450:10)
    at s.$$.update (ImageEditor.svelte:673:3)
    at oe (svelte.js:1:15679)
    at ee (svelte.js:1:15394)

原因分析

可能原因是 Gradio 5.x 版本(如 5.48.0)中,ImageEditor 组件的内部状态在隐藏/销毁后未能正确恢复,导致 resize.ts 在重新渲染时试图访问不存在的 background_image 属性。该问题在后续版本中已被修复,最新版本(6.19.0)下无法复现此崩溃。

环境排查

  • Gradio 版本(建议升级到 6.19.0 或更高)
  • 浏览器及操作系统(已在 Chrome Windows 11、Chrome MacOS 上复现)

解决步骤

  1. 升级 Gradio 到最新稳定版本(例如 6.19.0
  2. 重新运行应用,重复隐藏/显示操作,观察页面是否卡顿、编辑器内容是否保留

验证方法

在升级后的版本中,点击“隐藏”按钮后再点击“显示”按钮,ImageEditor 应能完整重新渲染(包括画布和工具栏),页面保持响应,控制台无相关报错。

参考来源

gradio-app/gradio #12021

celebrityanime
celebrityanime
文章: 11055

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注