【问题标题】:Python Kivy TabbedPanel - setting custom tab widthsPython Kivy TabbedPanel - 设置自定义标签宽度
【发布时间】:2018-06-22 14:58:37
【问题描述】:

感谢多年来在不知不觉中帮助我!我经常使用这个网站,但这是我第一次实际上必须发布一个问题才能获得我需要的信息,这太棒了。

我最近开始学习 Python(和 Kivy),所以我还是个初学者。 我正在使用:

[INFO   ] [Kivy        ] v1.10.0
[INFO   ] [Python      ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]

我正在尝试调整 TabbedPanel 中的选项卡 (TabbedPanelItem) 的大小,以使它们具有足够的水平尺寸,以允许显示文本。 还有另一个类似的问题here 已通过解决方案得到解答。 我发现这个解决方案也适合我,但前提是我不使用屏幕并将选项卡式面板放在其他任何东西中。

一旦我更改结构以嵌套选项卡式面板,解决方案就不再有效。

这是我从解决方案中获取的一些示例代码,但对其进行了修改,以使选项卡式面板不是根:

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

Builder.load_string("""

<Test2>:
    GridLayout:
        cols:1

        GridLayout:
            rows:1
            Label:
                text:'Does it show all 4 tabs?'
        GridLayout:
            rows:1
            TestForTabbedPanel

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

<TestForTabbedPanel>:
    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 Test2(Screen):
    pass

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

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


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

与此相比,其他线程中提供的没有嵌套选项卡式面板的解决方案有效;它显示了所有选项卡,并且它们已被动态调整大小:

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

如何编写代码,以便 TabbedPanel 嵌套在屏幕或其他小部件内,但显示所有选项卡的宽度足够? 我在这里不了解哪些基础知识?

【问题讨论】:

    标签: python tabs kivy kivy-language


    【解决方案1】:

    解决方案

    详情请参考示例。

    kv 文件

    TestForTabbedPanel:添加id

        GridLayout:
            rows:1
            TestForTabbedPanel:
                id: tp
    

    Python 代码

    1. __init__() 方法从TestForTabbedPanel 类移到Test2
    2. super(TestForTabbedPanel, self) 替换为super(Test2, self)
    3. self.on_tab_width 替换为self.ids.tp.on_tab_width
    4. pass 添加到类*TestForTabbedPanel

    class Test2(Screen):
        def __init__(self, *args, **kwargs):
            super(Test2, self).__init__(*args, **kwargs)
            Clock.schedule_once(self.ids.tp.on_tab_width, 0.1)
    
    
    class TestForTabbedPanel(TabbedPanel):
        pass
    

    示例

    from kivy.app import App
    from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
    from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
    from kivy.uix.boxlayout import BoxLayout
    from kivy.clock import Clock
    from kivy.lang import Builder
    
    Builder.load_string("""
    
    <Test2>:
        GridLayout:
            cols:1
    
            GridLayout:
                rows:1
                Label:
                    text:'Does it show all 4 tabs?'
            GridLayout:
                rows:1
                TestForTabbedPanel:
                    id: tp
    
    <CustomWidthTabb@TabbedPanelItem>
        width: self.texture_size[0]
        padding: 10, 0
        size_hint_x: None
    
    <TestForTabbedPanel>:
        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 Test2(Screen):
        def __init__(self, *args, **kwargs):
            super(Test2, self).__init__(*args, **kwargs)
            Clock.schedule_once(self.ids.tp.on_tab_width, 0.1)
    
    
    class TestForTabbedPanel(TabbedPanel):
        pass
    
    
    class TabbedPanelApp(App):
        def build(self):
            return Test2()
    
    
    if __name__ == '__main__':
        TabbedPanelApp().run()
    

    输出

    【讨论】:

    • 完美运行,非常感谢。
    • 请记住将问题标记为已回答,方法是单击解决了您的问题的答案左侧的绿色轮廓复选标记。这将答案标记为“已接受”,并通过扩展将问题标记为“已接受答案”。
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多