Python Kivy TabbedPanel-设置自定义标签宽度

时间:2018-06-22 14:58:37

标签: python tabs kivy kivy-language

感谢多年不知不觉中的帮助我!我已经使用了很多这个网站,但这是我第一次真正必须发布问题以获取所需的信息,这真是太好了。

我最近开始学习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()

ResultExampleImage

与此相比,另一个线程中提供的不嵌套选项卡式面板的解决方案有效;它显示所有选项卡,并且它们已经过动态调整大小:

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嵌套在屏幕或其他小部件中,但以适当的宽度显示所有选项卡? 我在这里不了解什么基础知识?

1 个答案:

答案 0 :(得分:0)

解决方案

有关详细信息,请参阅示例。

kv文件

id添加TestForTabbedPanel:

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

输出

Img01 - All tabs displayed