Kivy定制可关闭标签

2024-04-26 21:02:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在kivy中实现一个自定义的可关闭的tab头。在

我做的是把类: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()

Tags: 对象fromimportselfaddsizeinitdef
1条回答
网友
1楼 · 发布于 2024-04-26 21:02:28

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

相关问题 更多 >