You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Omniverse扩展动态更新CollapsableFrame下拉菜单文本?

解决CollapsableFrame标题动态更新问题

问题出在初始化CollapsableFrame时,标题字符串是静态生成的,后续修改self._items_count只会更新变量值,不会自动同步到UI元素上。要实现动态更新,需要把CollapsableFrame实例保存下来,在变量变化时手动更新它的标题文本。

修改步骤及代码示例

  1. 保存CollapsableFrame实例
    把原来的with ui.CollapsableFrame(...)改成直接赋值并保存为类属性,方便后续访问修改:

    self._window = ui.Window("My Window", width=300, height=200)
    self._items_count = 0
    
    with self._window.frame:
        with ui.VStack():
            # 保存CollapsableFrame实例到类属性
            self._collapsable_frame = ui.CollapsableFrame(
                f'My List {self._items_count} items:',
                collapsed=True
            )
            with self._collapsable_frame:
                tree_view = ui.TreeView(
                    self._model_all_usd,
                    root_visible=False,
                    header_visible=False,
                    columns_resizable=True,
                    column_widths=[ui.Fraction(0.4), ui.Fraction(0.3)],
                    style={"TreeView.Item": {"margin": 4}}
                )
    
  2. 在事件回调中更新标题
    修改_on_stage_event函数,更新self._items_count后,同步修改self._collapsable_frame.text:

    def _on_stage_event(self, event):
        if event.type == int(omni.usd.StageEventType.SELECTION_CHANGED):
            # do stuff
            self._items_count += 1
            # 动态更新CollapsableFrame的标题
            self._collapsable_frame.text = f'My List {self._items_count} items:'
    

这样每次选择变更时,CollapsableFrame的标题就会自动同步显示最新的计数了。

内容的提问来源于stack exchange,提问作者Tony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:05:14