2017-10-06 95 views
0

我想在kivy中實現自定義可關閉選項卡標題。Kivy中的自定義可關閉選項卡

我所做的是將一個類:TabbedPanelHeader對象與一個自定義類:CloseButton對象結合在一起。這兩個小部件都在類中:BoxLayout,並排。

但是,一旦我將此添加到類:TabbedPanel對象,什麼也沒有顯示出來.. 我不知道如何前進,將非常感謝所有的幫助!

以下是代碼的相關部分。

from kivy.uix.behaviors import ButtonBehavior 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.image import Image 
from kivy.graphics import * 
from kivy.uix.tabbedpanel import TabbedPanelHeader 

class CloseButton(ButtonBehavior, Image): 
    def __init__(self, **kwargs): 
     super(CloseButton, self).__init__(**kwargs) 
     self.source = 'atlas://data/images/defaulttheme/close' 
     self.size_hint_x = .2 

    def on_press(self): 
     self.source = 'atlas://data/images/defaulttheme/checkbox_radio_off' 

    def on_release(self): 
     self.source = 'atlas://data/images/defaulttheme/checkbox_radio_off' 
     ## do the actual closing of the tab 

class ClosableTabHeader(BoxLayout): 
    def __init__(self, **kwargs): 
     super(ClosableTabHeader, self).__init__(**kwargs) 
     self.size = (100, 30) 
     self.size_hint = (None, None) 
     self.canvas.before.add(Color(.25, .25, .25)) 
     self.canvas.before.add(Rectangle(size=(105, 30))) 
     self.add_widget(TabbedPanelHeader(background_color=(.65, .65, .65, 0), text='testing')) 
     self.add_widget(CloseButton()) 


if __name__ == '__main__': 
    from kivy.app import App 

    class TestApp(App): 
     def build(self): 
      return ClosableTabHeader() 

    TestApp().run() 

回答

0

enter image description here下面是一些代碼,接近實現你想實現

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 
      Image: 
       source: 'tools/theming/defaulttheme/close.png' 
       on_touch_down: 
        if self.collide_point(*args[1].pos) :\ 
         root.panel.remove_widget(root); \ 

''') 


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

什麼它是基於https://github.com/kivy/kivy/blob/master/examples/widgets/tabbed_panel_showcase.py

+0

反正它可以在Python,而不是KV寫語言。我鬆散地基於你與我聯繫的那個腳本的代碼。但是,我還沒有完全理解KV lang,因此爲什麼我的代碼不能完全工作=) – Icee

+0

確定您可以在kv中執行的所有操作都可以在python中完成。但是,這是更多的工作和kv真的很容易。值得學習它。我不想將它轉移到python,我甚至不確定我是否可以。如果我的問題幫助或解決了您的問題,請立即投訴或接受。 – PalimPalim

+0

沒問題。看起來像它回到我的繪圖板。不過謝謝。 – Icee