我想使用TabbedPanel小部件删除按钮上的选项卡。 我的面板上有两个标签,我想删除第二个标签,以便面板只有一个标签。
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string("""
<Test>:
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'first tab'
Label:
text: 'First tab content area'
TabbedPanelItem:
text: 'tab2'
BoxLayout:
Label:
text: 'Second tab content area'
Button:
text: 'Button that deletes tab 2'
background_color: (1, 0, 0, 1)
""")
class Test(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()
答案 0 :(得分:0)
一种可能的解决方案是通过传递remove_widget()
来使用TabbedPanelItem
,但为此我们可以搜索文本,但在此之前我们会切换到另一个标签。
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
Builder.load_string("""
<Test>:
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'first tab'
Label:
text: 'First tab content area'
TabbedPanelItem:
text: 'tab2'
BoxLayout:
Label:
text: 'Second tab content area'
Button:
text: 'Button that deletes tab 2'
background_color: (1, 0, 0, 1)
on_press: root.removeTab('tab2')
""")
class Test(TabbedPanel):
def removeTab(self, name):
tab = None
for i in self.tab_list:
if i.text == name:
tab = i
break
if tab:
self.switch_to(self.default_tab)
self.remove_widget(tab)
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()