2017-10-08 93 views
0

我尝试提取可关闭的代码TabbedPanelfrom github hereKivy可关闭TabbedPanel

它工作正常,但标签的内容也不会被清除,只是标题。 如何在关闭时删除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() 

如果我关闭Tab 2,而在标签2. enter image description here

是我希望看到的选项卡1,现在我仍然会看到TAB2的内容。

enter image description here

回答

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() 

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

+0

不幸的是,这只会清除小部件,但我希望如果关闭tab2,tab1会自动显示。 – PalimPalim

+0

我没有设法做的唯一事情是自动显示另一个标签... –

+0

@PalimPalim是否有效? –