在ipywidgets的Tab组件中如何通过Python代码切换指定标签页
ipywidgets Tab组件代码触发切换方案
ipywidgets的Tab、Accordion类多面板组件没有单独的switch_tab方法,直接修改**selected_index**属性即可实现自动切换,属性值为标签的对应索引(从0开始计数)。
你提供的示例代码修改后可正常运行,修改后的完整代码如下:
import ipywidgets as widgets def goto_tab2(btn): tab2.children[0].value='Been there' tabset.selected_index = 1 # 直接修改selected_index属性切换到第二个标签 def goto_tab1(btn): tabset.selected_index = 0 # 切换回第一个标签 pass tab1 = widgets.VBox([widgets.Text(), widgets.Button(description='set name')]) tab1.children[1].on_click(goto_tab2) tab2 = widgets.VBox([widgets.Text(), widgets.Button(description='GO BACK!')]) tab2.children[1].on_click(goto_tab1) tabset=widgets.Tab([tab1, tab2]) tabset.set_title(0, 'Tab1') tabset.set_title(1, 'Tab2') tabset
补充说明:
selected_index属性对Accordion组件同样适用,设置为对应索引即可展开对应面板- 若将
Accordion的selected_index设置为None,可折叠所有面板
内容的提问来源于stack exchange,提问作者pootle
相关产品推荐
相关产品推荐

