2017-10-06 97 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