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

kb5ga3dv  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(148)

我正在使用内置的omni库为Omniverse Create创建一个自定义扩展。我有一个简单的窗口,它创建了一个下拉菜单(CollapsableFrame),其中包含一个TreeView

self._window = ui.Window("My Window", width = 300, height = 200)
self._items_count = 0

with self._window.frame:
    with ui.VStack():
        with ui.CollapsableFrame(f'My List {self._items_count} items:', collapsed = True):
            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}})

我也有一个函数,我正在使用它来监听选择的变化:

def _on_stage_event(self, event):
    if event.type == int(omni.usd.StageEventType.SELECTION_CHANGED):
        // do stuff
        self._items_count += 1    # increase count whenever the selection changes

我目前拥有的将更新self._items_count值,但下拉文本不会更新。我如何才能做到这一点?

yfjy0ee7

yfjy0ee71#

只需要引用CollapsableFrame

with self._window.frame:
    with ui.VStack():
        self._dropdown_frame = ui.CollapsableFrame(f'My List {self._items_count} items:', collapsed = True)
        with self._dropdown_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}})

然后通过修改title属性进行更新:

def _on_stage_event(self, event):
    if event.type == int(omni.usd.StageEventType.SELECTION_CHANGED):
        // do stuff
        self._items_count += 1    # Increase count whenever the selection changes
        self._dropdown_frame.title = f'My List {self._items_count} items:'

相关问题