Kivy(Python)TabbedPanel - 不同(动态)大小的标签?

时间:2018-02-10 05:52:50

标签: python tabs kivy kivy-language

Kivy似乎想要使所有标签大小相同。 如果我想让一个标签比其他标签宽?调整TabbedPanelItem的tab_width似乎没有效果,因此较长的文本被截断。

示例略微修改了Kivy TabbedPanel docs,其中我将第一个标签的标题更改为"标签的长文字":

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder

Builder.load_string("""

<Test>:
    #size_hint: .5, .5
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False

    TabbedPanelItem:
        text: 'Long Text for a Tab'
        tab_width: self.texture_size[0]
        Label:
            text: 'First tab content area'
    TabbedPanelItem:
        text: 'tab2'
        BoxLayout:
            Label:
                text: 'Second tab content area'
            Button:
                text: 'Button that does nothing'
    TabbedPanelItem:
        text: 'tab3'
        RstDocument:
            text:
                '\\n'.join(("Hello world", "-----------",
                "You are in the third tab."))

""")


class Test(TabbedPanel):
    pass


class TabbedPanelApp(App):
    def build(self):
        return Test()


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

...所以&#34;长文&#34;被切断,因为所有3个标签都有相同的宽度。即使将第一个标签的tab_width设置为常量(例如300)也会被忽略。

似乎您可以向TabbedPanel提供tab_width并且它使所有选项卡都相同,但是如何使各个选项卡(TabbedPanelItems)具有不同(甚至动态)的宽度,即使某些选项卡比其他选项卡宽?

1 个答案:

答案 0 :(得分:1)

tab_widthTabbedPanel属性,不是TabbedPanelItem属性,适用于所有标签。

要为每个标签设置不同的尺寸,您可以将tab_widht设置为None,然后正确使用TabbedPanelItem属性(它基于Togglebutton,这样您就可以使用size_hint_xsize属性):

<Test>:
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False
    tab_width: None

    TabbedPanelItem:
        text: 'Long Text for a Tab'
        size_hint_x: None
        width: self.texture_size[0]
        padding: 10, 0

        Label:
            text: 'First tab content area'

修改

显然有必要在TabbedPanel初始化后显式更新选项卡的宽度,您可以使用Clock.schedule _一次和on_tab_width方法来执行此操作:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder

Builder.load_string("""

<CustomWidthTabb@TabbedPanelItem>
    width: self.texture_size[0]
    padding: 10, 0
    size_hint_x: None

<Test>:
    size_hint: 1,1
    do_default_tab: False
    tab_width: None

    CustomWidthTabb:
        text: "This is a Long Tab"
        Label:
            text: 'First tab content area'

    CustomWidthTabb:
        text: "This is a Long Tab"
        Label:
            text: 'Second tab content area'

    CustomWidthTabb:
        text: "Short Tab"     
        Label:
            text: 'Third tab content area'

    CustomWidthTabb:
        text: "Short Tab#2"   
        Label:
            text: 'Fourth tab content area'

""")



class Test(TabbedPanel):
    def __init__(self, *args, **kwargs):
        super(Test, self).__init__(*args, **kwargs)
        Clock.schedule_once(self.on_tab_width, 0.1)

class TabbedPanelApp(App):
    def build(self):
        return Test()


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

enter image description here