在ipywidgets选项卡中,我如何从python切换到另一个选项卡?



我想从代码切换到另一个选项卡,当一些动作完成在jupyter实验室,文档是相当稀疏!当然有一些方法在标签/风琴等,我可以让它切换,而不是让用户点击标签?

import ipywidgets as widgets
def goto_tab2(btn):    
tab2.children[0].value='Been there'
tabset.switch_tab(1) # what is the method to call here?
def goto_tab1(btn):
#    tabset.switch_tab(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!', on_click=goto_tab1)])
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,试着设置它,结果成功了!为什么这只发生在你发布问题之后?

import ipywidgets as widgets
def goto_tab2(btn):    
tab2.children[0].value='Been there'
tabset.selected_index=1
def goto_tab1(btn):
tabset.selected_index = 0

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!', on_click=goto_tab1)])
tab2.children[1].on_click(goto_tab1)
tabset=widgets.Tab([tab1, tab2])
tabset.set_title(0, 'Tab1')
tabset.set_title(1, 'Tab2')
tabset

最新更新