2017-10-08 42 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是否有效? –