Kivy(Python)TabbedPanel-不同(动态)大小的标签? [英] Kivy (Python) TabbedPanel - different (dynamic) sized tabs?

查看:111
本文介绍了Kivy(Python)TabbedPanel-不同(动态)大小的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kivy似乎想使所有标签的大小相同. 如果我想使一个标签比其他标签宽怎么办?调整TabbedPanelItem的tab_width似乎无效,因此,较长的文本将被截断.

Kivy seems to want to make all tabs the same size. What if I want to make one tab wider than others? Adjusting tab_width for a TabbedPanelItem seems to have no effect, and as such the longer text is getting cut off.

示例从 Kivy TabbedPanel文档进行了略微修改,第一个标签的标题为标签的长文本":

Example modified slightly from Kivy TabbedPanel docs, where I changed the first tab's heading to "Long Text for a Tab":

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()

...由于所有3个标签的宽度相同,因此长文本"被切掉了.甚至将第一个标签的tab_width设置为常数(例如300)也会被忽略.

...so the "Long Text" gets chopped up because all 3 tabs get the same width. Even setting the first tab's tab_width to a constant (e.g. 300) gets ignored.

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

Seems that you can supply tab_width to the TabbedPanel and it makes all tabs the same, but how do you make individual tabs (TabbedPanelItems) to have different (even dynamic) widths, i.e. so that some tabs are wider than others?

推荐答案

tab_widthTabbedPanel属性,而不是TabbedPanelItem属性,并应用于所有选项卡.

tab_width is a TabbedPanel property, not a TabbedPanelItem property and is applied to all tabs.

要为每个选项卡设置不同的大小,可以将tab_widht设置为None,然后正确使用TabbedPanelItem属性(它基于Togglebutton,因此可以使用size_hint_xsize属性) ):

To set a different size to each tab you can set tab_widht to None and then use TabbedPanelItem properties properly (it is based on Togglebutton, so you can use size_hint_x and size properties):

<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 _once和on_tab_width方法执行此操作:

Apparently it is necessary to explicitly update the width of the tabs after the initialization of TabbedPanel, you can use Clock.schedule_once and on_tab_width method to do this:

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()

这篇关于Kivy(Python)TabbedPanel-不同(动态)大小的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆