Kivy可关闭TabbedPanel

时间:2017-10-08 07:17:02

标签: python kivy kivy-language

我尝试为可关闭的TabbedPanel from github here提取代码。

它工作正常,但标签的内容也不会被清除,只是标题。 关闭时如何删除Tab的内容?

from kivy.app import App
from kivy.animation import Animation
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
from kivy.factory import Factory
from kivy.lang import Builder


class CloseableHeader(TabbedPanelHeader):
    pass

class TestTabApp(App):
    def build(self):
        return Builder.load_string('''
TabbedPanel:
    do_default_tab: False
    FloatLayout:
        BoxLayout:
            id: tab_1_content
            Label:
                text: 'Palim 1'
        BoxLayout:
            id: tab_2_content
            Label:
                text: 'Palim 2'
        BoxLayout:
            id: tab_3_content
            Label:
                text: 'Palim 3'


    CloseableHeader:
        text: 'tab1'
        panel: root
        content: tab_1_content.__self__
    CloseableHeader:
        text: 'tab2'
        panel: root
        content: tab_2_content.__self__
    CloseableHeader:
        text: 'tab3'
        panel: root
        content: tab_3_content.__self__


<CloseableHeader>
    color: 0,0,0,0
    disabled_color: self.color
    # variable tab_width
    text: 'tabx'
    size_hint_x: None
    width: self.texture_size[0] + 40
    BoxLayout:
        pos: root.pos
        size_hint: None, None
        size: root.size
        padding: 3
        Label:
            id: lbl
            text: root.text
        BoxLayout:
            size_hint: None, 1
            orientation: 'vertical'
            width: 22
            Button:
                on_press:
                    root.panel.remove_widget(root)

''')


if __name__ == '__main__':
    TestTabApp().run()

如果我在标签2上关闭标签2。 enter image description here

我想看标签1,现在我仍然会看到tab2的内容。

enter image description here

1 个答案:

答案 0 :(得分:2)

正如您所说,在您的代码中,您只需删除标题。要清除内容区域中的所有小部件,还必须添加以下命令:

Button:
    on_press:
        root.panel.switch_to(root.panel.tab_list[root.panel.tab_list.index(root.panel.current_tab) - 1])
        root.panel.remove_widget(root)
        root.content.clear_widgets()

现在清除了内容区域并显示了其他内容。