GoldenLayout(ipygoldenlayout) 如何通过编程方式切换标签页?
ipyGoldenLayout编程切换标签页实现方案

ipygoldenlayout的gl-stack标签容器组件内置activeItemIndex属性支持双向绑定,将该属性绑定到自定义Vuetify模板的同步traitlet属性上,修改属性值即可触发标签切换,完整实现如下:
import ipyvuetify as v from traitlets import Unicode, Int from ipygoldenlayout import GoldenLayout gl = GoldenLayout() class TestGL(v.VuetifyTemplate): # 定义可同步的激活标签索引属性,默认值为0(对应第一个标签) active_tab_index = Int(0).tag(sync=True) template = Unicode(""" <golden-layout style="height: 200px"> <gl-row> <gl-component title="component1"> <h1>Component 1</h1> </gl-component> <!-- 给gl-stack绑定activeItemIndex到自定义属性 --> <gl-stack v-model:activeItemIndex="active_tab_index"> <gl-component title="component2"> <h1>Component 2</h1> </gl-component> <gl-component title="component3"> <h1>Component 3</h1> </gl-component> </gl-stack> </gl-row> </golden-layout> """).tag(sync=True) # 实例化组件 test_gl = TestGL() # 渲染组件 test_gl
切换标签调用方法
标签索引从0开始计数,直接修改实例的active_tab_index属性即可完成切换:
# 切换到第二个标签(component3,索引为1) test_gl.active_tab_index = 1 # 切换回第一个标签(component2,索引为0) test_gl.active_tab_index = 0
内容的提问来源于stack exchange,提问作者user10427488
相关产品推荐
相关产品推荐

