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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 19:15:03